-
Notifications
You must be signed in to change notification settings - Fork 175
v0.20 PR 3: Cost dashboard tab #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4ba0937
agent: extract cost-queries helper for shared aggregation
mcheemaa 651bcc5
dashboard: shared chart primitive (svg stacked bar, tooltip, axes)
mcheemaa ddf5e16
api: GET /ui/api/cost endpoint with headline, daily, breakdowns, top …
mcheemaa bdcfb35
dashboard: cost tab
mcheemaa 8ba5fd8
cost: rank top sessions by in-window spend, fix mobile chart squash
mcheemaa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| // Cost aggregation queries shared between the MCP cost resource, the | ||
| // universal_metrics_read tool, and the dashboard cost API. All SQL uses | ||
| // COALESCE(SUM(...), 0) so empty result sets return 0 rather than NULL. | ||
|
|
||
| import type { Database } from "bun:sqlite"; | ||
|
|
||
| export type CostForPeriod = { total: number; events: number }; | ||
|
|
||
| export type CostHeadline = { | ||
| today: number; | ||
| yesterday: number; | ||
| this_week: number; | ||
| this_month: number; | ||
| all_time: number; | ||
| day_delta_pct: number; | ||
| week_delta_pct: number; | ||
| }; | ||
|
|
||
| export type DailyCostRow = { | ||
| day: string; | ||
| cost_usd: number; | ||
| input_tokens: number; | ||
| output_tokens: number; | ||
| by_model: Array<{ model: string; cost_usd: number }>; | ||
| }; | ||
|
|
||
| export type ByModelRow = { | ||
| model: string; | ||
| cost_usd: number; | ||
| pct: number; | ||
| input_tokens: number; | ||
| output_tokens: number; | ||
| events: number; | ||
| }; | ||
|
|
||
| export type ByChannelRow = { | ||
| channel_id: string; | ||
| cost_usd: number; | ||
| sessions: number; | ||
| avg_per_session: number; | ||
| input_tokens: number; | ||
| output_tokens: number; | ||
| }; | ||
|
|
||
| export type TopSessionRow = { | ||
| session_key: string; | ||
| channel_id: string; | ||
| conversation_id: string; | ||
| total_cost_usd: number; | ||
| turn_count: number; | ||
| last_active_at: string; | ||
| }; | ||
|
|
||
| // Percent change from `prior` to `now`, or 0 when prior is 0 (avoids NaN). | ||
| function deltaPct(now: number, prior: number): number { | ||
| return prior === 0 ? 0 : ((now - prior) / prior) * 100; | ||
| } | ||
|
|
||
| // Helper: run a single-row `SELECT COALESCE(SUM(cost_usd), 0) AS total` | ||
| // scoped by an inlined date expression. `dateExpr` is a trusted constant, | ||
| // never interpolated from user input. | ||
| function sumCost(db: Database, dateExpr: string): number { | ||
| const row = db.query(`SELECT COALESCE(SUM(cost_usd), 0) AS total FROM cost_events WHERE ${dateExpr}`).get() as { | ||
| total: number; | ||
| }; | ||
| return row.total; | ||
| } | ||
|
|
||
| // Shared by MCP cost resource and universal metrics tool. `dateFilter` is | ||
| // an inlined SQLite date expression (e.g. "date('now', '-7 days')"). | ||
| export function getCostForPeriod(db: Database, dateFilter: string): CostForPeriod { | ||
| const row = db | ||
| .query( | ||
| `SELECT COALESCE(SUM(cost_usd), 0) AS total, COUNT(*) AS events | ||
| FROM cost_events WHERE created_at >= ${dateFilter}`, | ||
| ) | ||
| .get() as { total: number; events: number }; | ||
| return row; | ||
| } | ||
|
|
||
| export function getCostHeadline(db: Database): CostHeadline { | ||
| const today = sumCost(db, "date(created_at) = date('now')"); | ||
| const yesterday = sumCost(db, "date(created_at) = date('now', '-1 day')"); | ||
| const thisWeek = sumCost(db, "created_at >= datetime('now', '-7 days')"); | ||
| const priorWeek = sumCost( | ||
| db, | ||
| "created_at >= datetime('now', '-14 days') AND created_at < datetime('now', '-7 days')", | ||
| ); | ||
| const thisMonth = sumCost(db, "created_at >= datetime('now', '-30 days')"); | ||
| const allTime = (db.query("SELECT COALESCE(SUM(cost_usd), 0) AS total FROM cost_events").get() as { total: number }) | ||
| .total; | ||
|
|
||
| return { | ||
| today, | ||
| yesterday, | ||
| this_week: thisWeek, | ||
| this_month: thisMonth, | ||
| all_time: allTime, | ||
| day_delta_pct: deltaPct(today, yesterday), | ||
| week_delta_pct: deltaPct(thisWeek, priorWeek), | ||
| }; | ||
| } | ||
|
|
||
| // `days` is null for all-time, otherwise clamped by the caller. Returns | ||
| // only days that have events; the chart skips missing days on its x-axis. | ||
| export function getDailyCost(db: Database, days: number | null): DailyCostRow[] { | ||
| const where = days === null ? "" : "WHERE created_at >= datetime('now', ?)"; | ||
| const params: string[] = days === null ? [] : [`-${days} days`]; | ||
|
|
||
| const dayRows = db | ||
| .query( | ||
| `SELECT date(created_at) AS day, | ||
| COALESCE(SUM(cost_usd), 0) AS cost_usd, | ||
| COALESCE(SUM(input_tokens), 0) AS input_tokens, | ||
| COALESCE(SUM(output_tokens), 0) AS output_tokens | ||
| FROM cost_events ${where} GROUP BY day ORDER BY day ASC`, | ||
| ) | ||
| .all(...params) as Array<{ day: string; cost_usd: number; input_tokens: number; output_tokens: number }>; | ||
|
|
||
| const modelRows = db | ||
| .query( | ||
| `SELECT date(created_at) AS day, model, COALESCE(SUM(cost_usd), 0) AS cost_usd | ||
| FROM cost_events ${where} GROUP BY day, model ORDER BY day ASC, cost_usd DESC`, | ||
| ) | ||
| .all(...params) as Array<{ day: string; model: string; cost_usd: number }>; | ||
|
|
||
| const byDay = new Map<string, Array<{ model: string; cost_usd: number }>>(); | ||
| for (const mr of modelRows) { | ||
| const list = byDay.get(mr.day) ?? []; | ||
| list.push({ model: mr.model, cost_usd: mr.cost_usd }); | ||
| byDay.set(mr.day, list); | ||
| } | ||
|
|
||
| return dayRows.map((r) => ({ ...r, by_model: byDay.get(r.day) ?? [] })); | ||
| } | ||
|
|
||
| export function getByModel(db: Database, days: number | null): ByModelRow[] { | ||
| const where = days === null ? "" : "WHERE created_at >= datetime('now', ?)"; | ||
| const params: string[] = days === null ? [] : [`-${days} days`]; | ||
|
|
||
| const rows = db | ||
| .query( | ||
| `SELECT model, COALESCE(SUM(cost_usd), 0) AS cost_usd, | ||
| COALESCE(SUM(input_tokens), 0) AS input_tokens, | ||
| COALESCE(SUM(output_tokens), 0) AS output_tokens, COUNT(*) AS events | ||
| FROM cost_events ${where} GROUP BY model ORDER BY cost_usd DESC`, | ||
| ) | ||
| .all(...params) as Array<Omit<ByModelRow, "pct">>; | ||
|
|
||
| const total = rows.reduce((acc, r) => acc + r.cost_usd, 0); | ||
| return rows.map((r) => ({ ...r, pct: total > 0 ? r.cost_usd / total : 0 })); | ||
| } | ||
|
|
||
| export function getByChannel(db: Database, days: number | null): ByChannelRow[] { | ||
| const where = days === null ? "" : "WHERE ce.created_at >= datetime('now', ?)"; | ||
| const params: string[] = days === null ? [] : [`-${days} days`]; | ||
|
|
||
| const rows = db | ||
| .query( | ||
| `SELECT s.channel_id AS channel_id, COALESCE(SUM(ce.cost_usd), 0) AS cost_usd, | ||
| COUNT(DISTINCT ce.session_key) AS sessions, | ||
| COALESCE(SUM(ce.input_tokens), 0) AS input_tokens, | ||
| COALESCE(SUM(ce.output_tokens), 0) AS output_tokens | ||
| FROM cost_events ce JOIN sessions s ON ce.session_key = s.session_key | ||
| ${where} GROUP BY s.channel_id ORDER BY cost_usd DESC`, | ||
| ) | ||
| .all(...params) as Array<Omit<ByChannelRow, "avg_per_session">>; | ||
|
|
||
| return rows.map((r) => ({ ...r, avg_per_session: r.sessions > 0 ? r.cost_usd / r.sessions : 0 })); | ||
| } | ||
|
|
||
| export function getTopSessions(db: Database, limit: number, days: number | null): TopSessionRow[] { | ||
| const where = days === null ? "" : "WHERE s.last_active_at >= datetime('now', ?)"; | ||
| const params: Array<string | number> = days === null ? [] : [`-${days} days`]; | ||
|
|
||
| return db | ||
| .query( | ||
| `SELECT s.session_key, s.channel_id, s.conversation_id, | ||
| s.total_cost_usd, s.turn_count, s.last_active_at | ||
| FROM sessions s ${where} | ||
| ORDER BY s.total_cost_usd DESC, s.last_active_at DESC LIMIT ?`, | ||
| ) | ||
| .all(...params, limit) as TopSessionRow[]; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This query uses
sessions.total_cost_usd, which is a lifetime cumulative value, so the Cost tab’s 7/30/90-day filter does not actually constrain the cost shown or ranking in top sessions. In practice, any session with large historical spend but minimal recent activity can dominate the table even when the selected range is short, producing misleading analytics for the “in this range” view. Compute and sort bySUM(cost_events.cost_usd)within the active window instead ofsessions.total_cost_usd.Useful? React with 👍 / 👎.