@@ -69,6 +69,11 @@ func TestUpdateStates(t *testing.T) {
69
69
}
70
70
batcher = & workspacestatstest.StatsBatcher {}
71
71
updateAgentMetricsFnCalled = false
72
+ tickCh = make (chan time.Time )
73
+ flushCh = make (chan int , 1 )
74
+ wut = workspacestats .NewTracker (dbM ,
75
+ workspacestats .TrackerWithTickFlush (tickCh , flushCh ),
76
+ )
72
77
73
78
req = & agentproto.UpdateStatsRequest {
74
79
Stats : & agentproto.Stats {
@@ -108,7 +113,7 @@ func TestUpdateStates(t *testing.T) {
108
113
Database : dbM ,
109
114
Pubsub : ps ,
110
115
StatsBatcher : batcher ,
111
- UsageTracker : workspacestats . NewTracker ( dbM ) ,
116
+ UsageTracker : wut ,
112
117
TemplateScheduleStore : templateScheduleStorePtr (templateScheduleStore ),
113
118
UpdateAgentMetricsFn : func (ctx context.Context , labels prometheusmetrics.AgentMetricLabels , metrics []* agentproto.Stats_Metric ) {
114
119
updateAgentMetricsFnCalled = true
@@ -126,6 +131,7 @@ func TestUpdateStates(t *testing.T) {
126
131
return now
127
132
},
128
133
}
134
+ defer wut .Close ()
129
135
130
136
// Workspace gets fetched.
131
137
dbM .EXPECT ().GetWorkspaceByAgentID (gomock .Any (), agent .ID ).Return (database.GetWorkspaceByAgentIDRow {
@@ -142,6 +148,12 @@ func TestUpdateStates(t *testing.T) {
142
148
NextAutostart : time.Time {}.UTC (),
143
149
}).Return (nil )
144
150
151
+ // Workspace last used at gets bumped.
152
+ dbM .EXPECT ().BatchUpdateWorkspaceLastUsedAt (gomock .Any (), database.BatchUpdateWorkspaceLastUsedAtParams {
153
+ IDs : []uuid.UUID {workspace .ID },
154
+ LastUsedAt : now ,
155
+ }).Return (nil )
156
+
145
157
// Ensure that pubsub notifications are sent.
146
158
notifyDescription := make (chan []byte )
147
159
ps .Subscribe (codersdk .WorkspaceNotifyChannel (workspace .ID ), func (_ context.Context , description []byte ) {
@@ -156,6 +168,10 @@ func TestUpdateStates(t *testing.T) {
156
168
ReportInterval : durationpb .New (10 * time .Second ),
157
169
}, resp )
158
170
171
+ tickCh <- now
172
+ count := <- flushCh
173
+ require .Equal (t , 1 , count , "expected one flush with one id" )
174
+
159
175
batcher .Mu .Lock ()
160
176
defer batcher .Mu .Unlock ()
161
177
require .Equal (t , int64 (1 ), batcher .Called )
@@ -301,6 +317,11 @@ func TestUpdateStates(t *testing.T) {
301
317
}
302
318
batcher = & workspacestatstest.StatsBatcher {}
303
319
updateAgentMetricsFnCalled = false
320
+ tickCh = make (chan time.Time )
321
+ flushCh = make (chan int , 1 )
322
+ wut = workspacestats .NewTracker (dbM ,
323
+ workspacestats .TrackerWithTickFlush (tickCh , flushCh ),
324
+ )
304
325
305
326
req = & agentproto.UpdateStatsRequest {
306
327
Stats : & agentproto.Stats {
@@ -320,7 +341,7 @@ func TestUpdateStates(t *testing.T) {
320
341
StatsReporter : workspacestats .NewReporter (workspacestats.ReporterOptions {
321
342
Database : dbM ,
322
343
Pubsub : ps ,
323
- UsageTracker : workspacestats . NewTracker ( dbM ) ,
344
+ UsageTracker : wut ,
324
345
StatsBatcher : batcher ,
325
346
TemplateScheduleStore : templateScheduleStorePtr (templateScheduleStore ),
326
347
UpdateAgentMetricsFn : func (ctx context.Context , labels prometheusmetrics.AgentMetricLabels , metrics []* agentproto.Stats_Metric ) {
@@ -339,6 +360,7 @@ func TestUpdateStates(t *testing.T) {
339
360
return now
340
361
},
341
362
}
363
+ defer wut .Close ()
342
364
343
365
// Workspace gets fetched.
344
366
dbM .EXPECT ().GetWorkspaceByAgentID (gomock .Any (), agent .ID ).Return (database.GetWorkspaceByAgentIDRow {
@@ -354,9 +376,9 @@ func TestUpdateStates(t *testing.T) {
354
376
}).Return (nil )
355
377
356
378
// Workspace last used at gets bumped.
357
- dbM .EXPECT ().UpdateWorkspaceLastUsedAt (gomock .Any (), database.UpdateWorkspaceLastUsedAtParams {
358
- ID : workspace .ID ,
359
- LastUsedAt : now ,
379
+ dbM .EXPECT ().BatchUpdateWorkspaceLastUsedAt (gomock .Any (), database.BatchUpdateWorkspaceLastUsedAtParams {
380
+ IDs : []uuid. UUID { workspace .ID } ,
381
+ LastUsedAt : now . UTC () ,
360
382
}).Return (nil )
361
383
362
384
// User gets fetched to hit the UpdateAgentMetricsFn.
@@ -368,6 +390,10 @@ func TestUpdateStates(t *testing.T) {
368
390
ReportInterval : durationpb .New (15 * time .Second ),
369
391
}, resp )
370
392
393
+ tickCh <- now
394
+ count := <- flushCh
395
+ require .Equal (t , 1 , count , "expected one flush with one id" )
396
+
371
397
require .True (t , updateAgentMetricsFnCalled )
372
398
})
373
399
@@ -391,6 +417,11 @@ func TestUpdateStates(t *testing.T) {
391
417
}
392
418
batcher = & workspacestatstest.StatsBatcher {}
393
419
updateAgentMetricsFnCalled = false
420
+ tickCh = make (chan time.Time )
421
+ flushCh = make (chan int , 1 )
422
+ wut = workspacestats .NewTracker (dbM ,
423
+ workspacestats .TrackerWithTickFlush (tickCh , flushCh ),
424
+ )
394
425
395
426
req = & agentproto.UpdateStatsRequest {
396
427
Stats : & agentproto.Stats {
@@ -421,6 +452,7 @@ func TestUpdateStates(t *testing.T) {
421
452
},
422
453
}
423
454
)
455
+ defer wut .Close ()
424
456
api := agentapi.StatsAPI {
425
457
AgentFn : func (context.Context ) (database.WorkspaceAgent , error ) {
426
458
return agent , nil
@@ -430,7 +462,7 @@ func TestUpdateStates(t *testing.T) {
430
462
Database : dbM ,
431
463
Pubsub : ps ,
432
464
StatsBatcher : batcher ,
433
- UsageTracker : workspacestats . NewTracker ( dbM ) ,
465
+ UsageTracker : wut ,
434
466
TemplateScheduleStore : templateScheduleStorePtr (templateScheduleStore ),
435
467
UpdateAgentMetricsFn : func (ctx context.Context , labels prometheusmetrics.AgentMetricLabels , metrics []* agentproto.Stats_Metric ) {
436
468
updateAgentMetricsFnCalled = true
@@ -465,8 +497,8 @@ func TestUpdateStates(t *testing.T) {
465
497
}).Return (nil )
466
498
467
499
// Workspace last used at gets bumped.
468
- dbM .EXPECT ().UpdateWorkspaceLastUsedAt (gomock .Any (), database.UpdateWorkspaceLastUsedAtParams {
469
- ID : workspace .ID ,
500
+ dbM .EXPECT ().BatchUpdateWorkspaceLastUsedAt (gomock .Any (), database.BatchUpdateWorkspaceLastUsedAtParams {
501
+ IDs : []uuid. UUID { workspace .ID } ,
470
502
LastUsedAt : now ,
471
503
}).Return (nil )
472
504
@@ -487,6 +519,10 @@ func TestUpdateStates(t *testing.T) {
487
519
ReportInterval : durationpb .New (10 * time .Second ),
488
520
}, resp )
489
521
522
+ tickCh <- now
523
+ count := <- flushCh
524
+ require .Equal (t , 1 , count , "expected one flush with one id" )
525
+
490
526
batcher .Mu .Lock ()
491
527
defer batcher .Mu .Unlock ()
492
528
require .EqualValues (t , 1 , batcher .Called )
0 commit comments