Skip to content

Commit 941e387

Browse files
authored
fix: implement fake DeleteOldWorkspaceAgentStats (#11076)
1 parent c0d68a4 commit 941e387

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

coderd/database/dbmem/dbmem.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,8 +1180,22 @@ func (q *FakeQuerier) DeleteOldWorkspaceAgentLogs(_ context.Context) error {
11801180
return nil
11811181
}
11821182

1183-
func (*FakeQuerier) DeleteOldWorkspaceAgentStats(_ context.Context) error {
1184-
// no-op
1183+
func (q *FakeQuerier) DeleteOldWorkspaceAgentStats(_ context.Context) error {
1184+
q.mutex.Lock()
1185+
defer q.mutex.Unlock()
1186+
1187+
now := dbtime.Now()
1188+
sixMonthInterval := 6 * 30 * 24 * time.Hour
1189+
sixMonthsAgo := now.Add(-sixMonthInterval)
1190+
1191+
var validStats []database.WorkspaceAgentStat
1192+
for _, stat := range q.workspaceAgentStats {
1193+
if stat.CreatedAt.Before(sixMonthsAgo) {
1194+
continue
1195+
}
1196+
validStats = append(validStats, stat)
1197+
}
1198+
q.workspaceAgentStats = validStats
11851199
return nil
11861200
}
11871201

coderd/database/dbpurge/dbpurge_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,57 @@ func TestPurge(t *testing.T) {
3434
require.NoError(t, err)
3535
}
3636

37+
func TestDeleteOldWorkspaceAgentStats(t *testing.T) {
38+
t.Parallel()
39+
40+
db, _ := dbtestutil.NewDB(t)
41+
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
42+
43+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
44+
defer cancel()
45+
46+
now := dbtime.Now()
47+
48+
// given
49+
// Let's use RxBytes to identify stat entries.
50+
// Stat inserted 6 months + 1 hour ago, should be deleted.
51+
first := dbgen.WorkspaceAgentStat(t, db, database.WorkspaceAgentStat{
52+
CreatedAt: now.Add(-6*30*24*time.Hour - time.Hour),
53+
ConnectionMedianLatencyMS: 1,
54+
RxBytes: 1111,
55+
})
56+
57+
// Stat inserted 6 months - 1 hour ago, should not be deleted.
58+
second := dbgen.WorkspaceAgentStat(t, db, database.WorkspaceAgentStat{
59+
CreatedAt: now.Add(-5*30*24*time.Hour + time.Hour),
60+
ConnectionMedianLatencyMS: 1,
61+
RxBytes: 2222,
62+
})
63+
64+
// when
65+
closer := dbpurge.New(ctx, logger, db)
66+
defer closer.Close()
67+
68+
// then
69+
var stats []database.GetWorkspaceAgentStatsRow
70+
var err error
71+
require.Eventually(t, func() bool {
72+
// Query all stats created not earlier than 7 months ago
73+
stats, err = db.GetWorkspaceAgentStats(ctx, now.Add(-7*30*24*time.Hour))
74+
if err != nil {
75+
return false
76+
}
77+
return !containsWorkspaceAgentStat(stats, first) &&
78+
containsWorkspaceAgentStat(stats, second)
79+
}, testutil.WaitShort, testutil.IntervalFast, stats)
80+
}
81+
82+
func containsWorkspaceAgentStat(stats []database.GetWorkspaceAgentStatsRow, needle database.WorkspaceAgentStat) bool {
83+
return slices.ContainsFunc(stats, func(s database.GetWorkspaceAgentStatsRow) bool {
84+
return s.WorkspaceRxBytes == needle.RxBytes
85+
})
86+
}
87+
3788
func TestDeleteOldWorkspaceAgentLogs(t *testing.T) {
3889
t.Parallel()
3990

coderd/database/queries.sql.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaceagentstats.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ ORDER BY
9090
date ASC;
9191

9292
-- name: DeleteOldWorkspaceAgentStats :exec
93-
DELETE FROM workspace_agent_stats WHERE created_at < NOW() - INTERVAL '6 months';
93+
DELETE FROM workspace_agent_stats WHERE created_at < NOW() - INTERVAL '180 days';
9494

9595
-- name: GetDeploymentWorkspaceAgentStats :one
9696
WITH agent_stats AS (

0 commit comments

Comments
 (0)