Skip to content

[usage] Save inserted and updated records in the DB #12671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2022
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
18 changes: 18 additions & 0 deletions components/usage/pkg/apiv1/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,24 @@ func (s *UsageService) ReconcileUsageWithLedger(ctx context.Context, req *v1.Rec
inserts, updates := reconcileUsageWithLedger(instances, usageDrafts, s.pricer, now)
logger.WithField("inserts", inserts).WithField("updates", updates).Infof("Identified %d inserts and %d updates against usage records.", len(inserts), len(updates))

if len(inserts) > 0 {
err = db.InsertUsage(ctx, s.conn, inserts...)
if err != nil {
logger.WithError(err).Errorf("Failed to insert %d usage records into the database.", len(inserts))
return nil, status.Errorf(codes.Internal, "Failed to insert usage records into the database.")
}
logger.Infof("Inserted %d new Usage records into the database.", len(inserts))
}

if len(updates) > 0 {
err = db.UpdateUsage(ctx, s.conn, updates...)
if err != nil {
logger.WithError(err).Error("Failed to update usage records in the database.")
return nil, status.Errorf(codes.Internal, "Failed to update usage records in the database.")
}
logger.Infof("Updated %d Usage records in the database.", len(updates))
}

return &v1.ReconcileUsageWithLedgerResponse{}, nil
}

Expand Down
26 changes: 21 additions & 5 deletions components/usage/pkg/apiv1/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,19 +561,26 @@ func TestUsageService_ReconcileUsageWithLedger(t *testing.T) {
dbconn := dbtest.ConnectForTests(t)
from := time.Date(2022, 05, 1, 0, 00, 00, 00, time.UTC)
to := time.Date(2022, 05, 1, 1, 00, 00, 00, time.UTC)
attributionID := db.NewTeamAttributionID(uuid.New().String())

// stopped instances
dbtest.CreateWorkspaceInstances(t, dbconn, dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
StoppingTime: db.NewVarcharTime(from.Add(1 * time.Minute)),
}))
instance := dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
UsageAttributionID: attributionID,
CreationTime: db.NewVarcharTime(from),
StoppingTime: db.NewVarcharTime(to.Add(-1 * time.Minute)),
})
dbtest.CreateWorkspaceInstances(t, dbconn, instance)

// running instances
dbtest.CreateWorkspaceInstances(t, dbconn, dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{}))

// usage drafts
dbtest.CreateUsageRecords(t, dbconn, dbtest.NewUsage(t, db.Usage{
Kind: db.WorkspaceInstanceUsageKind,
Draft: true,
ID: uuid.New(),
AttributionID: attributionID,
WorkspaceInstanceID: instance.ID,
Kind: db.WorkspaceInstanceUsageKind,
Draft: true,
}))

srv := baseserver.NewForTests(t,
Expand All @@ -593,6 +600,15 @@ func TestUsageService_ReconcileUsageWithLedger(t *testing.T) {
To: timestamppb.New(to),
})
require.NoError(t, err)

usage, err := db.FindUsage(context.Background(), dbconn, &db.FindUsageParams{
AttributionId: attributionID,
From: from,
To: to,
ExcludeDrafts: false,
})
require.NoError(t, err)
require.Len(t, usage, 1)
}

func TestReconcileWithLedger(t *testing.T) {
Expand Down
2 changes: 0 additions & 2 deletions components/usage/pkg/db/dbtest/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,5 @@ func CreateUsageRecords(t *testing.T, conn *gorm.DB, entries ...db.Usage) []db.U
require.NoError(t, conn.Where(ids).Delete(&db.Usage{}).Error)
})

t.Logf("stored %d", len(entries))

return records
}
17 changes: 12 additions & 5 deletions components/usage/pkg/db/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,15 @@ func InsertUsage(ctx context.Context, conn *gorm.DB, records ...Usage) error {
CreateInBatches(records, 1000).Error
}

func UpdateUsage(ctx context.Context, conn *gorm.DB, record Usage) error {
return conn.WithContext(ctx).Save(record).Error
func UpdateUsage(ctx context.Context, conn *gorm.DB, records ...Usage) error {
for _, record := range records {
err := conn.WithContext(ctx).Save(record).Error
if err != nil {
return fmt.Errorf("failed to update usage record ID: %s: %w", record.ID, err)
}
}

return nil
}

func FindAllDraftUsage(ctx context.Context, conn *gorm.DB) ([]Usage, error) {
Expand Down Expand Up @@ -97,7 +104,7 @@ func FindUsage(ctx context.Context, conn *gorm.DB, params *FindUsageParams) ([]U

db := conn.WithContext(ctx).
Where("attributionId = ?", params.AttributionId).
Where("? <= effectiveTime AND effectiveTime < ?", params.From, params.To)
Where("effectiveTime >= ? AND effectiveTime < ?", TimeToISO8601(params.From), TimeToISO8601(params.To))
if params.ExcludeDrafts {
db = db.Where("draft = ?", false)
}
Expand Down Expand Up @@ -130,7 +137,7 @@ func GetUsageSummary(ctx context.Context, conn *gorm.DB, attributionId Attributi
query1 := db.Table((&Usage{}).TableName()).
Select("sum(creditCents) as creditCentsBalanceAtStart").
Where("attributionId = ?", attributionId).
Where("effectiveTime < ?", from)
Where("effectiveTime < ?", TimeToISO8601(from))
if excludeDrafts {
query1 = query1.Where("draft = ?", false)
}
Expand All @@ -143,7 +150,7 @@ func GetUsageSummary(ctx context.Context, conn *gorm.DB, attributionId Attributi
query2 := db.Table((&Usage{}).TableName()).
Select("sum(creditCents) as creditCentsBalanceInPeriod", "count(id) as numRecordsInRange").
Where("attributionId = ?", attributionId).
Where("? <= effectiveTime AND effectiveTime < ?", from, to)
Where("? <= effectiveTime AND effectiveTime < ?", TimeToISO8601(from), TimeToISO8601(to))
if excludeDrafts {
query2 = query2.Where("draft = ?", false)
}
Expand Down