From 37a78f5b5f88e785dc76f2ea3e15e5918e547520 Mon Sep 17 00:00:00 2001 From: Milan Pavlik Date: Tue, 6 Sep 2022 19:26:51 +0000 Subject: [PATCH] [usage] Exclude workspace instances without a startedTime --- components/usage/pkg/apiv1/usage_test.go | 3 ++- components/usage/pkg/db/workspace_instance.go | 21 ++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/components/usage/pkg/apiv1/usage_test.go b/components/usage/pkg/apiv1/usage_test.go index 6dac06a613c6b1..00d89791dd04a7 100644 --- a/components/usage/pkg/apiv1/usage_test.go +++ b/components/usage/pkg/apiv1/usage_test.go @@ -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, })) diff --git a/components/usage/pkg/db/workspace_instance.go b/components/usage/pkg/db/workspace_instance.go index 3fe0dc693b08dd..d94d02b18ff206 100644 --- a/components/usage/pkg/db/workspace_instance.go +++ b/components/usage/pkg/db/workspace_instance.go @@ -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 { @@ -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, "+ + "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 (