Skip to content

Commit 123eee5

Browse files
fix(web): 7-day usage chart shows hourly variation
The 7-day window used daily buckets, so the chart drew 7 points and hid the variation the 24h and 30-day views show. Windows up to a week now use hourly buckets, the server accepts hourly windows up to 7 days, and hourly labels spanning more than a day include the date, plus the zone name for the repeated hour of a fall-back transition. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8d199b4 commit 123eee5

5 files changed

Lines changed: 59 additions & 28 deletions

File tree

‎apps/server/src/usage/UsageService.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const RATES_REFRESH_FLOOR_MS = 60 * 1000;
9191
* last write lands just before local midnight on the window's first day.
9292
*/
9393
const MTIME_SLACK_MS = 36 * 60 * 60 * 1000;
94-
const MAX_HOURLY_WINDOW_MS = 24 * 60 * 60 * 1000;
94+
const MAX_HOURLY_WINDOW_MS = 7 * 24 * 60 * 60 * 1000;
9595

9696
/**
9797
* The longest window the UI offers, 90 days, plus its `MTIME_SLACK_MS`, rounded
@@ -726,7 +726,7 @@ export const make = Effect.gen(function* () {
726726
if (durationMs <= 0 || durationMs > MAX_HOURLY_WINDOW_MS) {
727727
return yield* new UsageReadError({
728728
reason: "invalidWindow",
729-
detail: "Hourly usage window must be greater than zero and at most 24 hours",
729+
detail: "Hourly usage window must be greater than zero and at most 7 days",
730730
});
731731
}
732732
hourlyWindow = { sinceTimeMs, untilTimeMs };

‎apps/web/src/components/usage/UsagePage.tsx‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
formatUsageContractMismatch,
5050
formatUsd,
5151
makeWindow,
52+
resolutionForWindow,
5253
} from "@t3tools/shared/usageFormat";
5354
import { Button, InlineButton } from "../ui/button";
5455
import { ProviderInstanceIcon } from "../chat/ProviderInstanceIcon";
@@ -128,7 +129,7 @@ export function UsagePage() {
128129
window: makeWindow(
129130
preferences.windowDays,
130131
undefined,
131-
preferences.windowDays === 1 ? "hour" : "day",
132+
resolutionForWindow(preferences.windowDays),
132133
),
133134
}));
134135
const metric = preferences.metric;
@@ -146,7 +147,6 @@ export function UsagePage() {
146147
[preferences.hiddenProviders],
147148
);
148149
const { days: windowDays, window } = windowSelection;
149-
const isPast24Hours = windowDays === 1;
150150
const {
151151
merged: answeredUsage,
152152
environments,
@@ -217,6 +217,7 @@ export function UsagePage() {
217217
: enumerateHourStarts(shownWindow.sinceTime, shownWindow.untilTime),
218218
[shownWindow.sinceTime, shownWindow.untilTime],
219219
);
220+
const formatPeriodHour = hours.length > 24 ? formatDateTimeShort : formatHourShort;
220221
// Newest first: the window can run 90 periods, so the interesting end
221222
// belongs at the top of the table.
222223
const breakdownPeriods = useMemo<readonly (DailyTotals | HourlyTotals)[]>(
@@ -279,7 +280,7 @@ export function UsagePage() {
279280
updatePreferences({ windowDays: days });
280281
setWindowSelection({
281282
days,
282-
window: makeWindow(days, undefined, days === 1 ? "hour" : "day"),
283+
window: makeWindow(days, undefined, resolutionForWindow(days)),
283284
});
284285
};
285286
const selectMetric = (nextMetric: UsageMetric) => {
@@ -343,7 +344,7 @@ export function UsagePage() {
343344
});
344345
return;
345346
}
346-
const nextWindow = makeWindow(windowDays, undefined, isPast24Hours ? "hour" : "day");
347+
const nextWindow = makeWindow(windowDays, undefined, resolutionForWindow(windowDays));
347348
if (
348349
nextWindow.sinceDay !== window.sinceDay ||
349350
nextWindow.untilDay !== window.untilDay ||
@@ -936,7 +937,7 @@ export function UsagePage() {
936937
>
937938
<td className="py-2 text-foreground">
938939
{"hourStart" in period
939-
? formatHourShort(period.hourStart, shownWindow.timeZone)
940+
? formatPeriodHour(period.hourStart, shownWindow.timeZone)
940941
: formatDayShort(period.day)}
941942
</td>
942943
{activeProviders.map((provider) => (

‎apps/web/src/components/usage/UsageProviderChart.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useCallback, useLayoutEffect, useMemo, useRef, useState } from "react";
44

55
import type { DailyTotals, HourlyTotals } from "@t3tools/shared/usageMerge";
66
import {
7+
formatDateTimeShort,
78
formatDayShort,
89
formatHourShort,
910
formatRelativeHourShort,
@@ -333,8 +334,9 @@ export function UsageProviderChart({
333334
const hoveredPeriod = hoverIndex === null ? undefined : periods[hoverIndex];
334335
const hoveredColumn = hoverIndex === null ? undefined : columns[hoverIndex];
335336
const partial = providers.some((provider) => loadingProviders.has(provider));
337+
const formatHour = periods.length > 24 ? formatDateTimeShort : formatHourShort;
336338
const formatPeriod = (period: string) =>
337-
resolution === "hour" ? formatHourShort(period, timeZone) : formatDayShort(period);
339+
resolution === "hour" ? formatHour(period, timeZone) : formatDayShort(period);
338340
const formatTooltipPeriod = (period: string) =>
339341
resolution === "hour" && referenceTime !== undefined
340342
? formatRelativeHourShort(period, referenceTime, timeZone)

‎packages/shared/src/usageFormat.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
formatPercent,
99
formatRelativeHourShort,
1010
makeWindow,
11+
resolutionForWindow,
1112
} from "./usageFormat.ts";
1213

1314
describe("formatPercent", () => {
@@ -65,6 +66,15 @@ describe("hourly usage formatting", () => {
6566
expect(formatHourShort("2026-11-01T06:37:00.000Z", "America/New_York")).toBe("1 AM EST");
6667
});
6768

69+
it("disambiguates repeated hours in date-plus-hour labels", () => {
70+
expect(formatDateTimeShort("2026-11-01T05:37:00.000Z", "America/New_York")).toBe(
71+
"Nov 1, 1 AM EDT",
72+
);
73+
expect(formatDateTimeShort("2026-11-01T06:37:00.000Z", "America/New_York")).toBe(
74+
"Nov 1, 1 AM EST",
75+
);
76+
});
77+
6878
it("makes hourly tooltip dates relative to the window in its requested time zone", () => {
6979
const windowEnd = "2026-08-11T14:37:00.000Z";
7080

@@ -91,6 +101,15 @@ describe("hourly usage formatting", () => {
91101
expect(window.untilTime).toBe("2026-08-11T12:37:00.000Z");
92102
});
93103

104+
it("spans the whole week when a 7-day window is hourly", () => {
105+
const window = makeWindow(7, new Date("2026-08-11T12:37:42.123Z"), resolutionForWindow(7));
106+
107+
expect(window.resolution).toBe("hour");
108+
expect(window.sinceTime).toBe("2026-08-04T12:37:00.000Z");
109+
expect(window.untilTime).toBe("2026-08-11T12:37:00.000Z");
110+
expect(resolutionForWindow(30)).toBe("day");
111+
});
112+
94113
it("degrades an unknown resolved zone to UTC instead of crashing", () => {
95114
const resolved = new Intl.DateTimeFormat().resolvedOptions();
96115
const resolvedOptions = vi

‎packages/shared/src/usageFormat.ts‎

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -127,41 +127,44 @@ export function enumerateHourStarts(sinceTime: string, untilTime: string): reado
127127
}
128128

129129
/**
130-
* A rolling bucket start rendered in the viewer's requested time zone.
131-
*
132-
* Repeated wall-clock hours during a fall-back transition include their short
133-
* zone name so the two distinct buckets remain distinguishable.
130+
* True when the instant's wall-clock hour occurs twice in the zone, as during a
131+
* fall-back transition. Hour labels add the short zone name for these.
134132
*/
135-
export function formatHourShort(hourStart: string, timeZone?: string): string {
136-
const instant = new Date(hourStart);
137-
if (Number.isNaN(instant.getTime())) return hourStart;
138-
const options = timeZone === undefined ? {} : { timeZone };
139-
const hourFormat = dateTimeFormatter("en-US", {
140-
...options,
141-
hour: "numeric",
142-
});
133+
function isRepeatedWallHour(instant: Date, timeZone?: string): boolean {
143134
const wallHourFormat = dateTimeFormatter("en-CA", {
144-
...options,
135+
...(timeZone === undefined ? {} : { timeZone }),
145136
year: "numeric",
146137
month: "2-digit",
147138
day: "2-digit",
148139
hour: "2-digit",
149140
hourCycle: "h23",
150141
});
151142
const wallHour = wallHourFormat.format(instant);
152-
const isRepeatedHour = [-HOUR_MS, HOUR_MS].some(
143+
return [-HOUR_MS, HOUR_MS].some(
153144
(offset) => wallHourFormat.format(new Date(instant.getTime() + offset)) === wallHour,
154145
);
146+
}
155147

156-
if (!isRepeatedHour) return hourFormat.format(instant);
148+
/**
149+
* A rolling bucket start rendered in the viewer's requested time zone.
150+
*
151+
* Repeated wall-clock hours during a fall-back transition include their short
152+
* zone name so the two distinct buckets remain distinguishable.
153+
*/
154+
export function formatHourShort(hourStart: string, timeZone?: string): string {
155+
const instant = new Date(hourStart);
156+
if (Number.isNaN(instant.getTime())) return hourStart;
157157
return dateTimeFormatter("en-US", {
158158
...(timeZone === undefined ? {} : { timeZone }),
159159
hour: "numeric",
160-
timeZoneName: "short",
160+
...(isRepeatedWallHour(instant, timeZone) ? { timeZoneName: "short" } : {}),
161161
}).format(instant);
162162
}
163163

164-
/** `2026-08-11T14:37:00Z` to `Aug 11, 2 PM` in the requested zone. */
164+
/**
165+
* `2026-08-11T14:37:00Z` to `Aug 11, 2 PM` in the requested zone, with the
166+
* short zone name when that hour repeats during a fall-back transition.
167+
*/
165168
export function formatDateTimeShort(instant: string, timeZone?: string): string {
166169
const date = new Date(instant);
167170
if (Number.isNaN(date.getTime())) return instant;
@@ -170,6 +173,7 @@ export function formatDateTimeShort(instant: string, timeZone?: string): string
170173
month: "short",
171174
day: "numeric",
172175
hour: "numeric",
176+
...(isRepeatedWallHour(date, timeZone) ? { timeZoneName: "short" } : {}),
173177
}).format(date);
174178
}
175179

