1
1
package dbpurge_test
2
2
3
3
import (
4
+ "bufio"
5
+ "bytes"
4
6
"context"
5
7
"database/sql"
8
+ "encoding/json"
9
+ "fmt"
6
10
"testing"
7
11
"time"
8
12
@@ -41,9 +45,6 @@ func TestPurge(t *testing.T) {
41
45
func TestDeleteOldWorkspaceAgentStats (t * testing.T ) {
42
46
t .Parallel ()
43
47
44
- // https://github.com/coder/coder/issues/13165
45
- t .Skip ()
46
-
47
48
db , _ := dbtestutil .NewDB (t )
48
49
logger := slogtest .Make (t , & slogtest.Options {IgnoreErrors : true }).Leveled (slog .LevelDebug )
49
50
@@ -53,50 +54,59 @@ func TestDeleteOldWorkspaceAgentStats(t *testing.T) {
53
54
if t .Failed () {
54
55
t .Logf ("Test failed, printing rows..." )
55
56
ctx := testutil .Context (t , testutil .WaitShort )
57
+ buf := & bytes.Buffer {}
58
+ enc := json .NewEncoder (buf )
59
+ enc .SetIndent ("" , "\t " )
56
60
wasRows , err := db .GetWorkspaceAgentStats (ctx , now .AddDate (0 , - 7 , 0 ))
57
61
if err == nil {
58
- for _ , row := range wasRows {
59
- t .Logf ("workspace agent stat: %v" , row )
60
- }
62
+ _ , _ = fmt .Fprintf (buf , "workspace agent stats: " )
63
+ _ = enc .Encode (wasRows )
61
64
}
62
65
tusRows , err := db .GetTemplateUsageStats (context .Background (), database.GetTemplateUsageStatsParams {
63
66
StartTime : now .AddDate (0 , - 7 , 0 ),
64
67
EndTime : now ,
65
68
})
66
69
if err == nil {
67
- for _ , row := range tusRows {
68
- t .Logf ("template usage stat: %v" , row )
69
- }
70
+ _ , _ = fmt .Fprintf (buf , "template usage stats: " )
71
+ _ = enc .Encode (tusRows )
72
+ }
73
+ s := bufio .NewScanner (buf )
74
+ for s .Scan () {
75
+ t .Log (s .Text ())
70
76
}
77
+ _ = s .Err ()
71
78
}
72
79
}()
73
80
74
81
ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
75
82
defer cancel ()
76
83
77
84
// given
85
+ // Note: We use increments of 2 hours to ensure we avoid any DST
86
+ // conflicts, verifying DST behavior is beyond the scope of this
87
+ // test.
78
88
// Let's use RxBytes to identify stat entries.
79
- // Stat inserted 6 months + 1 hour ago, should be deleted.
89
+ // Stat inserted 6 months + 2 hour ago, should be deleted.
80
90
first := dbgen .WorkspaceAgentStat (t , db , database.WorkspaceAgentStat {
81
- CreatedAt : now .AddDate (0 , - 6 , 0 ).Add (- time .Hour ),
91
+ CreatedAt : now .AddDate (0 , - 6 , 0 ).Add (- 2 * time .Hour ),
82
92
ConnectionCount : 1 ,
83
93
ConnectionMedianLatencyMS : 1 ,
84
94
RxBytes : 1111 ,
85
95
SessionCountSSH : 1 ,
86
96
})
87
97
88
- // Stat inserted 6 months - 1 hour ago, should not be deleted before rollup.
98
+ // Stat inserted 6 months - 2 hour ago, should not be deleted before rollup.
89
99
second := dbgen .WorkspaceAgentStat (t , db , database.WorkspaceAgentStat {
90
- CreatedAt : now .AddDate (0 , - 6 , 0 ).Add (time .Hour ),
100
+ CreatedAt : now .AddDate (0 , - 6 , 0 ).Add (2 * time .Hour ),
91
101
ConnectionCount : 1 ,
92
102
ConnectionMedianLatencyMS : 1 ,
93
103
RxBytes : 2222 ,
94
104
SessionCountSSH : 1 ,
95
105
})
96
106
97
- // Stat inserted 6 months - 1 day - 2 hour ago, should not be deleted at all.
107
+ // Stat inserted 6 months - 1 day - 4 hour ago, should not be deleted at all.
98
108
third := dbgen .WorkspaceAgentStat (t , db , database.WorkspaceAgentStat {
99
- CreatedAt : now .AddDate (0 , - 6 , 0 ).AddDate (0 , 0 , 1 ).Add (2 * time .Hour ),
109
+ CreatedAt : now .AddDate (0 , - 6 , 0 ).AddDate (0 , 0 , 1 ).Add (4 * time .Hour ),
100
110
ConnectionCount : 1 ,
101
111
ConnectionMedianLatencyMS : 1 ,
102
112
RxBytes : 3333 ,
0 commit comments