Skip to content

feat: time range selector with a cycle range and spend over any window - #28

Open
alex-pex wants to merge 7 commits into
ofershap:mainfrom
alex-pex:feat/cycle-window
Open

alex-pex wants to merge 7 commits into
ofershap:mainfrom
alex-pex:feat/cycle-window

Conversation

@alex-pex

@alex-pex alex-pex commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Changes

1. Compute spend over any window and add a cycle range

Spend is billed per cycle but was shown next to windows of 1 to 30 days, so a 30-day view at the start of a cycle only covered the current cycle. Each cycle's billed total is now shared over the usage events that fall inside the window, per user and per day, across as many cycles as the window touches. The ranking, the team total, the daily charts and the model costs come from the same estimate, and the user stats gain the user's spend over the window.

  • getFullDashboard, getUserStats and the analytics queries also accept a cycle range that starts at the billing cycle start, where the estimate equals the billed total. /api/stats, /api/analytics and /api/users/:email take days=cycle.
  • Model Rankings said "Last N days" but covered all history. It now filters on the window.
  • Tests cover a window spanning two cycles, the cycle range and the list of cycle starts.

2. Shared time range selector with a cycle range

One selector (24h, 3d, 7d, 14d, 30d, Cycle) in the overview blue style, at the right of the toolbar or header, drives the overview, the insights page and the user page, which gets a picker for the first time. Cycle is the default.

  • The range lives in the URL (?range=7) and is read by the server pages: the first render already uses it, nothing is stored in the browser, and links are shareable. The main menu forwards it, so Overview and Insights keep the range you picked.
  • Labels follow the range: "Top Spenders (30d)" replaces "(Current Cycle)", and the spend column, $/req and spend rank use the same window. The user page shows the user's spend over it.
  • On the overview, the members count moves next to the search field.

3. Mark billing cycle changes on the spend charts

A thin vertical line, behind the data, marks where a billing cycle starts inside the displayed range, with "previous cycle" on its left and "new cycle" on its right. It shows on the daily spend trend (overview and user page) and on the stacked daily spend by user chart. The last two provisional days of the trend now fade out instead of sitting in an orange block.

4. Zoom the spend charts on the selected range

On 24h and 3d the charts still showed a week, with the older days dimmed on the side. They now cover exactly the selected range. The 24h range charts the last 24 hours: one point per hour on the trend and one bar every 2 hours on the stacked chart (hours are UTC). Its spend figures cover those same 24 hours. The user page does the same, with $/req computed from the events of each hour, while its daily table stays per day.

Screenshots

Overview, 30 days: selector on the right, members count next to the search, cycle change marker on the daily spend.
1-overview-30d

Top spenders and daily spend by user, 30 days: same marker between two bars.
2-top-spenders-and-daily-spend-30d

Overview, 24 hours: one point per hour (UTC), KPIs over the last 24 hours.
3-overview-24h-hourly

User page, 30 days: selector in the header, spend over the range, $/req line, marker on the daily spend.
4-user-page-30d

Insights, 14 days: shared selector, Model Rankings on the same window.
5-insights-14d

Checks

npm run typecheck, npm test (22 tests) and npm run lint pass.

🤖 Generated with Claude Code

@alex-pex
alex-pex force-pushed the feat/cycle-window branch 2 times, most recently from 4aa281c to 9049932 Compare November 9, 2026 09:43
@ofershap

ofershap commented Nov 9, 2026

Copy link
Copy Markdown
Owner

Two time-window issues to fix before merging this:

  1. last24HoursStart() uses floor(now / hour) - 23 hours. At 14:41 UTC it starts at 15:00 yesterday, so an event from 14:51 yesterday (23h50m ago) is excluded from "24h" spend. Please separate the rolling cutoff from chart bucket boundaries and add a fixed-clock regression for the oldest partial hour.

  2. On the same view, spend uses that hourly cutoff, but request totals and active-member/activity queries still use dayStart(1), yesterday at midnight. They therefore cover different windows. Daily aggregates cannot supply an exact rolling-24h cutoff. Please either use suitable event-level data for the matching metrics or label the different windows explicitly, including any $/request ratio that combines them.

I ran the existing 22 tests and typecheck at 9049932; both pass. A local fixed-clock boundary test reproduces the first issue: two equal-cost events, both within 24 hours, produce 500 cents instead of the full 1000-cent allocation because the oldest event falls in that partial hour. The current hourly tests cover recent events and bucket count, not this boundary.

Please add the boundary coverage and clarify the metric windows. Chart and navigation QA is still pending; this is not a merge approval.

alex-pex and others added 7 commits November 13, 2026 17:29
Spend is estimated over any window, across billing cycles, by sharing each
cycle's billed total over the usage events of the window. The 24h range is
a true rolling 24 hours: the window starts exactly 24 hours ago, so events
of the oldest partial hour count. It is returned hour by hour, from the
hour of the cutoff to the current hour, with per-hour request counts. The
stats, analytics and user APIs accept days=cycle.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The Overview, Insights and user pages share one selector: 24h, 3d, 7d,
14d, 30d or Cycle (default). The range lives in the URL (?range=) and the
main menu forwards it to Overview and Insights. The members table headers
no longer repeat the range, the selector already shows it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A vertical line marks each billing cycle change on the daily spend trend
and on the daily spend by user bars, labelled previous cycle / new cycle.
A label is left out when it would not fit inside the plot area, so it never
runs over the axis near the edge of the chart.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The charts show exactly the selected range. The 24h range is drawn hour by
hour (UTC), one bar or point per hour like the other ranges, with an axis
tick every 3 hours on fixed clock hours (0h, 3h, 6h...), an average over 24
hours and $/req computed from the events of each hour. The charts are titled
Hourly Spend there, and cycle markers are skipped. The trend no longer
states how many days or hours it shows.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…range

On the 24h range the spend covers the last 24 hours, but requests, active
members and the $/request ratios came from daily data starting at
yesterday's midnight, so the figures did not describe the same window.
They now come from the usage events of the last 24 hours, on the Overview
and on the user page. Lines are only kept per day, so the Lines card is
left out of the 24h range.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Daily Spend by Top Users now sits under Team Spend Trend and Top
Spenders under Model Cost Comparison, so the two charts over time share
a column instead of being read diagonally.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The row now opens the user page, keeping the selected range. The arrow
lightens on row hover instead of on its own hover.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@alex-pex

Copy link
Copy Markdown
Contributor Author

Thanks, both points are addressed. I reorganised the branch into one change per commit (force-pushed, so older commit links are stale).

  1. The 24h cutoff is now exactly now - 24h, separate from the chart's hourly buckets, with fixed-clock tests for your boundary case: f105bf3, 1cbbb36.
  2. On 24h, requests, active members and $/request now come from usage events over the same window as the spend, rather than labelling the different windows: 58241c4.

Unrelated to the review, one extra commit: the whole members row is now clickable (c50b93e).

I haven't rerun the production build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants