diff --git a/components/gitpod-db/go/workspace_instance.go b/components/gitpod-db/go/workspace_instance.go index 6201a050207fd4..9af32258be07fe 100644 --- a/components/gitpod-db/go/workspace_instance.go +++ b/components/gitpod-db/go/workspace_instance.go @@ -80,10 +80,12 @@ func FindRunningWorkspaceInstances(ctx context.Context, conn *gorm.DB) ([]Worksp var instancesInBatch []WorkspaceInstanceForUsage tx := queryWorkspaceInstanceForUsage(ctx, conn). - Where("wsi.stoppingTime = ?", ""). - Where("wsi.usageAttributionId != ?", ""). + Where("wsi.phasePersisted = ?", "running"). // We are only interested in instances that have been started within the last 10 days. Where("wsi.startedTime > ?", TimeToISO8601(time.Now().Add(-10*24*time.Hour))). + // All other selectors are there to ensure data quality + Where("wsi.stoppingTime = ?", ""). + Where("wsi.usageAttributionId != ?", ""). FindInBatches(&instancesInBatch, 1000, func(_ *gorm.DB, _ int) error { instances = append(instances, instancesInBatch...) return nil diff --git a/components/gitpod-db/go/workspace_instance_test.go b/components/gitpod-db/go/workspace_instance_test.go index 0a433f585bf6e5..75e8c20ca4b34c 100644 --- a/components/gitpod-db/go/workspace_instance_test.go +++ b/components/gitpod-db/go/workspace_instance_test.go @@ -138,19 +138,22 @@ func TestFindRunningWorkspace(t *testing.T) { }), // one unstopped before 10 days ago dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ - WorkspaceID: workspace.ID, - StartedTime: db.NewVarCharTime(moreThan10DaysAgo), + WorkspaceID: workspace.ID, + StartedTime: db.NewVarCharTime(moreThan10DaysAgo), + PhasePersisted: "running", }), // Two running instances dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ - ID: uuid.New(), - WorkspaceID: workspace.ID, - StartedTime: db.NewVarCharTime(tenMinAgo), + ID: uuid.New(), + WorkspaceID: workspace.ID, + StartedTime: db.NewVarCharTime(tenMinAgo), + PhasePersisted: "running", }), dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ - ID: uuid.New(), - WorkspaceID: workspace.ID, - StartedTime: db.NewVarCharTime(tenMinAgo), + ID: uuid.New(), + WorkspaceID: workspace.ID, + StartedTime: db.NewVarCharTime(tenMinAgo), + PhasePersisted: "running", }), } diff --git a/components/usage/pkg/apiv1/usage.go b/components/usage/pkg/apiv1/usage.go index e0092c51461300..ca073c9290ad4d 100644 --- a/components/usage/pkg/apiv1/usage.go +++ b/components/usage/pkg/apiv1/usage.go @@ -355,13 +355,13 @@ func reconcileUsage(instances []db.WorkspaceInstanceForUsage, drafts []db.Usage, instancesByID := dedupeWorkspaceInstancesForUsage(instances) - draftsByWorkspaceID := map[uuid.UUID]db.Usage{} + draftsByInstanceID := map[uuid.UUID]db.Usage{} for _, draft := range drafts { - draftsByWorkspaceID[*draft.WorkspaceInstanceID] = draft + draftsByInstanceID[*draft.WorkspaceInstanceID] = draft } for instanceID, instance := range instancesByID { - if usage, exists := draftsByWorkspaceID[instanceID]; exists { + if usage, exists := draftsByInstanceID[instanceID]; exists { updatedUsage, err := updateUsageFromInstance(instance, usage, pricer, now) if err != nil { return nil, nil, fmt.Errorf("failed to construct updated usage record: %w", err)