Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions builder/store/database/account_metering.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,20 @@ func (am *accountMeteringStoreImpl) Create(ctx context.Context, input AccountMet

func (am *accountMeteringStoreImpl) ListByUserIDAndTime(ctx context.Context, req types.ActStatementsReq) ([]AccountMetering, int, error) {
var accountMeters []AccountMetering
q := am.db.Operator.Core.NewSelect().Model(&accountMeters).Where("user_uuid = ? and scene = ? and customer_id = ? and recorded_at >= ? and recorded_at <= ?", req.UserUUID, req.Scene, req.InstanceName, req.StartTime, req.EndTime)
q := am.db.Operator.Core.NewSelect().Model(&accountMeters).Where("user_uuid = ? and scene = ?", req.UserUUID, req.Scene)
if len(req.InstanceName) > 0 {
q = q.Where("customer_id = ?", req.InstanceName)
}
q = q.Where("recorded_at >= ? and recorded_at <= ?", req.StartTime, req.EndTime)

count, err := q.Count(ctx)
if err != nil {
return nil, 0, fmt.Errorf("failed to counting recorders, error: %w", err)
return nil, 0, fmt.Errorf("failed to counting metering recorders, error: %w", err)
}

_, err = q.Order("id DESC").Limit(req.Per).Offset((req.Page-1)*req.Per).Exec(ctx, &accountMeters)
if err != nil {
return nil, 0, fmt.Errorf("list all meters, error: %w", err)
return nil, 0, fmt.Errorf("list all metering recorders, error: %w", err)
}
return accountMeters, count, nil
}
Expand Down
17 changes: 16 additions & 1 deletion builder/store/database/account_metering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestAccountMeteringStore_ListByUserIDAndTime(t *testing.T) {
},
{
UserUUID: "foo", Value: 12.34, ValueType: 1,
ResourceName: "r10", Scene: types.ScenePayOrder, CustomerID: "barz",
ResourceName: "r10", Scene: types.ScenePayOrder, CustomerID: "",
RecordedAt: dt.Add(-1 * time.Hour), EventUUID: uuid.New(),
},
}
Expand All @@ -115,6 +115,21 @@ func TestAccountMeteringStore_ListByUserIDAndTime(t *testing.T) {
names = append(names, am.ResourceName)
}
require.Equal(t, []string{"r5", "r4", "r3", "r2", "r1"}, names)

ams, total, err = store.ListByUserIDAndTime(ctx, types.ActStatementsReq{
UserUUID: "foo",
Scene: 2,
InstanceName: "",
StartTime: dt.Add(-5 * time.Hour).Format(time.RFC3339),
EndTime: dt.Add(5 * time.Hour).Format(time.RFC3339),
})
require.Nil(t, err)
require.Equal(t, 6, total)
names = []string{}
for _, am := range ams {
names = append(names, am.ResourceName)
}
require.Equal(t, []string{"r10", "r5", "r4", "r3", "r2", "r1"}, names)
}

func TestAccountMeteringStore_GetStatByDate(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SET statement_timeout = 0;

--bun:split

DROP INDEX IF EXISTS idx_account_meter_user_scene_recordat;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SET statement_timeout = 0;

--bun:split

CREATE INDEX IF NOT EXISTS idx_account_meter_user_scene_recordat ON account_meterings (user_uuid, scene, recorded_at);
Loading