@@ -11,12 +11,14 @@ import (
11
11
"go.uber.org/goleak"
12
12
"golang.org/x/exp/slices"
13
13
14
+ "cdr.dev/slog"
14
15
"cdr.dev/slog/sloggers/slogtest"
15
16
16
17
"github.com/coder/coder/v2/coderd/database"
17
18
"github.com/coder/coder/v2/coderd/database/dbgen"
18
19
"github.com/coder/coder/v2/coderd/database/dbmem"
19
20
"github.com/coder/coder/v2/coderd/database/dbpurge"
21
+ "github.com/coder/coder/v2/coderd/database/dbrollup"
20
22
"github.com/coder/coder/v2/coderd/database/dbtestutil"
21
23
"github.com/coder/coder/v2/coderd/database/dbtime"
22
24
"github.com/coder/coder/v2/provisionerd/proto"
@@ -40,27 +42,62 @@ func TestDeleteOldWorkspaceAgentStats(t *testing.T) {
40
42
t .Parallel ()
41
43
42
44
db , _ := dbtestutil .NewDB (t )
43
- logger := slogtest .Make (t , & slogtest.Options {IgnoreErrors : true })
45
+ logger := slogtest .Make (t , & slogtest.Options {IgnoreErrors : true }).Leveled (slog .LevelDebug )
46
+
47
+ now := dbtime .Now ()
48
+
49
+ defer func () {
50
+ if t .Failed () {
51
+ t .Logf ("Test failed, printing rows..." )
52
+ ctx := testutil .Context (t , testutil .WaitShort )
53
+ wasRows , err := db .GetWorkspaceAgentStats (ctx , now .AddDate (0 , - 7 , 0 ))
54
+ if err == nil {
55
+ for _ , row := range wasRows {
56
+ t .Logf ("workspace agent stat: %v" , row )
57
+ }
58
+ }
59
+ tusRows , err := db .GetTemplateUsageStats (context .Background (), database.GetTemplateUsageStatsParams {
60
+ StartTime : now .AddDate (0 , - 7 , 0 ),
61
+ EndTime : now ,
62
+ })
63
+ if err == nil {
64
+ for _ , row := range tusRows {
65
+ t .Logf ("template usage stat: %v" , row )
66
+ }
67
+ }
68
+ }
69
+ }()
44
70
45
71
ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
46
72
defer cancel ()
47
73
48
- now := dbtime .Now ()
49
-
50
74
// given
51
75
// Let's use RxBytes to identify stat entries.
52
76
// Stat inserted 6 months + 1 hour ago, should be deleted.
53
77
first := dbgen .WorkspaceAgentStat (t , db , database.WorkspaceAgentStat {
54
- CreatedAt : now .Add (- 6 * 30 * 24 * time .Hour - time .Hour ),
78
+ CreatedAt : now .AddDate (0 , - 6 , 0 ).Add (- time .Hour ),
79
+ ConnectionCount : 1 ,
55
80
ConnectionMedianLatencyMS : 1 ,
56
81
RxBytes : 1111 ,
82
+ SessionCountSSH : 1 ,
57
83
})
58
84
59
- // Stat inserted 6 months - 1 hour ago, should not be deleted.
85
+ // Stat inserted 6 months - 1 hour ago, should not be deleted before rollup .
60
86
second := dbgen .WorkspaceAgentStat (t , db , database.WorkspaceAgentStat {
61
- CreatedAt : now .Add (- 5 * 30 * 24 * time .Hour + time .Hour ),
87
+ CreatedAt : now .AddDate (0 , - 6 , 0 ).Add (time .Hour ),
88
+ ConnectionCount : 1 ,
62
89
ConnectionMedianLatencyMS : 1 ,
63
90
RxBytes : 2222 ,
91
+ SessionCountSSH : 1 ,
92
+ })
93
+
94
+ // Stat inserted 6 months - 1 day - 2 hour ago, should not be deleted at all.
95
+ third := dbgen .WorkspaceAgentStat (t , db , database.WorkspaceAgentStat {
96
+ CreatedAt : now .AddDate (0 , - 6 , 0 ).AddDate (0 , 0 , 1 ).Add (2 * time .Hour ),
97
+ ConnectionCount : 1 ,
98
+ ConnectionMedianLatencyMS : 1 ,
99
+ RxBytes : 3333 ,
100
+ SessionCountSSH : 1 ,
64
101
})
65
102
66
103
// when
@@ -70,15 +107,39 @@ func TestDeleteOldWorkspaceAgentStats(t *testing.T) {
70
107
// then
71
108
var stats []database.GetWorkspaceAgentStatsRow
72
109
var err error
73
- require .Eventually (t , func () bool {
110
+ require .Eventuallyf (t , func () bool {
74
111
// Query all stats created not earlier than 7 months ago
75
- stats , err = db .GetWorkspaceAgentStats (ctx , now .Add ( - 7 * 30 * 24 * time . Hour ))
112
+ stats , err = db .GetWorkspaceAgentStats (ctx , now .AddDate ( 0 , - 7 , 0 ))
76
113
if err != nil {
77
114
return false
78
115
}
79
116
return ! containsWorkspaceAgentStat (stats , first ) &&
80
117
containsWorkspaceAgentStat (stats , second )
81
- }, testutil .WaitShort , testutil .IntervalFast , stats )
118
+ }, testutil .WaitShort , testutil .IntervalFast , "it should delete old stats: %v" , stats )
119
+
120
+ // when
121
+ events := make (chan dbrollup.Event )
122
+ rolluper := dbrollup .New (logger , db , dbrollup .WithEventChannel (events ))
123
+ defer rolluper .Close ()
124
+
125
+ _ , _ = <- events , <- events
126
+
127
+ // Start a new purger to immediately trigger delete after rollup.
128
+ _ = closer .Close ()
129
+ closer = dbpurge .New (ctx , logger , db )
130
+ defer closer .Close ()
131
+
132
+ // then
133
+ require .Eventuallyf (t , func () bool {
134
+ // Query all stats created not earlier than 7 months ago
135
+ stats , err = db .GetWorkspaceAgentStats (ctx , now .AddDate (0 , - 7 , 0 ))
136
+ if err != nil {
137
+ return false
138
+ }
139
+ return ! containsWorkspaceAgentStat (stats , first ) &&
140
+ ! containsWorkspaceAgentStat (stats , second ) &&
141
+ containsWorkspaceAgentStat (stats , third )
142
+ }, testutil .WaitShort , testutil .IntervalFast , "it should delete old stats after rollup: %v" , stats )
82
143
}
83
144
84
145
func containsWorkspaceAgentStat (stats []database.GetWorkspaceAgentStatsRow , needle database.WorkspaceAgentStat ) bool {
0 commit comments