From a04a7baef487b74264e32be01faae8d45f535724 Mon Sep 17 00:00:00 2001 From: Milan Pavlik Date: Tue, 12 Jul 2022 12:00:30 +0000 Subject: [PATCH 1/4] [usage] Join workspace instances with workspaces to get project and type --- components/usage/pkg/controller/reconciler.go | 16 +-- components/usage/pkg/db/workspace_instance.go | 106 ++++++++++++------ .../usage/pkg/db/workspace_instance_test.go | 4 + 3 files changed, 83 insertions(+), 43 deletions(-) diff --git a/components/usage/pkg/controller/reconciler.go b/components/usage/pkg/controller/reconciler.go index d348556bcc3c53..639b6f4fd76fbd 100644 --- a/components/usage/pkg/controller/reconciler.go +++ b/components/usage/pkg/controller/reconciler.go @@ -117,7 +117,7 @@ func (u *UsageReconciler) ReconcileTimeRange(ctx context.Context, from, to time. return status, instancesByAttributionID, nil } -type UsageReport map[db.AttributionID][]db.WorkspaceInstance +type UsageReport map[db.AttributionID][]db.WorkspaceInstanceForUsage func (u UsageReport) CreditSummaryForTeams(pricer *WorkspacePricer, maxStopTime time.Time) map[string]int64 { creditsPerTeamID := map[string]int64{} @@ -149,7 +149,7 @@ type invalidWorkspaceInstance struct { workspaceInstanceID uuid.UUID } -func (u *UsageReconciler) loadWorkspaceInstances(ctx context.Context, from, to time.Time) ([]db.WorkspaceInstance, []invalidWorkspaceInstance, error) { +func (u *UsageReconciler) loadWorkspaceInstances(ctx context.Context, from, to time.Time) ([]db.WorkspaceInstanceForUsage, []invalidWorkspaceInstance, error) { log.Infof("Gathering usage data from %s to %s", from, to) instances, err := db.ListWorkspaceInstancesInRange(ctx, u.conn, from, to) if err != nil { @@ -162,7 +162,7 @@ func (u *UsageReconciler) loadWorkspaceInstances(ctx context.Context, from, to t return trimmed, invalid, nil } -func validateInstances(instances []db.WorkspaceInstance) (valid []db.WorkspaceInstance, invalid []invalidWorkspaceInstance) { +func validateInstances(instances []db.WorkspaceInstanceForUsage) (valid []db.WorkspaceInstanceForUsage, invalid []invalidWorkspaceInstance) { for _, i := range instances { // i is a pointer to the current element, we need to assign it to ensure we're copying the value, not the current pointer. instance := i @@ -196,8 +196,8 @@ func validateInstances(instances []db.WorkspaceInstance) (valid []db.WorkspaceIn } // trimStartStopTime ensures that start time or stop time of an instance is never outside of specified start or stop time range. -func trimStartStopTime(instances []db.WorkspaceInstance, maximumStart, minimumStop time.Time) []db.WorkspaceInstance { - var updated []db.WorkspaceInstance +func trimStartStopTime(instances []db.WorkspaceInstanceForUsage, maximumStart, minimumStop time.Time) []db.WorkspaceInstanceForUsage { + var updated []db.WorkspaceInstanceForUsage for _, instance := range instances { if instance.CreationTime.Time().Before(maximumStart) { @@ -213,11 +213,11 @@ func trimStartStopTime(instances []db.WorkspaceInstance, maximumStart, minimumSt return updated } -func groupInstancesByAttributionID(instances []db.WorkspaceInstance) map[db.AttributionID][]db.WorkspaceInstance { - result := map[db.AttributionID][]db.WorkspaceInstance{} +func groupInstancesByAttributionID(instances []db.WorkspaceInstanceForUsage) map[db.AttributionID][]db.WorkspaceInstanceForUsage { + result := map[db.AttributionID][]db.WorkspaceInstanceForUsage{} for _, instance := range instances { if _, ok := result[instance.UsageAttributionID]; !ok { - result[instance.UsageAttributionID] = []db.WorkspaceInstance{} + result[instance.UsageAttributionID] = []db.WorkspaceInstanceForUsage{} } result[instance.UsageAttributionID] = append(result[instance.UsageAttributionID], instance) diff --git a/components/usage/pkg/db/workspace_instance.go b/components/usage/pkg/db/workspace_instance.go index c534233d5626fb..057b06da9a1b4a 100644 --- a/components/usage/pkg/db/workspace_instance.go +++ b/components/usage/pkg/db/workspace_instance.go @@ -45,21 +45,6 @@ type WorkspaceInstance struct { _ bool `gorm:"column:deleted;type:tinyint;default:0;" json:"deleted"` } -// WorkspaceRuntimeSeconds computes how long this WorkspaceInstance has been running. -// If the instance is still running (no stop time set), maxStopTime is used to to compute the duration - this is an upper bound on stop -func (i *WorkspaceInstance) WorkspaceRuntimeSeconds(maxStopTime time.Time) int64 { - start := i.CreationTime.Time() - stop := maxStopTime - - if i.StoppedTime.IsSet() { - if i.StoppedTime.Time().Before(maxStopTime) { - stop = i.StoppedTime.Time() - } - } - - return int64(stop.Sub(start).Round(time.Second).Seconds()) -} - // TableName sets the insert table name for this struct type func (i *WorkspaceInstance) TableName() string { return "d_b_workspace_instance" @@ -71,26 +56,26 @@ func (i *WorkspaceInstance) TableName() string { // - running // - instances which only just terminated after the start period // - instances which only just started in the period specified -func ListWorkspaceInstancesInRange(ctx context.Context, conn *gorm.DB, from, to time.Time) ([]WorkspaceInstance, error) { - var instances []WorkspaceInstance - var instancesInBatch []WorkspaceInstance - tx := conn.WithContext(ctx). - Where( - conn.Where("stoppedTime >= ?", TimeToISO8601(from)).Or("stoppedTime = ?", ""), - ). - Where("creationTime < ?", TimeToISO8601(to)). - Where("startedTime != ?", ""). - Where("usageAttributionId != ?", ""). - FindInBatches(&instancesInBatch, 1000, func(_ *gorm.DB, _ int) error { - instances = append(instances, instancesInBatch...) - return nil - }) - if tx.Error != nil { - return nil, fmt.Errorf("failed to list workspace instances: %w", tx.Error) - } - - return instances, nil -} +//func ListWorkspaceInstancesInRange(ctx context.Context, conn *gorm.DB, from, to time.Time) ([]WorkspaceInstance, error) { +// var instances []WorkspaceInstance +// var instancesInBatch []WorkspaceInstance +// tx := conn.WithContext(ctx). +// Where( +// conn.Where("stoppedTime >= ?", TimeToISO8601(from)).Or("stoppedTime = ?", ""), +// ). +// Where("creationTime < ?", TimeToISO8601(to)). +// Where("startedTime != ?", ""). +// Where("usageAttributionId != ?", ""). +// FindInBatches(&instancesInBatch, 1000, func(_ *gorm.DB, _ int) error { +// instances = append(instances, instancesInBatch...) +// return nil +// }) +// if tx.Error != nil { +// return nil, fmt.Errorf("failed to list workspace instances: %w", tx.Error) +// } +// +// return instances, nil +//} const ( AttributionEntity_User = "user" @@ -125,3 +110,54 @@ func (a AttributionID) Values() (entity string, identifier string) { const ( WorkspaceClass_Default = "default" ) + +type WorkspaceInstanceForUsage struct { + ID uuid.UUID `gorm:"column:id;type:char;size:36;" json:"id"` + ProjectID sql.NullString `gorm:"column:projectId;type:char;size:36;" json:"projectId"` + WorkspaceClass string `gorm:"column:workspaceClass;type:varchar;size:255;" json:"workspaceClass"` + Type WorkspaceType `gorm:"column:workspaceType;type:char;size:16;default:regular;" json:"workspaceType"` + UsageAttributionID AttributionID `gorm:"column:usageAttributionId;type:varchar;size:60;" json:"usageAttributionId"` + + CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"` + StoppedTime VarcharTime `gorm:"column:stoppedTime;type:varchar;size:255;" json:"stoppedTime"` +} + +// WorkspaceRuntimeSeconds computes how long this WorkspaceInstance has been running. +// If the instance is still running (no stop time set), maxStopTime is used to to compute the duration - this is an upper bound on stop +func (i *WorkspaceInstanceForUsage) WorkspaceRuntimeSeconds(maxStopTime time.Time) int64 { + start := i.CreationTime.Time() + stop := maxStopTime + + if i.StoppedTime.IsSet() { + if i.StoppedTime.Time().Before(maxStopTime) { + stop = i.StoppedTime.Time() + } + } + + return int64(stop.Sub(start).Round(time.Second).Seconds()) +} + +func ListWorkspaceInstancesInRange(ctx context.Context, conn *gorm.DB, from, to time.Time) ([]WorkspaceInstanceForUsage, error) { + var instances []WorkspaceInstanceForUsage + var instancesInBatch []WorkspaceInstanceForUsage + + tx := 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.stoppedTime as stoppedTime, wsi.creationTime as creationTime"). + Joins(fmt.Sprintf("LEFT JOIN %s AS ws ON wsi.workspaceId = ws.id", (&Workspace{}).TableName())). + Where( + conn.Where("wsi.stoppedTime >= ?", TimeToISO8601(from)).Or("wsi.stoppedTime = ?", ""), + ). + Where("wsi.creationTime < ?", TimeToISO8601(to)). + Where("wsi.startedTime != ?", ""). + Where("wsi.usageAttributionId != ?", ""). + FindInBatches(&instancesInBatch, 1000, func(_ *gorm.DB, _ int) error { + instances = append(instances, instancesInBatch...) + return nil + }) + if tx.Error != nil { + return nil, fmt.Errorf("failed to list workspace instances: %w", tx.Error) + } + + return instances, nil +} diff --git a/components/usage/pkg/db/workspace_instance_test.go b/components/usage/pkg/db/workspace_instance_test.go index 0159875ef10dd4..35541d04f2e9bf 100644 --- a/components/usage/pkg/db/workspace_instance_test.go +++ b/components/usage/pkg/db/workspace_instance_test.go @@ -19,6 +19,10 @@ func TestListWorkspaceInstancesInRange(t *testing.T) { conn := dbtest.ConnectForTests(t) workspaceID := "gitpodio-gitpod-gyjr82jkfnd" + _ = dbtest.CreateWorkspaces(t, conn, dbtest.NewWorkspace(t, db.Workspace{ + ID: workspaceID, + })) + valid := []db.WorkspaceInstance{ // In the middle of May dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ From fccb8c7b711b1f7935d5c6e0396e9741fb79a8d3 Mon Sep 17 00:00:00 2001 From: Milan Pavlik Date: Tue, 12 Jul 2022 12:01:45 +0000 Subject: [PATCH 2/4] Fix up --- components/usage/pkg/db/workspace_instance.go | 69 +++++++------------ 1 file changed, 24 insertions(+), 45 deletions(-) diff --git a/components/usage/pkg/db/workspace_instance.go b/components/usage/pkg/db/workspace_instance.go index 057b06da9a1b4a..92291e383bf513 100644 --- a/components/usage/pkg/db/workspace_instance.go +++ b/components/usage/pkg/db/workspace_instance.go @@ -56,26 +56,30 @@ func (i *WorkspaceInstance) TableName() string { // - running // - instances which only just terminated after the start period // - instances which only just started in the period specified -//func ListWorkspaceInstancesInRange(ctx context.Context, conn *gorm.DB, from, to time.Time) ([]WorkspaceInstance, error) { -// var instances []WorkspaceInstance -// var instancesInBatch []WorkspaceInstance -// tx := conn.WithContext(ctx). -// Where( -// conn.Where("stoppedTime >= ?", TimeToISO8601(from)).Or("stoppedTime = ?", ""), -// ). -// Where("creationTime < ?", TimeToISO8601(to)). -// Where("startedTime != ?", ""). -// Where("usageAttributionId != ?", ""). -// FindInBatches(&instancesInBatch, 1000, func(_ *gorm.DB, _ int) error { -// instances = append(instances, instancesInBatch...) -// return nil -// }) -// if tx.Error != nil { -// return nil, fmt.Errorf("failed to list workspace instances: %w", tx.Error) -// } -// -// return instances, nil -//} +func ListWorkspaceInstancesInRange(ctx context.Context, conn *gorm.DB, from, to time.Time) ([]WorkspaceInstanceForUsage, error) { + var instances []WorkspaceInstanceForUsage + var instancesInBatch []WorkspaceInstanceForUsage + + tx := 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.stoppedTime as stoppedTime, wsi.creationTime as creationTime"). + Joins(fmt.Sprintf("LEFT JOIN %s AS ws ON wsi.workspaceId = ws.id", (&Workspace{}).TableName())). + Where( + conn.Where("wsi.stoppedTime >= ?", TimeToISO8601(from)).Or("wsi.stoppedTime = ?", ""), + ). + Where("wsi.creationTime < ?", TimeToISO8601(to)). + Where("wsi.startedTime != ?", ""). + Where("wsi.usageAttributionId != ?", ""). + FindInBatches(&instancesInBatch, 1000, func(_ *gorm.DB, _ int) error { + instances = append(instances, instancesInBatch...) + return nil + }) + if tx.Error != nil { + return nil, fmt.Errorf("failed to list workspace instances: %w", tx.Error) + } + + return instances, nil +} const ( AttributionEntity_User = "user" @@ -136,28 +140,3 @@ func (i *WorkspaceInstanceForUsage) WorkspaceRuntimeSeconds(maxStopTime time.Tim return int64(stop.Sub(start).Round(time.Second).Seconds()) } - -func ListWorkspaceInstancesInRange(ctx context.Context, conn *gorm.DB, from, to time.Time) ([]WorkspaceInstanceForUsage, error) { - var instances []WorkspaceInstanceForUsage - var instancesInBatch []WorkspaceInstanceForUsage - - tx := 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.stoppedTime as stoppedTime, wsi.creationTime as creationTime"). - Joins(fmt.Sprintf("LEFT JOIN %s AS ws ON wsi.workspaceId = ws.id", (&Workspace{}).TableName())). - Where( - conn.Where("wsi.stoppedTime >= ?", TimeToISO8601(from)).Or("wsi.stoppedTime = ?", ""), - ). - Where("wsi.creationTime < ?", TimeToISO8601(to)). - Where("wsi.startedTime != ?", ""). - Where("wsi.usageAttributionId != ?", ""). - FindInBatches(&instancesInBatch, 1000, func(_ *gorm.DB, _ int) error { - instances = append(instances, instancesInBatch...) - return nil - }) - if tx.Error != nil { - return nil, fmt.Errorf("failed to list workspace instances: %w", tx.Error) - } - - return instances, nil -} From 0ffb7bbb39e8bf12b8b968fc0036d252371c3006 Mon Sep 17 00:00:00 2001 From: Milan Pavlik Date: Tue, 12 Jul 2022 12:13:56 +0000 Subject: [PATCH 3/4] fix up --- .../usage/pkg/controller/reconciler_test.go | 54 +++++++++++-------- .../usage/pkg/db/workspace_instance_test.go | 23 ++++---- 2 files changed, 43 insertions(+), 34 deletions(-) diff --git a/components/usage/pkg/controller/reconciler_test.go b/components/usage/pkg/controller/reconciler_test.go index d8c8c32b918740..ecc0c6b1a46e9f 100644 --- a/components/usage/pkg/controller/reconciler_test.go +++ b/components/usage/pkg/controller/reconciler_test.go @@ -79,34 +79,45 @@ func TestUsageReport_CreditSummaryForTeams(t *testing.T) { }{ { Name: "no instances in report, no summary", - Report: map[db.AttributionID][]db.WorkspaceInstance{}, + Report: map[db.AttributionID][]db.WorkspaceInstanceForUsage{}, Expected: map[string]int64{}, }, { Name: "skips user attributions", - Report: map[db.AttributionID][]db.WorkspaceInstance{ + Report: map[db.AttributionID][]db.WorkspaceInstanceForUsage{ db.NewUserAttributionID(uuid.New().String()): { - dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{}), + db.WorkspaceInstanceForUsage{ + UsageAttributionID: db.NewUserAttributionID(uuid.New().String()), + }, + //dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{}), }, }, Expected: map[string]int64{}, }, { Name: "two workspace instances", - Report: map[db.AttributionID][]db.WorkspaceInstance{ + Report: map[db.AttributionID][]db.WorkspaceInstanceForUsage{ teamAttributionID: { - dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ + //db.WorkspaceInstanceForUsage{ + // UsageAttributionID: teamAttributionID, + // WorkspaceClass: defaultWorkspaceClass, + // CreationTime: db.NewVarcharTime(time.Date(2022, 05, 30, 00, 00, 00, 00, time.UTC)), + // StoppedTime: db.NewVarcharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)), + //}, + db.WorkspaceInstanceForUsage{ // has 1 day and 23 hours of usage - WorkspaceClass: defaultWorkspaceClass, - CreationTime: db.NewVarcharTime(time.Date(2022, 05, 30, 00, 00, 00, 00, time.UTC)), - StoppedTime: db.NewVarcharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)), - }), - dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ + UsageAttributionID: teamAttributionID, + WorkspaceClass: defaultWorkspaceClass, + CreationTime: db.NewVarcharTime(time.Date(2022, 05, 30, 00, 00, 00, 00, time.UTC)), + StoppedTime: db.NewVarcharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)), + }, + db.WorkspaceInstanceForUsage{ // has 1 hour of usage - WorkspaceClass: defaultWorkspaceClass, - CreationTime: db.NewVarcharTime(time.Date(2022, 05, 30, 00, 00, 00, 00, time.UTC)), - StoppedTime: db.NewVarcharTime(time.Date(2022, 05, 30, 1, 0, 0, 0, time.UTC)), - }), + UsageAttributionID: teamAttributionID, + WorkspaceClass: defaultWorkspaceClass, + CreationTime: db.NewVarcharTime(time.Date(2022, 05, 30, 00, 00, 00, 00, time.UTC)), + StoppedTime: db.NewVarcharTime(time.Date(2022, 05, 30, 1, 0, 0, 0, time.UTC)), + }, }, }, Expected: map[string]int64{ @@ -116,14 +127,15 @@ func TestUsageReport_CreditSummaryForTeams(t *testing.T) { }, { Name: "unknown workspace class uses default", - Report: map[db.AttributionID][]db.WorkspaceInstance{ + Report: map[db.AttributionID][]db.WorkspaceInstanceForUsage{ teamAttributionID: { - dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ - // has 1 hour of usage - WorkspaceClass: "yolo-workspace-class", - CreationTime: db.NewVarcharTime(time.Date(2022, 05, 30, 00, 00, 00, 00, time.UTC)), - StoppedTime: db.NewVarcharTime(time.Date(2022, 05, 30, 1, 0, 0, 0, time.UTC)), - }), + // has 1 hour of usage + db.WorkspaceInstanceForUsage{ + WorkspaceClass: "yolo-workspace-class", + UsageAttributionID: teamAttributionID, + CreationTime: db.NewVarcharTime(time.Date(2022, 05, 30, 00, 00, 00, 00, time.UTC)), + StoppedTime: db.NewVarcharTime(time.Date(2022, 05, 30, 1, 0, 0, 0, time.UTC)), + }, }, }, Expected: map[string]int64{ diff --git a/components/usage/pkg/db/workspace_instance_test.go b/components/usage/pkg/db/workspace_instance_test.go index 35541d04f2e9bf..7608ac345b608f 100644 --- a/components/usage/pkg/db/workspace_instance_test.go +++ b/components/usage/pkg/db/workspace_instance_test.go @@ -18,15 +18,12 @@ import ( func TestListWorkspaceInstancesInRange(t *testing.T) { conn := dbtest.ConnectForTests(t) - workspaceID := "gitpodio-gitpod-gyjr82jkfnd" - _ = dbtest.CreateWorkspaces(t, conn, dbtest.NewWorkspace(t, db.Workspace{ - ID: workspaceID, - })) + workspace := dbtest.CreateWorkspaces(t, conn, dbtest.NewWorkspace(t, db.Workspace{}))[0] valid := []db.WorkspaceInstance{ // In the middle of May dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ - WorkspaceID: workspaceID, + WorkspaceID: workspace.ID, CreationTime: db.NewVarcharTime(time.Date(2022, 05, 15, 12, 00, 00, 00, time.UTC)), StartedTime: db.NewVarcharTime(time.Date(2022, 05, 15, 12, 00, 00, 00, time.UTC)), StoppedTime: db.NewVarcharTime(time.Date(2022, 05, 15, 13, 00, 00, 00, time.UTC)), @@ -34,7 +31,7 @@ func TestListWorkspaceInstancesInRange(t *testing.T) { // Start of May dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), - WorkspaceID: workspaceID, + WorkspaceID: workspace.ID, CreationTime: db.NewVarcharTime(time.Date(2022, 05, 1, 0, 00, 00, 00, time.UTC)), StartedTime: db.NewVarcharTime(time.Date(2022, 05, 1, 0, 00, 00, 00, time.UTC)), StoppedTime: db.NewVarcharTime(time.Date(2022, 05, 1, 1, 00, 00, 00, time.UTC)), @@ -42,7 +39,7 @@ func TestListWorkspaceInstancesInRange(t *testing.T) { // End of May dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), - WorkspaceID: workspaceID, + WorkspaceID: workspace.ID, CreationTime: db.NewVarcharTime(time.Date(2022, 05, 31, 23, 00, 00, 00, time.UTC)), StartedTime: db.NewVarcharTime(time.Date(2022, 05, 31, 23, 00, 00, 00, time.UTC)), StoppedTime: db.NewVarcharTime(time.Date(2022, 05, 31, 23, 59, 59, 999999, time.UTC)), @@ -50,7 +47,7 @@ func TestListWorkspaceInstancesInRange(t *testing.T) { // Started in April, but continued into May dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), - WorkspaceID: workspaceID, + WorkspaceID: workspace.ID, CreationTime: db.NewVarcharTime(time.Date(2022, 04, 30, 23, 00, 00, 00, time.UTC)), StartedTime: db.NewVarcharTime(time.Date(2022, 04, 30, 23, 00, 00, 00, time.UTC)), StoppedTime: db.NewVarcharTime(time.Date(2022, 05, 1, 0, 0, 0, 0, time.UTC)), @@ -58,7 +55,7 @@ func TestListWorkspaceInstancesInRange(t *testing.T) { // Started in May, but continued into June dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), - WorkspaceID: workspaceID, + WorkspaceID: workspace.ID, CreationTime: db.NewVarcharTime(time.Date(2022, 05, 31, 23, 00, 00, 00, time.UTC)), StartedTime: db.NewVarcharTime(time.Date(2022, 05, 31, 23, 00, 00, 00, time.UTC)), StoppedTime: db.NewVarcharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)), @@ -66,7 +63,7 @@ func TestListWorkspaceInstancesInRange(t *testing.T) { // Started in April, but continued into June (ran for all of May) dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), - WorkspaceID: workspaceID, + WorkspaceID: workspace.ID, CreationTime: db.NewVarcharTime(time.Date(2022, 04, 31, 23, 00, 00, 00, time.UTC)), StartedTime: db.NewVarcharTime(time.Date(2022, 04, 31, 23, 00, 00, 00, time.UTC)), StoppedTime: db.NewVarcharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)), @@ -74,14 +71,14 @@ func TestListWorkspaceInstancesInRange(t *testing.T) { // Stopped in May, no creation time, should be retrieved but this is a poor data quality record. dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), - WorkspaceID: workspaceID, + WorkspaceID: workspace.ID, StartedTime: db.NewVarcharTime(time.Date(2022, 05, 1, 1, 0, 0, 0, time.UTC)), StoppedTime: db.NewVarcharTime(time.Date(2022, 05, 1, 1, 0, 0, 0, time.UTC)), }), // Started in April, no stop time, still running dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), - WorkspaceID: workspaceID, + WorkspaceID: workspace.ID, CreationTime: db.NewVarcharTime(time.Date(2022, 04, 31, 23, 00, 00, 00, time.UTC)), StartedTime: db.NewVarcharTime(time.Date(2022, 04, 31, 23, 00, 00, 00, time.UTC)), }), @@ -90,7 +87,7 @@ func TestListWorkspaceInstancesInRange(t *testing.T) { // Start of June dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), - WorkspaceID: workspaceID, + WorkspaceID: workspace.ID, CreationTime: db.NewVarcharTime(time.Date(2022, 06, 1, 00, 00, 00, 00, time.UTC)), StartedTime: db.NewVarcharTime(time.Date(2022, 06, 1, 00, 00, 00, 00, time.UTC)), StoppedTime: db.NewVarcharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)), From 7f8eb571c8f686a92f80b958107a7bcbac73010e Mon Sep 17 00:00:00 2001 From: Milan Pavlik Date: Tue, 12 Jul 2022 12:14:54 +0000 Subject: [PATCH 4/4] fix up --- components/usage/pkg/controller/reconciler_test.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/components/usage/pkg/controller/reconciler_test.go b/components/usage/pkg/controller/reconciler_test.go index ecc0c6b1a46e9f..5184041a74f4b7 100644 --- a/components/usage/pkg/controller/reconciler_test.go +++ b/components/usage/pkg/controller/reconciler_test.go @@ -89,7 +89,6 @@ func TestUsageReport_CreditSummaryForTeams(t *testing.T) { db.WorkspaceInstanceForUsage{ UsageAttributionID: db.NewUserAttributionID(uuid.New().String()), }, - //dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{}), }, }, Expected: map[string]int64{}, @@ -98,12 +97,6 @@ func TestUsageReport_CreditSummaryForTeams(t *testing.T) { Name: "two workspace instances", Report: map[db.AttributionID][]db.WorkspaceInstanceForUsage{ teamAttributionID: { - //db.WorkspaceInstanceForUsage{ - // UsageAttributionID: teamAttributionID, - // WorkspaceClass: defaultWorkspaceClass, - // CreationTime: db.NewVarcharTime(time.Date(2022, 05, 30, 00, 00, 00, 00, time.UTC)), - // StoppedTime: db.NewVarcharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)), - //}, db.WorkspaceInstanceForUsage{ // has 1 day and 23 hours of usage UsageAttributionID: teamAttributionID,