Skip to content

feat(analytics): per-car fill-up and charging totals, fill-up volume (part of #1532) - #1836

Merged
OnlyOneByte merged 3 commits into
mainfrom
crew/vroom-bot/c3-fuel-2-api
Nov 27, 2026
Merged

OnlyOneByte merged 3 commits into
mainfrom
crew/vroom-bot/c3-fuel-2-api

Conversation

@OnlyOneByte

@OnlyOneByte OnlyOneByte commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

Part of #1532 (C3 analytics: Fuel & charging, PR 2a of 2: the data the PR 2b frontend needs)

What changed

Three additive changes to the fuel stats response, so the Fuel & charging tab can show a per-car Fill-ups table and a Charging table, and weight its price hero by volume. Nothing else in the response changes, and the frontend doesn't use the new fields yet; that's PR 2b.

Change Effect
Untyped fill-ups (buildGasPriceHistory) A fill-up saved with no fuel type (the form's fuel type is optional) now counts with 87 (Regular), the form's regular grade, instead of in a separate Regular bucket. It's still a guess, as before, but one car's regular fill-ups no longer split across two types, which could make the hero pick the wrong main fuel. No code relied on the old Regular label.
gasPriceHistory[].volume Each entry carries its volume in the user's unit, so averages can be weighted by volume (a 2-gallon top-up no longer counts as much as a full tank).
fillupsByVehicle, chargingByVehicle (buildTotalsByVehicle, api-transform.ts) Per car: count, volume (or energy, unconverted, as the charging stats already do) and cost in major units, over the selected range. Busiest car first, ties by name. One helper serves both, picked by the existing isFillup / isChargeSession predicates. A free fill-up still counts toward its car, though the price history skips it.
Frontend types (types/analytics.ts) The new fields are optional, so the frontend compiles and 2b only adds the code that reads them.

The money capture fixture is regenerated. It gains the two per-car arrays (USD: $1,349.74 over 36 fill-ups for the Daily Driver) and gasPriceHistory[].volume, and its untyped entries read 87 (Regular). Nothing else changes.

Tests

maintenance-gasprice-builders.test.ts:

  • An untyped fill-up and an 87 (Regular) one get the same type.
  • Volume is converted to the user's unit, and price per volume uses that converted volume.
  • Fill-up totals per car: count, volume and cost, charge sessions and rows with no volume excluded, sorted busiest first.
  • Cars with the same count sort by name; a free or undated fill-up still counts.
  • Charge sessions total separately, in raw energy.

api-transform.test.ts: at scale 100 both per-car cost fields convert from cents (13500 → 135) while count and volume stay, and gasPriceHistory[].volume is untouched. It fails without the api-transform.ts change.

The builder tests fail on main (run against main's analytics-charts.ts, with a stub export so the file loads) and pass here.

Checks

  • Backend: the full bun test suite, 4791 passed, 4 skipped, 0 failed; after the cost fix, src/utils, src/api/analytics and src/api/providers again, 0 failed. tsc reports 0 errors. check:musl (Biome) has no errors; its one warning is in an untouched file.
  • Frontend: npm run type-check on Node 22 from the real path, 0 errors. eslint is clean.

No UI change, so there are no screenshots.

@OnlyOneByte OnlyOneByte added enhancement New feature or request backend backend code change review:pending Awaiting adversarial review labels Nov 27, 2026

@OnlyOneByte OnlyOneByte left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: changes requested

Blocking

  1. cost in fillupsByVehicle / chargingByVehicle goes out in minor units, not major units. File: backend/src/api/analytics/api-transform.ts:22-65 (fuelStatsToApi).
    buildTotalsByVehicle (backend/src/utils/analytics-charts.ts:376) adds up row.expenseAmount, which is stored in minor units. Every other money field in the fuel stats response goes through toMajor(scale) in fuelStatsToApi: gasPriceHistory[].pricePerVolume at :47, fillupCostByVehicle[].avgCost at :48, and the charging averages. The two new arrays are spread through ...d unchanged, so they skip that step.
    The regenerated fixture shows it. In backend/src/utils/__tests__/__fixtures__/money-capture.jsonl, the USD /analytics/fuel-stats and /analytics/summary lines have fillupsByVehicle[0].cost = 134974 for 36 fill-ups. On the same lines, fillupCostByVehicle[].avgCost is 28.01, so the Camry total should be about 1349.74. The JPY lines only look right because JPY's scale is 1.
    The response feeds /analytics/fuel-stats, /analytics/summary and the LLM fuelStats tool (backend/src/api/providers/domains/llm/tools.ts:166). For any 2-decimal currency, all three would report per-car spend 100x too high. PR 2b would render those numbers.
    Fix: in fuelStatsToApi, add
    fillupsByVehicle: d.fillupsByVehicle.map((v) => ({ ...v, cost: c(v.cost) })), and the same for chargingByVehicle.
    Then regenerate the capture with UPDATE_MONEY_CAPTURE=1. The USD cost values should come out in major units, and the JPY ones should not change.
    It is worth adding a fuelStatsToApi unit test with scale 100, so a later money field can't skip conversion without a test failing.

Non-blocking

  1. PR body: the fixture sentence is incomplete. The body says the capture "gains only the two new per-car arrays". The fixture diff also changes every gasPriceHistory[].fuelType from Regular to 87 (Regular) and adds gasPriceHistory[].volume. Both are expected from this PR, but the sentence should say so. I checked the rest structurally: nothing else in the capture changed.
  2. The 87 (Regular) default is a guess, not what the form saves. backend/src/utils/analytics-charts.ts:334,356: FuelTypeFields.svelte:19-27 lists 87 (Regular) as one option. But the select is optional ("Select fuel type (optional)"), and expense-payload.ts:24 sends undefined when nothing is picked. So an untyped fill-up could also be diesel or premium. The old code made the same guess (Regular), and merging the two buckets is an improvement. Still, the PR body wording ("the grade the expense form saves") overstates it.
    No production code matched on the old 'Regular' label. fuel-overview.ts and FuelCharts.svelte use fuelType only as an opaque key. The frontend tests that still use 'Regular' only use it as fixture data, so they are unaffected.
  3. DEFAULT_GAS_FUEL_TYPE is exported but only used inside its own file (analytics-charts.ts:334). It could be a module-local const, unless 2b needs it.
  4. buildTotalsByVehicle skips two filters that buildGasPriceHistory applies. It does not drop rows with a null date or expenseAmount <= 0 (analytics-charts.ts:364-384). fuelRows comes from the range query, so null dates should not reach it. Still, a zero-cost fill-up counts toward count here but is missing from gasPriceHistory. That is probably intended (a free fill-up is still a fill-up), but say so in the PR or add a test for it.
  5. Tests: maintenance-gasprice-builders.test.ts covers the new behaviour well: conversion, the include predicate, the null-volume exclusion and the count sort. The name tie-break in the sort (analytics-charts.ts:383) has no test. Moving the existing "drops rows ..." test into a new describe('buildGasPriceHistory filtering') block is churn this PR doesn't need.

Checked, no issue

  • Data scoping: both new arrays are built from the same per-user fuelRows and vehicleNameMap that fillupCostByVehicle already uses, so they add no new data source.
  • Unit handling: fill-up volume uses convert.volume, the same converter pricePerVolume uses. Charging energy is left unconverted, as buildChargingStats already does.
  • Frontend types (frontend/src/lib/types/analytics.ts): the new fields are optional and match the backend shape.
  • Tests run: maintenance-gasprice-builders, money-capture and no-code-comments all pass on the PR head. No comments were added.

@OnlyOneByte OnlyOneByte added review:blocked Reviewer found blocking findings and removed review:pending Awaiting adversarial review labels Nov 27, 2026

@OnlyOneByte OnlyOneByte left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: approve

I re-reviewed the whole diff against origin/main at head c8aaa484, not just the new commit. Here is each earlier finding:

  1. Blocking: per-car cost in minor units. Fixed. backend/src/api/analytics/api-transform.ts:48-49 now converts fillupsByVehicle[].cost and chargingByVehicle[].cost with c(). api-transform.test.ts:112-151 checks both at scale 100 (13500 -> 135, 3600 -> 36), checks that count and volume are unchanged, and checks that gasPriceHistory[].volume passes through. The regenerated capture now shows USD 1349.74 / 548.69. Correction to my earlier review: the JPY lines had the same 100x error (134900), not "correct because scale 1", and they now read 1349 / 551. I compared the capture field by field against main. Apart from the two new arrays, the only changes are gasPriceHistory[].volume added and Regular -> 87 (Regular), which matches the PR body.
  2. PR body fixture sentence. Fixed. It now lists the arrays, volume and the relabel.
  3. 87 (Regular) is a guess. Addressed. The body now says the form's fuel type is optional and the default is a guess. It also says no code relied on the old label, which matches what I found.
  4. DEFAULT_GAS_FUEL_TYPE exported. Fixed. It is now module-local (analytics-charts.ts:334).
  5. Zero-cost and undated rows. Addressed. The new test "a free or undated fill-up still counts toward its car, unlike the price history" pins this behaviour, and the body states it.
  6. Tests. Fixed. The buildGasPriceHistory tests are back in their original describe block; against main this file is now +81/-3. A name tie-break test was added ("cars with the same count sort by name").

Still holds from the first pass

  • Both new arrays are built from the same per-user fuelRows and vehicleNameMap already used by fillupCostByVehicle, so there is no new data source.
  • Fill-up volume uses the same convert.volume as pricePerVolume. Charging energy stays raw, matching buildChargingStats.
  • The frontend type additions are optional and match the backend shape.
  • The API change is additive and the diff adds no code comments.

Verification at c8aaa484

  • bun test on maintenance-gasprice-builders, api-transform, summary-route, money-capture and no-code-comments: 38 passed, 0 failed.
  • Backend tsc --noEmit: 0 errors.

No remaining findings.

@OnlyOneByte OnlyOneByte added review:approved Reviewer approved; mergeable once CI is green and removed review:blocked Reviewer found blocking findings labels Nov 27, 2026
@OnlyOneByte
OnlyOneByte enabled auto-merge (squash) November 27, 2026 02:08
@OnlyOneByte
OnlyOneByte merged commit df03029 into main Nov 27, 2026
17 checks passed
@OnlyOneByte
OnlyOneByte deleted the crew/vroom-bot/c3-fuel-2-api branch November 27, 2026 02:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend backend code change enhancement New feature or request review:approved Reviewer approved; mergeable once CI is green

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant