@@ -70,6 +70,11 @@ func TestUpdateStates(t *testing.T) {
70
70
}
71
71
batcher = & workspacestatstest.StatsBatcher {}
72
72
updateAgentMetricsFnCalled = false
73
+ tickCh = make (chan time.Time )
74
+ flushCh = make (chan int , 1 )
75
+ wut = workspacestats .NewTracker (dbM ,
76
+ workspacestats .TrackerWithTickFlush (tickCh , flushCh ),
77
+ )
73
78
74
79
req = & agentproto.UpdateStatsRequest {
75
80
Stats : & agentproto.Stats {
@@ -109,6 +114,7 @@ func TestUpdateStates(t *testing.T) {
109
114
Database : dbM ,
110
115
Pubsub : ps ,
111
116
StatsBatcher : batcher ,
117
+ UsageTracker : wut ,
112
118
TemplateScheduleStore : templateScheduleStorePtr (templateScheduleStore ),
113
119
UpdateAgentMetricsFn : func (ctx context.Context , labels prometheusmetrics.AgentMetricLabels , metrics []* agentproto.Stats_Metric ) {
114
120
updateAgentMetricsFnCalled = true
@@ -126,25 +132,26 @@ func TestUpdateStates(t *testing.T) {
126
132
return now
127
133
},
128
134
}
135
+ defer wut .Close ()
129
136
130
137
// Workspace gets fetched.
131
138
dbM .EXPECT ().GetWorkspaceByAgentID (gomock .Any (), agent .ID ).Return (workspace , nil )
132
139
140
+ // User gets fetched to hit the UpdateAgentMetricsFn.
141
+ dbM .EXPECT ().GetUserByID (gomock .Any (), user .ID ).Return (user , nil )
142
+
133
143
// We expect an activity bump because ConnectionCount > 0.
134
144
dbM .EXPECT ().ActivityBumpWorkspace (gomock .Any (), database.ActivityBumpWorkspaceParams {
135
145
WorkspaceID : workspace .ID ,
136
146
NextAutostart : time.Time {}.UTC (),
137
147
}).Return (nil )
138
148
139
149
// Workspace last used at gets bumped.
140
- dbM .EXPECT ().UpdateWorkspaceLastUsedAt (gomock .Any (), database.UpdateWorkspaceLastUsedAtParams {
141
- ID : workspace .ID ,
150
+ dbM .EXPECT ().BatchUpdateWorkspaceLastUsedAt (gomock .Any (), database.BatchUpdateWorkspaceLastUsedAtParams {
151
+ IDs : []uuid. UUID { workspace .ID } ,
142
152
LastUsedAt : now ,
143
153
}).Return (nil )
144
154
145
- // User gets fetched to hit the UpdateAgentMetricsFn.
146
- dbM .EXPECT ().GetUserByID (gomock .Any (), user .ID ).Return (user , nil )
147
-
148
155
// Ensure that pubsub notifications are sent.
149
156
notifyDescription := make (chan []byte )
150
157
ps .Subscribe (codersdk .WorkspaceNotifyChannel (workspace .ID ), func (_ context.Context , description []byte ) {
@@ -159,6 +166,10 @@ func TestUpdateStates(t *testing.T) {
159
166
ReportInterval : durationpb .New (10 * time .Second ),
160
167
}, resp )
161
168
169
+ tickCh <- now
170
+ count := <- flushCh
171
+ require .Equal (t , 1 , count , "expected one flush with one id" )
172
+
162
173
batcher .Mu .Lock ()
163
174
defer batcher .Mu .Unlock ()
164
175
require .Equal (t , int64 (1 ), batcher .Called )
@@ -211,6 +222,7 @@ func TestUpdateStates(t *testing.T) {
211
222
StatsReporter : workspacestats .NewReporter (workspacestats.ReporterOptions {
212
223
Database : dbM ,
213
224
Pubsub : ps ,
225
+ UsageTracker : workspacestats .NewTracker (dbM ),
214
226
StatsBatcher : batcher ,
215
227
TemplateScheduleStore : templateScheduleStorePtr (templateScheduleStore ),
216
228
// Ignored when nil.
@@ -225,12 +237,6 @@ func TestUpdateStates(t *testing.T) {
225
237
// Workspace gets fetched.
226
238
dbM .EXPECT ().GetWorkspaceByAgentID (gomock .Any (), agent .ID ).Return (workspace , nil )
227
239
228
- // Workspace last used at gets bumped.
229
- dbM .EXPECT ().UpdateWorkspaceLastUsedAt (gomock .Any (), database.UpdateWorkspaceLastUsedAtParams {
230
- ID : workspace .ID ,
231
- LastUsedAt : now ,
232
- }).Return (nil )
233
-
234
240
_ , err := api .UpdateStats (context .Background (), req )
235
241
require .NoError (t , err )
236
242
})
@@ -306,6 +312,11 @@ func TestUpdateStates(t *testing.T) {
306
312
}
307
313
batcher = & workspacestatstest.StatsBatcher {}
308
314
updateAgentMetricsFnCalled = false
315
+ tickCh = make (chan time.Time )
316
+ flushCh = make (chan int , 1 )
317
+ wut = workspacestats .NewTracker (dbM ,
318
+ workspacestats .TrackerWithTickFlush (tickCh , flushCh ),
319
+ )
309
320
310
321
req = & agentproto.UpdateStatsRequest {
311
322
Stats : & agentproto.Stats {
@@ -325,6 +336,7 @@ func TestUpdateStates(t *testing.T) {
325
336
StatsReporter : workspacestats .NewReporter (workspacestats.ReporterOptions {
326
337
Database : dbM ,
327
338
Pubsub : ps ,
339
+ UsageTracker : wut ,
328
340
StatsBatcher : batcher ,
329
341
TemplateScheduleStore : templateScheduleStorePtr (templateScheduleStore ),
330
342
UpdateAgentMetricsFn : func (ctx context.Context , labels prometheusmetrics.AgentMetricLabels , metrics []* agentproto.Stats_Metric ) {
@@ -343,6 +355,7 @@ func TestUpdateStates(t *testing.T) {
343
355
return now
344
356
},
345
357
}
358
+ defer wut .Close ()
346
359
347
360
// Workspace gets fetched.
348
361
dbM .EXPECT ().GetWorkspaceByAgentID (gomock .Any (), agent .ID ).Return (workspace , nil )
@@ -355,9 +368,9 @@ func TestUpdateStates(t *testing.T) {
355
368
}).Return (nil )
356
369
357
370
// Workspace last used at gets bumped.
358
- dbM .EXPECT ().UpdateWorkspaceLastUsedAt (gomock .Any (), database.UpdateWorkspaceLastUsedAtParams {
359
- ID : workspace .ID ,
360
- LastUsedAt : now ,
371
+ dbM .EXPECT ().BatchUpdateWorkspaceLastUsedAt (gomock .Any (), database.BatchUpdateWorkspaceLastUsedAtParams {
372
+ IDs : []uuid. UUID { workspace .ID } ,
373
+ LastUsedAt : now . UTC () ,
361
374
}).Return (nil )
362
375
363
376
// User gets fetched to hit the UpdateAgentMetricsFn.
@@ -369,6 +382,10 @@ func TestUpdateStates(t *testing.T) {
369
382
ReportInterval : durationpb .New (15 * time .Second ),
370
383
}, resp )
371
384
385
+ tickCh <- now
386
+ count := <- flushCh
387
+ require .Equal (t , 1 , count , "expected one flush with one id" )
388
+
372
389
require .True (t , updateAgentMetricsFnCalled )
373
390
})
374
391
@@ -392,6 +409,11 @@ func TestUpdateStates(t *testing.T) {
392
409
}
393
410
batcher = & workspacestatstest.StatsBatcher {}
394
411
updateAgentMetricsFnCalled = false
412
+ tickCh = make (chan time.Time )
413
+ flushCh = make (chan int , 1 )
414
+ wut = workspacestats .NewTracker (dbM ,
415
+ workspacestats .TrackerWithTickFlush (tickCh , flushCh ),
416
+ )
395
417
396
418
req = & agentproto.UpdateStatsRequest {
397
419
Stats : & agentproto.Stats {
@@ -422,6 +444,7 @@ func TestUpdateStates(t *testing.T) {
422
444
},
423
445
}
424
446
)
447
+ defer wut .Close ()
425
448
api := agentapi.StatsAPI {
426
449
AgentFn : func (context.Context ) (database.WorkspaceAgent , error ) {
427
450
return agent , nil
@@ -431,6 +454,7 @@ func TestUpdateStates(t *testing.T) {
431
454
Database : dbM ,
432
455
Pubsub : ps ,
433
456
StatsBatcher : batcher ,
457
+ UsageTracker : wut ,
434
458
TemplateScheduleStore : templateScheduleStorePtr (templateScheduleStore ),
435
459
UpdateAgentMetricsFn : func (ctx context.Context , labels prometheusmetrics.AgentMetricLabels , metrics []* agentproto.Stats_Metric ) {
436
460
updateAgentMetricsFnCalled = true
@@ -462,8 +486,8 @@ func TestUpdateStates(t *testing.T) {
462
486
}).Return (nil )
463
487
464
488
// Workspace last used at gets bumped.
465
- dbM .EXPECT ().UpdateWorkspaceLastUsedAt (gomock .Any (), database.UpdateWorkspaceLastUsedAtParams {
466
- ID : workspace .ID ,
489
+ dbM .EXPECT ().BatchUpdateWorkspaceLastUsedAt (gomock .Any (), database.BatchUpdateWorkspaceLastUsedAtParams {
490
+ IDs : []uuid. UUID { workspace .ID } ,
467
491
LastUsedAt : now ,
468
492
}).Return (nil )
469
493
@@ -484,6 +508,10 @@ func TestUpdateStates(t *testing.T) {
484
508
ReportInterval : durationpb .New (10 * time .Second ),
485
509
}, resp )
486
510
511
+ tickCh <- now
512
+ count := <- flushCh
513
+ require .Equal (t , 1 , count , "expected one flush with one id" )
514
+
487
515
batcher .Mu .Lock ()
488
516
defer batcher .Mu .Unlock ()
489
517
require .EqualValues (t , 1 , batcher .Called )
0 commit comments