Skip to content

Commit

Permalink
[usage] FindRunningWorkspaceInstances: Make sure we use an index
Browse files Browse the repository at this point in the history
  • Loading branch information
geropl committed Sep 26, 2023
1 parent 2dd5fcd commit 6061942
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 4 additions & 2 deletions components/gitpod-db/go/workspace_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions components/usage/pkg/apiv1/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6061942

Please sign in to comment.