Skip to content

[usage] Exclude workspace instances without a startedTime #12707

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 7, 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
3 changes: 2 additions & 1 deletion components/usage/pkg/apiv1/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,13 +564,14 @@ func TestUsageService_ReconcileUsageWithLedger(t *testing.T) {
// stopped instances
instance := dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
UsageAttributionID: attributionID,
CreationTime: db.NewVarcharTime(from),
StartedTime: 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{
StartedTime: db.NewVarcharTime(to.Add(-1 * time.Minute)),
UsageAttributionID: attributionID,
}))

Expand Down
21 changes: 11 additions & 10 deletions components/usage/pkg/db/workspace_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ func ListWorkspaceInstancesInRange(ctx context.Context, conn *gorm.DB, from, to
Where(
conn.Where("wsi.stoppingTime >= ?", TimeToISO8601(from)).Or("wsi.stoppingTime = ?", ""),
).
Where("wsi.startedTime != ?", "").
Where("wsi.startedTime < ?", TimeToISO8601(to)).
Where("wsi.usageAttributionId != ?", "").
FindInBatches(&instancesInBatch, 1000, func(_ *gorm.DB, _ int) error {
Expand All @@ -151,17 +150,19 @@ func ListWorkspaceInstancesInRange(ctx context.Context, conn *gorm.DB, from, to
func queryWorkspaceInstanceForUsage(ctx context.Context, conn *gorm.DB) *gorm.DB {
return conn.WithContext(ctx).
Table(fmt.Sprintf("%s as wsi", (&WorkspaceInstance{}).TableName())).
Select("wsi.id as id, " +
"ws.projectId as projectId, " +
"ws.type as workspaceType, " +
"wsi.workspaceClass as workspaceClass, " +
"wsi.usageAttributionId as usageAttributionId, " +
"wsi.startedTime as startedTime, " +
"wsi.stoppingTime as stoppingTime, " +
"ws.ownerId as ownerId, " +
Select("wsi.id as id, "+
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just go fmt auto-formatting as per spec

"ws.projectId as projectId, "+
"ws.type as workspaceType, "+
"wsi.workspaceClass as workspaceClass, "+
"wsi.usageAttributionId as usageAttributionId, "+
"wsi.startedTime as startedTime, "+
"wsi.stoppingTime as stoppingTime, "+
"ws.ownerId as ownerId, "+
"ws.id as workspaceId",
).
Joins(fmt.Sprintf("LEFT JOIN %s AS ws ON wsi.workspaceId = ws.id", (&Workspace{}).TableName()))
Joins(fmt.Sprintf("LEFT JOIN %s AS ws ON wsi.workspaceId = ws.id", (&Workspace{}).TableName())).
// Instances without a StartedTime never actually started, we're not interested in these.
Where("wsi.startedTime != ?", "")
}

const (
Expand Down