@@ -201,6 +205,11 @@ export function formatRelativeHourShort(
201205
return formatDateTimeShort(hourStart, timeZone);
202206
}
203207

208+
/** Hourly buckets for windows up to a week, daily beyond that. */
209+
export function resolutionForWindow(days: number): UsageResolution {
210+
return days <= 7 ? "hour" : "day";
211+
}
212+
204213
/**
205214
* The window the page requests, expressed in the viewer's own time zone so days
206215
* line up with what they actually experienced.
@@ -232,10 +241,10 @@ export function makeWindow(
232241
const untilDay = format.format(now);
233242
if (resolution === "hour") {
234243
// Minute-aligned bounds keep labels readable while still representing an
235-
// exact rolling 24-hour duration. Fixed-duration buckets remain correct
236-
// across offset changes and daylight-saving transitions.
244+
// exact rolling duration of `days` whole days. Fixed-duration buckets remain
245+
// correct across offset changes and daylight-saving transitions.
237246
const untilTimeMs = Math.floor(now.getTime() / 60_000) * 60_000;
238-
const sinceTimeMs = untilTimeMs - 24 * HOUR_MS;
247+
const sinceTimeMs = untilTimeMs - days * 24 * HOUR_MS;
239248
const sinceTime = new Date(sinceTimeMs);
240249
const untilTime = new Date(untilTimeMs);
241250
return {

0 commit comments

Comments
 (0)