@@ -26,6 +26,7 @@ import (
26
26
"github.com/coder/coder/v2/coderd/database"
27
27
"github.com/coder/coder/v2/coderd/database/dbauthz"
28
28
"github.com/coder/coder/v2/coderd/database/dbgen"
29
+ "github.com/coder/coder/v2/coderd/database/dbrollup"
29
30
"github.com/coder/coder/v2/coderd/database/dbtestutil"
30
31
"github.com/coder/coder/v2/coderd/rbac"
31
32
"github.com/coder/coder/v2/coderd/workspaceapps"
@@ -44,10 +45,19 @@ func TestDeploymentInsights(t *testing.T) {
44
45
clientTz , err := time .LoadLocation ("America/Chicago" )
45
46
require .NoError (t , err )
46
47
48
+ db , ps := dbtestutil .NewDB (t )
49
+ logger := slogtest .Make (t , nil )
47
50
client := coderdtest .New (t , & coderdtest.Options {
48
- IncludeProvisionerDaemon : true ,
49
- AgentStatsRefreshInterval : time .Millisecond * 100 ,
50
- MetricsCacheRefreshInterval : time .Millisecond * 100 ,
51
+ Database : db ,
52
+ Pubsub : ps ,
53
+ Logger : & logger ,
54
+ IncludeProvisionerDaemon : true ,
55
+ AgentStatsRefreshInterval : time .Millisecond * 50 ,
56
+ DatabaseRolluper : dbrollup .New (
57
+ logger .Named ("dbrollup" ),
58
+ db ,
59
+ dbrollup .WithInterval (time .Millisecond * 100 ),
60
+ ),
51
61
})
52
62
53
63
user := coderdtest .CreateFirstUser (t , client )
@@ -119,10 +129,19 @@ func TestDeploymentInsights(t *testing.T) {
119
129
func TestUserActivityInsights_SanityCheck (t * testing.T ) {
120
130
t .Parallel ()
121
131
132
+ db , ps := dbtestutil .NewDB (t )
122
133
logger := slogtest .Make (t , nil )
123
134
client := coderdtest .New (t , & coderdtest.Options {
135
+ Database : db ,
136
+ Pubsub : ps ,
137
+ Logger : & logger ,
124
138
IncludeProvisionerDaemon : true ,
125
- AgentStatsRefreshInterval : time .Millisecond * 100 ,
139
+ AgentStatsRefreshInterval : time .Millisecond * 50 ,
140
+ DatabaseRolluper : dbrollup .New (
141
+ logger .Named ("dbrollup" ),
142
+ db ,
143
+ dbrollup .WithInterval (time .Millisecond * 100 ),
144
+ ),
126
145
})
127
146
128
147
// Create two users, one that will appear in the report and another that
@@ -207,10 +226,19 @@ func TestUserActivityInsights_SanityCheck(t *testing.T) {
207
226
func TestUserLatencyInsights (t * testing.T ) {
208
227
t .Parallel ()
209
228
229
+ db , ps := dbtestutil .NewDB (t )
210
230
logger := slogtest .Make (t , nil )
211
231
client := coderdtest .New (t , & coderdtest.Options {
232
+ Database : db ,
233
+ Pubsub : ps ,
234
+ Logger : & logger ,
212
235
IncludeProvisionerDaemon : true ,
213
- AgentStatsRefreshInterval : time .Millisecond * 100 ,
236
+ AgentStatsRefreshInterval : time .Millisecond * 50 ,
237
+ DatabaseRolluper : dbrollup .New (
238
+ logger .Named ("dbrollup" ),
239
+ db ,
240
+ dbrollup .WithInterval (time .Millisecond * 100 ),
241
+ ),
214
242
})
215
243
216
244
// Create two users, one that will appear in the report and another that
@@ -474,16 +502,24 @@ func TestTemplateInsights_Golden(t *testing.T) {
474
502
return templates , users , testData
475
503
}
476
504
477
- prepare := func (t * testing.T , templates []* testTemplate , users []* testUser , testData map [* testWorkspace ]testDataGen ) * codersdk.Client {
505
+ prepare := func (t * testing.T , templates []* testTemplate , users []* testUser , testData map [* testWorkspace ]testDataGen ) ( * codersdk.Client , chan dbrollup. Event ) {
478
506
logger := slogtest .Make (t , & slogtest.Options {IgnoreErrors : false }).Leveled (slog .LevelDebug )
479
- db , pubsub := dbtestutil .NewDB (t )
507
+ db , ps := dbtestutil .NewDB (t )
508
+ events := make (chan dbrollup.Event )
480
509
client := coderdtest .New (t , & coderdtest.Options {
481
510
Database : db ,
482
- Pubsub : pubsub ,
511
+ Pubsub : ps ,
483
512
Logger : & logger ,
484
513
IncludeProvisionerDaemon : true ,
485
514
AgentStatsRefreshInterval : time .Hour , // Not relevant for this test.
515
+ DatabaseRolluper : dbrollup .New (
516
+ logger .Named ("dbrollup" ),
517
+ db ,
518
+ dbrollup .WithInterval (time .Millisecond * 50 ),
519
+ dbrollup .WithEventChannel (events ),
520
+ ),
486
521
})
522
+
487
523
firstUser := coderdtest .CreateFirstUser (t , client )
488
524
489
525
// Prepare all test users.
@@ -706,7 +742,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
706
742
err = reporter .Report (dbauthz .AsSystemRestricted (ctx ), stats )
707
743
require .NoError (t , err , "want no error inserting app stats" )
708
744
709
- return client
745
+ return client , events
710
746
}
711
747
712
748
baseTemplateAndUserFixture := func () ([]* testTemplate , []* testUser ) {
@@ -1200,7 +1236,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
1200
1236
require .NotNil (t , tt .makeFixture , "test bug: makeFixture must be set" )
1201
1237
require .NotNil (t , tt .makeTestData , "test bug: makeTestData must be set" )
1202
1238
templates , users , testData := prepareFixtureAndTestData (t , tt .makeFixture , tt .makeTestData )
1203
- client := prepare (t , templates , users , testData )
1239
+ client , events := prepare (t , templates , users , testData )
1204
1240
1205
1241
for _ , req := range tt .requests {
1206
1242
req := req
@@ -1209,6 +1245,11 @@ func TestTemplateInsights_Golden(t *testing.T) {
1209
1245
1210
1246
ctx := testutil .Context (t , testutil .WaitMedium )
1211
1247
1248
+ // Drain two events, the first one resumes rolluper
1249
+ // operation and the second one waits for the rollup
1250
+ // to complete.
1251
+ _ , _ = <- events , <- events
1252
+
1212
1253
report , err := client .TemplateInsights (ctx , req .makeRequest (templates ))
1213
1254
require .NoError (t , err , "want no error getting template insights" )
1214
1255
@@ -1381,15 +1422,22 @@ func TestUserActivityInsights_Golden(t *testing.T) {
1381
1422
return templates , users , testData
1382
1423
}
1383
1424
1384
- prepare := func (t * testing.T , templates []* testTemplate , users []* testUser , testData map [* testWorkspace ]testDataGen ) * codersdk.Client {
1425
+ prepare := func (t * testing.T , templates []* testTemplate , users []* testUser , testData map [* testWorkspace ]testDataGen ) ( * codersdk.Client , chan dbrollup. Event ) {
1385
1426
logger := slogtest .Make (t , & slogtest.Options {IgnoreErrors : false }).Leveled (slog .LevelDebug )
1386
- db , pubsub := dbtestutil .NewDB (t )
1427
+ db , ps := dbtestutil .NewDB (t )
1428
+ events := make (chan dbrollup.Event )
1387
1429
client := coderdtest .New (t , & coderdtest.Options {
1388
1430
Database : db ,
1389
- Pubsub : pubsub ,
1431
+ Pubsub : ps ,
1390
1432
Logger : & logger ,
1391
1433
IncludeProvisionerDaemon : true ,
1392
1434
AgentStatsRefreshInterval : time .Hour , // Not relevant for this test.
1435
+ DatabaseRolluper : dbrollup .New (
1436
+ logger .Named ("dbrollup" ),
1437
+ db ,
1438
+ dbrollup .WithInterval (time .Millisecond * 50 ),
1439
+ dbrollup .WithEventChannel (events ),
1440
+ ),
1393
1441
})
1394
1442
firstUser := coderdtest .CreateFirstUser (t , client )
1395
1443
@@ -1593,7 +1641,7 @@ func TestUserActivityInsights_Golden(t *testing.T) {
1593
1641
err = reporter .Report (dbauthz .AsSystemRestricted (ctx ), stats )
1594
1642
require .NoError (t , err , "want no error inserting app stats" )
1595
1643
1596
- return client
1644
+ return client , events
1597
1645
}
1598
1646
1599
1647
baseTemplateAndUserFixture := func () ([]* testTemplate , []* testUser ) {
@@ -1974,7 +2022,7 @@ func TestUserActivityInsights_Golden(t *testing.T) {
1974
2022
require .NotNil (t , tt .makeFixture , "test bug: makeFixture must be set" )
1975
2023
require .NotNil (t , tt .makeTestData , "test bug: makeTestData must be set" )
1976
2024
templates , users , testData := prepareFixtureAndTestData (t , tt .makeFixture , tt .makeTestData )
1977
- client := prepare (t , templates , users , testData )
2025
+ client , events := prepare (t , templates , users , testData )
1978
2026
1979
2027
for _ , req := range tt .requests {
1980
2028
req := req
@@ -1983,6 +2031,11 @@ func TestUserActivityInsights_Golden(t *testing.T) {
1983
2031
1984
2032
ctx := testutil .Context (t , testutil .WaitMedium )
1985
2033
2034
+ // Drain two events, the first one resumes rolluper
2035
+ // operation and the second one waits for the rollup
2036
+ // to complete.
2037
+ _ , _ = <- events , <- events
2038
+
1986
2039
report , err := client .UserActivityInsights (ctx , req .makeRequest (templates ))
1987
2040
require .NoError (t , err , "want no error getting template insights" )
1988
2041
0 commit comments