Skip to content
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
2 changes: 1 addition & 1 deletion components/usage/pkg/db/dbtest/workspace_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewWorkspaceInstance(t *testing.T, instance db.WorkspaceInstance) db.Worksp
id = instance.ID
}

workspaceID := generateWorkspaceID()
workspaceID := GenerateWorkspaceID()
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This also fixes broken master due to a semantic merge.

if instance.WorkspaceID != "" {
workspaceID = instance.WorkspaceID
}
Expand Down
6 changes: 5 additions & 1 deletion components/usage/pkg/db/workspace_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,17 @@ func (i *WorkspaceInstance) TableName() string {
// - 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 != ?", "").
Find(&instances)
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)
}
Expand Down
26 changes: 26 additions & 0 deletions components/usage/pkg/db/workspace_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,29 @@ func TestListWorkspaceInstancesInRange(t *testing.T) {

require.Len(t, retrieved, len(valid))
}

func TestListWorkspaceInstancesInRange_InBatches(t *testing.T) {
conn := db.ConnectForTests(t)

workspaceID := "gitpodio-gitpod-gyjr82jkfnd"
var instances []db.WorkspaceInstance
for i := 0; i < 1100; i++ {
instances = append(instances, dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
ID: uuid.New(),
WorkspaceID: workspaceID,
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)),
}))

}

tx := conn.CreateInBatches(&instances, 1000)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we need to CreateInBatches here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We don't strictly, but felt like I should start a pattern of not doing things with more than 1k entries outside batches

require.NoError(t, tx.Error)

startOfMay := time.Date(2022, 05, 1, 0, 00, 00, 00, time.UTC)
startOfJune := time.Date(2022, 06, 1, 0, 00, 00, 00, time.UTC)
results, err := db.ListWorkspaceInstancesInRange(context.Background(), conn, startOfMay, startOfJune)
require.NoError(t, err)
require.Len(t, results, len(instances))
}