@@ -22,6 +22,7 @@ import (
22
22
"github.com/coder/coder/v2/coderd/database/pubsub"
23
23
"github.com/coder/coder/v2/coderd/prometheusmetrics"
24
24
"github.com/coder/coder/v2/coderd/schedule"
25
+ "github.com/coder/coder/v2/coderd/workspacestats"
25
26
"github.com/coder/coder/v2/codersdk"
26
27
"github.com/coder/coder/v2/testutil"
27
28
)
@@ -129,21 +130,24 @@ func TestUpdateStates(t *testing.T) {
129
130
AgentFn : func (context.Context ) (database.WorkspaceAgent , error ) {
130
131
return agent , nil
131
132
},
132
- Database : dbM ,
133
- Pubsub : ps ,
134
- StatsBatcher : batcher ,
135
- TemplateScheduleStore : templateScheduleStorePtr (templateScheduleStore ),
133
+ Database : dbM ,
134
+ StatsReporter : workspacestats .NewReporter (workspacestats.ReporterOptions {
135
+ Database : dbM ,
136
+ Pubsub : ps ,
137
+ StatsBatcher : batcher ,
138
+ TemplateScheduleStore : templateScheduleStorePtr (templateScheduleStore ),
139
+ UpdateAgentMetricsFn : func (ctx context.Context , labels prometheusmetrics.AgentMetricLabels , metrics []* agentproto.Stats_Metric ) {
140
+ updateAgentMetricsFnCalled = true
141
+ assert .Equal (t , prometheusmetrics.AgentMetricLabels {
142
+ Username : user .Username ,
143
+ WorkspaceName : workspace .Name ,
144
+ AgentName : agent .Name ,
145
+ TemplateName : template .Name ,
146
+ }, labels )
147
+ assert .Equal (t , req .Stats .Metrics , metrics )
148
+ },
149
+ }),
136
150
AgentStatsRefreshInterval : 10 * time .Second ,
137
- UpdateAgentMetricsFn : func (ctx context.Context , labels prometheusmetrics.AgentMetricLabels , metrics []* agentproto.Stats_Metric ) {
138
- updateAgentMetricsFnCalled = true
139
- assert .Equal (t , prometheusmetrics.AgentMetricLabels {
140
- Username : user .Username ,
141
- WorkspaceName : workspace .Name ,
142
- AgentName : agent .Name ,
143
- TemplateName : template .Name ,
144
- }, labels )
145
- assert .Equal (t , req .Stats .Metrics , metrics )
146
- },
147
151
TimeNowFn : func () time.Time {
148
152
return now
149
153
},
@@ -232,13 +236,16 @@ func TestUpdateStates(t *testing.T) {
232
236
AgentFn : func (context.Context ) (database.WorkspaceAgent , error ) {
233
237
return agent , nil
234
238
},
235
- Database : dbM ,
236
- Pubsub : ps ,
237
- StatsBatcher : batcher ,
238
- TemplateScheduleStore : templateScheduleStorePtr (templateScheduleStore ),
239
+ Database : dbM ,
240
+ StatsReporter : workspacestats .NewReporter (workspacestats.ReporterOptions {
241
+ Database : dbM ,
242
+ Pubsub : ps ,
243
+ StatsBatcher : batcher ,
244
+ TemplateScheduleStore : templateScheduleStorePtr (templateScheduleStore ),
245
+ // Ignored when nil.
246
+ UpdateAgentMetricsFn : nil ,
247
+ }),
239
248
AgentStatsRefreshInterval : 10 * time .Second ,
240
- // Ignored when nil.
241
- UpdateAgentMetricsFn : nil ,
242
249
TimeNowFn : func () time.Time {
243
250
return now
244
251
},
@@ -274,12 +281,15 @@ func TestUpdateStates(t *testing.T) {
274
281
AgentFn : func (context.Context ) (database.WorkspaceAgent , error ) {
275
282
return agent , nil
276
283
},
277
- Database : dbM ,
278
- Pubsub : ps ,
279
- StatsBatcher : nil , // should not be called
280
- TemplateScheduleStore : nil , // should not be called
284
+ Database : dbM ,
285
+ StatsReporter : workspacestats .NewReporter (workspacestats.ReporterOptions {
286
+ Database : dbM ,
287
+ Pubsub : ps ,
288
+ StatsBatcher : nil , // should not be called
289
+ TemplateScheduleStore : nil , // should not be called
290
+ UpdateAgentMetricsFn : nil , // should not be called
291
+ }),
281
292
AgentStatsRefreshInterval : 10 * time .Second ,
282
- UpdateAgentMetricsFn : nil , // should not be called
283
293
TimeNowFn : func () time.Time {
284
294
panic ("should not be called" )
285
295
},
@@ -343,21 +353,24 @@ func TestUpdateStates(t *testing.T) {
343
353
AgentFn : func (context.Context ) (database.WorkspaceAgent , error ) {
344
354
return agent , nil
345
355
},
346
- Database : dbM ,
347
- Pubsub : ps ,
348
- StatsBatcher : batcher ,
349
- TemplateScheduleStore : templateScheduleStorePtr (templateScheduleStore ),
356
+ Database : dbM ,
357
+ StatsReporter : workspacestats .NewReporter (workspacestats.ReporterOptions {
358
+ Database : dbM ,
359
+ Pubsub : ps ,
360
+ StatsBatcher : batcher ,
361
+ TemplateScheduleStore : templateScheduleStorePtr (templateScheduleStore ),
362
+ UpdateAgentMetricsFn : func (ctx context.Context , labels prometheusmetrics.AgentMetricLabels , metrics []* agentproto.Stats_Metric ) {
363
+ updateAgentMetricsFnCalled = true
364
+ assert .Equal (t , prometheusmetrics.AgentMetricLabels {
365
+ Username : user .Username ,
366
+ WorkspaceName : workspace .Name ,
367
+ AgentName : agent .Name ,
368
+ TemplateName : template .Name ,
369
+ }, labels )
370
+ assert .Equal (t , req .Stats .Metrics , metrics )
371
+ },
372
+ }),
350
373
AgentStatsRefreshInterval : 15 * time .Second ,
351
- UpdateAgentMetricsFn : func (ctx context.Context , labels prometheusmetrics.AgentMetricLabels , metrics []* agentproto.Stats_Metric ) {
352
- updateAgentMetricsFnCalled = true
353
- assert .Equal (t , prometheusmetrics.AgentMetricLabels {
354
- Username : user .Username ,
355
- WorkspaceName : workspace .Name ,
356
- AgentName : agent .Name ,
357
- TemplateName : template .Name ,
358
- }, labels )
359
- assert .Equal (t , req .Stats .Metrics , metrics )
360
- },
361
374
TimeNowFn : func () time.Time {
362
375
return now
363
376
},
0 commit comments