🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
fix(api): adjust date range calculations for past-minutes mode in GSC and PDF exports
Bill Yang3fca277291a1
diff --git a/client/src/api/gsc/gscDateRange.ts b/client/src/api/gsc/gscDateRange.tsindex e71266318..78e483838 100644--- a/client/src/api/gsc/gscDateRange.ts+++ b/client/src/api/gsc/gscDateRange.ts@@ -8,9 +8,11 @@ import { getStartAndEndDate } from "../utils";export function getGSCDateRange(time: Time, timeZone: string): { startDate: string; endDate: string } {const today = DateTime.now().setZone(timeZone);if (time.mode === "past-minutes") {+ // The newer edge is only "now" until the window is stepped back, after which+ // ending on today would widen the report past what the dashboard shows.return {startDate: today.minus({ minutes: time.pastMinutesStart }).toISODate() ?? "",- endDate: today.toISODate() ?? "",+ endDate: today.minus({ minutes: time.pastMinutesEnd }).toISODate() ?? "",};}if (time.mode === "all-time") {diff --git a/client/src/app/[site]/components/SubHeader/Export/exportPdf.ts b/client/src/app/[site]/components/SubHeader/Export/exportPdf.tsindex 0d00f0765..db28be2f9 100644--- a/client/src/app/[site]/components/SubHeader/Export/exportPdf.ts+++ b/client/src/app/[site]/components/SubHeader/Export/exportPdf.ts@@ -19,9 +19,11 @@ export function getPdfDateRange(time: Time, timeZone: string): { startDate: striconst today = DateTime.now().setZone(timeZone);if (time.mode === "past-minutes") {+ // The newer edge is only "now" until the window is stepped back, after which+ // ending on today would widen the report past what the dashboard shows.return {startDate: today.minus({ minutes: time.pastMinutesStart }).toISODate() ?? "",- endDate: today.toISODate() ?? "",+ endDate: today.minus({ minutes: time.pastMinutesEnd }).toISODate() ?? "",};}if (time.mode === "all-time") {diff --git a/client/src/components/charts/timeSeriesChartUtils.test.ts b/client/src/components/charts/timeSeriesChartUtils.test.tsindex 5e66981ca..ca80137c0 100644--- a/client/src/components/charts/timeSeriesChartUtils.test.ts+++ b/client/src/components/charts/timeSeriesChartUtils.test.ts@@ -30,9 +30,11 @@ describe("getChartTimeBounds", () => {});});- it("month: weekly buckets land on Sunday, including a month ending on one", () => {- expect(boundsIso({ mode: "month", month: "2026-08-01" }, "week").max).toBe("2026-08-30T00:00:00.000-04:00");- // January 2027 ends on a Sunday: flooring to Monday would drop that bucket.+ it("month: weekly buckets clear both the Sunday and the Monday convention", () => {+ // August 2026 ends on a Monday, which `toStartOfInterval(.., 1 WEEK)` buckets+ // a day after `toStartOfWeek` does; stopping on the Sunday would clip it.+ expect(boundsIso({ mode: "month", month: "2026-08-01" }, "week").max).toBe("2026-08-31T00:00:00.000-04:00");+ // January 2027 ends on a Sunday, where the Sunday floor is the later one.expect(boundsIso({ mode: "month", month: "2027-01-01" }, "week").max).toBe("2027-01-31T00:00:00.000-05:00");});diff --git a/client/src/components/charts/timeSeriesChartUtils.ts b/client/src/components/charts/timeSeriesChartUtils.tsindex f21af5969..9e468d610 100644--- a/client/src/components/charts/timeSeriesChartUtils.ts+++ b/client/src/components/charts/timeSeriesChartUtils.ts@@ -142,12 +142,21 @@ export type ChartTimeBounds = {max: Date | undefined;};-// ClickHouse's `toStartOfWeek` floors to Sunday where Luxon's `startOf("week")`-// floors to Monday, so weekly buckets land on Sundays. Flooring with Luxon would-// put the axis max *before* the final bucket of a period that ends on a Sunday,-// and consumers drop points past the max.+// Weekly buckets follow two conventions at once: the analytics queries floor+// with ClickHouse's `toStartOfWeek` (Sunday) and the dashboard cards with+// `toStartOfInterval(..., INTERVAL 1 WEEK)` (Monday) — verified, they differ by+// a day. Luxon's `startOf("week")` is Monday. Take the later of the two floors+// so neither convention's final bucket lands past the max, where consumers drop+// it and the axis clips it; the cost is at most a day of trailing gutter.+const floorToWeekStart = (dt: DateTime): DateTime => {+ const day = dt.startOf("day");+ const sunday = day.minus({ days: dt.weekday % 7 }); // Luxon weekday: 1 = Monday, 7 = Sunday+ const monday = day.minus({ days: dt.weekday - 1 });+ return sunday > monday ? sunday : monday;+};+const floorToBucketStart = (dt: DateTime, bucket: TimeBucket): DateTime =>- bucket === "week" ? dt.startOf("day").minus({ days: dt.weekday % 7 }) : floorToBucket(dt, bucket);+ bucket === "week" ? floorToWeekStart(dt) : floorToBucket(dt, bucket);/*** The start of the final bucket in `[start, endExclusive)`. Bounds end there