fix(pricing): book daily cost to the day tokens were spent
token_usage holds a session's cumulative totals with no time, so /api/pricing/cost booked a multi-day session's whole cost to its start date and the status bar's Today read ~$0. TranscriptCache now also splits usage by each message's UTC hour (tokensByHour); the hook writer stores it in the new token_usage_hourly table (MAX per hour) and the server back-fills existing sessions at start. The daily split books each total over its hourly rows' local days and the rest to the start date, so the days still add up to the total.
This commit is contained in:
@@ -469,6 +469,19 @@ function startBackgroundServices() {
|
||||
}
|
||||
}
|
||||
|
||||
// Per-hour tokens for sessions recorded before `token_usage_hourly`, so the
|
||||
// daily cost split covers them too. Picks only sessions with no hourly rows,
|
||||
// so a later start re-reads nothing already done; non-blocking.
|
||||
{
|
||||
const t = setTimeout(() => {
|
||||
require("./lib/hourly-usage")
|
||||
.backfillHourly()
|
||||
.then((n) => n > 0 && console.log(`Hourly token usage: back-filled ${n} session(s)`))
|
||||
.catch((err) => console.warn("hourly token backfill failed:", err?.message || err));
|
||||
}, 2_000);
|
||||
if (t.unref) t.unref();
|
||||
}
|
||||
|
||||
const { startUpdateScheduler } = require("./update-scheduler");
|
||||
const { broadcast } = require("./websocket");
|
||||
startUpdateScheduler({ broadcast });
|
||||
|
||||
Reference in New Issue
Block a user