Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6287502

Browse files
fix: delete workspace agent stats after 180 days (#14489)
Fixes #13430. The test for purging old workspace agent stats from the DB was consistently failing when ran with Postgres towards the end of the month, but not with the in-memory DB. This was because month intervals are calculated differently for `time.Time` and the `interval` type in Postgres: ``` ethan=# SELECT '2024-08-30'::DATE AS original_date, ('2024-08-30'::DATE - INTERVAL '6 months') AS sub_date; original_date | sub_date ---------------+--------------------- 2024-08-30 | 2024-02-29 00:00:00 (1 row) ``` Using `func (t Time) AddDate(years int, months int, days int) Time`, where `months` is `-6`: ``` Original: 2024-08-30 00:00:00 +0000 UTC 6 Months Earlier: 2024-03-01 00:00:00 +0000 UTC ``` Since 6 months was chosen arbitrarily, we should be able to change it to 180 days, to remove any ambiguity between the in-memory DB, and the Postgres DB. The alternative solution would involve implementing Postgres' month interval algorithm in Go. The UI only shows stats as old as 168 days (24 weeks), so a frontend change isn't required for the extra days of stats we lose in some cases.
1 parent 4672849 commit 6287502

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

coderd/database/dbmem/dbmem.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1749,10 +1749,10 @@ func (q *FakeQuerier) DeleteOldWorkspaceAgentStats(_ context.Context) error {
17491749
-- use between 15 mins and 1 hour of data. We keep a
17501750
-- little bit more (1 day) just in case.
17511751
MAX(start_time) - '1 days'::interval,
1752-
-- Fall back to 6 months ago if there are no template
1752+
-- Fall back to ~6 months ago if there are no template
17531753
-- usage stats so that we don't delete the data before
17541754
-- it's rolled up.
1755-
NOW() - '6 months'::interval
1755+
NOW() - '180 days'::interval
17561756
)
17571757
FROM
17581758
template_usage_stats
@@ -1778,7 +1778,7 @@ func (q *FakeQuerier) DeleteOldWorkspaceAgentStats(_ context.Context) error {
17781778
}
17791779
// COALESCE
17801780
if limit.IsZero() {
1781-
limit = now.AddDate(0, -6, 0)
1781+
limit = now.AddDate(0, 0, -180)
17821782
}
17831783

17841784
var validStats []database.WorkspaceAgentStat

coderd/database/dbpurge/dbpurge_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,27 @@ func TestDeleteOldWorkspaceAgentStats(t *testing.T) {
8686
// conflicts, verifying DST behavior is beyond the scope of this
8787
// test.
8888
// Let's use RxBytes to identify stat entries.
89-
// Stat inserted 6 months + 2 hour ago, should be deleted.
89+
// Stat inserted 180 days + 2 hour ago, should be deleted.
9090
first := dbgen.WorkspaceAgentStat(t, db, database.WorkspaceAgentStat{
91-
CreatedAt: now.AddDate(0, -6, 0).Add(-2 * time.Hour),
91+
CreatedAt: now.AddDate(0, 0, -180).Add(-2 * time.Hour),
9292
ConnectionCount: 1,
9393
ConnectionMedianLatencyMS: 1,
9494
RxBytes: 1111,
9595
SessionCountSSH: 1,
9696
})
9797

98-
// Stat inserted 6 months - 2 hour ago, should not be deleted before rollup.
98+
// Stat inserted 180 days - 2 hour ago, should not be deleted before rollup.
9999
second := dbgen.WorkspaceAgentStat(t, db, database.WorkspaceAgentStat{
100-
CreatedAt: now.AddDate(0, -6, 0).Add(2 * time.Hour),
100+
CreatedAt: now.AddDate(0, 0, -180).Add(2 * time.Hour),
101101
ConnectionCount: 1,
102102
ConnectionMedianLatencyMS: 1,
103103
RxBytes: 2222,
104104
SessionCountSSH: 1,
105105
})
106106

107-
// Stat inserted 6 months - 1 day - 4 hour ago, should not be deleted at all.
107+
// Stat inserted 179 days - 4 hour ago, should not be deleted at all.
108108
third := dbgen.WorkspaceAgentStat(t, db, database.WorkspaceAgentStat{
109-
CreatedAt: now.AddDate(0, -6, 0).AddDate(0, 0, 1).Add(4 * time.Hour),
109+
CreatedAt: now.AddDate(0, 0, -179).Add(4 * time.Hour),
110110
ConnectionCount: 1,
111111
ConnectionMedianLatencyMS: 1,
112112
RxBytes: 3333,
@@ -121,8 +121,8 @@ func TestDeleteOldWorkspaceAgentStats(t *testing.T) {
121121
var stats []database.GetWorkspaceAgentStatsRow
122122
var err error
123123
require.Eventuallyf(t, func() bool {
124-
// Query all stats created not earlier than 7 months ago
125-
stats, err = db.GetWorkspaceAgentStats(ctx, now.AddDate(0, -7, 0))
124+
// Query all stats created not earlier than ~7 months ago
125+
stats, err = db.GetWorkspaceAgentStats(ctx, now.AddDate(0, 0, -210))
126126
if err != nil {
127127
return false
128128
}
@@ -144,8 +144,8 @@ func TestDeleteOldWorkspaceAgentStats(t *testing.T) {
144144

145145
// then
146146
require.Eventuallyf(t, func() bool {
147-
// Query all stats created not earlier than 7 months ago
148-
stats, err = db.GetWorkspaceAgentStats(ctx, now.AddDate(0, -7, 0))
147+
// Query all stats created not earlier than ~7 months ago
148+
stats, err = db.GetWorkspaceAgentStats(ctx, now.AddDate(0, 0, -210))
149149
if err != nil {
150150
return false
151151
}

coderd/database/queries.sql.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaceagentstats.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ WHERE
7878
-- use between 15 mins and 1 hour of data. We keep a
7979
-- little bit more (1 day) just in case.
8080
MAX(start_time) - '1 days'::interval,
81-
-- Fall back to 6 months ago if there are no template
81+
-- Fall back to ~6 months ago if there are no template
8282
-- usage stats so that we don't delete the data before
8383
-- it's rolled up.
84-
NOW() - '6 months'::interval
84+
NOW() - '180 days'::interval
8585
)
8686
FROM
8787
template_usage_stats

0 commit comments

Comments
 (0)