Skip to content

[usage] Fix querying workspace instances to use startedTime existence to filter #10778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2022
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
27 changes: 13 additions & 14 deletions components/usage/pkg/controller/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func TestUsageReconciler_ReconcileTimeRange(t *testing.T) {

startOfMay := time.Date(2022, 05, 1, 0, 00, 00, 00, time.UTC)
startOfJune := time.Date(2022, 06, 1, 0, 00, 00, 00, time.UTC)
instanceStatus := []byte(`{"phase": "stopped", "conditions": {"deployed": false, "pullingImages": false, "serviceExists": false}}`)

scenarios := []Scenario{
(func() Scenario {
Expand All @@ -48,33 +47,33 @@ func TestUsageReconciler_ReconcileTimeRange(t *testing.T) {
})
instances := []db.WorkspaceInstance{
// Ran throughout the reconcile period
{
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
ID: uuid.New(),
WorkspaceID: workspace.ID,
CreationTime: db.NewVarcharTime(time.Date(2022, 05, 1, 00, 00, 00, 00, time.UTC)),
StartedTime: db.NewVarcharTime(time.Date(2022, 05, 1, 00, 00, 00, 00, time.UTC)),
StoppedTime: db.NewVarcharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)),
Status: instanceStatus,
},
}),
// Still running
{
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
ID: uuid.New(),
WorkspaceID: workspace.ID,
CreationTime: db.NewVarcharTime(time.Date(2022, 05, 30, 00, 00, 00, 00, time.UTC)),
Status: instanceStatus,
},
StartedTime: db.NewVarcharTime(time.Date(2022, 05, 30, 00, 00, 00, 00, time.UTC)),
}),
// No creation time, invalid record
{
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
ID: uuid.New(),
WorkspaceID: workspace.ID,
StartedTime: db.NewVarcharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)),
StoppedTime: db.NewVarcharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)),
Status: instanceStatus,
},
}),
}

expectedRuntime := instances[0].WorkspaceRuntimeSeconds(scenarioRunTime) + instances[1].WorkspaceRuntimeSeconds(scenarioRunTime)

return Scenario{
Name: "oen team with one workspace",
Name: "one team with one workspace",
Memberships: []db.TeamMembership{membership},
Workspaces: []db.Workspace{workspace},
Instances: instances,
Expand All @@ -101,13 +100,13 @@ func TestUsageReconciler_ReconcileTimeRange(t *testing.T) {
workspaceID := "gitpodio-gitpod-gyjr82jkfnd"
var instances []db.WorkspaceInstance
for i := 0; i < 100; i++ {
instances = append(instances, db.WorkspaceInstance{
instances = append(instances, dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
ID: uuid.New(),
WorkspaceID: workspaceID,
CreationTime: db.NewVarcharTime(time.Date(2022, 05, 01, 00, 00, 00, 00, time.UTC)),
StartedTime: db.NewVarcharTime(time.Date(2022, 05, 01, 00, 00, 00, 00, time.UTC)),
StoppedTime: db.NewVarcharTime(time.Date(2022, 05, 31, 23, 59, 59, 999999, time.UTC)),
Status: instanceStatus,
})
}))
}

