4aae61297f
Analytics parity phases 2–7 (docs/plans/analytics-parity/), built on
138e706 and ported onto v2.3.1:
- Cost by AI tool (?tool= on cost/analytics/stats/workflows, by_tool,
Codex fast/priority and flex tiers, agenthub_cost_usd_total{tool,model})
- Date ranges (from/to) on cost/analytics/workflows; Analytics range select
with Today / This week (Monday) / This month (1st) / 7d / 30d / 90d / All /
Custom; workflow filters carried into workflow-stats.js (read worker)
- Budgets and cost alerts as alert rule types (alert_rules rebuilt without
its rule_type CHECK), GET /api/budgets/status, rule preview, spend pace,
status-bar budget chip; rules stay editable in place
- Query Explorer (/query, allowlisted sort, CSV/JSON export) and scheduled
reports (report_definitions/report_runs, Settings → Reports)
- Session Replay tab and immutable snapshots (session_snapshots, /snapshot/:token)
- Remote Codex sync, push ingest with a hashed token, privacy redaction and
ignored folders (Settings → Data), Cursor sessions (no cost)
- Read-only MCP server, statusline, Grafana/Prometheus files
Also: resizable sidebar, pet run/wave animations, the workspace home keeps a
half-typed prompt, and the pre-commit hook runs only the related tests for a
small commit.
41 lines
1.5 KiB
TypeScript
41 lines
1.5 KiB
TypeScript
/**
|
||
* @file Spend pace from the daily cost series (`daily_costs` of GET
|
||
* /api/pricing/cost, already in local dates): the average cost per day over
|
||
* the last 7 days, the month to date, and a month-end forecast that carries
|
||
* that pace over the days left in the month.
|
||
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
|
||
*/
|
||
|
||
export interface SpendPace {
|
||
/** Average cost per day over the 7 days ending today (today included). */
|
||
perDay7: number;
|
||
/** Spend since the 1st of this month. */
|
||
monthToDate: number;
|
||
/** monthToDate + perDay7 × the days left after today. */
|
||
forecastMonth: number;
|
||
}
|
||
|
||
/**
|
||
* @param daily `{date: "YYYY-MM-DD", cost}` per local day, any order.
|
||
* @param today Today's local date, `YYYY-MM-DD`.
|
||
*/
|
||
export function spendPace(daily: Array<{ date: string; cost: number }>, today: string): SpendPace {
|
||
const [y, m, d] = today.split("-").map(Number) as [number, number, number];
|
||
const day = (offset: number) => {
|
||
const t = new Date(Date.UTC(y, m - 1, d + offset));
|
||
return t.toISOString().slice(0, 10);
|
||
};
|
||
const weekStart = day(-6);
|
||
const monthStart = today.slice(0, 8) + "01";
|
||
let week = 0;
|
||
let mtd = 0;
|
||
for (const r of daily) {
|
||
if (r.date > today) continue;
|
||
if (r.date >= weekStart) week += r.cost;
|
||
if (r.date >= monthStart) mtd += r.cost;
|
||
}
|
||
const daysInMonth = new Date(Date.UTC(y, m, 0)).getUTCDate();
|
||
const perDay7 = week / 7;
|
||
return { perDay7, monthToDate: mtd, forecastMonth: mtd + perDay7 * (daysInMonth - d) };
|
||
}
|