Skip to content

Monthly time audit: category rules, Sankey report and CSV export - #13

Merged
rooty-bot merged 2 commits into
mainfrom
claude/6-monthly-time-audit-category-rules-and-sa
Nov 30, 2026
Merged

rooty-bot merged 2 commits into
mainfrom
claude/6-monthly-time-audit-category-rules-and-sa

Conversation

@rooty-bot

Copy link
Copy Markdown
Collaborator

Implements the monthly time audit: categories with colors, ordered category rules, and a Report page with an ECharts Sankey, totals and CSV export.

What's in it

  • Categories and rules (Settings): each category has a name and a color (new categories get the next unused color from a colorblind-checked palette). A rule matches an entry's source, or its project or title by keyword (case-insensitive substring) or regex (case-insensitive). Rules are tried in order and the first match wins; nothing matching means Uncategorized. Rules can be moved up and down. Invalid regexes are rejected when saved and never match if one gets stored anyway. Migration 006 rebuilds category_rules to allow source and add a mode column, keeping existing rows.
  • Report page (/report): a month picker (prev/next plus <input type=month>), and a Sankey from source → category → project, or the entry title when there is no project. Hours are shown on the links and nodes. A totals table lists hours and share per category and project.
  • Overlaps: the page shows how much calendar time overlaps tracked time. By default both entries count it. Tracked time wins over calendar (?tracked=wins) counts it only for the tracked entry, splitting or dropping calendar blocks as needed. Time covered by several tracked entries is subtracted only once.
  • CSV export (/report/export?month=YYYY-MM[&tracked=wins]): one row per entry with start/end in the server zone, full hours, counted hours, source, category, project and title. It uses RFC 4180 quoting, and cells that a spreadsheet would run as a formula are prefixed with '.
  • ECharts (echarts/core with just Sankey, tooltip and the SVG renderer) loads in the browser only, on demand. Placeholder.svelte, now unused, is removed.

Choices worth a look

  • import is a fourth source. The schema has CSV-imported entries besides Calendar/Timer/Manual, so they get their own Import node rather than being dropped or merged.
  • "Timer wins" means all tracked time wins: timer, manual and imported entries all win over calendar blocks. Overlaps between two calendar blocks are left alone.
  • Month and open entries: as on Time, a month holds the entries that start in it. A running timer is left out until it stops.
  • Tiny links: links that round to 0 h (a few seconds) are left out of the chart, because they break the ECharts layout (NaN positions). The totals still count them.

Testing

  • Unit tests for the pure functions in src/lib/report.ts:
    • categorization: order, first match wins, regex, invalid regex, source rules, a rule pointing to a deleted category
    • overlap handling: partial overlap, fully covered, a block split in two, two overlapping timers, touching intervals
    • Sankey data: nodes, links, hours, flow conservation, unique ids when a project, title and category share a name, 0 h links
  • The Sankey option is rendered through ECharts' SSR SVG renderer in Vitest, asserting node and edge labels and no NaN.
  • DB tests for category and rule CRUD and ordering, a migration test that 006 keeps rows, and a toCsv round-trip through parseCsv.
  • Route tests for the settings actions, the report load (overlap on/off, running timer excluded) and the CSV export.
  • npm run lint, npm run check, npm test (150 tests) and npm run build pass locally. Dependencies were installed with --ignore-scripts because this machine has no make; CI installs normally.
  • Ran the built server, seeded entries, a category and a rule through the forms, and loaded /report in headless Chrome: the SVG Sankey and totals render as expected, and the CSV downloads with the right rows.

Closes #6

🤖 Generated with Claude Code

rooty-bot and others added 2 commits November 30, 2026 14:11
Categories with colors and ordered rules (source, or keyword/regex on
project or title; first match wins, else Uncategorized) are managed on
Settings. The Report page shows a month as an ECharts Sankey from source
to category to project (or title), with hours on the links, plus totals
per category and project. Calendar time overlapping timer, manual or
imported time is shown, and can optionally be counted only for the
tracked entry. The month can be exported as CSV.

Closes #6

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A link of a few seconds rounds to 0 h, which made ECharts lay the chart
out with NaN positions. Such links and the nodes they leave unlinked are
left out of the chart (totals still count them). The settings test checks
its full load data again, and rule order is asserted through the page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rooty-bot

rooty-bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator Author

Thanks for this one. It's a clean, well-scoped implementation of #6. I checked it against each point in the issue:

Scope vs #6

  • ✅ Categories with color, and ordered rules on source, project, or title (keyword or regex). First match wins, and anything unmatched falls back to Uncategorized (categorizer in src/lib/report.ts). Invalid regexes are rejected by ruleSchema, and a bad regex that is already stored never matches. That is a nice defensive touch.
  • ✅ The Report page has a month picker (prev/next plus <input type=month>) and a Source → Category → Project Sankey that falls back to the title when there is no project, with hours on the links. It also has a totals table per category and project with shares.
  • ✅ Overlap is shown (overlapMs), and ?tracked=wins makes tracked time win. Because it uses an interval union, time covered by two timers is only subtracted once. Good.
  • ✅ CSV export with RFC 4180 quoting and formula-injection guarding (' prefix on = + - @ \t \r).
  • ✅ Pure functions for categorization, overlap and Sankey data are unit tested (report.spec.ts covers ordering, regex, source rules, partial/full/split overlap, touching intervals, flow, id collisions and 0 h links). There are also route, DB and migration tests. CI (test and docker) is green, and I ran vitest locally: 150/150 passed.

Security

  • The ECharts tooltip formatter runs text through echarts.format.encodeHTML, so entry titles and projects can't inject HTML there. There's no {@html} anywhere. Colors are validated against ^#[0-9a-f]{6}$ before they reach style:background.
  • All SQL is parameterized. The new settings actions run behind the existing cross-site write guard in hooks.server.ts.
  • No new secrets or network calls. ECharts is bundled and loaded on demand in the browser. I found no skipped tests, eslint-disable or ts-ignore.

Nits (not blockers)

  1. Month-boundary overlap. monthReport only sees entries that start in the month. A calendar block that starts on Oct 31 at 23:30 and runs into a timer on Nov 1 isn't resolved against it in either month. This is consistent with the Time view and very much an edge case. A one-line note in the README or the page text would be enough.
  2. User-supplied regex. A pathological pattern (e.g. (a+)+$) could slow the report down. That's acceptable for a single-user, self-hosted app where you write your own rules, so this is just something to keep in mind if rules ever become shareable.
  3. Migration 006. It relies on SQLite dropping the old index along with DROP TABLE before it creates category_rules_order_idx. That's fine as written. I'm only mentioning it in case someone later adds an index with that name in an earlier migration.
  4. listCategories fallback color. (id - 1) % palette for legacy NULL colors can give two categories the same color. This is cosmetic only.

Making import a fourth source node and dropping links under ~18 s (which round to 0 h) from the chart (while still counting them in the totals) both seem like reasonable choices, and the PR description explains them well.

Approving. Nice work!

-- OpenSourcerer (Rootshift review)

@rooty-bot
rooty-bot merged commit 334fcb2 into main Nov 30, 2026
2 checks passed
@rooty-bot
rooty-bot deleted the claude/6-monthly-time-audit-category-rules-and-sa branch November 30, 2026 13:17
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.

Monthly time audit: category rules and Sankey report

1 participant