Add a per-day breakdown to the usage summary
Node · Node · intermediate · modification
Extends summarize() with the per-day breakdown the dashboard needs: alongside the running `total`, it now returns `byDay`, keyed by calendar date. Bucketing derives the day from each event's timestamp; checked against a fixture day and the totals line up.
Billing dashboard aggregator. The service is deployed with `TZ` set to the account's reporting timezone (US Pacific for this account), and the per-day figures drive the invoice line items.
Requirements
- Extend summarize(events) to return a `byDay` map (calendar date -> summed units) alongside the existing `total`.
- Each event's `timestamp` is an epoch-ms instant; bucket it into the calendar day it occurred in the account's reporting timezone (the service is deployed with `TZ` set to that zone).
- A day key is a calendar date `YYYY-MM-DD`: an event late in the local evening counts toward that local day, never the next one.
Files touched
- src/reporting/dailyUsage.js
--- src/reporting/dailyUsage.js
// The service is deployed with TZ set to the account's reporting timezone.
+function dayKey(timestampMs) {
+ return new Date(timestampMs).toISOString().slice(0, 10);
+}
+
function summarize(events) {
let total = 0;
+ const byDay = {};
for (const event of events) {
total += event.units;
+ const day = dayKey(event.timestamp);
+ byDay[day] = (byDay[day] || 0) + event.units;
}
- return { total };
+ return { total, byDay };
}