Files
multica/server/internal/handler/parse_since_param_test.go
Jiayuan Zhang 8d30d76300 feat(dashboard): add 1d range to workspace Usage tab (#2837)
* feat(dashboard): add 1d time range to workspace Usage tab

1d means "today" — the natural calendar day from 00:00 UTC, matching the
rollup's bucket_date axis — not the trailing 24 hours. The client-side
dailyCutoffIso filter is now applied in daily dim too so 1d collapses
strictly to today even at the midnight UTC edge where the server's
wall-clock since cutoff would otherwise include yesterday.

Co-authored-by: multica-agent <github@multica.ai>

* fix(dashboard): scope `1d` to today only on aggregate endpoints

The pre-aggregated `byAgent` / `runTime` dashboard endpoints leaked
yesterday into the agent leaderboard and KPI cards for the `1d` time
range because `parseSinceParam(days=1)` returned `now-24h` (wall clock)
and the downstream SQL then applied `DATE_TRUNC('day', @since)`, which
landed on yesterday 00:00 UTC. The PR's client-side `dailyCutoffIso`
filter could only fix the date-bearing daily endpoints; aggregate
responses are already collapsed across dates.

Anchor `parseSinceParam` at UTC start-of-today instead, so `days=N`
covers N natural calendar days (today + N-1 prior). This matches the
frontend `dailyCutoffIso = today - (days-1)` semantic that the
workspace dashboard already assumes, and removes the off-by-one that
previously made `30d` return 31 buckets.

The runtime-detail page uses `parseSinceParamInTZ` (timezone-aware),
which is unchanged — it has no `1d` option.

Co-authored-by: multica-agent <github@multica.ai>

---------

Co-authored-by: multica-agent <github@multica.ai>
2026-05-19 08:28:04 +02:00

49 lines
1.9 KiB
Go

package handler
import (
"net/http/httptest"
"testing"
"time"
)
// TestParseSinceParam locks in the natural-calendar-day semantic that the
// workspace dashboard's `1d` / `7d` / `30d` / `90d` selectors depend on:
//
// - `days=N` returns UTC start-of-today minus (N-1) full days, so the
// window covers N calendar days (today + N-1 prior).
// - `days=1` therefore means "today only" — not the trailing 24h that the
// previous wall-clock implementation produced (which leaked yesterday
// into the pre-aggregated `byAgent` / `runTime` endpoints).
func TestParseSinceParam(t *testing.T) {
now := time.Now().UTC()
startOfToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
cases := []struct {
name string
path string
want time.Time
}{
{name: "default (no days)", path: "/x", want: startOfToday.AddDate(0, 0, -29)}, // defaultDays=30
{name: "1d = today only", path: "/x?days=1", want: startOfToday}, // critical: PR 2837 fix
{name: "7d", path: "/x?days=7", want: startOfToday.AddDate(0, 0, -6)},
{name: "30d", path: "/x?days=30", want: startOfToday.AddDate(0, 0, -29)},
{name: "90d", path: "/x?days=90", want: startOfToday.AddDate(0, 0, -89)},
{name: "invalid falls back to default", path: "/x?days=abc", want: startOfToday.AddDate(0, 0, -29)},
{name: "zero falls back to default", path: "/x?days=0", want: startOfToday.AddDate(0, 0, -29)},
{name: "over cap falls back to default", path: "/x?days=400", want: startOfToday.AddDate(0, 0, -29)},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
req := httptest.NewRequest("GET", tc.path, nil)
got := parseSinceParam(req, 30)
if !got.Valid {
t.Fatalf("expected Valid timestamptz, got invalid")
}
if !got.Time.Equal(tc.want) {
t.Errorf("days param in %q: got %s, want %s", tc.path, got.Time.Format(time.RFC3339), tc.want.Format(time.RFC3339))
}
})
}
}