Skip to content

feat: custom analytics period, sortable Sessions & Runs with date filters - #445

Merged
artempartos merged 2 commits into
developfrom
feat/analytics-custom-period-list-sorting
Oct 8, 2026
Merged

artempartos merged 2 commits into
developfrom
feat/analytics-custom-period-list-sorting

Conversation

@artempartos

Copy link
Copy Markdown
Contributor

Why

Two asks from the team:

  • Analytics had only fixed presets (7 / 30 / 90 days, a year). Someone wanted to look at the last two weeks and could not.
  • The Sessions & Runs lists could not be sorted, so finding the most expensive or the longest session or run meant scrolling the whole feed.

What changes

Custom period in analytics

  • Every period picker gets a Custom range option: project analytics, company analytics, Profile → Usage, and a member's page. It opens a date range picker, and the page reloads once both days are picked.
  • AnalyticsPeriod.window turns a request into a Window (a preset or custom, both ends inclusive). Every analytics service now filters on window.range instead of an open-ended since...
  • A custom range is grouped the way the preset closest in length is: ≤31 days by day, ≤180 days by week, longer by month.
  • An end date in the future is clamped to today. An unreadable range (malformed date, or end before start) falls back to 30d, and the picker shows the window the charts actually used.
  • New URL params: period=custom&from=YYYY-MM-DD&to=YYYY-MM-DD. Days are UTC, like the rest of analytics.

Sorting and filters on Sessions & Runs (project feed and company list)

  • The Tokens / Cost / Duration / Started headers sort the list. The first click sorts highest first; a second click reverses it.

  • New filters:

    • a date range, with presets (Today, 7 / 14 / 30 / 90 days), on both lists;
    • Workflow on the project feed;
    • Project on the company list.
  • Sort and dates travel as a ransack q: q[s]=cost_cents desc, q[created_from], q[created_until].

    • TerminalSession / WorkflowRun allowlist the new attributes and gain created_from / created_until scopes.
    • New ransackers on WorkflowRun give a run's cost and tokens as the sum over its step sessions, each counted once. Both models get a duration ransacker, and a live session or run counts its duration up to now.
  • SessionListSort applies the order itself instead of ransack's sort, for two reasons:

    • rows with no duration have to go last in both directions;
    • the project feed orders a UNION of sessions and runs by one column.

    Leaving ransack's global fields_sort_option alone keeps the members list and the board-tasks API sorting exactly as before. The count query leaves out the sort value, because for a run that value is a subquery per row.

  • The existing status / agent / search / user filters stay as top-level params. Each one either covers two state machines, goes through a run's steps, or follows the prompt-visibility rule, and none of that fits a single ransack condition.

Docs: analytics.md and sessions-and-runs.md are updated.

Verification

  • Backend: new tests for AnalyticsPeriod, the custom range in the session/workflow cost services and in all four controllers, and in the feed (cost and tokens sorting across the union, duration with NULLs last and live rows, inclusive date range on both sides, workflow filter, unknown sort falling back). There are also controller tests for q on both lists, including a q that is not a hash. Every affected test file passes locally (≈320 runs).
  • Frontend: tests for the custom-range picker (choosing Custom does not reload; picking two days does), the sort headers (first click sorts highest first, the second click reverses), and sort plus dates surviving a filter change. Locally, vitest ran 1285 of 1285 across shared/ and every affected page.
  • tsc, eslint, steiger (FSD), rubocop and brakeman are clean.
  • Checked by hand in a browser on a seeded stack: sorting by cost and by duration across sessions and runs, the 14-day preset combined with sorting, a Sep 24 – Oct 7 custom range in project analytics, and the company list sorted by tokens with a date range.
  • I did not run the full check_all locally; CI is the gate for that.

Notes

  • The Tokens column is hidden at narrower widths by the existing responsive CSS, so its sort header is hidden with it.
  • The contribution heatmap still always shows the last year, as before.

🤖 Generated with Claude Code

artempartos and others added 2 commits October 8, 2026 21:12
The picker only offered fixed presets, so a two-week window — the most
common question after a sprint — could not be asked at all. "Custom range"
opens a date range picker and reloads once both days are picked.

AnalyticsPeriod now resolves a request to a Window (preset or custom, both
ends inclusive) and every analytics service filters on its range instead
of an open-ended `since..`. A custom range buckets like the preset of
about its length; an unreadable one falls back to the default preset. It
applies to project and company analytics, Profile -> Usage and a member's
page.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Artem Petrov <[email protected]>
Finding the most expensive or the longest run meant scrolling the whole
feed. The Tokens, Cost, Duration and Started headers now sort the project
feed and the company list (highest first, a second click flips it), and
both lists gain a date-range filter; the project feed adds a Workflow
filter and the company list a Project filter.

Sort and dates travel as a ransack `q` (`q[s]`, `q[created_from]`,
`q[created_until]`). A run sorts by what its step sessions add up to, via
ransackers on WorkflowRun; a live session or run counts its duration up to
now. SessionListSort applies the order itself rather than ransack's sort:
rows without a duration must sink in both directions, and the project feed
orders a UNION of two tables by one column. The existing status, agent,
search and user filters stay as they were — each spans both state machines
or the prompt-visibility rule, which a single ransack predicate cannot.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Artem Petrov <[email protected]>
@artempartos
artempartos merged commit 9ce5d68 into develop Oct 8, 2026
8 checks passed
@artempartos artempartos mentioned this pull request Oct 8, 2026
artempartos added a commit that referenced this pull request Oct 8, 2026
Adds the entries #441, #445 and #447 merged without, then moves
[Unreleased] into 1.1.0. Minor, not patch: the release carries new
features (comment editing, custom analytics range, Sessions & Runs
sorting and date filters, /changelog, Managed by Aixle).

Signed-off-by: Artem Petrov <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

1 participant