Skip to content

Commit 8b88f26

Browse files
easyCZroboquat
authored andcommitted
[usage] Use workspace class when computing credits used
1 parent 2cb9ed7 commit 8b88f26

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

components/usage/pkg/controller/reconciler.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ func (u UsageReport) CreditSummaryForTeams(pricer *WorkspacePricer, maxStopTime
128128
var credits int64
129129
for _, instance := range instances {
130130
runtime := instance.WorkspaceRuntimeSeconds(maxStopTime)
131-
class := "default"
131+
class := defaultWorkspaceClass
132+
if instance.WorkspaceClass != "" {
133+
class = instance.WorkspaceClass
134+
}
132135
credits += pricer.Credits(class, runtime)
133136
}
134137

components/usage/pkg/controller/reconciler_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,78 @@ func TestUsageReconciler_ReconcileTimeRange(t *testing.T) {
6565
InvalidWorkspaceInstances: 1,
6666
}, status)
6767
}
68+
69+
func TestUsageReport_CreditSummaryForTeams(t *testing.T) {
70+
maxStopTime := time.Date(2022, 05, 31, 23, 00, 00, 00, time.UTC)
71+
72+
teamID := uuid.New().String()
73+
teamAttributionID := db.NewTeamAttributionID(teamID)
74+
75+
scenarios := []struct {
76+
Name string
77+
Report UsageReport
78+
Expected map[string]int64
79+
}{
80+
{
81+
Name: "no instances in report, no summary",
82+
Report: map[db.AttributionID][]db.WorkspaceInstance{},
83+
Expected: map[string]int64{},
84+
},
85+
{
86+
Name: "skips user attributions",
87+
Report: map[db.AttributionID][]db.WorkspaceInstance{
88+
db.NewUserAttributionID(uuid.New().String()): {
89+
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{}),
90+
},
91+
},
92+
Expected: map[string]int64{},
93+
},
94+
{
95+
Name: "two workspace instances",
96+
Report: map[db.AttributionID][]db.WorkspaceInstance{
97+
teamAttributionID: {
98+
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
99+
// has 1 day and 23 hours of usage
100+
WorkspaceClass: defaultWorkspaceClass,
101+
CreationTime: db.NewVarcharTime(time.Date(2022, 05, 30, 00, 00, 00, 00, time.UTC)),
102+
StoppedTime: db.NewVarcharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)),
103+
}),
104+
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
105+
// has 1 hour of usage
106+
WorkspaceClass: defaultWorkspaceClass,
107+
CreationTime: db.NewVarcharTime(time.Date(2022, 05, 30, 00, 00, 00, 00, time.UTC)),
108+
StoppedTime: db.NewVarcharTime(time.Date(2022, 05, 30, 1, 0, 0, 0, time.UTC)),
109+
}),
110+
},
111+
},
112+
Expected: map[string]int64{
113+
// total of 2 days runtime, at 10 credits per hour, that's 480 credits
114+
teamID: 480,
115+
},
116+
},
117+
{
118+
Name: "unknown workspace class uses default",
119+
Report: map[db.AttributionID][]db.WorkspaceInstance{
120+
teamAttributionID: {
121+
dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
122+
// has 1 hour of usage
123+
WorkspaceClass: "yolo-workspace-class",
124+
CreationTime: db.NewVarcharTime(time.Date(2022, 05, 30, 00, 00, 00, 00, time.UTC)),
125+
StoppedTime: db.NewVarcharTime(time.Date(2022, 05, 30, 1, 0, 0, 0, time.UTC)),
126+
}),
127+
},
128+
},
129+
Expected: map[string]int64{
130+
// total of 1 hour usage, at default cost of 10 credits per hour
131+
teamID: 10,
132+
},
133+
},
134+
}
135+
136+
for _, s := range scenarios {
137+
t.Run(s.Name, func(t *testing.T) {
138+
actual := s.Report.CreditSummaryForTeams(DefaultWorkspacePricer, maxStopTime)
139+
require.Equal(t, s.Expected, actual)
140+
})
141+
}
142+
}

0 commit comments

Comments
 (0)