Skip to content
723 linesCodeBlameRaw
1//! The Usage page: a workspace's usage over a range of days, at price, by
2//! product family, meter, project and day, with what paid for it.
3//!
4//! Every figure comes from the ledger's own lines, measured as the
5//! statement measures them (`statement::PRICE_SQL`: what was charged, plus
6//! what included usage, the trial, a pool or g1t paid, plus what a discount
7//! took off), so the Usage page, Billing, mission control and the agent
8//! fleet show the same numbers. Usage metered through the month and charged
9//! when it closes (storage, git operations, scans, embeddings, domains,
10//! app traffic) is added from `pending_usage`, at cost plus the margin, and
11//! said to be pending.
12
13use std::collections::{BTreeMap, BTreeSet};
14
15use futures_util::future::{try_join, try_join5};
16use g1t_contracts::billing::{
17 Allowance, FeatureUsage, MeterLine, ModelTokens, PlanKind, ProductUsage, ProjectUsage, UsageDay, UsageReport, UsageReportArgs, UsageShare,
18 UsageTotals, PRODUCTS,
19};
20use g1t_contracts::time::{parse_rfc3339, rfc3339};
21use g1t_contracts::{FailureCode, Outcome};
22use g1t_kit::now_ms;
23use serde::Deserialize;
24use worker::Result;
25
26use crate::charged::{charged, pending_split};
27use crate::statement::{PRICE_SQL, RUN_SQL};
28use crate::{Billing, members_only};
29
30/// The meters that make up the agent product, as `METER_KEY_SQL` names
31/// them: what agents cost, attributed to the agent and the person asking.
32pub(crate) const AGENT_METERS_SQL: &str = "('agent_models', 'agent_rate', 'agent_rate_own', 'agent_sandbox')";
33
34/// The longest range the page reads at once.
35pub(crate) const MAX_DAYS: u64 = 400;
36const DAY_MS: u64 = 86_400_000;
37
38/// Which meter a ledger line is on. One SQL expression, so every query
39/// agrees with `METERS`.
40pub(crate) const METER_KEY_SQL: &str = "CASE
41 WHEN task = 'sandbox' AND compute = 'agent' THEN 'agent_sandbox'
42 WHEN task = 'sandbox' THEN 'sandbox'
43 WHEN task = 'self_hosted' THEN 'self_hosted'
44 WHEN task = 'deployments' AND reference LIKE 'deploy/%' THEN 'builds'
45 WHEN task = 'deployments' THEN 'requests'
46 WHEN task = 'domains' THEN 'domains'
47 WHEN task = 'git' THEN 'git'
48 WHEN task = 'storage' THEN 'storage'
49 WHEN task = 'cache' THEN 'cache'
50 WHEN task = 'package_storage' THEN 'packages'
51 WHEN task = 'security' THEN 'security'
52 WHEN task = 'context' THEN 'context'
53 WHEN task = 'gateway' THEN 'gateway'
54 WHEN reference LIKE '%/agent-own%' THEN 'agent_rate_own'
55 WHEN reference LIKE '%/agent%' THEN 'agent_rate'
56 ELSE 'agent_models' END";
57
58/// Every meter: key, name, product family and the unit of its quantity.
59pub(crate) const METERS: [(&str, &str, &str, &str); 17] = [
60 ("agent_models", "Model tokens", "agent", "tokens"),
61 ("agent_rate", "Agent rate", "agent", "tokens"),
62 ("agent_rate_own", "Agent rate, your own model key", "agent", "tokens"),
63 ("agent_sandbox", "Agent sandbox time", "agent", "seconds"),
64 ("sandbox", "Sandbox time", "sandboxes", "seconds"),
65 ("self_hosted", "Self-hosted runner time", "sandboxes", "seconds"),
66 ("gateway", "AI Gateway models", "gateway", "requests"),
67 ("builds", "Builds", "deployments", "entries"),
68 ("requests", "Requests & CPU", "deployments", "entries"),
69 ("domains", "Custom domains", "deployments", "entries"),
70 ("git", "Git operations", "git_storage", "operations"),
71 ("storage", "Private storage", "git_storage", "bytes"),
72 ("cache", "Actions cache", "git_storage", "entries"),
73 ("packages", "Package storage", "packages", "entries"),
74 ("security", "Security scans", "security", "entries"),
75 ("context", "Search embeddings", "search", "entries"),
76 // Kept apart so nothing is ever lost: a line whose meter is unknown.
77 ("other", "Other", "agent", "entries"),
78];
79
80/// The meter month-end usage noted so far (`pending_usage.source`) is on.
81pub(crate) fn meter_of_pending(source: &str) -> &'static str {
82 match source {
83 "deployments" => "requests",
84 "domains" => "domains",
85 "storage" => "storage",
86 "git" => "git",
87 "cache" => "cache",
88 "package_storage" => "packages",
89 "security" => "security",
90 "context" => "context",
91 _ => "other",
92 }
93}
94
95/// What the agent was doing, by a run's task.
96pub(crate) fn feature_of(task: &str) -> (&'static str, &'static str) {
97 match task {
98 "review" => ("reviews", "Reviews"),
99 "plan" => ("plans", "Plans"),
100 "update" => ("catch_ups", "Catch-ups"),
101 "check" | "mergecheck" | "queue" => ("checks", "Checks"),
102 _ => ("runs", "Runs"),
103 }
104}
105
106/// Every day from `from` to `until`, both included, or why not.
107pub(crate) fn days_between(from: &str, until: &str) -> std::result::Result<Vec<String>, &'static str> {
108 let (Some(start), Some(end)) = (parse_rfc3339(&format!("{from}T00:00:00Z")), parse_rfc3339(&format!("{until}T00:00:00Z"))) else {
109 return Err("Give the range as days, YYYY-MM-DD.");
110 };
111 if end < start {
112 return Err("The range ends before it starts.");
113 }
114 if (end - start) / DAY_MS + 1 > MAX_DAYS {
115 return Err("Ask for at most 400 days at a time.");
116 }
117 Ok((0..=(end - start) / DAY_MS).map(|n| rfc3339(start + n * DAY_MS)[..10].to_owned()).collect())
118}
119
120/// One group of ledger lines: a day, a meter and a project.
121#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
122pub(crate) struct Cell {
123 pub day: String,
124 pub meter: String,
125 #[serde(default)]
126 pub project: String,
127 #[serde(default)]
128 pub price: Option<f64>,
129 #[serde(default)]
130 pub quantity: Option<f64>,
131 #[serde(default)]
132 pub entries: Option<f64>,
133 #[serde(default)]
134 pub discount: Option<f64>,
135 #[serde(default)]
136 pub covered: Option<f64>,
137 #[serde(default)]
138 pub cost: Option<f64>,
139}
140
141/// Tokens a day's runs used, by project.
142#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
143pub(crate) struct TokenCell {
144 pub day: String,
145 #[serde(default)]
146 pub project: String,
147 #[serde(default)]
148 pub tokens: Option<f64>,
149}
150
151/// What is metered this month and charged when it closes, by meter: at
152/// price, with what g1t covers of it and what the discount takes off, as
153/// the close will enter it (`charged::pending_split`).
154#[derive(Clone, Debug, Default, PartialEq)]
155pub(crate) struct Pending {
156 pub meter: &'static str,
157 pub micros: i64,
158 pub covered: i64,
159 pub discount: i64,
160}
161
162/// One agent's and one asker's agent-product lines over the range.
163#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
164pub(crate) struct Attributed {
165 #[serde(default)]
166 pub agent: String,
167 #[serde(default)]
168 pub person: String,
169 #[serde(default)]
170 pub price: Option<f64>,
171 #[serde(default)]
172 pub entries: Option<f64>,
173}
174
175/// The agent product by agent and by person, most first, from its lines:
176/// each group's price under the agent's handle and the asker's username,
177/// and lines attributed to no one under an empty key. Both sum to the
178/// agent product's total.
179pub(crate) fn attribution(rows: &[Attributed]) -> (Vec<UsageShare>, Vec<UsageShare>) {
180 fn fold(rows: &[Attributed], key_of: impl Fn(&Attributed) -> &str, label_of: impl Fn(&str) -> String) -> Vec<UsageShare> {
181 let mut by: BTreeMap<&str, UsageShare> = BTreeMap::new();
182 for row in rows {
183 let key = key_of(row);
184 let slice = by.entry(key).or_insert_with(|| UsageShare { key: key.to_owned(), label: label_of(key), ..UsageShare::default() });
185 slice.micros += row.price.unwrap_or(0.0).round() as i64;
186 slice.count += row.entries.unwrap_or(0.0).max(0.0) as u32;
187 }
188 let mut out: Vec<UsageShare> = by.into_values().filter(|s| s.micros != 0 || s.count > 0).collect();
189 out.sort_by(|a, b| b.micros.cmp(&a.micros).then(a.key.cmp(&b.key)));
190 out
191 }
192 let agents = fold(rows, |r| r.agent.as_str(), |k| if k.is_empty() { "Not attributed to an agent".to_owned() } else { format!("@{k}") });
193 let people = fold(rows, |r| r.person.as_str(), |k| if k.is_empty() { "No one asked".to_owned() } else { format!("@{k}") });
194 (agents, people)
195}
196
197/// Shapes the groups into the page: products in order, each with its
198/// meters, each meter with every day of the range and its projects. Only
199/// the products and projects asked for (all, when none are).
200pub(crate) fn shape(
201 days: &[String],
202 cells: &[Cell],
203 tokens: &[TokenCell],
204 pending: &[Pending],
205 products: &[String],
206 projects: &[String],
207) -> (Vec<ProductUsage>, Vec<UsageDay>, UsageTotals, Vec<String>) {
208 let wanted_product = |product: &str| products.is_empty() || products.iter().any(|p| p == product);
209 let wanted_project = |project: &str| projects.is_empty() || projects.iter().any(|p| p == project);
210 let index: BTreeMap<&str, usize> = days.iter().enumerate().map(|(i, d)| (d.as_str(), i)).collect();
211 let product_of = |meter: &str| METERS.iter().find(|m| m.0 == meter).map_or("agent", |m| m.2);
212 let mut all_projects = BTreeSet::new();
213 let mut lines: Vec<MeterLine> = METERS
214 .iter()
215 .map(|(key, label, product, unit)| MeterLine {
216 key: (*key).to_owned(),
217 label: (*label).to_owned(),
218 product: (*product).to_owned(),
219 unit: (*unit).to_owned(),
220 quantity: 0.0,
221 micros: 0,
222 pending_micros: 0,
223 daily: vec![0; days.len()],
224 allowance: None,
225 by_project: vec![],
226 note: None,
227 })
228 .collect();
229 let mut by_day: BTreeMap<(String, String), i64> = BTreeMap::new();
230 let mut totals = UsageTotals::default();
231 let mut projects_of: BTreeMap<(String, String), (i64, f64)> = BTreeMap::new();
232 for cell in cells {
233 if !cell.project.is_empty() {
234 all_projects.insert(cell.project.clone());
235 }
236 let product = product_of(&cell.meter);
237 if !wanted_product(product) || !wanted_project(&cell.project) {
238 continue;
239 }
240 let Some(line) = lines.iter_mut().find(|l| l.key == cell.meter) else { continue };
241 let price = cell.price.unwrap_or(0.0).round() as i64;
242 line.micros += price;
243 // Tokens for the model meter come from the token counts below.
244 if line.key != "agent_models" {
245 line.quantity += if line.unit == "entries" { cell.entries.unwrap_or(0.0) } else { cell.quantity.unwrap_or(0.0) };
246 }
247 if let Some(i) = index.get(cell.day.as_str()) {
248 line.daily[*i] += price;
249 }
250 *by_day.entry((cell.day.clone(), product.to_owned())).or_default() += price;
251 let part = projects_of.entry((cell.meter.clone(), cell.project.clone())).or_default();
252 part.0 += price;
253 part.1 += if line.unit == "entries" { cell.entries.unwrap_or(0.0) } else { cell.quantity.unwrap_or(0.0) };
254 totals.price_micros += price;
255 totals.discount_micros += cell.discount.unwrap_or(0.0).round() as i64;
256 totals.included_micros += cell.covered.unwrap_or(0.0).round() as i64;
257 totals.cost_micros += cell.cost.unwrap_or(0.0).round() as i64;
258 }
259 if wanted_product("agent") {
260 for cell in tokens {
261 if !wanted_project(&cell.project) {
262 continue;
263 }
264 let tokens = cell.tokens.unwrap_or(0.0);
265 if let Some(line) = lines.iter_mut().find(|l| l.key == "agent_models") {
266 line.quantity += tokens;
267 }
268 projects_of.entry(("agent_models".to_owned(), cell.project.clone())).or_default().1 += tokens;
269 }
270 }
271 // Pending usage is no one project's: only when all projects are shown.
272 if projects.is_empty() {
273 for p in pending {
274 let product = product_of(p.meter);
275 if !wanted_product(product) {
276 continue;
277 }
278 if let Some(line) = lines.iter_mut().find(|l| l.key == p.meter) {
279 line.micros += p.micros;
280 line.pending_micros += p.micros;
281 }
282 totals.price_micros += p.micros;
283 totals.pending_micros += p.micros;
284 totals.included_micros += p.covered;
285 totals.discount_micros += p.discount;
286 }
287 }
288 for ((meter, project), (micros, quantity)) in projects_of {
289 if let Some(line) = lines.iter_mut().find(|l| l.key == meter) {
290 line.by_project.push(ProjectUsage { project, micros, quantity });
291 }
292 }
293 for line in &mut lines {
294 line.by_project.sort_by(|a, b| b.micros.cmp(&a.micros).then(a.project.cmp(&b.project)));
295 }
296 let products_out = PRODUCTS
297 .iter()
298 .filter(|(key, _)| wanted_product(key))
299 .map(|(key, label)| {
300 let meters: Vec<MeterLine> = lines.iter().filter(|l| l.product == *key && (l.key != "other" || l.micros != 0)).cloned().collect();
301 ProductUsage {
302 key: (*key).to_owned(),
303 label: (*label).to_owned(),
304 micros: meters.iter().map(|m| m.micros).sum(),
305 meters,
306 features: vec![],
307 }
308 })
309 .collect();
310 let days_out = by_day.into_iter().filter(|(_, micros)| *micros != 0).map(|((day, product), micros)| UsageDay { day, product, micros }).collect();
311 (products_out, days_out, totals, all_projects.into_iter().collect())
312}
313
314impl Billing {
315 /// `usage_report`: members only.
316 pub(crate) async fn usage_report(&self, a: UsageReportArgs) -> Result<Outcome<UsageReport>> {
317 let workspace = a.workspace.to_lowercase();
318 if !a.viewer.is_some_and(|viewer| viewer.is_member(&workspace)) {
319 return Ok(members_only());
320 }
321 let days = match days_between(&a.from, &a.until) {
322 Ok(days) => days,
323 Err(why) => return Ok(Outcome::fail(FailureCode::Invalid, why)),
324 };
325 let from = days[0].clone();
326 let until = days[days.len() - 1].clone();
327 // Lines entered on the last day count, whatever the hour.
328 let end = rfc3339(parse_rfc3339(&format!("{until}T00:00:00Z")).unwrap_or(0) + DAY_MS);
329 let measure = if self.free { "COALESCE(cost_micros, 0)" } else { PRICE_SQL };
330 let now = rfc3339(now_ms());
331 let month = now[..7].to_owned();
332 let this_month = from.as_str() <= now.as_str() && until.as_str() >= &format!("{month}-01")[..];
333
334 let cells = async {
335 self.db
336 .prepare(format!(
337 "SELECT substr(created_at, 1, 10) AS day, {meter} AS meter, COALESCE(repo, '') AS project,
338 SUM({measure}) AS price, SUM(COALESCE(quantity, 0)) AS quantity, COUNT(*) AS entries,
339 SUM(COALESCE(discount_micros, 0)) AS discount,
340 SUM(COALESCE(credit_micros, 0) + COALESCE(trial_micros, 0) + COALESCE(oss_micros, 0) + COALESCE(given_micros, 0)) AS covered,
341 SUM(COALESCE(cost_micros, 0)) AS cost
342 FROM ledger WHERE workspace = ?1 AND kind = 'usage' AND created_at >= ?2 AND created_at < ?3
343 GROUP BY 1, 2, 3 LIMIT 20000",
344 meter = METER_KEY_SQL
345 ))
346 .bind(&[workspace.as_str().into(), from.as_str().into(), end.as_str().into()])?
347 .all()
348 .await?
349 .results::<Cell>()
350 };
351 let tokens = async {
352 self.db
353 .prepare(
354 "SELECT t.day AS day, COALESCE(r.repo, '') AS project,
355 SUM(t.input + t.output + t.cache_read + t.cache_write) AS tokens
356 FROM token_usage t LEFT JOIN runs r ON r.session_id = t.session
357 WHERE t.workspace = ?1 AND t.day >= ?2 AND t.day <= ?3
358 GROUP BY 1, 2 LIMIT 20000",
359 )
360 .bind(&[workspace.as_str().into(), from.as_str().into(), until.as_str().into()])?
361 .all()
362 .await?
363 .results::<TokenCell>()
364 };
365 #[derive(Deserialize)]
366 struct Feature {
367 task: Option<String>,
368 price: Option<f64>,
369 runs: Option<f64>,
370 }
371 let features = async {
372 self.db
373 .prepare(format!(
374 "SELECT COALESCE(task, 'implement') AS task, SUM({measure}) AS price, SUM(CASE WHEN {RUN_SQL} THEN 1 ELSE 0 END) AS runs
375 FROM ledger WHERE workspace = ?1 AND kind = 'usage' AND created_at >= ?2 AND created_at < ?3
376 AND ({meter}) IN ('agent_models', 'agent_rate', 'agent_rate_own', 'agent_sandbox')
377 GROUP BY 1",
378 meter = METER_KEY_SQL
379 ))
380 .bind(&[workspace.as_str().into(), from.as_str().into(), end.as_str().into()])?
381 .all()
382 .await?
383 .results::<Feature>()
384 };
385 let pending = async {
386 if !this_month {
387 return Ok(vec![]);
388 }
389 self.pending_this_month(&[workspace.as_str().into()], &month).await
390 };
391 // The agent product by the agent that did the work and by who
392 // asked, from the same lines: only when the agent product is shown.
393 let agents_wanted = a.products.is_empty() || a.products.iter().any(|p| p == "agent");
394 let attributed = async {
395 if !agents_wanted {
396 return Ok(vec![]);
397 }
398 let mut binds: Vec<worker::wasm_bindgen::JsValue> = vec![workspace.as_str().into(), from.as_str().into(), end.as_str().into()];
399 let projects = if a.projects.is_empty() {
400 String::new()
401 } else {
402 binds.extend(a.projects.iter().map(|p| worker::wasm_bindgen::JsValue::from(p.as_str())));
403 format!(" AND COALESCE(repo, '') IN ({})", vec!["?"; a.projects.len()].join(", "))
404 };
405 self.db
406 .prepare(format!(
407 "SELECT COALESCE(agent, '') AS agent, COALESCE(asked_by, '') AS person, SUM({measure}) AS price, COUNT(*) AS entries
408 FROM ledger WHERE workspace = ?1 AND kind = 'usage' AND created_at >= ?2 AND created_at < ?3
409 AND ({meter}) IN {AGENT_METERS_SQL}{projects}
410 GROUP BY 1, 2 LIMIT 5000",
411 meter = METER_KEY_SQL
412 ))
413 .bind(&binds)?
414 .all()
415 .await?
416 .results::<Attributed>()
417 };
418 let credits = self.credit_paid_between(&workspace, &from, &end);
419 let ((cells, tokens, features, pending, (credits_paid, credits)), (attributed, account)) =
420 try_join(try_join5(cells, tokens, features, pending, credits), try_join(attributed, self.account_of(&workspace))).await?;
421 let percent = account.terms.percent_off();
422 // What is metered so far is entered on the account's terms when the
423 // month closes: its discount, and what g1t covers, come off it now.
424 let pending: Vec<Pending> = pending
425 .into_iter()
426 .map(|p| {
427 let split = pending_split(p.cost(), p.charge(), self.margin_percent, percent);
428 Pending { meter: meter_of_pending(&p.source), micros: split.price, covered: split.covered, discount: split.discount }
429 })
430 .filter(|p| p.micros > 0)
431 .collect();
432 let (mut products, days_out, mut totals, all_projects) = shape(&days, &cells, &tokens, &pending, &a.products, &a.projects);
433 let (by_agent, by_person) = attribution(&attributed);
434 let filtered = !a.products.is_empty() || !a.projects.is_empty();
435 // Credit is the workspace's, not a product's or a project's: only
436 // without a filter is it taken off.
437 totals.credits_micros = if filtered { 0 } else { credits_paid.max(0) };
438 totals.charged_micros = charged(&totals);
439 if let Some(agent) = products.iter_mut().find(|p| p.key == "agent") {
440 let mut grouped: BTreeMap<&str, FeatureUsage> = BTreeMap::new();
441 for f in &features {
442 let (key, label) = feature_of(f.task.as_deref().unwrap_or("implement"));
443 let entry = grouped.entry(key).or_insert_with(|| FeatureUsage { key: key.to_owned(), label: label.to_owned(), micros: 0, count: 0 });
444 entry.micros += f.price.unwrap_or(0.0).round() as i64;
445 entry.count += f.runs.unwrap_or(0.0) as u32;
446 }
447 agent.features = grouped.into_values().filter(|f| f.micros != 0 || f.count > 0).collect();
448 agent.features.sort_by_key(|f| std::cmp::Reverse(f.micros));
449 }
450 let plan = self.plan_kind_for(&workspace, &account).await?;
451 // Allowances, for the range that includes this month.
452 let mut included = None;
453 let mut trial = None;
454 if this_month {
455 let (used, stored, operations) = futures_util::future::try_join3(
456 async {
457 if plan == PlanKind::Paid { self.allowance_used("plan_credit", &workspace, &month).await } else { Ok(0) }
458 },
459 self.private_storage(&workspace),
460 self.git_operations_this_month(&workspace),
461 )
462 .await?;
463 if plan == PlanKind::Paid {
464 included = Some(Allowance { used: used as f64, of: self.plans.plan_included_micros as f64, unit: "micros".to_owned() });
465 }
466 for product in &mut products {
467 for meter in &mut product.meters {
468 match meter.key.as_str() {
469 "storage" => {
470 meter.quantity = stored as f64;
471 meter.allowance = Some(Allowance { used: stored as f64, of: self.plans.free_storage_bytes as f64, unit: "bytes".to_owned() });
472 }
473 "git" => {
474 meter.quantity = operations as f64;
475 meter.allowance = Some(Allowance { used: operations as f64, of: self.plans.git_included as f64, unit: "operations".to_owned() });
476 }
477 _ => {}
478 }
479 }
480 }
481 if plan == PlanKind::Free
482 && let Some(grant) = self.grant_of(&workspace).await?
483 {
484 trial = Some(crate::credits::left(grant.granted_micros, grant.used_micros));
485 }
486 }
487 // The agent rate's tokens are weighted by kind: say how.
488 let weights = self.token_weights().await?;
489 for product in &mut products {
490 for meter in &mut product.meters {
491 if meter.key == "agent_rate" || meter.key == "agent_rate_own" {
492 meter.note = Some(agent_rate_note(&weights));
493 }
494 }
495 }
496 let ai: i64 = credits.grants.iter().filter(|g| g.scope == "models").map(|g| g.left_micros).sum();
497 let models = self.tokens_by_model(&workspace, &from, &until).await?;
498 Ok(Outcome::Ok(UsageReport {
499 from,
500 until,
501 totals,
502 days: days_out,
503 products,
504 projects: all_projects,
505 models,
506 by_agent,
507 by_person,
508 included,
509 discount_percent: (percent > 0).then_some(percent),
510 ai_credit_micros: ai,
511 credit_micros: credits.left_micros - ai,
512 trial_micros: trial,
513 plan,
514 free: self.free,
515 }))
516 }
517}
518
519/// What the agent rate's meters say of their tokens.
520pub(crate) fn agent_rate_note(weights: &crate::ai::TokenWeights) -> String {
521 if weights.is_flat() {
522 "Weighted tokens: every token counts once (input ×1, output ×1, cache reads ×1, cache writes ×1)".to_owned()
523 } else {
524 format!("Weighted tokens: {}", weights.describe())
525 }
526}
527
528impl Billing {
529 /// Agent tokens by model over the days `from` to `until`, most first:
530 /// what the model proxy counted, on g1t's models and the workspace's
531 /// own provider alike.
532 async fn tokens_by_model(&self, workspace: &str, from: &str, until: &str) -> Result<Vec<ModelTokens>> {
533 #[derive(Deserialize)]
534 struct Row {
535 model: String,
536 input: Option<f64>,
537 output: Option<f64>,
538 cache_read: Option<f64>,
539 cache_write: Option<f64>,
540 }
541 let rows = self
542 .db
543 .prepare(
544 "SELECT model, SUM(input) AS input, SUM(output) AS output, SUM(cache_read) AS cache_read, SUM(cache_write) AS cache_write
545 FROM token_usage WHERE workspace = ?1 AND day >= ?2 AND day <= ?3
546 GROUP BY model ORDER BY SUM(input + output + cache_read + cache_write) DESC LIMIT 20",
547 )
548 .bind(&[workspace.into(), from.into(), until.into()])?
549 .all()
550 .await?
551 .results::<Row>()?;
552 let n = |v: Option<f64>| v.unwrap_or(0.0).max(0.0) as u64;
553 Ok(rows
554 .into_iter()
555 .map(|r| ModelTokens { model: r.model, input: n(r.input), output: n(r.output), cache_read: n(r.cache_read), cache_write: n(r.cache_write) })
556 .collect())
557 }
558}
559
560#[cfg(test)]
561mod tests {
562 use super::*;
563
564 fn cell(day: &str, meter: &str, project: &str, price: f64) -> Cell {
565 Cell { day: day.into(), meter: meter.into(), project: project.into(), price: Some(price), entries: Some(1.0), ..Cell::default() }
566 }
567
568 #[test]
569 fn a_range_is_every_day_in_it() {
570 let days = days_between("2026-10-01", "2026-10-03").unwrap();
571 assert_eq!(days, ["2026-10-01", "2026-10-02", "2026-10-03"]);
572 assert_eq!(days_between("2026-10-01", "2026-10-01").unwrap().len(), 1);
573 assert!(days_between("2026-10-03", "2026-10-01").is_err());
574 assert!(days_between("2025-01-01", "2026-10-01").is_err());
575 assert!(days_between("Oct 1", "2026-10-01").is_err());
576 }
577
578 #[test]
579 fn usage_is_shaped_into_products_meters_days_and_projects() {
580 let days = days_between("2026-10-01", "2026-10-03").unwrap();
581 let mut sandbox = cell("2026-10-02", "sandbox", "acme/web", 120_000.0);
582 sandbox.quantity = Some(600.0);
583 let cells = vec![
584 cell("2026-10-01", "agent_models", "acme/web", 2_000_000.0),
585 cell("2026-10-01", "agent_rate", "acme/web", 500_000.0),
586 cell("2026-10-03", "agent_models", "acme/api", 1_000_000.0),
587 sandbox,
588 Cell { covered: Some(1_000_000.0), discount: Some(0.0), ..cell("2026-10-03", "storage", "", 30_000.0) },
589 ];
590 let tokens = vec![TokenCell { day: "2026-10-01".into(), project: "acme/web".into(), tokens: Some(2_000_000.0) }];
591 let pending = vec![Pending { meter: "git", micros: 40_000, ..Pending::default() }];
592 let (products, by_day, totals, projects) = shape(&days, &cells, &tokens, &pending, &[], &[]);
593 // Every product family, in order.
594 assert_eq!(products.iter().map(|p| p.key.as_str()).collect::<Vec<_>>(), ["agent", "sandboxes", "gateway", "deployments", "git_storage", "packages", "security", "search"]);
595 let agent = &products[0];
596 assert_eq!(agent.micros, 3_500_000);
597 let models = agent.meters.iter().find(|m| m.key == "agent_models").unwrap();
598 assert_eq!(models.daily, [2_000_000, 0, 1_000_000]);
599 assert_eq!(models.quantity, 2_000_000.0);
600 assert_eq!(models.by_project[0].project, "acme/web");
601 let sandbox = products[1].meters.iter().find(|m| m.key == "sandbox").unwrap();
602 assert_eq!((sandbox.micros, sandbox.quantity), (120_000, 600.0));
603 // Pending usage counts in the totals and its meter, not on a day.
604 let git = products[4].meters.iter().find(|m| m.key == "git").unwrap();
605 assert_eq!((git.micros, git.pending_micros), (40_000, 40_000));
606 assert_eq!(totals.price_micros, 3_500_000 + 120_000 + 30_000 + 40_000);
607 assert_eq!(totals.pending_micros, 40_000);
608 assert_eq!(totals.included_micros, 1_000_000);
609 assert_eq!(by_day.iter().filter(|d| d.product == "agent").map(|d| d.micros).sum::<i64>(), 3_500_000);
610 assert_eq!(projects, ["acme/api", "acme/web"]);
611 }
612
613 #[test]
614 fn filters_keep_only_the_products_and_projects_asked_for() {
615 let days = days_between("2026-10-01", "2026-10-01").unwrap();
616 let cells = vec![cell("2026-10-01", "agent_models", "acme/web", 100.0), cell("2026-10-01", "agent_models", "acme/api", 50.0), cell("2026-10-01", "sandbox", "acme/web", 10.0)];
617 let pending = vec![Pending { meter: "git", micros: 7, ..Pending::default() }];
618 let (products, _, totals, projects) = shape(&days, &cells, &[], &pending, &["agent".into()], &["acme/web".into()]);
619 assert_eq!(products.len(), 1);
620 assert_eq!(totals.price_micros, 100);
621 // Every project stays listed, for the filter itself.
622 assert_eq!(projects, ["acme/api", "acme/web"]);
623 }
624
625 #[test]
626 fn pending_usage_counts_at_price_with_what_is_covered_and_discounted() {
627 let days = days_between("2026-10-01", "2026-10-02").unwrap();
628 // Storage on a 100% discount, and scans g1t covers on a free workspace.
629 let pending = [
630 Pending { meter: "storage", micros: 9_000, covered: 0, discount: 9_000 },
631 Pending { meter: "security", micros: 2_400, covered: 2_400, discount: 0 },
632 ];
633 let (_, _, totals, _) = shape(&days, &[], &[], &pending, &[], &[]);
634 assert_eq!(totals.price_micros, 11_400);
635 assert_eq!(totals.pending_micros, 11_400);
636 assert_eq!(totals.included_micros, 2_400);
637 assert_eq!(totals.discount_micros, 9_000);
638 assert_eq!(charged(&totals), 0);
639 // With a project chosen, pending usage (no project's) is left out.
640 let (_, _, filtered, _) = shape(&days, &[], &[], &pending, &[], &["acme/api".to_owned()]);
641 assert_eq!(filtered.pending_micros, 0);
642 }
643
644 fn attributed(agent: &str, person: &str, price: f64, entries: f64) -> Attributed {
645 Attributed { agent: agent.into(), person: person.into(), price: Some(price), entries: Some(entries) }
646 }
647
648 #[test]
649 fn the_agent_product_is_attributed_to_agents_and_to_who_asked() {
650 let rows = [
651 attributed("g1t", "chase", 2_000_000.0, 3.0),
652 attributed("mike", "chase", 20_000.0, 2.0),
653 attributed("mike", "", 5_000.0, 1.0),
654 attributed("", "", 500_000.0, 4.0),
655 ];
656 let (agents, people) = attribution(&rows);
657 assert_eq!(agents.iter().map(|s| (s.key.as_str(), s.micros, s.count)).collect::<Vec<_>>(), [("g1t", 2_000_000, 3), ("", 500_000, 4), ("mike", 25_000, 3)]);
658 assert_eq!(agents[0].label, "@g1t");
659 assert_eq!(agents[1].label, "Not attributed to an agent");
660 assert_eq!(people.iter().map(|s| (s.key.as_str(), s.micros)).collect::<Vec<_>>(), [("chase", 2_020_000), ("", 505_000)]);
661 assert_eq!(people[1].label, "No one asked");
662 // Both views are the same money.
663 let total: i64 = rows.iter().map(|r| r.price.unwrap() as i64).sum();
664 assert_eq!(agents.iter().map(|s| s.micros).sum::<i64>(), total);
665 assert_eq!(people.iter().map(|s| s.micros).sum::<i64>(), total);
666 }
667
668 /// The figures every page shows come from one ledger: Home's and
669 /// Spend's "Spent" are the products' sum at price, the top bar's and
670 /// Spend's "Agents" are the agent product, agents by handle sum to it,
671 /// and a full discount is charged nothing.
672 #[test]
673 fn home_spend_and_the_top_bar_reconcile_on_the_ledger() {
674 let days = days_between("2026-10-01", "2026-10-10").unwrap();
675 // The same lines, grouped two ways: by day, meter and project for
676 // the products; by agent and asker for the attribution.
677 let lines: [(&str, &str, &str, &str, &str, f64); 7] = [
678 ("2026-10-02", "agent_models", "flagon-io/g1t", "g1t", "chase", 1_900_000.0),
679 ("2026-10-02", "agent_rate", "flagon-io/g1t", "g1t", "chase", 400_000.0),
680 ("2026-10-03", "agent_sandbox", "flagon-io/g1t", "g1t", "chase", 220_000.0),
681 ("2026-10-04", "agent_models", "flagon-io/@mike", "mike", "chase", 20_000.0),
682 ("2026-10-05", "agent_models", "flagon-io/@margo", "margo", "", 4_000.0),
683 ("2026-10-03", "sandbox", "flagon-io/g1t", "", "", 3_360_000.0),
684 ("2026-10-06", "requests", "flagon-io/site", "", "", 4_480_000.0),
685 ];
686 let cells: Vec<Cell> = lines
687 .iter()
688 .map(|(day, meter, project, _, _, price)| Cell { discount: Some(*price), ..cell(day, meter, project, *price) })
689 .collect();
690 let rows: Vec<Attributed> = lines
691 .iter()
692 .filter(|(_, meter, ..)| meter.starts_with("agent"))
693 .map(|(_, _, _, agent, person, price)| attributed(agent, person, *price, 1.0))
694 .collect();
695 let (products, by_day, totals, _) = shape(&days, &cells, &[], &[], &[], &[]);
696 let (agents, people) = attribution(&rows);
697 // Spent, on Home and Spend: every product at price.
698 let spent: i64 = lines.iter().map(|l| l.5 as i64).sum();
699 assert_eq!(totals.price_micros, spent);
700 assert_eq!(products.iter().map(|p| p.micros).sum::<i64>(), spent);
701 assert_eq!(by_day.iter().map(|d| d.micros).sum::<i64>(), spent);
702 // Agents, in the top bar and on Spend: the agent product, which is
703 // what the agents by handle add up to, g1t's own runs included.
704 let agent_product = products.iter().find(|p| p.key == "agent").unwrap().micros;
705 assert_eq!(agent_product, 1_900_000 + 400_000 + 220_000 + 20_000 + 4_000);
706 assert_eq!(agents.iter().map(|s| s.micros).sum::<i64>(), agent_product);
707 assert_eq!(people.iter().map(|s| s.micros).sum::<i64>(), agent_product);
708 assert!(agents.iter().all(|s| s.micros <= agent_product));
709 assert_eq!(agents[0].key, "g1t");
710 // Charged, on Billing, Spend and in the top bar: nothing, on a 100% discount.
711 assert_eq!(totals.discount_micros, spent);
712 assert_eq!(charged(&totals), 0);
713 }
714
715 #[test]
716 fn the_agent_is_counted_by_what_it_was_doing() {
717 assert_eq!(feature_of("review").0, "reviews");
718 assert_eq!(feature_of("plan").0, "plans");
719 assert_eq!(feature_of("implement").0, "runs");
720 assert_eq!(meter_of_pending("package_storage"), "packages");
721 assert_eq!(meter_of_pending("nonsense"), "other");
722 }
723}