ws: record daily repo stats history alongside current upsert

This commit is contained in:
2026-05-07 18:46:26 +01:00
parent d317d2e561
commit 871490b9d4
2 changed files with 43 additions and 0 deletions
+4
View File
@@ -339,6 +339,10 @@ func dispatchAgentMessage(ctx context.Context, c *Conn, hostID string, env api.E
} else { } else {
slog.Info("ws: repo stats refreshed", "host_id", hostID) slog.Info("ws: repo stats refreshed", "host_id", hostID)
} }
day := time.Now().UTC().Format("2006-01-02")
if err := deps.Store.UpsertHostRepoStatsHistory(ctx, hostID, day, patch, time.Now().UTC()); err != nil {
slog.Warn("ws: upsert host repo stats history", "host_id", hostID, "err", err)
}
case api.MsgCommandResult: case api.MsgCommandResult:
// TODO(P2): persist command.result acks for "did the agent // TODO(P2): persist command.result acks for "did the agent
+39
View File
@@ -133,3 +133,42 @@ func TestRepoStatsReportPartialUpdate(t *testing.T) {
t.Errorf("LastCheckStatus: got %q want ok", got.LastCheckStatus) t.Errorf("LastCheckStatus: got %q want ok", got.LastCheckStatus)
} }
} }
func TestRepoStatsReportWritesHistoryRow(t *testing.T) {
t.Parallel()
s := openWSTestStore(t)
ctx := context.Background()
const hostID = "h-stats-history"
seedHostWS(t, s, hostID)
payload := api.RepoStatsPayload{
TotalSizeBytes: int64ptrWS(12345),
SnapshotCount: int64ptrWS(7),
}
env, err := api.Marshal(api.MsgRepoStats, "", payload)
if err != nil {
t.Fatalf("marshal: %v", err)
}
deps := HandlerDeps{Store: s}
dispatchAgentMessage(ctx, nil, hostID, env, deps)
pts, err := s.ListHostRepoStatsHistory(ctx, hostID, time.Time{})
if err != nil {
t.Fatalf("list history: %v", err)
}
if len(pts) != 1 {
t.Fatalf("want 1 history row, got %d", len(pts))
}
wantDay := time.Now().UTC().Format("2006-01-02")
if got := pts[0].Day.Format("2006-01-02"); got != wantDay {
t.Errorf("day: want %s, got %s", wantDay, got)
}
if pts[0].TotalSizeBytes == nil || *pts[0].TotalSizeBytes != 12345 {
t.Errorf("TotalSizeBytes: want 12345, got %v", pts[0].TotalSizeBytes)
}
if pts[0].SnapshotCount == nil || *pts[0].SnapshotCount != 7 {
t.Errorf("SnapshotCount: want 7, got %v", pts[0].SnapshotCount)
}
}