Skip to content

feat(web): savings-card historical vs flat-rate dashboard tile - #281

Merged
frahlg merged 1 commit into
masterfrom
280-dashboard-savings-card
May 18, 2026
Merged

frahlg merged 1 commit into
masterfrom
280-dashboard-savings-card

Conversation

@frahlg

@frahlg frahlg commented May 18, 2026

Copy link
Copy Markdown
Member

Summary

  • New `` web component, mounted on the main dashboard above the controls row.
  • Mono tabular headline (`+/- N.NN SEK (+/-NN%)`), color per sign via `--ftw-savings-color` so per-poll re-paint doesn't restyle the tree.
  • Inline-SVG diverging sparkline: per-day bars above (green) or below (red) a 1 px hairline zero baseline. Same gestalt the plan view's "vs flat avg price" badge uses, extended along the time axis.
  • Week/Month toggle copied from `ftw-history-card` so the dashboard's history block and savings block share one pill vocabulary.

Three terminal states

  • `empty` — `/api/savings/daily` returned `[]` (no `price.zone` configured). Card shows the "No price provider configured" muted line, no sparkline.
  • `awaiting_prices` — every day comes back with `resolution: "no_prices"` (zone set, price table empty for the range). Card shows "Awaiting price data for the selected range…", no sparkline. This matters on a fresh dev boot — otherwise a flat zero sparkline looks like a silent failure.
  • `ready` — at least one day has real prices → render headline + sub + sparkline.

Test plan

  • `node --check web/components/ftw-savings-card.js` — syntax clean.
  • Smoke against running binary: `GET /api/savings/daily?days=7` 200 with the expected JSON shape, including `resolution: "no_prices"` on a fresh DB.
  • HTML mount + `components/index.js` registration verified by grep.
  • Live browser render — Claude in Chrome extension wasn't reachable from this session; please eyeball the card on `/` once feat(api): historical vs flat-rate savings — /api/savings/daily #279 lands and confirm light + dark theme both pick up tokens cleanly (DESIGN.md compliance).

Depends on

#279 (`/api/savings/daily` endpoint). This PR is stacked on `278-historical-savings-vs-flat` so the API exists during local testing; will need rebase onto master once #279 merges.

Closes #280.

Mounts above the controls row on the main dashboard. Self-contained
web component: own Week/Month toggle (same pill shape ftw-history-card
uses, single shared accent), own fetch loop against /api/savings/daily,
own paint cycle. No external JS wiring.

Headline is a mono tabular SEK total with sign + percentage, color
flipped per sign via a CSS custom property so re-paint on each poll
tick doesn't restyle the world. Sparkline is inline SVG — diverging
bars on a hairline zero baseline (green above, red below), the same
gestalt the plan view's "vs flat avg price" badge uses but extended
along the time axis.

Three terminal states:
  - empty       : /api/savings/daily returned []  (no price.zone)
  - awaiting    : every day comes back resolution=no_prices
                  (zone set, price provider hasn't filled yet)
  - ready       : at least one day has real prices → render normally

The "awaiting" state matters on a fresh dev system — the API otherwise
returns 7 zeroed days, and a flat sparkline at zero would look like a
silent failure rather than the legitimate "no data yet" it is.

Closes #280. Depends on #279 (server-side endpoint).
@frahlg
frahlg force-pushed the 280-dashboard-savings-card branch from a51ad58 to 79faad3 Compare May 18, 2026 13:56
@chatgpt-codex-connector

Copy link
Copy Markdown

💡 Codex Review

https://github.com/frahlg/forty-two-watts/blob/a51ad58f05e0b59a7509489c2790674563ad0699/go/internal/state/cost.go#L139
P1 Badge Use end-exclusive range for average price slots

Change the flat-rate average query to treat untilMs as exclusive; using slot_ts_ms BETWEEN ? AND ? includes a slot that starts exactly at untilMs. For cached past days, untilMs is the next midnight, so the first slot of the following day is incorrectly included in AvgImportOreKwh/AvgExportOreKwh, which biases flat_cost_ore and therefore saved_ore for every completed day.


https://github.com/frahlg/forty-two-watts/blob/a51ad58f05e0b59a7509489c2790674563ad0699/go/internal/api/api_savings.go#L146
P2 Badge Key savings cache with pricing context

The cache is indexed only by YYYY-MM-DD, but the computed value also depends on zone and export-pricing settings. After a live config reload (for example changing price.zone, export bonus/fee, or flat export override), historical days are served from the old cached value while today is recomputed with new settings, producing stale/mixed totals until process restart.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@frahlg
frahlg merged commit f695c5b into master May 18, 2026
1 check passed
@frahlg
frahlg deleted the 280-dashboard-savings-card branch May 18, 2026 14:02
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.

Dashboard: savings card showing historical vs flat-rate

1 participant