return Scenario{
Expand Down
83 changes: 83 additions & 0 deletions components/usage/pkg/db/dbtest/workspace_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package dbtest

import (
"database/sql"
"github.com/gitpod-io/gitpod/usage/pkg/db"
"github.com/google/uuid"
"testing"
"time"
)

var (
workspaceInstanceStatus = `{"phase": "stopped", "conditions": {"deployed": false, "pullingImages": false, "serviceExists": false}}`
)

func NewWorkspaceInstance(t *testing.T, instance db.WorkspaceInstance) db.WorkspaceInstance {
t.Helper()

id := uuid.New()
if instance.ID.ID() != 0 { // empty value
id = instance.ID
}

workspaceID := generateWorkspaceID()
if instance.WorkspaceID != "" {
workspaceID = instance.WorkspaceID
}

creationTime := db.VarcharTime{}
if instance.CreationTime.IsSet() {
creationTime = instance.CreationTime
}

startedTime := db.VarcharTime{}
if instance.StartedTime.IsSet() {
startedTime = instance.StartedTime
}

deployedTime := db.VarcharTime{}
if instance.DeployedTime.IsSet() {
deployedTime = instance.DeployedTime
}

stoppedTime := db.VarcharTime{}
if instance.StoppedTime.IsSet() {
stoppedTime = instance.StoppedTime
}

stoppingTime := db.VarcharTime{}
if instance.StoppingTime.IsSet() {
stoppingTime = instance.StoppingTime
}

status := []byte(workspaceInstanceStatus)
if instance.Status.String() != "" {
status = instance.Status
}

return db.WorkspaceInstance{
ID: id,
WorkspaceID: workspaceID,
Configuration: nil,
Region: "",
ImageBuildInfo: sql.NullString{},
IdeURL: "",
WorkspaceBaseImage: "",
WorkspaceImage: "",
CreationTime: creationTime,
StartedTime: startedTime,
DeployedTime: deployedTime,
StoppedTime: stoppedTime,
LastModified: time.Time{},
StoppingTime: stoppingTime,
LastHeartbeat: "",
StatusOld: sql.NullString{},
Status: status,
Phase: sql.NullString{},
PhasePersisted: "",
}
}
12 changes: 7 additions & 5 deletions components/usage/pkg/db/workspace_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ type WorkspaceInstance struct {
// 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.StartedTime.Time()
start := i.CreationTime.Time()
stop := maxStopTime

if i.StoppedTime.IsSet() {
stop = i.StoppedTime.Time()
if i.StoppedTime.Time().Before(maxStopTime) {
stop = i.StoppedTime.Time()
}
}

return int64(stop.Sub(start).Round(time.Second).Seconds())
Expand All @@ -71,9 +74,8 @@ func ListWorkspaceInstancesInRange(ctx context.Context, conn *gorm.DB, from, to
Where(
conn.Where("stoppedTime >= ?", TimeToISO8601(from)).Or("stoppedTime = ?", ""),
).
Where(
conn.Where("creationTime < ?", TimeToISO8601(to)).Or("creationTime = ?", ""),
).
Where("creationTime < ?", TimeToISO8601(to)).
Where("startedTime != ?", "").
Find(&instances)
if tx.Error != nil {
return nil, fmt.Errorf("failed to list workspace instances: %w", tx.Error)
Expand Down
65 changes: 32 additions & 33 deletions components/usage/pkg/db/workspace_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"fmt"
"github.com/gitpod-io/gitpod/usage/pkg/db"
"github.com/gitpod-io/gitpod/usage/pkg/db/dbtest"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
Expand Down Expand Up @@ -100,88 +101,86 @@ func TestListWorkspaceInstancesInRange(t *testing.T) {
conn := db.ConnectForTests(t)

workspaceID := "gitpodio-gitpod-gyjr82jkfnd"
status := []byte(`{"phase": "stopped", "conditions": {"deployed": false, "pullingImages": false, "serviceExists": false}}`)
valid := []*db.WorkspaceInstance{
valid := []db.WorkspaceInstance{
// In the middle of May
{
ID: uuid.New(),
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
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)),
Status: status,
},
}),
// Start of May
{
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
ID: uuid.New(),
WorkspaceID: workspaceID,
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)),
Status: status,
},
}),
// End of May
{
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
ID: uuid.New(),
WorkspaceID: workspaceID,
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)),
Status: status,
},
}),
// Started in April, but continued into May
{
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
ID: uuid.New(),
WorkspaceID: workspaceID,
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)),
Status: status,
},
}),
// Started in May, but continued into June
{
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
ID: uuid.New(),
WorkspaceID: workspaceID,
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)),
Status: status,
},
}),
// Started in April, but continued into June (ran for all of May)
{
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
ID: uuid.New(),
WorkspaceID: workspaceID,
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)),
Status: status,
},
}),
// 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,
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)),
Status: status,
},
}),
// Started in April, no stop time, still running
{
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
ID: uuid.New(),
WorkspaceID: workspaceID,
CreationTime: db.NewVarcharTime(time.Date(2022, 04, 31, 23, 00, 00, 00, time.UTC)),
Status: status,
},
StartedTime: db.NewVarcharTime(time.Date(2022, 04, 31, 23, 00, 00, 00, time.UTC)),
}),
}
invalid := []*db.WorkspaceInstance{
invalid := []db.WorkspaceInstance{
// Start of June
{
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
ID: uuid.New(),
WorkspaceID: workspaceID,
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)),
Status: status,
},
}),
}

var all []*db.WorkspaceInstance
var all []db.WorkspaceInstance
all = append(all, valid...)
all = append(all, invalid...)

for _, instance := range all {
tx := conn.Create(instance)
tx := conn.Create(&instance)
require.NoError(t, tx.Error)
}

Expand Down