Skip to content

fix: implement fake DeleteOldWorkspaceAgentStats #11076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
go impl
  • Loading branch information
mtojek committed Dec 7, 2023
commit e813d9f5faea4b4d9208f920fd6ebf2546fba2b3
5 changes: 3 additions & 2 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,9 +1190,10 @@ func (q *FakeQuerier) DeleteOldWorkspaceAgentStats(_ context.Context) error {

var validStats []database.WorkspaceAgentStat
for _, stat := range q.workspaceAgentStats {
if !stat.CreatedAt.Before(sixMonthsAgo) {
validStats = append(validStats, stat)
if stat.CreatedAt.Before(sixMonthsAgo) {
continue
}
validStats = append(validStats, stat)
}
q.workspaceAgentStats = validStats
return nil
Expand Down
46 changes: 45 additions & 1 deletion coderd/database/dbpurge/dbpurge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,51 @@ func TestPurge(t *testing.T) {
func TestDeleteOldWorkspaceAgentStats(t *testing.T) {
t.Parallel()

// TODO
db, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
defer cancel()

now := dbtime.Now()

// given
// Let's use RxBytes to identify stat entries.
// Stat inserted 6 months + 1 hour ago, should be deleted.
first := dbgen.WorkspaceAgentStat(t, db, database.WorkspaceAgentStat{
CreatedAt: now.Add(-6*30*24*time.Hour - time.Hour),
ConnectionMedianLatencyMS: 1,
RxBytes: 1,
})

// Stat inserted 6 months - 1 hour ago, should not be deleted.
second := dbgen.WorkspaceAgentStat(t, db, database.WorkspaceAgentStat{
CreatedAt: now.Add(-5*30*24*time.Hour + time.Hour),
ConnectionMedianLatencyMS: 1,
RxBytes: 3,
})

// when
closer := dbpurge.New(ctx, logger, db)
defer closer.Close()

// then
require.Eventually(t, func() bool {
// Query all stats created not earlier than 7 months ago
stats, err := db.GetWorkspaceAgentStats(ctx, now.Add(-7*30*24*time.Hour))
if err != nil {
return false
}
return !containsWorkspaceAgentStat(stats, first) &&
containsWorkspaceAgentStat(stats, second)
}, testutil.WaitShort, testutil.IntervalFast)

}

func containsWorkspaceAgentStat(stats []database.GetWorkspaceAgentStatsRow, needle database.WorkspaceAgentStat) bool {
return slices.ContainsFunc(stats, func(s database.GetWorkspaceAgentStatsRow) bool {
return s.WorkspaceRxBytes == needle.RxBytes
})
}

func TestDeleteOldWorkspaceAgentLogs(t *testing.T) {
Expand Down