nolint:paralleltest // It uses LockIDDBPurge.
(t *testing.T)
| 385 | |
| 386 | //nolint:paralleltest // It uses LockIDDBPurge. |
| 387 | func TestDeleteOldWorkspaceAgentStats(t *testing.T) { |
| 388 | ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) |
| 389 | defer cancel() |
| 390 | |
| 391 | now := dbtime.Now() |
| 392 | // TODO: must refactor DeleteOldWorkspaceAgentStats to allow passing in cutoff |
| 393 | // before using quarts.NewMock() |
| 394 | clk := quartz.NewReal() |
| 395 | db, _ := dbtestutil.NewDB(t) |
| 396 | logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug) |
| 397 | |
| 398 | defer func() { |
| 399 | if t.Failed() { |
| 400 | t.Log("Test failed, printing rows...") |
| 401 | ctx := testutil.Context(t, testutil.WaitShort) |
| 402 | buf := &bytes.Buffer{} |
| 403 | enc := json.NewEncoder(buf) |
| 404 | enc.SetIndent("", "\t") |
| 405 | wasRows, err := db.GetWorkspaceAgentStats(ctx, now.AddDate(0, -7, 0)) |
| 406 | if err == nil { |
| 407 | _, _ = fmt.Fprintf(buf, "workspace agent stats: ") |
| 408 | _ = enc.Encode(wasRows) |
| 409 | } |
| 410 | tusRows, err := db.GetTemplateUsageStats(context.Background(), database.GetTemplateUsageStatsParams{ |
| 411 | StartTime: now.AddDate(0, -7, 0), |
| 412 | EndTime: now, |
| 413 | }) |
| 414 | if err == nil { |
| 415 | _, _ = fmt.Fprintf(buf, "template usage stats: ") |
| 416 | _ = enc.Encode(tusRows) |
| 417 | } |
| 418 | s := bufio.NewScanner(buf) |
| 419 | for s.Scan() { |
| 420 | t.Log(s.Text()) |
| 421 | } |
| 422 | _ = s.Err() |
| 423 | } |
| 424 | }() |
| 425 | |
| 426 | // given |
| 427 | // Note: We use increments of 2 hours to ensure we avoid any DST |
| 428 | // conflicts, verifying DST behavior is beyond the scope of this |
| 429 | // test. |
| 430 | // Let's use RxBytes to identify stat entries. |
| 431 | // Stat inserted 180 days + 2 hour ago, should be deleted. |
| 432 | first := dbgen.WorkspaceAgentStat(t, db, database.WorkspaceAgentStat{ |
| 433 | CreatedAt: now.AddDate(0, 0, -180).Add(-2 * time.Hour), |
| 434 | ConnectionCount: 1, |
| 435 | ConnectionMedianLatencyMS: 1, |
| 436 | RxBytes: 1111, |
| 437 | SessionCountSSH: 1, |
| 438 | }) |
| 439 | |
| 440 | // Stat inserted 180 days - 2 hour ago, should not be deleted before rollup. |
| 441 | second := dbgen.WorkspaceAgentStat(t, db, database.WorkspaceAgentStat{ |
| 442 | CreatedAt: now.AddDate(0, 0, -180).Add(2 * time.Hour), |
| 443 | ConnectionCount: 1, |
| 444 | ConnectionMedianLatencyMS: 1, |
nothing calls this directly
no test coverage detected