Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

2,872 linesCodeBlameRaw
1//! What g1t earns on each thing it sells, measured against what
2//! Cloudflare actually charged for it.
3//!
4//! Once a day, after `costs` has read Cloudflare's bill, the reconciler
5//! puts three figures side by side for every day and each of g1t's
6//! products (a "bucket": sandboxes, deployments, git, repository storage,
7//! …):
8//!
9//! 1. **What Cloudflare charged**: the day's cost lines `cost_map` gives
10//! the bucket.
11//! 2. **What g1t's meters recorded**: the cost on the ledger's entries for
12//! it (the price book's cost at the time) and, where a mapping names
13//! one, g1t's own count of the same units (git operations).
14//! 3. **What customers were charged**: the entries' value at price, before
15//! the plan's included usage, a trial or a pool paid part of it; and of
16//! that, what workspaces paid. Month-end meters (git, storage, scans,
17//! embeddings, the actions cache) come from daily snapshots of what they
18//! had come to (`pending_days`). The plan's price is the `platform`
19//! bucket's: the plan pays for running g1t.
20//!
21//! From those: margin per product (value against cost) and for all of g1t
22//! (money in against every cost); drift (counts or costs that disagree past
23//! a mapping's threshold, and leaks: cost with no revenue, or a Cloudflare
24//! meter no one mapped); each workspace's cost, Cloudflare's figure shared
25//! out by each workspace's own meters; and price proposals when a unit's
26//! real cost has moved (`pricing`). Alerts go to staff by email and as a
27//! banner in sudo.
28
29use std::collections::{BTreeMap, BTreeSet};
30
31use g1t_contracts::billing::*;
32use g1t_contracts::{FailureCode, Outcome, new_id};
33use g1t_contracts::time::rfc3339;
34use g1t_kit::now_ms;
35use serde::{Deserialize, Serialize};
36use worker::wasm_bindgen::JsValue;
37use worker::{Env, Result};
38
39use crate::Billing;
40use crate::costs::{self, ARTIFACTS_OPERATIONS, DAY_MS, Rule, SOURCE_ARTIFACTS, SOURCE_BILLABLE, UNMAPPED};
41
42/// Buckets that are the cost of running g1t, paid by the plan rather than
43/// sold by the unit: never a leak for having no revenue of their own.
44pub(crate) const OVERHEAD: [&str; 1] = ["platform"];
45/// Buckets Cloudflare does not bill: their cost is g1t's own figure.
46pub(crate) const NOT_CLOUDFLARE: [&str; 1] = ["models"];
47/// The key an agent's planning run is reconciled under. Its ledger task
48/// is `plan`, which is also the plan's payments' key (`revenue_map`: the
49/// platform bucket), so read as `plan` its model cost went to running g1t,
50/// where Cloudflare's bill is the cost, and was lost. No `revenue_map`
51/// row: models, like every agent run.
52pub(crate) const PLANNING_KEY: &str = "planning";
53/// The days drift is judged over.
54const DRIFT_DAYS: u64 = 7;
55/// The days a workspace's cost is set against its revenue.
56const ANOMALY_DAYS: u64 = 30;
57/// The days a unit's cost is measured over.
58const MEASURE_DAYS: u64 = 30;
59/// Fewer of g1t's units than this say nothing about cost per unit.
60const MIN_UNITS: f64 = 1_000.0;
61/// An open alert is emailed again after this long.
62const REMIND_MS: u64 = 7 * DAY_MS;
63
64// ---------------------------------------------------------------------
65// The arithmetic, apart from the database so it can be tested.
66// ---------------------------------------------------------------------
67
68/// One of g1t's products on one day.
69#[derive(Clone, Debug, Default, PartialEq)]
70pub(crate) struct ProductDay {
71 pub day: String,
72 pub bucket: String,
73 /// What Cloudflare charged g1t, in millionths of a dollar.
74 pub cf_cost_micros: i64,
75 /// What g1t's meters recorded it cost (the price book's cost).
76 pub own_cost_micros: i64,
77 /// What customers were charged for it at price, before what paid.
78 pub value_micros: i64,
79 /// Of that, what workspaces paid themselves.
80 pub cash_micros: i64,
81 /// Units Cloudflare counted and units g1t counted, where a mapping
82 /// says they are the same units.
83 pub cf_quantity: f64,
84 pub own_quantity: f64,
85 /// Of `cost()`, what went on usage g1t gave away (the workspaces'
86 /// `WorkspaceDay::given`, added up).
87 pub given: Given,
88}
89
90impl ProductDay {
91 /// What it cost: Cloudflare's figure where Cloudflare bills it, else
92 /// g1t's own (models are billed by their providers, through the gateway).
93 pub fn cost(&self) -> i64 {
94 if NOT_CLOUDFLARE.contains(&self.bucket.as_str()) { self.own_cost_micros } else { self.cf_cost_micros }
95 }
96}
97
98/// A line of Cloudflare's bill, as stored.
99#[derive(Clone, Debug, Deserialize)]
100pub(crate) struct LineRow {
101 pub day: String,
102 pub source: String,
103 pub product: String,
104 pub meter: String,
105 pub quantity: f64,
106 pub cost_usd: f64,
107}
108
109/// A count of g1t's own, as stored.
110#[derive(Clone, Debug, Deserialize)]
111pub(crate) struct OwnRow {
112 pub day: String,
113 pub meter: String,
114 pub workspace: String,
115 pub quantity: f64,
116}
117
118/// What g1t gave away, by why: its own comped workspaces, free use (a
119/// free period, free allowances, overruns g1t covered), the trial, and the
120/// open-source pool, and discounts on an account's terms (what they took
121/// below cost plus the margin, `ledger.discount_micros`), and credits g1t
122/// staff gave, promotional and goodwill, when spent (`grants`), and usage a
123/// testing reset wiped (`reset_costs`): g1t paid for it and nobody will;
124/// and what was charged while payments were not live (Stripe's test mode),
125/// which brought in no real money (`without_real_money`).
126/// The Team plan's included usage is paid for by the plan's price, so it is
127/// sold, not given; so is what a refund pays for.
128#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
129pub(crate) struct Given {
130 pub comped: i64,
131 pub free: i64,
132 pub trial: i64,
133 pub pool: i64,
134 pub discount: i64,
135 pub credit_promotional: i64,
136 pub credit_goodwill: i64,
137 pub reset: i64,
138 pub unpaid: i64,
139}
140
141impl Given {
142 pub fn total(&self) -> i64 {
143 self.comped + self.free + self.trial + self.pool + self.discount + self.credit() + self.reset + self.unpaid
144 }
145
146 /// Credits from g1t, both kinds.
147 pub fn credit(&self) -> i64 {
148 self.credit_promotional + self.credit_goodwill
149 }
150
151 fn add(&mut self, other: &Given) {
152 self.comped += other.comped;
153 self.free += other.free;
154 self.trial += other.trial;
155 self.pool += other.pool;
156 self.discount += other.discount;
157 self.credit_promotional += other.credit_promotional;
158 self.credit_goodwill += other.credit_goodwill;
159 self.reset += other.reset;
160 self.unpaid += other.unpaid;
161 }
162
163 /// The same shares of `cost` as these are of `value`, at most all of it.
164 fn of(&self, cost: i64, value: i64) -> Given {
165 let total = self.total();
166 if value <= 0 || cost <= 0 || total <= 0 {
167 return Given::default();
168 }
169 let given = cost as i128 * total.min(value) as i128 / value as i128;
170 let part = |x: i64| (given * x.max(0) as i128 / total as i128) as i64;
171 Given {
172 comped: part(self.comped),
173 free: part(self.free),
174 trial: part(self.trial),
175 pool: part(self.pool),
176 discount: part(self.discount),
177 credit_promotional: part(self.credit_promotional),
178 credit_goodwill: part(self.credit_goodwill),
179 reset: part(self.reset),
180 unpaid: part(self.unpaid),
181 }
182 }
183}
184
185/// Credits from g1t in the reconciliation (`grants`): usage paid for with
186/// promotional or goodwill credit is given, not money in; a refund comes
187/// off money in on the day it refunds, shared over that day's paid usage.
188/// What credit paid for that is not among `rows` (month-end meters, or
189/// what was owed from before) is a row of its own on its day.
190pub(crate) fn apply_credits(rows: &mut Vec<UsageRow>, draws: &[(String, crate::grants::Draw)], refunds: &[crate::grants::Refunded]) {
191 let mut paid: BTreeMap<(String, String, String), Given> = BTreeMap::new();
192 for (workspace, draw) in draws {
193 let given = paid
194 .entry((draw.at[..10].to_owned(), workspace.clone(), crate::grants::usage_key(draw.task.as_deref(), &draw.reference)))
195 .or_default();
196 match draw.kind {
197 CreditKind::Promotional => given.credit_promotional += draw.micros,
198 CreditKind::Goodwill => given.credit_goodwill += draw.micros,
199 // Money already paid: what it pays for is paid for.
200 CreditKind::Refund | CreditKind::Purchased => {}
201 }
202 }
203 for ((day, workspace, key), given) in paid {
204 if given.credit() == 0 {
205 continue;
206 }
207 match rows.iter_mut().find(|r| r.day == day && r.workspace == workspace && r.key == key) {
208 Some(row) => {
209 row.cash -= given.credit();
210 row.given.add(&given);
211 }
212 None => rows.push(UsageRow { day, workspace, key, bucket: None, value: 0, cash: -given.credit(), cost: 0, given }),
213 }
214 }
215 for refund in refunds {
216 let weights: Vec<(String, f64)> = rows
217 .iter()
218 .enumerate()
219 .filter(|(_, r)| r.day == refund.day && r.workspace == refund.workspace && r.cash > 0)
220 .map(|(i, r)| (format!("{i:08}"), r.cash as f64))
221 .collect();
222 let shares = attribute(refund.micros, &weights);
223 if shares.is_empty() {
224 rows.push(UsageRow {
225 day: refund.day.clone(),
226 workspace: refund.workspace.clone(),
227 key: "other".into(),
228 cash: -refund.micros,
229 ..UsageRow::default()
230 });
231 }
232 for (index, micros) in shares {
233 if let Ok(i) = index.parse::<usize>() {
234 rows[i].cash -= micros;
235 }
236 }
237 }
238}
239
240/// Money in only where it is real. Charges made while payments were not
241/// live (Stripe's test mode) brought in nothing: a row's cash from before
242/// `live_since` (all of it while payments are not live, `None`) is taken
243/// out of cash and counted as given away (`unpaid`), so it is never money
244/// in, never margin, and never what a workspace paid.
245pub(crate) fn without_real_money(rows: &mut [UsageRow], live_since: Option<&str>) {
246 for row in rows {
247 if live_since.is_some_and(|since| row.day.as_str() >= since) || row.cash == 0 {
248 continue;
249 }
250 // What else gave it away already (a 100% discount) stays that.
251 row.given.unpaid += row.cash.min(row.value - row.given.total()).max(0);
252 row.cash = 0;
253 }
254}
255
256/// The day payments went live, from `cost_settings` (`payments_live_since`)
257/// as a value read there: None while they are not live, the day kept when
258/// they are, else `today` (the first time they are seen live).
259pub(crate) fn live_since(live: bool, kept: Option<&str>, today: &str) -> Option<String> {
260 if !live {
261 return None;
262 }
263 Some(kept.filter(|d| d.len() >= 10).map_or_else(|| today.to_owned(), |d| d[..10].to_owned()))
264}
265
266/// What a workspace was charged for one key on one day.
267#[derive(Clone, Debug, Default, PartialEq)]
268pub(crate) struct UsageRow {
269 pub day: String,
270 pub workspace: String,
271 /// A ledger task (or `builds`), a month-end source, or `plan`.
272 pub key: String,
273 /// The bucket, where it is known already (what a testing reset kept,
274 /// `reset_costs`); else `key`'s, from `revenue_map`.
275 pub bucket: Option<String>,
276 pub value: i64,
277 pub cash: i64,
278 pub cost: i64,
279 /// Of `value`, what g1t gave away: all of it for g1t's own (comped)
280 /// workspaces and in a free period, else what the trial and the pool
281 /// paid and the overruns g1t covered.
282 pub given: Given,
283}
284
285/// One workspace's share of a product's cost on one day.
286#[derive(Clone, Debug, PartialEq)]
287pub(crate) struct WorkspaceDay {
288 pub day: String,
289 pub workspace: String,
290 pub bucket: String,
291 pub cost: i64,
292 /// What the workspace paid in cash.
293 pub revenue: i64,
294 /// What its usage was priced at, whoever paid for it.
295 pub value: i64,
296 /// Of `cost`, the part g1t gave away: all of it for a comped workspace
297 /// or one with nothing priced that day (free use), else the cost times
298 /// the shares of its usage that day that g1t paid for.
299 pub given: Given,
300}
301
302fn micros(dollars: f64) -> i64 {
303 (dollars * 1_000_000.0).round() as i64
304}
305
306/// Puts the day's bill, g1t's counts and what customers were charged side
307/// by side, a row per day and bucket, and shares each bucket's cost out
308/// to workspaces.
309pub(crate) fn fold(
310 rules: &[Rule],
311 revenue_map: &BTreeMap<String, String>,
312 lines: &[LineRow],
313 own: &[OwnRow],
314 usage: &[UsageRow],
315 internal: &BTreeSet<String>,
316) -> (Vec<ProductDay>, Vec<WorkspaceDay>) {
317 let mut days: BTreeMap<(String, String), ProductDay> = BTreeMap::new();
318 let entry = |day: &str, bucket: &str| -> ProductDay {
319 ProductDay { day: day.to_owned(), bucket: bucket.to_owned(), ..ProductDay::default() }
320 };
321 // Which of g1t's own meters count each bucket's units.
322 let mut own_meters: BTreeMap<&str, BTreeSet<&str>> = BTreeMap::new();
323 for rule in rules {
324 if let Some(meter) = &rule.own_meter {
325 own_meters.entry(rule.bucket.as_str()).or_default().insert(meter.as_str());
326 }
327 }
328 let mut events: BTreeMap<(String, String), f64> = BTreeMap::new();
329 for line in lines {
330 let rule = costs::classify(rules, &line.product, &line.meter);
331 let bucket = rule.map_or(UNMAPPED, |r| r.bucket.as_str());
332 let key = (line.day.clone(), bucket.to_owned());
333 if line.source == SOURCE_ARTIFACTS {
334 // What Artifacts counted: operations only, and only where the
335 // bill does not count them itself.
336 if ARTIFACTS_OPERATIONS.contains(&line.meter.as_str()) {
337 *events.entry(key).or_default() += line.quantity;
338 }
339 continue;
340 }
341 let row = days.entry(key.clone()).or_insert_with(|| entry(&key.0, &key.1));
342 row.cf_cost_micros += micros(line.cost_usd);
343 if line.source == SOURCE_BILLABLE && rule.is_some_and(|r| r.own_meter.is_some()) {
344 row.cf_quantity += line.quantity;
345 }
346 }
347 for (key, quantity) in events {
348 let row = days.entry(key.clone()).or_insert_with(|| entry(&key.0, &key.1));
349 if row.cf_quantity == 0.0 {
350 row.cf_quantity = quantity;
351 }
352 }
353 // g1t's own counts of the same units, by bucket and by workspace.
354 let mut own_by: BTreeMap<(String, String), Vec<(String, f64)>> = BTreeMap::new();
355 // Cloudflare's own count by workspace, where it gives one
356 // (`cloudflare_<bucket>`): the best way to share its cost.
357 let mut cf_by: BTreeMap<(String, String), Vec<(String, f64)>> = BTreeMap::new();
358 for count in own {
359 if let Some(bucket) = count.meter.strip_prefix("cloudflare_") {
360 cf_by.entry((count.day.clone(), bucket.to_owned())).or_default().push((count.workspace.clone(), count.quantity));
361 continue;
362 }
363 for (bucket, meters) in &own_meters {
364 if meters.contains(count.meter.as_str()) {
365 let key = (count.day.clone(), (*bucket).to_owned());
366 days.entry(key.clone()).or_insert_with(|| entry(&key.0, &key.1)).own_quantity += count.quantity;
367 own_by.entry(key).or_default().push((count.workspace.clone(), count.quantity));
368 }
369 }
370 }
371 // What customers were charged.
372 let bucket_of = |key: &str| revenue_map.get(key).cloned().unwrap_or_else(|| "models".to_owned());
373 let mut value_by: BTreeMap<(String, String), Vec<(String, f64)>> = BTreeMap::new();
374 let mut cost_by: BTreeMap<(String, String), Vec<(String, f64)>> = BTreeMap::new();
375 let mut revenue: BTreeMap<(String, String, String), i64> = BTreeMap::new();
376 let mut valued: BTreeMap<(String, String, String), i64> = BTreeMap::new();
377 let mut active: BTreeMap<String, Vec<(String, f64)>> = BTreeMap::new();
378 let mut gave: BTreeMap<(String, String), (Given, i64)> = BTreeMap::new();
379 for u in usage {
380 let g = gave.entry((u.day.clone(), u.workspace.clone())).or_default();
381 g.0.add(&u.given);
382 g.1 += u.value;
383 let bucket = u.bucket.clone().unwrap_or_else(|| bucket_of(&u.key));
384 let key = (u.day.clone(), bucket.clone());
385 let row = days.entry(key.clone()).or_insert_with(|| entry(&key.0, &key.1));
386 row.own_cost_micros += u.cost;
387 row.value_micros += u.value;
388 row.cash_micros += u.cash;
389 value_by.entry(key.clone()).or_default().push((u.workspace.clone(), u.value as f64));
390 cost_by.entry(key).or_default().push((u.workspace.clone(), u.cost as f64));
391 *revenue.entry((u.day.clone(), u.workspace.clone(), bucket.clone())).or_default() += u.cash;
392 *valued.entry((u.day.clone(), u.workspace.clone(), bucket)).or_default() += u.value;
393 active.entry(u.day.clone()).or_default().push((u.workspace.clone(), u.value.max(u.cost) as f64));
394 }
395 // Each bucket's cost shared out: by Cloudflare's own count per
396 // workspace, else by g1t's own count of its units, else by what its
397 // usage cost (so free use carries its own cost), else by what it was
398 // charged; running g1t, and what no one mapped, by each workspace's
399 // share of all usage that day.
400 let mut shares: BTreeMap<(String, String, String), i64> = BTreeMap::new();
401 for ((day, bucket), row) in &days {
402 let key = (day.clone(), bucket.clone());
403 let weigh = |m: &BTreeMap<(String, String), Vec<(String, f64)>>| m.get(&key).filter(|w| w.iter().any(|(_, v)| *v > 0.0)).cloned();
404 let weights = if OVERHEAD.contains(&bucket.as_str()) || bucket == UNMAPPED {
405 active.get(day).cloned()
406 } else {
407 weigh(&cf_by).or_else(|| weigh(&own_by)).or_else(|| weigh(&cost_by)).or_else(|| weigh(&value_by)).or_else(|| active.get(day).cloned())
408 };
409 for (workspace, micros) in attribute(row.cost(), &weights.unwrap_or_default()) {
410 *shares.entry((day.clone(), workspace, bucket.clone())).or_default() += micros;
411 }
412 }
413 let keys: BTreeSet<(String, String, String)> = shares.keys().chain(revenue.keys()).cloned().collect();
414 let workspaces: Vec<WorkspaceDay> = keys
415 .into_iter()
416 .map(|(day, workspace, bucket)| {
417 let cost = shares.get(&(day.clone(), workspace.clone(), bucket.clone())).copied().unwrap_or(0);
418 // The day's shares given away apply to every bucket, so a
419 // comped workspace's part of running g1t is given too. A
420 // workspace with nothing priced that day used g1t for free.
421 let given = if internal.contains(&workspace) {
422 Given { comped: cost, ..Given::default() }
423 } else {
424 match gave.get(&(day.clone(), workspace.clone())) {
425 Some((given, value)) if *value > 0 => given.of(cost, *value),
426 _ => Given { free: cost.max(0), ..Given::default() },
427 }
428 };
429 WorkspaceDay {
430 cost,
431 revenue: revenue.get(&(day.clone(), workspace.clone(), bucket.clone())).copied().unwrap_or(0),
432 value: valued.get(&(day.clone(), workspace.clone(), bucket.clone())).copied().unwrap_or(0),
433 given,
434 day,
435 workspace,
436 bucket,
437 }
438 })
439 .collect();
440 for w in &workspaces {
441 if let Some(row) = days.get_mut(&(w.day.clone(), w.bucket.clone())) {
442 row.given.add(&w.given);
443 }
444 }
445 (days.into_values().collect(), workspaces)
446}
447
448/// A month-end source's day, from the snapshots of what it had come to:
449/// each day's figure less the day before's in the same month (the first
450/// day of a month, or the first snapshot, is its own).
451pub(crate) fn pending_deltas(snapshots: &[(String, String, String, i64, i64)]) -> Vec<UsageRow> {
452 // (day, workspace, source, cost, charge), any order.
453 let mut sorted = snapshots.to_vec();
454 sorted.sort_by(|a, b| (&a.1, &a.2, &a.0).cmp(&(&b.1, &b.2, &b.0)));
455 let mut out = Vec::new();
456 let mut previous: Option<&(String, String, String, i64, i64)> = None;
457 for snap in &sorted {
458 let (day, workspace, source, cost, charge) = snap;
459 let (before_cost, before_charge) = match previous {
460 Some(p) if p.1 == *workspace && p.2 == *source && p.0[..7] == day[..7] => (p.3, p.4),
461 _ => (0, 0),
462 };
463 let (cost, charge) = ((cost - before_cost).max(0), (charge - before_charge).max(0));
464 if cost > 0 || charge > 0 {
465 out.push(UsageRow { day: day.clone(), workspace: workspace.clone(), key: source.clone(), bucket: None, value: charge, cash: charge, cost, given: Given::default() });
466 }
467 previous = Some(snap);
468 }
469 out
470}
471
472/// A month-end meter's day on a 100%-discount workspace: all of it given
473/// (comped) and none of it money in. The snapshot holds what the month
474/// would charge before the discount, which the month's close takes off in
475/// full; counted as paid, flagon-io's cache, embeddings and scans read as
476/// money in ($0.0023 on 2026-10-08).
477pub(crate) fn comped_meter(mut u: UsageRow) -> UsageRow {
478 u.given = Given { comped: u.value, ..Given::default() };
479 u.cash = 0;
480 u
481}
482
483/// A complimentary workspace's month-end meter: its allowance (or g1t,
484/// past it) paid, so it is given away as promotional, never money in.
485pub(crate) fn gifted_meter(mut u: UsageRow) -> UsageRow {
486 u.given = Given { credit_promotional: u.value, ..Given::default() };
487 u.cash = 0;
488 u
489}
490
491/**
492 * All of g1t over some rolled-up days: money in, every cost, what was
493 * given by why, and the margins on everything and on what was sold. The
494 * Costs page and the Overview both add up the days with this, so they
495 * cannot disagree.
496 */
497pub(crate) fn overall_of(days: &[ProductDay]) -> OverallMargin {
498 let mut overall = OverallMargin::default();
499 for d in days {
500 overall.cost_micros += d.cost();
501 overall.given_micros += d.given.total();
502 if NOT_CLOUDFLARE.contains(&d.bucket.as_str()) {
503 overall.models_cost_micros += d.cost();
504 } else {
505 overall.cloudflare_cost_micros += d.cost();
506 }
507 overall.given_comped_micros += d.given.comped;
508 overall.given_free_micros += d.given.free;
509 overall.given_trial_micros += d.given.trial;
510 overall.given_pool_micros += d.given.pool;
511 overall.given_discount_micros += d.given.discount;
512 overall.given_credit_promotional_micros += d.given.credit_promotional;
513 overall.given_credit_goodwill_micros += d.given.credit_goodwill;
514 overall.given_reset_micros += d.given.reset;
515 overall.given_unpaid_micros += d.given.unpaid;
516 let sold = (d.cost() - d.given.total()).max(0);
517 if OVERHEAD.contains(&d.bucket.as_str()) {
518 overall.plans_micros += d.cash_micros;
519 overall.running_cost_micros += sold;
520 } else if d.bucket == UNMAPPED {
521 overall.usage_micros += d.cash_micros;
522 overall.unmapped_cost_micros += sold;
523 } else {
524 overall.usage_micros += d.cash_micros;
525 overall.usage_cost_micros += sold;
526 }
527 }
528 let revenue = overall.usage_micros + overall.plans_micros;
529 overall.margin_micros = revenue - overall.cost_micros;
530 overall.margin_percent = margin_percent(revenue, overall.cost_micros);
531 let sold = (overall.cost_micros - overall.given_micros).max(0);
532 overall.sold_margin_micros = revenue - sold;
533 overall.sold_margin_percent = margin_percent(revenue, sold);
534 overall
535}
536
537/// Margin as a share of what was charged, in percent; None when nothing was.
538pub(crate) fn margin_percent(revenue_micros: i64, cost_micros: i64) -> Option<f64> {
539 (revenue_micros > 0).then(|| (revenue_micros - cost_micros) as f64 * 100.0 / revenue_micros as f64)
540}
541
542/// How far `ours` is from `theirs`, in percent of theirs; None when theirs
543/// is nothing.
544pub(crate) fn delta_percent(ours: f64, theirs: f64) -> Option<f64> {
545 (theirs > 0.0).then(|| (ours - theirs) * 100.0 / theirs)
546}
547
548#[derive(Clone, Copy, Debug, PartialEq, Eq)]
549pub(crate) enum DriftKind {
550 /// g1t counted a different number of units than Cloudflare did.
551 Count,
552 /// The price book's cost of a bucket's usage differs from what the
553 /// same usage comes to at Cloudflare's list prices, before the included
554 /// amounts (a price may be stale).
555 Cost,
556 /// Cloudflare charged for something nothing charges customers for.
557 Leak,
558}
559
560impl DriftKind {
561 pub fn as_str(self) -> &'static str {
562 match self {
563 DriftKind::Count => "count",
564 DriftKind::Cost => "cost",
565 DriftKind::Leak => "leak",
566 }
567 }
568}
569
570#[derive(Clone, Debug, PartialEq)]
571pub(crate) struct Drift {
572 pub bucket: String,
573 pub kind: DriftKind,
574 pub ours: f64,
575 pub cloudflare: f64,
576 pub delta_percent: Option<f64>,
577}
578
579/// Drift over a window for one bucket: counts more than `threshold`
580/// percent apart, the price book's cost of the usage that far from what the
581/// same usage comes to at Cloudflare's list prices (`list_micros`, from
582/// `list_costs`), and cost with nothing charged for it. Under
583/// `min_cost_micros` in all, cost says nothing.
584///
585/// The price book is set against the list cost, never against what
586/// Cloudflare billed: the bill is net of the included amounts and the price
587/// book's cost is of every unit, so a bucket whose usage mostly fits in
588/// them would read as a stale price when nothing changed. Without a list
589/// cost (a meter in the bucket with no list price) the price book is not
590/// checked. A leak is still what Cloudflare billed: money spent.
591pub(crate) fn drifts(bucket: &str, days: &[ProductDay], threshold: f64, counted: bool, min_cost_micros: i64, list_micros: Option<f64>) -> Vec<Drift> {
592 // Models: g1t no longer runs agents or sells model access. What their
593 // usage cost before is the ledger's own figure, with nothing to set it
594 // against.
595 if NOT_CLOUDFLARE.contains(&bucket) {
596 return Vec::new();
597 }
598 let overhead = OVERHEAD.contains(&bucket);
599 let sum = |f: &dyn Fn(&ProductDay) -> f64| days.iter().map(f).sum::<f64>();
600 let cf_cost = sum(&|d| d.cf_cost_micros as f64);
601 let own_cost = sum(&|d| d.own_cost_micros as f64);
602 let value = sum(&|d| d.value_micros as f64);
603 // Counts are compared from the first day g1t counted: before its meter
604 // was deployed there is only Cloudflare's side. A meter that never
605 // counted anything is compared over every day, so it still shows.
606 let first_counted = days.iter().filter(|d| d.own_quantity > 0.0).map(|d| d.day.as_str()).min();
607 let compared = |d: &&ProductDay| first_counted.is_none_or(|from| d.day.as_str() >= from);
608 let (cf_quantity, own_quantity) = days.iter().filter(compared).fold((0.0, 0.0), |(cf, own), d| (cf + d.cf_quantity, own + d.own_quantity));
609 let mut out = Vec::new();
610 if counted && cf_quantity > 0.0 {
611 let delta = delta_percent(own_quantity, cf_quantity);
612 if delta.is_some_and(|d| d.abs() > threshold) {
613 out.push(Drift { bucket: bucket.into(), kind: DriftKind::Count, ours: own_quantity, cloudflare: cf_quantity, delta_percent: delta });
614 }
615 }
616 // Its usage at Cloudflare's list prices, before the included amounts.
617 let theirs = list_micros.unwrap_or(0.0);
618 let enough = theirs.max(own_cost) >= min_cost_micros as f64;
619 if enough && !overhead && theirs > 0.0 && own_cost > 0.0 {
620 let delta = delta_percent(own_cost, theirs);
621 if delta.is_some_and(|d| d.abs() > threshold) {
622 out.push(Drift { bucket: bucket.into(), kind: DriftKind::Cost, ours: own_cost, cloudflare: theirs, delta_percent: delta });
623 }
624 }
625 if !overhead && cf_cost >= min_cost_micros as f64 && value <= 0.0 {
626 out.push(Drift { bucket: bucket.into(), kind: DriftKind::Leak, ours: value, cloudflare: cf_cost, delta_percent: None });
627 }
628 out
629}
630
631/// What each bucket's billable usage over a window comes to at
632/// Cloudflare's list prices, before the included amounts (`cycle::list_cost`
633/// line by line, in micros): what the price book's cost of the same usage
634/// is checked against. None for a bucket with usage on a meter that has no
635/// list price: its usage cannot be priced like for like, so it is not
636/// checked. Other sources (Artifacts events, AI Gateway) are left out.
637pub(crate) fn list_costs(rules: &[Rule], lines: &[LineRow]) -> BTreeMap<String, Option<f64>> {
638 let mut out: BTreeMap<String, Option<f64>> = BTreeMap::new();
639 for line in lines.iter().filter(|l| l.source == SOURCE_BILLABLE) {
640 let bucket = costs::classify(rules, &line.product, &line.meter).map_or(UNMAPPED, |r| r.bucket.as_str());
641 let entry = out.entry(bucket.to_owned()).or_insert(Some(0.0));
642 if line.quantity <= 0.0 {
643 continue;
644 }
645 *entry = match (*entry, crate::cycle::list_cost(&line.product, &line.meter, line.quantity)) {
646 (Some(sum), Some(cost)) => Some(sum + cost * 1_000_000.0),
647 _ => None,
648 };
649 }
650 out
651}
652
653/// Where a testing reset's history starts: all of a workspace's ledger.
654pub(crate) const RESET_HISTORY_FROM: &str = "2000-01-01";
655
656/// What a testing reset wiped that g1t paid for, on one day for one
657/// bucket (`reset_costs`).
658#[derive(Clone, Debug, PartialEq)]
659pub(crate) struct Wiped {
660 pub day: String,
661 pub bucket: String,
662 pub cost: i64,
663 pub value: i64,
664}
665
666/// A workspace's usage rows, about to be wiped, as what g1t paid for: the
667/// rows with a cost, by day and bucket, valued as the reconciliation
668/// valued them (at price where nothing paid). Plan payments, credits and
669/// a workspace's own model provider cost g1t nothing and are left out.
670pub(crate) fn wiped(rows: &[UsageRow], revenue_map: &BTreeMap<String, String>, margin_percent: u32) -> Vec<Wiped> {
671 let mut by: BTreeMap<(String, String), (i64, i64)> = BTreeMap::new();
672 for u in rows.iter().filter(|u| u.cost > 0) {
673 let bucket = u.bucket.clone().unwrap_or_else(|| revenue_map.get(&u.key).cloned().unwrap_or_else(|| NOT_CLOUDFLARE[0].to_owned()));
674 let sums = by.entry((u.day.clone(), bucket)).or_default();
675 sums.0 += u.cost;
676 sums.1 += u.value.max(0);
677 }
678 by.into_iter()
679 .map(|((day, bucket), (cost, value))| Wiped {
680 day,
681 bucket,
682 cost,
683 value: if value > 0 { value } else { crate::credits::with_margin(cost, margin_percent) },
684 })
685 .collect()
686}
687
688/// What testing resets kept, each (day, workspace, bucket, cost, value),
689/// as usage rows: valued as before, nothing paid, all of it given away
690/// (why "testing resets"). A reset's own row (bucket '') is not usage.
691pub(crate) fn reset_usage(kept: &[(String, String, String, i64, i64)]) -> Vec<UsageRow> {
692 kept.iter()
693 .filter(|(_, _, bucket, cost, value)| !bucket.is_empty() && (*cost != 0 || *value != 0))
694 .map(|(day, workspace, bucket, cost, value)| UsageRow {
695 day: day.clone(),
696 workspace: workspace.clone(),
697 key: "reset".into(),
698 bucket: Some(bucket.clone()),
699 value: *value,
700 cash: 0,
701 cost: *cost,
702 given: Given { reset: *value, ..Given::default() },
703 })
704 .collect()
705}
706
707/// When the last `days` in a row (each with enough cost to say something)
708/// were all under the floor: the first of them and the worst margin.
709/// Each item is a day's (day, revenue, cost).
710pub(crate) fn breach(series: &[(String, i64, i64)], floor_percent: f64, days: usize, min_cost_micros: i64) -> Option<(String, f64)> {
711 if days == 0 || series.len() < days {
712 return None;
713 }
714 let tail = &series[series.len() - days..];
715 let mut worst = f64::INFINITY;
716 for (_, revenue, cost) in tail {
717 if *cost < min_cost_micros {
718 return None;
719 }
720 let margin = margin_percent(*revenue, *cost).unwrap_or(-100.0);
721 if margin >= floor_percent {
722 return None;
723 }
724 worst = worst.min(margin);
725 }
726 Some((tail[0].0.clone(), worst))
727}
728
729/// `total` shared out in proportion to `weights`, in whole millionths that
730/// add up to it exactly (largest remainder first). Nothing to share, or no
731/// weight, shares nothing.
732pub(crate) fn attribute(total: i64, weights: &[(String, f64)]) -> Vec<(String, i64)> {
733 let mut merged: BTreeMap<String, f64> = BTreeMap::new();
734 for (key, w) in weights {
735 *merged.entry(key.clone()).or_default() += w.max(0.0);
736 }
737 let sum: f64 = merged.values().sum();
738 if total <= 0 || sum <= 0.0 {
739 return Vec::new();
740 }
741 let mut shares: Vec<(String, i64, f64)> = merged
742 .into_iter()
743 .map(|(key, w)| {
744 let exact = total as f64 * w / sum;
745 (key, exact.floor() as i64, exact - exact.floor())
746 })
747 .collect();
748 let mut left = total - shares.iter().map(|s| s.1).sum::<i64>();
749 let mut order: Vec<usize> = (0..shares.len()).collect();
750 order.sort_by(|a, b| shares[*b].2.total_cmp(&shares[*a].2).then(shares[*a].0.cmp(&shares[*b].0)));
751 for index in order {
752 if left <= 0 {
753 break;
754 }
755 shares[index].1 += 1;
756 left -= 1;
757 }
758 shares.into_iter().filter(|s| s.1 > 0).map(|(key, micros, _)| (key, micros)).collect()
759}
760
761/// Workspaces that cost g1t more than `factor` times what they paid, with
762/// at least `floor_micros` of cost: each (workspace, cost, revenue), the
763/// biggest gap first.
764/// What a day's usage was worth at price. g1t's own workspaces are valued
765/// at price. So is usage nothing paid for, neither charged nor drawn from
766/// the plan, a trial, a pool or a gift (a free period): it was given away at
767/// its price, not sold for nothing. Anything paid keeps what it was paid, so
768/// a discount still shows as one.
769pub(crate) fn usage_value(internal: bool, cost: i64, paid: i64, margin_percent: u32) -> i64 {
770 if internal || (paid == 0 && cost > 0) {
771 return crate::credits::with_margin(cost, margin_percent);
772 }
773 paid
774}
775
776/// What the overall alert says: the money as money, and a percentage only
777/// while there is enough coming in for one to mean something (a few cents
778/// against dollars of cost reads as -8000%).
779pub(crate) fn overall_detail(took: i64, spent: i64, days: usize, floor: f64, worst: f64) -> String {
780 if took < 1_000_000 * days as i64 {
781 return format!(
782 "All of g1t, comped workspaces left out: took in {} against {} of Cloudflare's bill over {days} days.",
783 dollars(took),
784 dollars(spent)
785 );
786 }
787 format!("All of g1t, comped workspaces left out: money in against Cloudflare's bill under {floor:.0}% for {days} days running, as low as {worst:.1}%.")
788}
789
790pub(crate) fn anomalies(rows: &[(String, i64, i64)], factor: f64, floor_micros: i64) -> Vec<(String, i64, i64)> {
791 let mut out: Vec<(String, i64, i64)> = rows
792 .iter()
793 .filter(|(_, cost, revenue)| *cost >= floor_micros && *cost as f64 > *revenue as f64 * factor)
794 .cloned()
795 .collect();
796 out.sort_by(|a, b| (b.1 - b.2).cmp(&(a.1 - a.2)).then(a.0.cmp(&b.0)));
797 out
798}
799
800/// Cloudflare's rate for one of its units: the median over the costed
801/// days of cost over quantity, in dollars. None while nothing is costed.
802/// Each item is a day's (quantity, cost), from `rate_line`.
803pub(crate) fn billed_rate(days: &[(f64, f64)]) -> Option<f64> {
804 let mut rates: Vec<f64> = days.iter().filter(|(q, c)| *q > 0.0 && *c > 0.0).map(|(q, c)| c / q).collect();
805 if rates.is_empty() {
806 return None;
807 }
808 rates.sort_by(f64::total_cmp);
809 Some(rates[rates.len() / 2])
810}
811
812/// A line's (quantity, cost) for `billed_rate`: at its list price for all
813/// of the quantity where the meter has one, never what the cycle billed,
814/// which is net of the included amounts (nothing until the cycle passes
815/// them, part of a day's usage on the day it does, then whole millions) and
816/// so says nothing about the price of each unit. A meter with no list
817/// price has only what Cloudflare billed; the median over the charged days
818/// leaves out the day its included amount ran out.
819pub(crate) fn rate_line(product: &str, meter: &str, quantity: f64, cost_usd: f64) -> (f64, f64) {
820 (quantity, crate::cycle::list_cost(product, meter, quantity).unwrap_or(cost_usd))
821}
822
823/// What one of g1t's units costs, from Cloudflare's rate per its own unit
824/// and how many of Cloudflare's units each of g1t's took: if Cloudflare
825/// counts three operations for every git operation g1t counts, a git
826/// operation costs three of Cloudflare's. None without enough of g1t's
827/// units to say.
828pub(crate) fn derived_unit_cost(rate_per_cf_unit: f64, cf_units: f64, own_units: f64) -> Option<f64> {
829 (own_units >= MIN_UNITS && cf_units > 0.0 && rate_per_cf_unit > 0.0).then(|| rate_per_cf_unit * cf_units / own_units)
830}
831
832/// How many units a price is per: `1,000 operations` → 1,000, `million
833/// requests` → 1,000,000, `second` → 1.
834pub(crate) fn unit_size(unit: &str) -> f64 {
835 let first = unit.split_whitespace().next().unwrap_or_default().replace(',', "");
836 match first.as_str() {
837 "million" => 1_000_000.0,
838 "thousand" => 1_000.0,
839 n => n.parse().unwrap_or(1.0),
840 }
841}
842
843fn day_before(day: &str, days: u64) -> String {
844 let ms = g1t_contracts::time::parse_rfc3339(&format!("{day}T00:00:00Z")).unwrap_or(0);
845 rfc3339(ms.saturating_sub(days * DAY_MS))[..10].to_owned()
846}
847
848/// Dollars to the cent from a dollar up, finer below: `$17.02`, `$0.063`.
849fn dollars(micros: i64) -> String {
850 if micros.abs() >= 1_000_000 {
851 let cents = (micros as f64 / 10_000.0).round() as i64;
852 format!("{}${}.{:02}", if cents < 0 { "-" } else { "" }, cents.abs() / 100, cents.abs() % 100)
853 } else {
854 crate::features::dollars(micros)
855 }
856}
857
858/// The days a plan payment is spread over.
859const PLAN_DAYS: u64 = 30;
860
861/// `micros` paid on `day` spread evenly over `days` days from it, in
862/// whole micros that add up to it (the first days take the remainder).
863pub(crate) fn spread(day: &str, micros: i64, days: u64) -> Vec<(String, i64)> {
864 if micros <= 0 || days == 0 {
865 return Vec::new();
866 }
867 let start = g1t_contracts::time::parse_rfc3339(&format!("{}T00:00:00Z", &day[..10.min(day.len())])).unwrap_or(0);
868 let each = micros / days as i64;
869 let rest = micros % days as i64;
870 (0..days)
871 .map(|n| (rfc3339(start + n * DAY_MS)[..10].to_owned(), each + i64::from((n as i64) < rest)))
872 .collect()
873}
874
875// ---------------------------------------------------------------------
876// The daily run, and what sudo reads.
877// ---------------------------------------------------------------------
878
879#[derive(Serialize)]
880struct Mail<'a> {
881 to: &'a str,
882 from: &'a str,
883 subject: &'a str,
884 text: String,
885 html: String,
886}
887
888fn escape(text: &str) -> String {
889 text.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;").replace('"', "&quot;")
890}
891
892/// Emails staff through Cloudflare Email Sending, the `EMAIL` binding.
893pub(crate) async fn email_staff(env: &Env, to: &str, subject: &str, lines: &[String]) -> Result<()> {
894 email_staff_page(env, to, subject, lines, ("Costs & margin", "https://sudo.g1t.sh/costs"), "g1t-billing's margin guard").await
895}
896
897/// Emails staff, linking to a page of sudo (`page`: its name and address)
898/// and saying what sent it.
899pub(crate) async fn email_staff_page(env: &Env, to: &str, subject: &str, lines: &[String], page: (&str, &str), sender: &str) -> Result<()> {
900 let (name, link) = page;
901 let text = format!("{}\n\n{name}: {link}\n\nSent by {sender} (COSTS_ALERT_EMAIL).\n", lines.join("\n\n"));
902 let mut html = String::from("<div style=\"font-family:system-ui,sans-serif;max-width:560px;margin:0 auto;padding:24px 16px;color:#16150f\">");
903 for line in lines {
904 html.push_str(&format!("<p style=\"font-size:15px;line-height:1.6\">{}</p>", escape(line)));
905 }
906 html.push_str(&format!(
907 "<p><a href=\"{link}\">Open {} in sudo</a></p><p style=\"font-size:13px;color:#6e6a5e\">Sent by {} (COSTS_ALERT_EMAIL).</p></div>",
908 escape(name),
909 escape(sender)
910 ));
911 let mail = Mail { to, from: "g1t <noreply@g1t.sh>", subject, text, html };
912 let binding = g1t_kit::js::binding(env, "EMAIL")?;
913 g1t_kit::js::call(&binding, "send", &[g1t_kit::js::to_js(&mail)?]).await?;
914 Ok(())
915}
916
917#[derive(Deserialize)]
918struct AlertRow {
919 id: String,
920 kind: String,
921 subject: String,
922 detail: String,
923 since: String,
924 opened_at: String,
925 emailed_at: Option<String>,
926}
927
928impl From<AlertRow> for MarginAlert {
929 fn from(r: AlertRow) -> Self {
930 MarginAlert { id: r.id, kind: r.kind, subject: r.subject, detail: r.detail, since: r.since, opened_at: r.opened_at, emailed_at: r.emailed_at }
931 }
932}
933
934#[derive(Deserialize)]
935struct MarginRow {
936 day: String,
937 bucket: String,
938 cf_cost_micros: i64,
939 own_cost_micros: i64,
940 value_micros: i64,
941 cash_micros: i64,
942 cf_quantity: f64,
943 own_quantity: f64,
944 #[serde(default)]
945 given_comped_micros: Option<i64>,
946 #[serde(default)]
947 given_free_micros: Option<i64>,
948 #[serde(default)]
949 given_trial_micros: Option<i64>,
950 #[serde(default)]
951 given_pool_micros: Option<i64>,
952 #[serde(default)]
953 given_discount_micros: Option<i64>,
954 #[serde(default)]
955 given_credit_promotional_micros: Option<i64>,
956 #[serde(default)]
957 given_credit_goodwill_micros: Option<i64>,
958 #[serde(default)]
959 given_reset_micros: Option<i64>,
960 #[serde(default)]
961 given_unpaid_micros: Option<i64>,
962}
963
964impl From<MarginRow> for ProductDay {
965 fn from(r: MarginRow) -> Self {
966 ProductDay {
967 day: r.day,
968 bucket: r.bucket,
969 cf_cost_micros: r.cf_cost_micros,
970 own_cost_micros: r.own_cost_micros,
971 value_micros: r.value_micros,
972 cash_micros: r.cash_micros,
973 cf_quantity: r.cf_quantity,
974 own_quantity: r.own_quantity,
975 given: Given {
976 comped: r.given_comped_micros.unwrap_or(0),
977 free: r.given_free_micros.unwrap_or(0),
978 trial: r.given_trial_micros.unwrap_or(0),
979 pool: r.given_pool_micros.unwrap_or(0),
980 discount: r.given_discount_micros.unwrap_or(0),
981 credit_promotional: r.given_credit_promotional_micros.unwrap_or(0),
982 credit_goodwill: r.given_credit_goodwill_micros.unwrap_or(0),
983 reset: r.given_reset_micros.unwrap_or(0),
984 unpaid: r.given_unpaid_micros.unwrap_or(0),
985 },
986 }
987 }
988}
989
990impl Billing {
991 /// The day's work: read Cloudflare's bill and g1t's own counts,
992 /// reconcile, look for drift, measure unit costs, apply prices whose
993 /// day has come, and raise or clear alerts.
994 pub(crate) async fn costs_daily(&self, env: &Env, keeper: &crate::keeper::Keeper) -> Result<CostsRun> {
995 let mut run = CostsRun::default();
996 // The subscriptions first: they say when the billing cycle starts,
997 // which the bill is priced by. Not a problem for the run: the last
998 // read, or the estimate, stays.
999 if keeper.can_read_bill()
1000 && let Err(error) = self.read_subscriptions(keeper).await
1001 {
1002 worker::console_error!("Cloudflare's subscriptions were not read: {error}");
1003 }
1004 let (since, until, bill_since) = match self.read_cloudflare(keeper, &mut run.problems).await? {
1005 Some((since, until, lines, bill_since)) => {
1006 run.lines = lines;
1007 (since, until, Some(bill_since))
1008 }
1009 // Without the bill, still reconcile what g1t knows itself, over
1010 // the same days the bill would be read for.
1011 None => {
1012 #[derive(Deserialize)]
1013 struct Last {
1014 day: Option<String>,
1015 }
1016 let last = self.db.prepare("SELECT MAX(day) AS day FROM margin_days").first::<Last>(None).await?.and_then(|l| l.day);
1017 let (since, until) = costs::window(last.as_deref(), now_ms());
1018 (since, until, None)
1019 }
1020 };
1021 if let Err(error) = self.count_own(&since, &until).await {
1022 run.problems.push(format!("g1t's own counts could not be read: {error}"));
1023 }
1024 self.snapshot_pending(&until).await?;
1025 // Reconciled over the whole window sudo shows, not only the days the
1026 // bill was read for: it reads only what is already kept, so a change
1027 // in how a day is valued reaches every day shown at the next run.
1028 let window = day_before(&until, costs::BACKFILL_DAYS - 1);
1029 let reconcile_from = [Some(window), Some(since.clone()), bill_since].into_iter().flatten().min().unwrap_or_default();
1030 run.days = self.reconcile_range(&reconcile_from, &until).await?;
1031 let drift = self.find_drift(&until).await?;
1032 run.proposals = self.measure_units(&until).await?;
1033 self.apply_due_versions().await?;
1034 run.alerts = self.raise_alerts(env, &until, &drift).await?;
1035 if let Some(identity) = &self.identity
1036 && let Err(error) = self.tell_owners_of_rises(identity).await
1037 {
1038 run.problems.push(format!("owners could not be told of a price rise: {error}"));
1039 }
1040 for problem in &run.problems {
1041 worker::console_warn!("costs: {problem}");
1042 }
1043 Ok(run)
1044 }
1045
1046 /// What each month-end source had come to by the end of `day`.
1047 async fn snapshot_pending(&self, day: &str) -> Result<()> {
1048 self.db
1049 .prepare(
1050 "INSERT INTO pending_days (day, workspace, source, cost_micros, charge_micros)
1051 SELECT ?1, workspace, source, COALESCE(cost_micros, 0), COALESCE(charge_micros, 0) FROM pending_usage WHERE month = ?2
1052 ON CONFLICT (day, workspace, source) DO UPDATE SET cost_micros = excluded.cost_micros, charge_micros = excluded.charge_micros",
1053 )
1054 .bind(&[day.into(), day[..7].into()])?
1055 .run()
1056 .await?;
1057 Ok(())
1058 }
1059
1060 /// What customers were charged on the days, by workspace and key: every
1061 /// workspace's, or only `only`'s.
1062 async fn usage_rows(&self, since: &str, until: &str, only: Option<&str>) -> Result<Vec<UsageRow>> {
1063 let only_sql = only.unwrap_or("");
1064 #[derive(Deserialize)]
1065 struct Row {
1066 day: String,
1067 workspace: String,
1068 key: String,
1069 internal: i64,
1070 own_provider: i64,
1071 cash: Option<i64>,
1072 drawn: Option<i64>,
1073 gift: Option<i64>,
1074 trial: Option<i64>,
1075 oss: Option<i64>,
1076 covered: Option<i64>,
1077 discount: Option<i64>,
1078 cost: Option<i64>,
1079 }
1080 let charged_here = crate::storage::CHARGED_HERE.iter().map(|s| format!("'{s}'")).collect::<Vec<_>>().join(", ");
1081 let end = format!("{until}T23:59:59.999Z");
1082 let rows = self
1083 .db
1084 .prepare(format!(
1085 "SELECT substr(created_at, 1, 10) AS day, workspace,
1086 CASE WHEN task = 'deployments' AND reference LIKE 'deploy/%' THEN 'builds'
1087 WHEN task = 'plan' THEN '{planning}' ELSE COALESCE(task, 'other') END AS key,
1088 CASE WHEN workspace IN ({internal}) THEN 1 ELSE 0 END AS internal,
1089 CASE WHEN billed_to = 'workspace' THEN 1 ELSE 0 END AS own_provider,
1090 -SUM(amount_micros) AS cash,
1091 SUM(COALESCE(credit_micros, 0) + COALESCE(gift_micros, 0) + COALESCE(trial_micros, 0) + COALESCE(oss_micros, 0) + COALESCE(given_micros, 0)) AS drawn,
1092 SUM(COALESCE(gift_micros, 0)) AS gift,
1093 SUM(COALESCE(trial_micros, 0)) AS trial,
1094 SUM(COALESCE(oss_micros, 0)) AS oss,
1095 SUM(COALESCE(given_micros, 0)) AS covered,
1096 SUM(COALESCE(discount_micros, 0)) AS discount,
1097 SUM(COALESCE(cost_micros, 0)) AS cost
1098 FROM ledger
1099 WHERE kind = 'usage' AND created_at >= ?1 AND created_at <= ?2 AND COALESCE(task, '') NOT IN ({charged_here})
1100 AND (?3 = '' OR workspace = ?3)
1101 GROUP BY 1, 2, 3, 4, 5",
1102 internal = crate::sales::INTERNAL_SQL,
1103 planning = PLANNING_KEY
1104 ))
1105 .bind(&[since.into(), end.as_str().into(), only_sql.into()])?
1106 .all()
1107 .await?
1108 .results::<Row>()?;
1109 let mut internal = BTreeSet::new();
1110 let mut out: Vec<UsageRow> = rows
1111 .into_iter()
1112 .map(|r| {
1113 // A workspace's own model provider was paid there: no cost
1114 // to g1t. g1t's own workspaces are valued at price.
1115 let cost = if r.own_provider == 1 { 0 } else { r.cost.unwrap_or(0) };
1116 let cash = r.cash.unwrap_or(0);
1117 // A discount took its part below cost plus the margin: it is
1118 // valued at price and that part counted as given, so a
1119 // discounted sale never reads as margin lost.
1120 let discount = r.discount.unwrap_or(0).max(0);
1121 let paid = cash + r.drawn.unwrap_or(0) + discount;
1122 let value = usage_value(r.internal == 1, cost, paid, self.margin_percent);
1123 let given = if r.internal == 1 {
1124 Given { comped: value, ..Given::default() }
1125 } else if paid == 0 && cost > 0 {
1126 Given { free: value, ..Given::default() }
1127 } else {
1128 // A complimentary allowance is promotional: given, never money in.
1129 Given { free: r.covered.unwrap_or(0), trial: r.trial.unwrap_or(0), pool: r.oss.unwrap_or(0), discount, credit_promotional: r.gift.unwrap_or(0), ..Given::default() }
1130 };
1131 if r.internal == 1 {
1132 internal.insert(r.workspace.clone());
1133 }
1134 UsageRow { day: r.day, workspace: r.workspace, key: r.key, bucket: None, value, cash, cost, given }
1135 })
1136 .collect();
1137 // Credits from g1t: what promotional and goodwill credit paid for
1138 // is given, not money in; a refund gives money back on its day.
1139 let (mut draws, mut refunds) = self.credit_effects(since, until).await?;
1140 if let Some(only) = only {
1141 draws.retain(|(workspace, _)| workspace == only);
1142 refunds.retain(|r| r.workspace == only);
1143 }
1144 apply_credits(&mut out, &draws, &refunds);
1145 // Month-end sources, from their daily snapshots.
1146 #[derive(Deserialize)]
1147 struct Snap {
1148 day: String,
1149 workspace: String,
1150 source: String,
1151 cost_micros: i64,
1152 charge_micros: i64,
1153 }
1154 let snaps = self
1155 .db
1156 .prepare("SELECT day, workspace, source, cost_micros, charge_micros FROM pending_days WHERE day >= ?1 AND day <= ?2 AND (?3 = '' OR workspace = ?3)")
1157 .bind(&[day_before(since, 1).into(), until.into(), only_sql.into()])?
1158 .all()
1159 .await?
1160 .results::<Snap>()?
1161 .into_iter()
1162 .filter(|s| crate::storage::CHARGED_HERE.contains(&s.source.as_str()) || s.source == "domains")
1163 .map(|s| (s.day, s.workspace, s.source, s.cost_micros, s.charge_micros))
1164 .collect::<Vec<_>>();
1165 // Complimentary enrollments' days: their month-end meters were paid
1166 // by the allowance (or covered past it), never money in.
1167 #[derive(Deserialize)]
1168 struct Enrolled {
1169 workspace: String,
1170 starts_at: String,
1171 ends_at: Option<String>,
1172 }
1173 let enrolled = self
1174 .db
1175 .prepare("SELECT workspace, starts_at, ends_at FROM complimentary_periods WHERE starts_at <= ?2 AND (ends_at IS NULL OR ends_at >= ?1)")
1176 .bind(&[since.into(), end.as_str().into()])?
1177 .all()
1178 .await?
1179 .results::<Enrolled>()?;
1180 let gifted_on = |workspace: &str, day: &str| {
1181 enrolled.iter().any(|p| {
1182 p.workspace == workspace && &p.starts_at[..10.min(p.starts_at.len())] <= day && p.ends_at.as_deref().is_none_or(|e| &e[..10.min(e.len())] >= day)
1183 })
1184 };
1185 out.extend(
1186 pending_deltas(&snaps)
1187 .into_iter()
1188 .filter(|u| u.day.as_str() >= since)
1189 .map(|u| {
1190 if internal.contains(&u.workspace) {
1191 comped_meter(u)
1192 } else if gifted_on(&u.workspace, &u.day) {
1193 gifted_meter(u)
1194 } else {
1195 u
1196 }
1197 }),
1198 );
1199 // The plan's price, spread over the 30 days it pays for, so a month's
1200 // payment does not read as one very good day and 29 bad ones.
1201 #[derive(Deserialize)]
1202 struct Plan {
1203 day: String,
1204 workspace: String,
1205 micros: Option<i64>,
1206 }
1207 let plans = self
1208 .db
1209 .prepare(
1210 "SELECT substr(paid_at, 1, 10) AS day, workspace, SUM(amount_micros) AS micros FROM plan_payments
1211 WHERE paid_at >= ?1 AND paid_at <= ?2 AND (?3 = '' OR workspace = ?3) GROUP BY 1, 2",
1212 )
1213 .bind(&[day_before(since, PLAN_DAYS - 1).into(), end.as_str().into(), only_sql.into()])?
1214 .all()
1215 .await?
1216 .results::<Plan>()?;
1217 for p in plans {
1218 for (day, micros) in spread(&p.day, p.micros.unwrap_or(0), PLAN_DAYS) {
1219 if day.as_str() >= since && day.as_str() <= until {
1220 out.push(UsageRow { day, workspace: p.workspace.clone(), key: "plan".into(), bucket: None, value: micros, cash: micros, cost: 0, given: Given::default() });
1221 }
1222 }
1223 }
1224 // Charges without real money behind them are not money in.
1225 without_real_money(&mut out, self.payments_live_since().await?.as_deref());
1226 Ok(out)
1227 }
1228
1229 /// The day payments went live (`live_since`): kept in `cost_settings`
1230 /// the first time they are seen live, so charges from before it stay
1231 /// test money after the switch.
1232 pub(crate) async fn payments_live_since(&self) -> Result<Option<String>> {
1233 #[derive(Deserialize)]
1234 struct Row {
1235 value: String,
1236 }
1237 let live = self.stripe.as_ref().is_some_and(crate::stripe::Stripe::live);
1238 let kept = self
1239 .db
1240 .prepare("SELECT value FROM cost_settings WHERE key = 'payments_live_since'")
1241 .first::<Row>(None)
1242 .await?
1243 .map(|r| r.value)
1244 .filter(|v| v.len() >= 10);
1245 let today = rfc3339(now_ms())[..10].to_owned();
1246 let since = live_since(live, kept.as_deref(), &today);
1247 if let Some(day) = since.as_deref().filter(|_| kept.is_none()) {
1248 self.db
1249 .prepare(
1250 "INSERT INTO cost_settings (key, value, updated_at, updated_by) VALUES ('payments_live_since', ?1, ?2, 'billing')
1251 ON CONFLICT (key) DO UPDATE SET value = ?1, updated_at = ?2, updated_by = 'billing'",
1252 )
1253 .bind(&[day.into(), rfc3339(now_ms()).into()])?
1254 .run()
1255 .await?;
1256 }
1257 Ok(since)
1258 }
1259
1260 /// Which bucket each ledger key (and month-end source) is revenue of.
1261 async fn revenue_map(&self) -> Result<BTreeMap<String, String>> {
1262 #[derive(Deserialize)]
1263 struct Map {
1264 key: String,
1265 bucket: String,
1266 }
1267 Ok(self
1268 .db
1269 .prepare("SELECT key, bucket FROM revenue_map")
1270 .all()
1271 .await?
1272 .results::<Map>()?
1273 .into_iter()
1274 .map(|m| (m.key, m.bucket))
1275 .collect())
1276 }
1277
1278 /// What a testing reset of `workspace` is about to wipe that g1t paid
1279 /// for, a row per day and bucket: its whole ledger and month-end
1280 /// snapshots, valued as the reconciliation values them.
1281 pub(crate) async fn wiped_by_reset(&self, workspace: &str) -> Result<Vec<Wiped>> {
1282 let today = rfc3339(now_ms())[..10].to_owned();
1283 let rows = self.usage_rows(RESET_HISTORY_FROM, &today, Some(workspace)).await?;
1284 Ok(wiped(&rows, &self.revenue_map().await?, self.margin_percent))
1285 }
1286
1287 /// What testing resets kept for the days, as usage rows.
1288 async fn reset_rows(&self, since: &str, until: &str) -> Result<Vec<UsageRow>> {
1289 #[derive(Deserialize)]
1290 struct Kept {
1291 day: String,
1292 workspace: String,
1293 bucket: String,
1294 cost: Option<i64>,
1295 value: Option<i64>,
1296 }
1297 let kept = self
1298 .db
1299 .prepare(
1300 "SELECT day, workspace, bucket, SUM(cost_micros) AS cost, SUM(value_micros) AS value FROM reset_costs
1301 WHERE day >= ?1 AND day <= ?2 AND bucket <> '' GROUP BY day, workspace, bucket",
1302 )
1303 .bind(&[since.into(), until.into()])?
1304 .all()
1305 .await?
1306 .results::<Kept>()?;
1307 Ok(reset_usage(
1308 &kept.into_iter().map(|k| (k.day, k.workspace, k.bucket, k.cost.unwrap_or(0), k.value.unwrap_or(0))).collect::<Vec<_>>(),
1309 ))
1310 }
1311
1312 /// Reconciles the days and writes `margin_days` and `workspace_costs`.
1313 async fn reconcile_range(&self, since: &str, until: &str) -> Result<u32> {
1314 let rules = self.rules().await?;
1315 let revenue_map = self.revenue_map().await?;
1316 let lines = self
1317 .db
1318 .prepare("SELECT day, source, product, meter, quantity, cost_usd FROM cost_lines WHERE day >= ?1 AND day <= ?2")
1319 .bind(&[since.into(), until.into()])?
1320 .all()
1321 .await?
1322 .results::<LineRow>()?;
1323 let own = self
1324 .db
1325 .prepare("SELECT day, meter, workspace, quantity FROM own_counts WHERE day >= ?1 AND day <= ?2")
1326 .bind(&[since.into(), until.into()])?
1327 .all()
1328 .await?
1329 .results::<OwnRow>()?;
1330 let mut usage = self.usage_rows(since, until, None).await?;
1331 // What testing resets wiped: still paid for, now given away.
1332 usage.extend(self.reset_rows(since, until).await?);
1333 #[derive(Deserialize)]
1334 struct Internal {
1335 workspace: String,
1336 }
1337 let internal: BTreeSet<String> = self
1338 .db
1339 .prepare(format!("WITH i(workspace) AS ({}) SELECT DISTINCT workspace FROM i", crate::sales::INTERNAL_SQL))
1340 .all()
1341 .await?
1342 .results::<Internal>()?
1343 .into_iter()
1344 .map(|i| i.workspace)
1345 .collect();
1346 let (days, workspaces) = fold(&rules, &revenue_map, &lines, &own, &usage, &internal);
1347 let now = rfc3339(now_ms());
1348 self.db
1349 .batch(vec![
1350 self.db.prepare("DELETE FROM margin_days WHERE day >= ?1 AND day <= ?2").bind(&[since.into(), until.into()])?,
1351 self.db.prepare("DELETE FROM workspace_costs WHERE day >= ?1 AND day <= ?2").bind(&[since.into(), until.into()])?,
1352 ])
1353 .await?;
1354 for chunk in days.chunks(50) {
1355 let mut statements = Vec::with_capacity(chunk.len());
1356 for d in chunk {
1357 statements.push(
1358 self.db
1359 .prepare(
1360 "INSERT OR REPLACE INTO margin_days (day, bucket, cf_cost_micros, own_cost_micros, value_micros, cash_micros, cf_quantity, own_quantity, given_micros, given_comped_micros, given_free_micros, given_trial_micros, given_pool_micros, given_discount_micros, given_credit_promotional_micros, given_credit_goodwill_micros, given_reset_micros, given_unpaid_micros, computed_at)
1361 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
1362 )
1363 .bind(&[
1364 d.day.as_str().into(),
1365 d.bucket.as_str().into(),
1366 (d.cf_cost_micros as f64).into(),
1367 (d.own_cost_micros as f64).into(),
1368 (d.value_micros as f64).into(),
1369 (d.cash_micros as f64).into(),
1370 d.cf_quantity.into(),
1371 d.own_quantity.into(),
1372 (d.given.total() as f64).into(),
1373 (d.given.comped as f64).into(),
1374 (d.given.free as f64).into(),
1375 (d.given.trial as f64).into(),
1376 (d.given.pool as f64).into(),
1377 (d.given.discount as f64).into(),
1378 (d.given.credit_promotional as f64).into(),
1379 (d.given.credit_goodwill as f64).into(),
1380 (d.given.reset as f64).into(),
1381 (d.given.unpaid as f64).into(),
1382 now.as_str().into(),
1383 ])?,
1384 );
1385 }
1386 self.db.batch(statements).await?;
1387 }
1388 for chunk in workspaces.chunks(50) {
1389 let mut statements = Vec::with_capacity(chunk.len());
1390 for w in chunk {
1391 statements.push(
1392 self.db
1393 .prepare("INSERT OR REPLACE INTO workspace_costs (day, workspace, bucket, cost_micros, revenue_micros, value_micros, given_micros) VALUES (?, ?, ?, ?, ?, ?, ?)")
1394 .bind(&[
1395 w.day.as_str().into(),
1396 w.workspace.as_str().into(),
1397 w.bucket.as_str().into(),
1398 (w.cost as f64).into(),
1399 (w.revenue as f64).into(),
1400 (w.value as f64).into(),
1401 (w.given.total() as f64).into(),
1402 ])?,
1403 );
1404 }
1405 self.db.batch(statements).await?;
1406 }
1407 Ok(costs::days_between(since, until).len() as u32)
1408 }
1409
1410 async fn margin_days(&self, since: &str, until: &str) -> Result<Vec<ProductDay>> {
1411 Ok(self
1412 .db
1413 .prepare("SELECT * FROM margin_days WHERE day >= ?1 AND day <= ?2 ORDER BY day, bucket")
1414 .bind(&[since.into(), until.into()])?
1415 .all()
1416 .await?
1417 .results::<MarginRow>()?
1418 .into_iter()
1419 .map(ProductDay::from)
1420 .collect())
1421 }
1422
1423 /// Drift over the last week, written to `cost_drift` (replacing the
1424 /// last run's), with unmapped Cloudflare meters as leaks.
1425 async fn find_drift(&self, until: &str) -> Result<Vec<(Drift, String)>> {
1426 let since = day_before(until, DRIFT_DAYS - 1);
1427 let settings = self.cost_settings().await?;
1428 let rules = self.rules().await?;
1429 let days = self.margin_days(&since, until).await?;
1430 let mut by: BTreeMap<String, Vec<ProductDay>> = BTreeMap::new();
1431 for d in days {
1432 by.entry(d.bucket.clone()).or_default().push(d);
1433 }
1434 // The same days' usage at list prices, before the included amounts:
1435 // what the price book is checked against (never the bill, which is
1436 // net of them).
1437 let lines = self
1438 .db
1439 .prepare("SELECT day, source, product, meter, quantity, cost_usd FROM cost_lines WHERE source = ?1 AND day >= ?2 AND day <= ?3")
1440 .bind(&[SOURCE_BILLABLE.into(), since.as_str().into(), until.into()])?
1441 .all()
1442 .await?
1443 .results::<LineRow>()?;
1444 let list = list_costs(&rules, &lines);
1445 let mut found = Vec::new();
1446 for (bucket, days) in &by {
1447 let bucket_rules: Vec<&Rule> = rules.iter().filter(|r| &r.bucket == bucket).collect();
1448 let threshold = bucket_rules.iter().map(|r| r.drift_percent).fold(f64::INFINITY, f64::min);
1449 let threshold = if threshold.is_finite() { threshold } else { 10.0 };
1450 let counted = bucket_rules.iter().any(|r| r.own_meter.is_some());
1451 let list_micros = list.get(bucket).copied().flatten();
1452 for drift in drifts(bucket, days, threshold, counted, settings.min_daily_cost_micros, list_micros) {
1453 let title = costs::bucket_title(bucket);
1454 let detail = match drift.kind {
1455 DriftKind::Count => format!(
1456 "{title}: g1t counted {}, Cloudflare {} over the last {DRIFT_DAYS} days ({:+.1}%). Customers are charged for what g1t counts; check what Cloudflare counts as a unit and change the repos service's operation_mapping (set_operation_mapping).",
1457 crate::features::thousands(drift.ours.max(0.0).round() as u64),
1458 crate::features::thousands(drift.cloudflare.max(0.0).round() as u64),
1459 drift.delta_percent.unwrap_or(0.0)
1460 ),
1461 DriftKind::Cost => format!(
1462 "{title}: the last {DRIFT_DAYS} days' usage comes to {} at Cloudflare's list prices, before the included amounts; the price book's cost of the same usage is {} ({:+.1}%). A price may be stale: see the proposals.",
1463 dollars(drift.cloudflare as i64),
1464 dollars(drift.ours as i64),
1465 drift.delta_percent.unwrap_or(0.0)
1466 ),
1467 DriftKind::Leak if bucket == UNMAPPED => {
1468 format!("Cloudflare charged {} for meters no mapping claims. Map them on Costs & margin.", dollars(drift.cloudflare as i64))
1469 }
1470 DriftKind::Leak => format!(
1471 "{title}: Cloudflare charged {} over the last {DRIFT_DAYS} days and customers were charged nothing for it.",
1472 dollars(drift.cloudflare as i64)
1473 ),
1474 };
1475 found.push((drift, detail));
1476 }
1477 }
1478 let now = rfc3339(now_ms());
1479 let mut statements = vec![self.db.prepare("DELETE FROM cost_drift")];
1480 for (drift, detail) in &found {
1481 statements.push(
1482 self.db
1483 .prepare("INSERT OR REPLACE INTO cost_drift (bucket, kind, ours, cloudflare, delta_percent, detail, found_at) VALUES (?, ?, ?, ?, ?, ?, ?)")
1484 .bind(&[
1485 drift.bucket.as_str().into(),
1486 drift.kind.as_str().into(),
1487 drift.ours.into(),
1488 drift.cloudflare.into(),
1489 drift.delta_percent.map_or(JsValue::NULL, JsValue::from),
1490 detail.as_str().into(),
1491 now.as_str().into(),
1492 ])?,
1493 );
1494 }
1495 self.db.batch(statements).await?;
1496 Ok(found)
1497 }
1498
1499 /// Unit costs from the bill for mappings that scale to g1t's own count
1500 /// (git operations), proposed to the price book.
1501 async fn measure_units(&self, until: &str) -> Result<u32> {
1502 #[derive(Deserialize)]
1503 struct Scaled {
1504 product: String,
1505 meter: String,
1506 price_meter: String,
1507 own_meter: String,
1508 unit: Option<String>,
1509 }
1510 let scaled = self
1511 .db
1512 .prepare(
1513 "SELECT m.product, m.meter, m.price_meter, m.own_meter, p.unit FROM cost_map m LEFT JOIN prices p ON p.meter = m.price_meter
1514 WHERE m.scale_to_own = 1 AND m.price_meter IS NOT NULL AND m.own_meter IS NOT NULL",
1515 )
1516 .all()
1517 .await?
1518 .results::<Scaled>()?;
1519 let since = day_before(until, MEASURE_DAYS - 1);
1520 let rules = self.rules().await?;
1521 let mut proposed = 0;
1522 for s in scaled {
1523 #[derive(Deserialize)]
1524 struct Day {
1525 product: String,
1526 meter: String,
1527 quantity: f64,
1528 cost_usd: f64,
1529 }
1530 let lines = self
1531 .db
1532 .prepare("SELECT product, meter, quantity, cost_usd FROM cost_lines WHERE source = ?1 AND product = ?2 AND day >= ?3 AND day <= ?4")
1533 .bind(&[SOURCE_BILLABLE.into(), s.product.as_str().into(), since.as_str().into(), until.into()])?
1534 .all()
1535 .await?
1536 .results::<Day>()?;
1537 // Only the lines this very mapping claims.
1538 let mine: Vec<(f64, f64)> = lines
1539 .iter()
1540 .filter(|l| costs::classify(&rules, &l.product, &l.meter).is_some_and(|r| r.product == s.product && r.meter == s.meter))
1541 .map(|l| rate_line(&l.product, &l.meter, l.quantity, l.cost_usd))
1542 .collect();
1543 let Some(rate) = billed_rate(&mine) else { continue };
1544 let cf_units: f64 = mine.iter().map(|(q, _)| q).sum();
1545 #[derive(Deserialize)]
1546 struct Own {
1547 total: Option<f64>,
1548 }
1549 let own_units = self
1550 .db
1551 .prepare("SELECT SUM(quantity) AS total FROM own_counts WHERE meter = ?1 AND day >= ?2 AND day <= ?3")
1552 .bind(&[s.own_meter.as_str().into(), since.as_str().into(), until.into()])?
1553 .first::<Own>(None)
1554 .await?
1555 .and_then(|o| o.total)
1556 .unwrap_or(0.0);
1557 let Some(per_unit) = derived_unit_cost(rate, cf_units, own_units) else { continue };
1558 let size = unit_size(s.unit.as_deref().unwrap_or("1"));
1559 let measured = per_unit * size * 1_000_000.0;
1560 let reason = format!(
1561 "Cloudflare billed ${:.4} per 1,000 of its units and counted {:.2} of them for each one g1t counted over the last {MEASURE_DAYS} days ({} against {})",
1562 rate * 1000.0,
1563 cf_units / own_units,
1564 crate::features::thousands(cf_units.round() as u64),
1565 crate::features::thousands(own_units.round() as u64)
1566 );
1567 if self.propose(&s.price_meter, measured, &reason, "reconciler").await?.is_some() {
1568 proposed += 1;
1569 }
1570 }
1571 Ok(proposed)
1572 }
1573
1574 /// Opens, updates and closes margin alerts, and emails staff about new
1575 /// ones (and open ones each week).
1576 async fn raise_alerts(&self, env: &Env, until: &str, drift: &[(Drift, String)]) -> Result<u32> {
1577 let settings = self.cost_settings().await?;
1578 let since = day_before(until, u64::from(settings.alert_days.max(1)) - 1);
1579 let days = self.margin_days(&since, until).await?;
1580 let mut conditions: Vec<(String, String, String, String)> = Vec::new();
1581 // Each product under the floor.
1582 let mut by: BTreeMap<String, Vec<(String, i64, i64)>> = BTreeMap::new();
1583 let mut all: BTreeMap<String, (i64, i64)> = BTreeMap::new();
1584 for d in &days {
1585 let overall = all.entry(d.day.clone()).or_default();
1586 // What g1t gave away (comped workspaces, free periods, the
1587 // trial and the pools) is a budget it chose to spend, watched on
1588 // its own (budget.rs): not part of whether what is sold pays.
1589 overall.0 += d.cash_micros;
1590 overall.1 += (d.cost() - d.given.total()).max(0);
1591 if !OVERHEAD.contains(&d.bucket.as_str()) && d.bucket != UNMAPPED {
1592 by.entry(d.bucket.clone()).or_default().push((d.day.clone(), d.value_micros, d.cost()));
1593 }
1594 }
1595 let floor = settings.margin_floor_percent;
1596 let n = settings.alert_days as usize;
1597 for (bucket, series) in &by {
1598 if let Some((from, worst)) = breach(series, floor, n, settings.min_daily_cost_micros) {
1599 conditions.push((
1600 "margin".into(),
1601 bucket.clone(),
1602 format!("{}: margin under {floor:.0}% for {n} days running, as low as {worst:.1}%.", costs::bucket_title(bucket)),
1603 from,
1604 ));
1605 }
1606 }
1607 let series: Vec<(String, i64, i64)> = all.into_iter().map(|(day, (revenue, cost))| (day, revenue, cost)).collect();
1608 if let Some((from, worst)) = breach(&series, floor, n, settings.min_daily_cost_micros) {
1609 let tail = &series[series.len().saturating_sub(n)..];
1610 let (took, spent) = tail.iter().fold((0i64, 0i64), |(r, c), (_, revenue, cost)| (r + revenue, c + cost));
1611 conditions.push(("overall".into(), "g1t".into(), overall_detail(took, spent, n, floor, worst), from));
1612 }
1613 for (d, detail) in drift {
1614 let kind = if d.kind == DriftKind::Leak { "leak" } else { "drift" };
1615 conditions.push((kind.into(), format!("{}:{}", d.bucket, d.kind.as_str()), detail.clone(), until.to_owned()));
1616 }
1617 // Workspaces costing more than they pay.
1618 for (workspace, cost, revenue) in self.workspace_anomalies(until, &settings).await? {
1619 conditions.push((
1620 "workspace".into(),
1621 workspace.clone(),
1622 format!(
1623 "{workspace} cost g1t {} on Cloudflare over {ANOMALY_DAYS} days, and its usage was priced at {}: its prices are below cost.",
1624 dollars(cost),
1625 dollars(revenue)
1626 ),
1627 day_before(until, ANOMALY_DAYS - 1),
1628 ));
1629 }
1630
1631 let open = self
1632 .db
1633 .prepare("SELECT * FROM margin_alerts WHERE resolved_at IS NULL")
1634 .all()
1635 .await?
1636 .results::<AlertRow>()?;
1637 let now = now_ms();
1638 let stamp = rfc3339(now);
1639 let mut to_email: Vec<String> = Vec::new();
1640 let mut kept: BTreeSet<String> = BTreeSet::new();
1641 for (kind, subject, detail, from) in &conditions {
1642 match open.iter().find(|a| &a.kind == kind && &a.subject == subject) {
1643 Some(alert) => {
1644 kept.insert(alert.id.clone());
1645 self.db
1646 .prepare("UPDATE margin_alerts SET detail = ? WHERE id = ?")
1647 .bind(&[detail.as_str().into(), alert.id.as_str().into()])?
1648 .run()
1649 .await?;
1650 let stale = alert
1651 .emailed_at
1652 .as_deref()
1653 .and_then(g1t_contracts::time::parse_rfc3339)
1654 .is_none_or(|at| now.saturating_sub(at) >= REMIND_MS);
1655 if stale && kind != "workspace" {
1656 to_email.push(format!("Still open: {detail}"));
1657 kept.insert(format!("email:{}", alert.id));
1658 }
1659 }
1660 None => {
1661 let id = new_id("mal", now);
1662 self.db
1663 .prepare("INSERT INTO margin_alerts (id, kind, subject, detail, since, opened_at) VALUES (?, ?, ?, ?, ?, ?)")
1664 .bind(&[id.as_str().into(), kind.as_str().into(), subject.as_str().into(), detail.as_str().into(), from.as_str().into(), stamp.as_str().into()])?
1665 .run()
1666 .await?;
1667 kept.insert(id.clone());
1668 // A workspace's is for Reach out, not the inbox.
1669 if kind != "workspace" {
1670 to_email.push(detail.clone());
1671 }
1672 kept.insert(format!("email:{id}"));
1673 }
1674 }
1675 }
1676 for alert in &open {
1677 if !kept.contains(&alert.id) {
1678 self.db
1679 .prepare("UPDATE margin_alerts SET resolved_at = ? WHERE id = ?")
1680 .bind(&[stamp.as_str().into(), alert.id.as_str().into()])?
1681 .run()
1682 .await?;
1683 }
1684 }
1685 let to = env.var("COSTS_ALERT_EMAIL").map(|v| v.to_string()).unwrap_or_default();
1686 if !to_email.is_empty() && !to.trim().is_empty() {
1687 let subject = format!("[g1t costs] {} margin alert{}", to_email.len(), if to_email.len() == 1 { "" } else { "s" });
1688 match email_staff(env, to.trim(), &subject, &to_email).await {
1689 Ok(()) => {
1690 for marker in kept.iter().filter_map(|k| k.strip_prefix("email:")) {
1691 self.db
1692 .prepare("UPDATE margin_alerts SET emailed_at = ? WHERE id = ?")
1693 .bind(&[stamp.as_str().into(), marker.into()])?
1694 .run()
1695 .await?;
1696 }
1697 }
1698 Err(error) => worker::console_error!("could not email the margin alerts: {error}"),
1699 }
1700 }
1701 Ok(conditions.len() as u32)
1702 }
1703
1704 /// Workspaces costing g1t more than they pay over 30 days, not g1t's own.
1705 /// Each day's cost shared out to comped workspaces.
1706 async fn workspace_anomalies(&self, until: &str, settings: &CostSettings) -> Result<Vec<(String, i64, i64)>> {
1707 #[derive(Deserialize)]
1708 struct Row {
1709 workspace: String,
1710 cost: Option<i64>,
1711 revenue: Option<i64>,
1712 }
1713 let rows = self
1714 .db
1715 .prepare(format!(
1716 // Against what its usage was priced at, not the cash it
1717 // paid: a trial or a gift paying for usage is not a price
1718 // below cost.
1719 // Days from before value_micros was kept have none: only days
1720 // since the first one that does are compared.
1721 "SELECT workspace, SUM(cost_micros) AS cost, SUM(value_micros) AS revenue FROM workspace_costs
1722 WHERE day >= ?1 AND day <= ?2 AND workspace NOT IN ({})
1723 AND day >= (SELECT MIN(day) FROM workspace_costs WHERE value_micros > 0)
1724 GROUP BY workspace",
1725 crate::sales::INTERNAL_SQL
1726 ))
1727 .bind(&[day_before(until, ANOMALY_DAYS - 1).into(), until.into()])?
1728 .all()
1729 .await?
1730 .results::<Row>()?;
1731 let rows: Vec<(String, i64, i64)> = rows.into_iter().map(|r| (r.workspace, r.cost.unwrap_or(0), r.revenue.unwrap_or(0))).collect();
1732 Ok(anomalies(&rows, settings.anomaly_factor, settings.anomaly_floor_micros))
1733 }
1734
1735 /// For Reach out: workspaces with an open cost-over-revenue alert,
1736 /// each with its detail and cost.
1737 pub(crate) async fn costing_more_than_they_pay(&self) -> Result<Vec<(String, String, i64)>> {
1738 let alerts = self
1739 .db
1740 .prepare("SELECT * FROM margin_alerts WHERE resolved_at IS NULL AND kind = 'workspace' ORDER BY opened_at DESC LIMIT 50")
1741 .all()
1742 .await?
1743 .results::<AlertRow>()?;
1744 let mut out = Vec::new();
1745 for alert in alerts {
1746 #[derive(Deserialize)]
1747 struct Cost {
1748 cost: Option<i64>,
1749 }
1750 let cost = self
1751 .db
1752 .prepare("SELECT SUM(cost_micros) AS cost FROM workspace_costs WHERE workspace = ? AND day >= ?")
1753 .bind(&[alert.subject.as_str().into(), alert.since.as_str().into()])?
1754 .first::<Cost>(None)
1755 .await?
1756 .and_then(|c| c.cost)
1757 .unwrap_or(0);
1758 out.push((alert.subject, alert.detail, cost));
1759 }
1760 Ok(out)
1761 }
1762
1763 /// `admin_cost_alerts`: what sudo's banner says.
1764 pub(crate) async fn admin_cost_alerts(&self, _: AdminCostAlertsArgs) -> Result<Vec<MarginAlert>> {
1765 Ok(self
1766 .db
1767 .prepare("SELECT * FROM margin_alerts WHERE resolved_at IS NULL ORDER BY opened_at DESC LIMIT 50")
1768 .all()
1769 .await?
1770 .results::<AlertRow>()?
1771 .into_iter()
1772 .map(MarginAlert::from)
1773 .collect())
1774 }
1775
1776 /// `admin_run_costs`: the daily run, now.
1777 pub(crate) async fn admin_run_costs(&self, env: &Env, a: AdminRunCostsArgs) -> Result<Outcome<CostsRun>> {
1778 let keeper = crate::keeper::Keeper::from_env(env);
1779 let run = self.costs_daily(env, &keeper).await?;
1780 if !a.by.is_empty() {
1781 self.audit(
1782 "costs",
1783 "costs_run",
1784 &format!("{} lines, {} days, {} proposals, {} alerts", run.lines, run.days, run.proposals, run.alerts),
1785 &a.by,
1786 )
1787 .await?;
1788 }
1789 Ok(Outcome::Ok(run))
1790 }
1791
1792 /// `admin_set_cost_mapping`.
1793 pub(crate) async fn admin_set_cost_mapping(&self, a: AdminSetCostMappingArgs) -> Result<Outcome<CostMapping>> {
1794 let product = costs::slug(&a.product);
1795 let meter = if a.meter.trim() == "*" { "*".to_owned() } else { costs::slug(&a.meter) };
1796 if product.is_empty() || meter.is_empty() {
1797 return Ok(Outcome::fail(FailureCode::Invalid, "Name Cloudflare's product and a meter (or * for all of it)."));
1798 }
1799 let now = rfc3339(now_ms());
1800 if a.remove {
1801 self.db
1802 .prepare("DELETE FROM cost_map WHERE product = ? AND meter = ?")
1803 .bind(&[product.as_str().into(), meter.as_str().into()])?
1804 .run()
1805 .await?;
1806 self.audit("costs", "cost_mapping_removed", &format!("{product}/{meter}"), &a.by).await?;
1807 return Ok(Outcome::Ok(CostMapping {
1808 product,
1809 meter,
1810 bucket: String::new(),
1811 price_meter: None,
1812 own_meter: None,
1813 scale_to_own: false,
1814 drift_percent: 0.0,
1815 note: String::new(),
1816 updated_at: now,
1817 updated_by: a.by,
1818 }));
1819 }
1820 let bucket = costs::slug(&a.bucket);
1821 if bucket.is_empty() {
1822 return Ok(Outcome::fail(FailureCode::Invalid, "Say which of g1t's products it is a cost of."));
1823 }
1824 let clean = |v: Option<String>| v.map(|v| v.trim().to_owned()).filter(|v| !v.is_empty());
1825 let (price_meter, own_meter) = (clean(a.price_meter), clean(a.own_meter));
1826 let drift = a.drift_percent.filter(|d| d.is_finite() && *d > 0.0).unwrap_or(10.0);
1827 self.db
1828 .prepare(
1829 "INSERT INTO cost_map (product, meter, bucket, price_meter, own_meter, scale_to_own, drift_percent, note, updated_at, updated_by)
1830 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)
1831 ON CONFLICT (product, meter) DO UPDATE SET bucket = ?3, price_meter = ?4, own_meter = ?5, scale_to_own = ?6,
1832 drift_percent = ?7, note = ?8, updated_at = ?9, updated_by = ?10",
1833 )
1834 .bind(&[
1835 product.as_str().into(),
1836 meter.as_str().into(),
1837 bucket.as_str().into(),
1838 crate::optional(price_meter.as_deref()),
1839 crate::optional(own_meter.as_deref()),
1840 i32::from(a.scale_to_own).into(),
1841 drift.into(),
1842 a.note.trim().into(),
1843 now.as_str().into(),
1844 a.by.as_str().into(),
1845 ])?
1846 .run()
1847 .await?;
1848 self.audit("costs", "cost_mapping", &format!("{product}/{meter} → {bucket}"), &a.by).await?;
1849 Ok(Outcome::Ok(CostMapping {
1850 product,
1851 meter,
1852 bucket,
1853 price_meter,
1854 own_meter,
1855 scale_to_own: a.scale_to_own,
1856 drift_percent: drift,
1857 note: a.note.trim().to_owned(),
1858 updated_at: now,
1859 updated_by: a.by,
1860 }))
1861 }
1862
1863 /// The month starting `month_start` (YYYY-MM-01) so far, as rolled up:
1864 /// its figures and the last day they hold; None before its first day
1865 /// is rolled up.
1866 pub(crate) async fn month_margin(&self, month_start: &str) -> Result<Option<(OverallMargin, String)>> {
1867 let until = rfc3339(now_ms())[..10].to_owned();
1868 let days = self.margin_days(month_start, &until).await?;
1869 let Some(last) = days.iter().map(|d| d.day.clone()).max() else {
1870 return Ok(None);
1871 };
1872 Ok(Some((overall_of(&days), last)))
1873 }
1874
1875 /// `admin_costs`: the Costs & margin page.
1876 pub(crate) async fn admin_costs(&self, a: AdminCostsArgs, configured: bool) -> Result<CostsReport> {
1877 let until = rfc3339(now_ms())[..10].to_owned();
1878 let span = u64::from(a.days.unwrap_or(30).clamp(7, 90));
1879 let since = day_before(&until, span - 1);
1880 let days = self.margin_days(&since, &until).await?;
1881 let rules = self.rules().await?;
1882
1883 let mut products: BTreeMap<String, ProductMargin> = BTreeMap::new();
1884 for d in &days {
1885 let p = products.entry(d.bucket.clone()).or_insert_with(|| ProductMargin {
1886 bucket: d.bucket.clone(),
1887 title: costs::bucket_title(&d.bucket),
1888 cost_source: if NOT_CLOUDFLARE.contains(&d.bucket.as_str()) { "ledger" } else { "cloudflare" }.into(),
1889 overhead: OVERHEAD.contains(&d.bucket.as_str()),
1890 ..ProductMargin::default()
1891 });
1892 p.cf_cost_micros += d.cf_cost_micros;
1893 p.own_cost_micros += d.own_cost_micros;
1894 p.value_micros += d.value_micros;
1895 p.cost_micros += d.cost();
1896 }
1897 for p in products.values_mut() {
1898 p.margin_micros = p.value_micros - p.cost_micros;
1899 p.margin_percent = margin_percent(p.value_micros, p.cost_micros);
1900 }
1901 let mut overall = overall_of(&days);
1902 // The plan's included usage was paid for by the plan's price: it is
1903 // money in for the usage it covered, and out of what the plans
1904 // leave for running g1t.
1905 #[derive(Deserialize)]
1906 struct Included {
1907 micros: Option<i64>,
1908 }
1909 // Only what a plan paid for with real money (`without_real_money`).
1910 let live_since = self.payments_live_since().await?;
1911 overall.included_micros = match &live_since {
1912 None => 0,
1913 Some(live) => self
1914 .db
1915 .prepare(format!(
1916 "SELECT SUM(COALESCE(credit_micros, 0)) AS micros FROM ledger
1917 WHERE kind = 'usage' AND created_at >= ?1 AND created_at <= ?2 AND workspace NOT IN ({})",
1918 crate::sales::INTERNAL_SQL
1919 ))
1920 .bind(&[std::cmp::max(since.clone(), live.clone()).into(), format!("{until}T23:59:59.999Z").into()])?
1921 .first::<Included>(None)
1922 .await?
1923 .and_then(|r| r.micros)
1924 .unwrap_or(0),
1925 };
1926 let usage_in = overall.usage_micros + overall.included_micros;
1927 overall.usage_margin_micros = usage_in - overall.usage_cost_micros;
1928 overall.usage_margin_percent = margin_percent(usage_in, overall.usage_cost_micros);
1929 // Credits from g1t over the range: given, spent, and refunds' money
1930 // given back.
1931 overall.credits_given_micros = self
1932 .db
1933 .prepare("SELECT SUM(amount_micros) AS micros FROM credit_grants WHERE created_at >= ?1 AND created_at <= ?2")
1934 .bind(&[since.as_str().into(), format!("{until}T23:59:59.999Z").into()])?
1935 .first::<Included>(None)
1936 .await?
1937 .and_then(|r| r.micros)
1938 .unwrap_or(0);
1939 let (draws, refunds) = self.credit_effects(&since, &until).await?;
1940 overall.credits_used_micros = draws.iter().map(|(_, d)| d.micros).sum();
1941 overall.credits_refunded_micros = refunds.iter().map(|r| r.micros).sum();
1942 // Tax and card fees came in with payments but are neither cash nor
1943 // revenue: balances and plan payments are credited without them
1944 // (tax.rs), so cash above never holds them. Shown apart.
1945 (overall.tax_collected_micros, overall.card_fees_micros) = self.extras_between(&since, &until).await?;
1946 let mut products: Vec<ProductMargin> = products.into_values().collect();
1947 products.sort_by_key(|p| std::cmp::Reverse(p.cost_micros.max(p.value_micros)));
1948
1949 #[derive(Deserialize)]
1950 struct DriftRow {
1951 bucket: String,
1952 kind: String,
1953 ours: f64,
1954 cloudflare: f64,
1955 delta_percent: Option<f64>,
1956 detail: String,
1957 found_at: String,
1958 }
1959 let drift = self
1960 .db
1961 .prepare("SELECT * FROM cost_drift ORDER BY kind, bucket")
1962 .all()
1963 .await?
1964 .results::<DriftRow>()?
1965 .into_iter()
1966 .map(|r| CostDrift {
1967 title: costs::bucket_title(&r.bucket),
1968 bucket: r.bucket,
1969 kind: r.kind,
1970 ours: r.ours,
1971 cloudflare: r.cloudflare,
1972 delta_percent: r.delta_percent,
1973 detail: r.detail,
1974 found_at: r.found_at,
1975 })
1976 .collect();
1977
1978 #[derive(Deserialize)]
1979 struct Top {
1980 workspace: String,
1981 cost: Option<i64>,
1982 revenue: Option<i64>,
1983 given: Option<i64>,
1984 internal: i64,
1985 }
1986 let top_workspaces = self
1987 .db
1988 .prepare(format!(
1989 "SELECT workspace, SUM(cost_micros) AS cost, SUM(revenue_micros) AS revenue, SUM(given_micros) AS given,
1990 CASE WHEN workspace IN ({}) THEN 1 ELSE 0 END AS internal
1991 FROM workspace_costs WHERE day >= ?1 AND day <= ?2 GROUP BY workspace ORDER BY cost DESC LIMIT 15",
1992 crate::sales::INTERNAL_SQL
1993 ))
1994 .bind(&[since.as_str().into(), until.as_str().into()])?
1995 .all()
1996 .await?
1997 .results::<Top>()?
1998 .into_iter()
1999 .map(|t| WorkspaceCost { workspace: t.workspace, cost_micros: t.cost.unwrap_or(0), revenue_micros: t.revenue.unwrap_or(0), given_micros: t.given.unwrap_or(0), internal: t.internal == 1 })
2000 .collect();
2001
2002 #[derive(Deserialize)]
2003 struct Summary {
2004 source: String,
2005 product: String,
2006 meter: String,
2007 raw_name: String,
2008 unit: String,
2009 quantity: f64,
2010 cost_usd: f64,
2011 }
2012 let lines = self
2013 .db
2014 .prepare(
2015 "SELECT source, product, meter, MAX(raw_name) AS raw_name, MAX(unit) AS unit, SUM(quantity) AS quantity, SUM(cost_usd) AS cost_usd
2016 FROM cost_lines WHERE day >= ?1 AND day <= ?2 GROUP BY source, product, meter ORDER BY cost_usd DESC, product, meter LIMIT 200",
2017 )
2018 .bind(&[since.as_str().into(), until.as_str().into()])?
2019 .all()
2020 .await?
2021 .results::<Summary>()?
2022 .into_iter()
2023 .map(|l| CostLineSummary {
2024 bucket: costs::classify(&rules, &l.product, &l.meter).map(|r| r.bucket.clone()),
2025 product: l.product,
2026 meter: l.meter,
2027 raw_name: l.raw_name,
2028 unit: l.unit,
2029 source: l.source,
2030 quantity: l.quantity,
2031 cost_micros: micros(l.cost_usd),
2032 })
2033 .collect();
2034
2035 #[derive(Deserialize)]
2036 struct MapRow {
2037 product: String,
2038 meter: String,
2039 bucket: String,
2040 price_meter: Option<String>,
2041 own_meter: Option<String>,
2042 scale_to_own: i64,
2043 drift_percent: f64,
2044 note: String,
2045 updated_at: String,
2046 updated_by: String,
2047 }
2048 let mappings = self
2049 .db
2050 .prepare("SELECT * FROM cost_map ORDER BY product, meter")
2051 .all()
2052 .await?
2053 .results::<MapRow>()?
2054 .into_iter()
2055 .map(|m| CostMapping {
2056 product: m.product,
2057 meter: m.meter,
2058 bucket: m.bucket,
2059 price_meter: m.price_meter,
2060 own_meter: m.own_meter,
2061 scale_to_own: m.scale_to_own == 1,
2062 drift_percent: m.drift_percent,
2063 note: m.note,
2064 updated_at: m.updated_at,
2065 updated_by: m.updated_by,
2066 })
2067 .collect();
2068
2069 #[derive(Deserialize)]
2070 struct Fetched {
2071 at: Option<String>,
2072 }
2073 let fetched_at = self.db.prepare("SELECT MAX(fetched_at) AS at FROM cost_lines").first::<Fetched>(None).await?.and_then(|f| f.at);
2074
2075 // Cloudflare's subscriptions over the range, day by day as each
2076 // day's share of its billing cycle; and the cycle itself.
2077 let anchor = self.cycle_anchor().await?;
2078 let fixed = self.fixed_monthly(self.caps.fixed_monthly).await?;
2079 overall.subscriptions_micros = crate::cycle::accrued(fixed.monthly_micros, &since, &until, anchor);
2080 let cycle = self.cloudflare_cycle(&until, anchor, fixed.monthly_micros).await?;
2081 let bill_read = self.bill_read().await?;
2082 // What AI Gateway priced g1t's own provider traffic at, beside the
2083 // ledger's model cost (Cloudflare-billed requests are Cloudflare's).
2084 overall.gateway_cost_micros = self
2085 .db
2086 .prepare("SELECT SUM(cost_usd) AS cost FROM cost_lines WHERE source = ?1 AND day >= ?2 AND day <= ?3 AND substr(meter, 1, 11) <> ?4")
2087 .bind(&[costs::SOURCE_GATEWAY.into(), since.as_str().into(), until.as_str().into(), costs::GATEWAY_WHOLESALE.into()])?
2088 .first::<CostSum>(None)
2089 .await?
2090 .and_then(|c| c.cost)
2091 .map_or(0, micros);
2092 // The workspaces' shares of the cost, and what no one's usage carried.
2093 #[derive(Deserialize)]
2094 struct Shared {
2095 micros: Option<i64>,
2096 }
2097 let shared = self
2098 .db
2099 .prepare("SELECT SUM(cost_micros) AS micros FROM workspace_costs WHERE day >= ?1 AND day <= ?2")
2100 .bind(&[since.as_str().into(), until.as_str().into()])?
2101 .first::<Shared>(None)
2102 .await?
2103 .and_then(|s| s.micros)
2104 .unwrap_or(0);
2105 let unattributed_micros = unattributed(overall.cost_micros, shared);
2106
2107 Ok(CostsReport {
2108 configured,
2109 fetched_at,
2110 days: days
2111 .iter()
2112 .map(|d| CostDay {
2113 day: d.day.clone(),
2114 bucket: d.bucket.clone(),
2115 cf_cost_micros: d.cf_cost_micros,
2116 own_cost_micros: d.own_cost_micros,
2117 value_micros: d.value_micros,
2118 cash_micros: d.cash_micros,
2119 })
2120 .collect(),
2121 since,
2122 until,
2123 products,
2124 overall,
2125 drift,
2126 alerts: self.admin_cost_alerts(AdminCostAlertsArgs {}).await?,
2127 proposals: self.proposals().await?,
2128 versions: self.versions().await?,
2129 top_workspaces,
2130 lines,
2131 mappings,
2132 settings: self.cost_settings().await?,
2133 caps: self.spend_caps().await?,
2134 cycle,
2135 bill_read,
2136 unattributed_micros,
2137 })
2138 }
2139
2140 /// Cloudflare's billing cycle that `today` is in: its usage cost so far,
2141 /// by meter, with the included amounts, and where it is heading. None
2142 /// while nothing of it has been read.
2143 async fn cloudflare_cycle(&self, today: &str, anchor: u32, monthly_micros: i64) -> Result<Option<CloudflareCycle>> {
2144 #[derive(Deserialize)]
2145 struct Row {
2146 product: String,
2147 meter: String,
2148 raw_name: String,
2149 unit: String,
2150 quantity: f64,
2151 billable_quantity: Option<f64>,
2152 cost_usd: f64,
2153 basis: Option<String>,
2154 }
2155 let cycle = crate::cycle::cycle_of(today, anchor);
2156 let rows = self
2157 .db
2158 .prepare(
2159 "SELECT product, meter, MAX(raw_name) AS raw_name, MAX(unit) AS unit, SUM(quantity) AS quantity,
2160 SUM(billable_quantity) AS billable_quantity, SUM(cost_usd) AS cost_usd, MAX(basis) AS basis
2161 FROM cost_lines WHERE source = ?1 AND day >= ?2 AND day <= ?3 GROUP BY product, meter",
2162 )
2163 .bind(&[SOURCE_BILLABLE.into(), cycle.start.as_str().into(), today.into()])?
2164 .all()
2165 .await?
2166 .results::<Row>()?;
2167 if rows.is_empty() {
2168 return Ok(None);
2169 }
2170 let elapsed = cycle.days_elapsed(today);
2171 let meters = rows
2172 .into_iter()
2173 .map(|r| {
2174 let list = crate::cycle::list_price(&r.product, &r.meter);
2175 CycleMeter {
2176 included: list.map(|p| if p.daily { p.included * elapsed as f64 } else { p.included }),
2177 billable_quantity: r.billable_quantity.unwrap_or(0.0),
2178 cost_micros: micros(r.cost_usd),
2179 basis: r.basis.filter(|b| !b.is_empty()).unwrap_or_else(|| crate::cycle::BASIS_NONE.to_owned()),
2180 product: r.product,
2181 meter: r.meter,
2182 raw_name: r.raw_name,
2183 unit: r.unit,
2184 quantity: r.quantity,
2185 }
2186 })
2187 .collect();
2188 Ok(Some(cycle_report(&cycle, elapsed, meters, monthly_micros)))
2189 }
2190
2191 /// The last read of billable usage (`cost_reads`).
2192 async fn bill_read(&self) -> Result<Option<BillRead>> {
2193 #[derive(Deserialize)]
2194 struct Row {
2195 read_at: String,
2196 since: String,
2197 until: String,
2198 rows: u32,
2199 pages: u32,
2200 consumed_rows: u32,
2201 pricing_only_rows: u32,
2202 costed_rows: u32,
2203 }
2204 Ok(self
2205 .db
2206 .prepare("SELECT * FROM cost_reads WHERE source = ?1")
2207 .bind(&[SOURCE_BILLABLE.into()])?
2208 .first::<Row>(None)
2209 .await?
2210 .map(|r| BillRead {
2211 read_at: r.read_at,
2212 since: r.since,
2213 until: r.until,
2214 rows: r.rows,
2215 pages: r.pages,
2216 consumed_rows: r.consumed_rows,
2217 pricing_only_rows: r.pricing_only_rows,
2218 costed_rows: r.costed_rows,
2219 }))
2220 }
2221}
2222
2223#[derive(Deserialize)]
2224struct CostSum {
2225 cost: Option<f64>,
2226}
2227
2228/// Of `total` cost, what the workspaces' shares did not carry: running g1t
2229/// on days no workspace used anything. Never below zero.
2230pub(crate) fn unattributed(total: i64, shared: i64) -> i64 {
2231 (total - shared).max(0)
2232}
2233
2234/// The cycle as sudo shows it: the meters, most costly first, their total,
2235/// the average day and Cloudflare's projection.
2236pub(crate) fn cycle_report(cycle: &crate::cycle::Cycle, elapsed: u32, mut meters: Vec<CycleMeter>, monthly_micros: i64) -> CloudflareCycle {
2237 meters.sort_by(|a, b| b.cost_micros.cmp(&a.cost_micros).then(b.quantity.total_cmp(&a.quantity)).then(a.meter.cmp(&b.meter)));
2238 let usage: i64 = meters.iter().map(|m| m.cost_micros).sum();
2239 let days = cycle.days();
2240 CloudflareCycle {
2241 start: cycle.start.clone(),
2242 end: cycle.end.clone(),
2243 days,
2244 days_elapsed: elapsed,
2245 usage_micros: usage,
2246 projected_micros: crate::cycle::project(usage, elapsed, days),
2247 average_daily_micros: if elapsed > 0 { usage / elapsed as i64 } else { usage },
2248 subscriptions_micros: monthly_micros,
2249 meters,
2250 }
2251}
2252
2253#[cfg(test)]
2254mod tests {
2255 use super::*;
2256
2257 #[test]
2258 fn usage_nothing_paid_for_is_valued_at_price_and_paid_usage_at_what_was_paid() {
2259 // A free period: charged nothing, drawn from nothing.
2260 assert_eq!(usage_value(false, 1_000_000, 0, 20), 1_200_000);
2261 // Charged, or drawn from a trial: what was paid.
2262 assert_eq!(usage_value(false, 1_000_000, 1_200_000, 20), 1_200_000);
2263 assert_eq!(usage_value(false, 1_000_000, 900_000, 20), 900_000);
2264 // g1t's own: at price.
2265 assert_eq!(usage_value(true, 1_000_000, 0, 20), 1_200_000);
2266 // No cost, nothing paid: nothing.
2267 assert_eq!(usage_value(false, 0, 0, 20), 0);
2268 }
2269
2270 #[test]
2271 fn the_overall_alert_says_dollars_while_little_comes_in() {
2272 let small = overall_detail(90_000, 7_500_000, 3, 10.0, -8239.7);
2273 assert!(small.contains("took in $0.09 against $7.50"), "{small}");
2274 assert!(!small.contains('%'), "{small}");
2275 let real = overall_detail(30_000_000, 40_000_000, 3, 10.0, -33.3);
2276 assert!(real.contains("as low as -33.3%"), "{real}");
2277 }
2278
2279 fn rule(product: &str, meter: &str, bucket: &str, own: Option<&str>) -> Rule {
2280 Rule { product: product.into(), meter: meter.into(), bucket: bucket.into(), price_meter: None, own_meter: own.map(Into::into), drift_percent: 10.0 }
2281 }
2282
2283 fn rules() -> Vec<Rule> {
2284 vec![
2285 rule("containers", "*", "sandboxes", None),
2286 rule("workers", "*", "platform", None),
2287 rule("artifacts", "*", "git", Some("git_operations")),
2288 rule("artifacts", "events_", "git", Some("git_operations")),
2289 ]
2290 }
2291
2292 fn revenue_map() -> BTreeMap<String, String> {
2293 [("sandbox", "sandboxes"), ("git", "git"), ("plan", "platform")].iter().map(|(k, v)| (k.to_string(), v.to_string())).collect()
2294 }
2295
2296 fn line(day: &str, source: &str, product: &str, meter: &str, quantity: f64, cost: f64) -> LineRow {
2297 LineRow { day: day.into(), source: source.into(), product: product.into(), meter: meter.into(), quantity, cost_usd: cost }
2298 }
2299
2300 fn usage(day: &str, workspace: &str, key: &str, value: i64, cash: i64, cost: i64) -> UsageRow {
2301 UsageRow { day: day.into(), workspace: workspace.into(), key: key.into(), bucket: None, value, cash, cost, given: Given::default() }
2302 }
2303
2304 #[test]
2305 fn a_day_puts_the_bill_g1ts_counts_and_charges_side_by_side() {
2306 let lines = vec![
2307 line("2026-10-15", SOURCE_BILLABLE, "containers", "container_memory", 1000.0, 2.00),
2308 line("2026-10-15", SOURCE_BILLABLE, "artifacts", "artifacts_operations", 30_000.0, 3.00),
2309 // Artifacts' own events: not used while the bill has a count.
2310 line("2026-10-15", SOURCE_ARTIFACTS, "artifacts", "events_pull", 29_000.0, 0.0),
2311 line("2026-10-15", SOURCE_BILLABLE, "workers", "workers_cpu_ms", 1.0, 0.50),
2312 line("2026-10-15", SOURCE_BILLABLE, "browser_rendering", "browser_hours", 2.0, 0.25),
2313 ];
2314 let own = vec![
2315 OwnRow { day: "2026-10-15".into(), meter: "git_operations".into(), workspace: "acme".into(), quantity: 7_500.0 },
2316 OwnRow { day: "2026-10-15".into(), meter: "git_operations".into(), workspace: "beta".into(), quantity: 2_500.0 },
2317 ];
2318 let usage = vec![
2319 usage("2026-10-15", "acme", "sandbox", 2_400_000, 1_000_000, 2_000_000),
2320 usage("2026-10-15", "beta", "sandbox", 1_200_000, 1_200_000, 1_000_000),
2321 usage("2026-10-15", "acme", "git", 600_000, 600_000, 500_000),
2322 usage("2026-10-15", "acme", "implement", 120_000, 120_000, 100_000),
2323 usage("2026-10-15", "beta", "plan", 20_000_000, 20_000_000, 0),
2324 ];
2325 let (days, workspaces) = fold(&rules(), &revenue_map(), &lines, &own, &usage, &BTreeSet::new());
2326 let get = |bucket: &str| days.iter().find(|d| d.bucket == bucket).unwrap();
2327 let sandboxes = get("sandboxes");
2328 assert_eq!((sandboxes.cf_cost_micros, sandboxes.own_cost_micros, sandboxes.value_micros, sandboxes.cash_micros), (2_000_000, 3_000_000, 3_600_000, 2_200_000));
2329 let git = get("git");
2330 assert_eq!(git.cf_cost_micros, 3_000_000);
2331 assert_eq!((git.cf_quantity, git.own_quantity), (30_000.0, 10_000.0));
2332 assert_eq!(get("platform").value_micros, 20_000_000);
2333 // Not mapped: a leak until someone maps it.
2334 assert_eq!(get(UNMAPPED).cf_cost_micros, 250_000);
2335 // Models: no Cloudflare line, their cost is g1t's own.
2336 assert_eq!(get("models").cost(), 100_000);
2337 // Git's cost shared by g1t's own counts (Cloudflare gave none per
2338 // workspace here): three quarters to acme.
2339 let share = |ws: &str, bucket: &str| workspaces.iter().find(|w| w.workspace == ws && w.bucket == bucket).map(|w| (w.cost, w.revenue));
2340 assert_eq!(share("acme", "git"), Some((2_250_000, 600_000)));
2341 assert_eq!(share("beta", "git"), Some((750_000, 0)));
2342 // Every bucket's cost is shared out exactly.
2343 for d in &days {
2344 let shared: i64 = workspaces.iter().filter(|w| w.bucket == d.bucket).map(|w| w.cost).sum();
2345 assert_eq!(shared, d.cost(), "{}", d.bucket);
2346 }
2347 }
2348
2349 #[test]
2350 fn a_planning_run_is_model_cost_not_running_g1t() {
2351 // flagon-io's planning run on 2026-10-07 cost $0.0748 of model
2352 // calls; read under the ledger's task, `plan`, it went to running
2353 // g1t, where Cloudflare's bill is the cost, and the model cost was
2354 // lost from the statement and the drift.
2355 let usage = vec![
2356 usage("2026-10-07", "flagon-io", PLANNING_KEY, 89_741, 0, 74_784),
2357 usage("2026-10-07", "acme", "plan", 666_666, 666_666, 0),
2358 ];
2359 let (days, _) = fold(&rules(), &revenue_map(), &[], &[], &usage, &BTreeSet::new());
2360 let get = |bucket: &str| days.iter().find(|d| d.bucket == bucket).unwrap();
2361 assert_eq!((get("models").cost(), get("models").value_micros), (74_784, 89_741));
2362 assert_eq!((get("platform").own_cost_micros, get("platform").cash_micros), (0, 666_666));
2363 assert!(!revenue_map().contains_key(PLANNING_KEY));
2364 }
2365
2366 #[test]
2367 fn a_comped_workspaces_month_end_meters_are_given_never_money_in() {
2368 let snap = |day: &str, cost: i64, charge: i64| (day.to_string(), "flagon-io".to_string(), "cache".to_string(), cost, charge);
2369 let rows: Vec<UsageRow> = pending_deltas(&[snap("2026-10-07", 1, 2), snap("2026-10-08", 473, 568)]).into_iter().map(comped_meter).collect();
2370 assert_eq!(rows.iter().map(|r| (r.cash, r.value, r.given.comped)).collect::<Vec<_>>(), vec![(0, 2, 2), (0, 566, 566)]);
2371 let internal: BTreeSet<String> = ["flagon-io".to_string()].into();
2372 let (days, workspaces) = fold(&rules(), &revenue_map(), &[], &[], &rows, &internal);
2373 assert!(days.iter().all(|d| d.cash_micros == 0));
2374 assert!(workspaces.iter().all(|w| w.revenue == 0));
2375 }
2376
2377 #[test]
2378 fn artifacts_events_count_when_the_bill_does_not() {
2379 let lines = vec![
2380 line("2026-10-05", SOURCE_ARTIFACTS, "artifacts", "events_pull", 120.0, 0.0),
2381 line("2026-10-05", SOURCE_ARTIFACTS, "artifacts", "events_push", 30.0, 0.0),
2382 line("2026-10-05", SOURCE_ARTIFACTS, "artifacts", "events_ratelimited", 9.0, 0.0),
2383 ];
2384 let (days, _) = fold(&rules(), &revenue_map(), &lines, &[], &[], &BTreeSet::new());
2385 assert_eq!(days[0].cf_quantity, 150.0);
2386 assert_eq!(days[0].cf_cost_micros, 0);
2387 }
2388
2389 #[test]
2390 fn month_end_meters_are_told_by_the_day_from_snapshots() {
2391 let snap = |day: &str, cost: i64, charge: i64| (day.to_string(), "acme".to_string(), "git".to_string(), cost, charge);
2392 let rows = pending_deltas(&[snap("2026-10-30", 100, 120), snap("2026-10-31", 250, 300), snap("2026-11-01", 40, 48), snap("2026-11-02", 40, 48)]);
2393 assert_eq!(
2394 rows.iter().map(|r| (r.day.as_str(), r.cost, r.value)).collect::<Vec<_>>(),
2395 vec![("2026-10-30", 100, 120), ("2026-10-31", 150, 180), ("2026-11-01", 40, 48)]
2396 );
2397 }
2398
2399 #[test]
2400 fn a_plan_payment_is_spread_over_the_month_it_pays_for() {
2401 let days = spread("2026-10-01T00:00:00.000Z", 20_000_000, 30);
2402 assert_eq!(days.len(), 30);
2403 assert_eq!(days[0], ("2026-10-01".to_string(), 666_667));
2404 assert_eq!(days[29], ("2026-10-30".to_string(), 666_666));
2405 assert_eq!(days.iter().map(|d| d.1).sum::<i64>(), 20_000_000);
2406 assert!(spread("2026-10-01", 0, 30).is_empty());
2407 assert_eq!(dollars(17_024_000), "$17.02");
2408 assert_eq!(dollars(-27_668_620), "-$27.67");
2409 assert_eq!(dollars(63_000), "$0.063");
2410 }
2411
2412 #[test]
2413 fn margins_and_deltas() {
2414 assert_eq!(margin_percent(1_200_000, 1_000_000).map(|m| (m * 100.0).round() / 100.0), Some(16.67));
2415 assert_eq!(margin_percent(0, 5), None);
2416 assert_eq!(delta_percent(110.0, 100.0), Some(10.0));
2417 assert_eq!(delta_percent(1.0, 0.0), None);
2418 }
2419
2420 fn day(bucket: &str, cf: i64, own: i64, value: i64, cfq: f64, ownq: f64) -> ProductDay {
2421 ProductDay { day: "2026-10-15".into(), bucket: bucket.into(), cf_cost_micros: cf, own_cost_micros: own, value_micros: value, cash_micros: value, cf_quantity: cfq, own_quantity: ownq, given: Given::default() }
2422 }
2423
2424 #[test]
2425 fn counts_more_than_the_threshold_apart_are_drift() {
2426 // Cloudflare counted 30,000 operations where g1t counted 10,000:
2427 // binding reads, perhaps. -66.7%.
2428 let drift = drifts("git", &[day("git", 3_000_000, 1_500_000, 1_800_000, 30_000.0, 10_000.0)], 10.0, true, 100_000, Some(3_000_000.0));
2429 assert_eq!(drift.iter().map(|d| d.kind).collect::<Vec<_>>(), vec![DriftKind::Count, DriftKind::Cost]);
2430 assert!((drift[0].delta_percent.unwrap() + 66.666).abs() < 0.01);
2431 // 9% apart: within 10%.
2432 assert!(drifts("git", &[day("git", 1_000_000, 1_000_000, 1_200_000, 10_000.0, 10_900.0)], 10.0, true, 100_000, Some(1_000_000.0)).is_empty());
2433 // Uncounted products have no count drift.
2434 assert!(drifts("sandboxes", &[day("sandboxes", 1_000_000, 1_050_000, 1_200_000, 5.0, 0.0)], 10.0, false, 100_000, Some(1_000_000.0)).is_empty());
2435 }
2436
2437 #[test]
2438 fn cost_with_no_revenue_is_a_leak_but_not_for_running_g1t() {
2439 let leak = drifts("actions_cache", &[day("actions_cache", 400_000, 0, 0, 0.0, 0.0)], 10.0, false, 100_000, None);
2440 assert_eq!(leak.len(), 1);
2441 assert_eq!(leak[0].kind, DriftKind::Leak);
2442 assert!(drifts("platform", &[day("platform", 5_000_000, 0, 0, 0.0, 0.0)], 10.0, false, 100_000, None).is_empty());
2443 // Pennies say nothing.
2444 assert!(drifts("actions_cache", &[day("actions_cache", 50_000, 0, 0, 0.0, 0.0)], 10.0, false, 100_000, None).is_empty());
2445 assert!(drifts(UNMAPPED, &[day(UNMAPPED, 250_000, 0, 0, 0.0, 0.0)], 10.0, false, 100_000, None)[0].kind == DriftKind::Leak);
2446 }
2447
2448 /// A week of sandboxes mostly inside the cycle's included amounts:
2449 /// Cloudflare billed $0.0922 (the memory past 25 GiB-hours), and the
2450 /// same usage is $1.70 at list prices.
2451 fn sandbox_week() -> (Vec<Rule>, Vec<LineRow>) {
2452 let mut rules = rules();
2453 rules.push(rule("durable_objects", "durable_objects_compute_duration", "sandboxes", None));
2454 let lines = vec![
2455 line("2026-10-07", SOURCE_BILLABLE, "containers", "container_memory_per_gib_second", 126_870.0, 0.092_175),
2456 line("2026-10-07", SOURCE_BILLABLE, "containers", "container_vcpu", 12_000.0, 0.0),
2457 line("2026-10-07", SOURCE_BILLABLE, "containers", "container_disk_per_gb_second", 253_740.0, 0.0),
2458 line("2026-10-07", SOURCE_BILLABLE, "durable_objects", "durable_objects_compute_duration", 90_000.0, 0.0),
2459 // Not billable usage: no part of the list cost.
2460 line("2026-10-07", SOURCE_ARTIFACTS, "artifacts", "events_push", 40.0, 0.0),
2461 ];
2462 (rules, lines)
2463 }
2464
2465 #[test]
2466 fn usage_inside_the_included_amounts_is_not_a_stale_price() {
2467 let (rules, lines) = sandbox_week();
2468 let list = list_costs(&rules, &lines);
2469 let sandboxes = list["sandboxes"].unwrap();
2470 assert!((sandboxes - 1_699_936.8).abs() < 1.0, "{sandboxes}");
2471 // The price book's cost of the same usage is $1.69: within 1% of
2472 // the list, though Cloudflare billed $0.0922 after the included
2473 // amounts. Before, that read as +1733.6%.
2474 let week = day("sandboxes", 92_175, 1_690_000, 2_028_000, 0.0, 0.0);
2475 assert!(drifts("sandboxes", std::slice::from_ref(&week), 10.0, false, 100_000, Some(sandboxes)).is_empty());
2476 let net = drifts("sandboxes", &[week], 10.0, false, 100_000, Some(92_175.0));
2477 assert_eq!(net[0].kind, DriftKind::Cost, "billed against the price book was the false alarm");
2478 }
2479
2480 #[test]
2481 fn a_stale_price_is_still_drift() {
2482 let (rules, lines) = sandbox_week();
2483 let sandboxes = list_costs(&rules, &lines)["sandboxes"].unwrap();
2484 // The price book still costs the same usage at $1.20: a list price
2485 // rose and the book did not follow.
2486 let found = drifts("sandboxes", &[day("sandboxes", 92_175, 1_200_000, 1_440_000, 0.0, 0.0)], 10.0, false, 100_000, Some(sandboxes));
2487 assert_eq!(found.len(), 1);
2488 assert_eq!((found[0].kind, found[0].ours, found[0].cloudflare.round()), (DriftKind::Cost, 1_200_000.0, 1_699_937.0));
2489 assert!((found[0].delta_percent.unwrap() + 29.4).abs() < 0.1);
2490 // Nothing billed at all (the whole week inside the included
2491 // amounts) is checked all the same.
2492 assert_eq!(drifts("sandboxes", &[day("sandboxes", 0, 1_200_000, 1_440_000, 0.0, 0.0)], 10.0, false, 100_000, Some(sandboxes)).len(), 1);
2493 }
2494
2495 #[test]
2496 fn a_bucket_with_a_meter_with_no_list_price_is_not_checked() {
2497 let (rules, mut lines) = sandbox_week();
2498 lines.push(line("2026-10-07", SOURCE_BILLABLE, "artifacts", "storage", 3.0, 0.4));
2499 lines.push(line("2026-10-07", SOURCE_BILLABLE, "artifacts", "operations", 0.0, 0.0));
2500 let list = list_costs(&rules, &lines);
2501 assert_eq!(list["git"], None);
2502 assert!(list["sandboxes"].is_some());
2503 // No list cost: no cost drift, but a leak is still what was billed.
2504 assert!(drifts("git", &[day("git", 3_000_000, 1_000_000, 1_200_000, 0.0, 0.0)], 10.0, false, 100_000, None).is_empty());
2505 assert_eq!(drifts("git", &[day("git", 3_000_000, 0, 0, 0.0, 0.0)], 10.0, false, 100_000, None)[0].kind, DriftKind::Leak);
2506 }
2507
2508 #[test]
2509 fn a_unit_rate_is_the_list_price_where_there_is_one() {
2510 // Billed $0.20 for 11.16M CPU ms (9.16M past the included 30M, in
2511 // whole millions): $0.02 a million at list, whatever was billed.
2512 let (q, c) = rate_line("workers", "workers_cpu_ms", 11_160_000.0, 0.20);
2513 assert!((billed_rate(&[(q, c)]).unwrap() * 1e6 - 0.02).abs() < 1e-12);
2514 // No list price: what Cloudflare billed.
2515 assert_eq!(rate_line("artifacts", "operations", 1_000.0, 0.5), (1_000.0, 0.5));
2516 }
2517
2518 #[test]
2519 fn a_margin_alert_needs_n_days_in_a_row_under_the_floor() {
2520 let s = |d: &str, revenue: i64, cost: i64| (d.to_string(), revenue, cost);
2521 // 5%, 0%, -20%: three days under 10%.
2522 let series = vec![s("10-13", 1_200_000, 1_000_000), s("10-14", 1_050_000, 1_000_000), s("10-15", 1_000_000, 1_000_000), s("10-16", 1_000_000, 1_200_000)];
2523 let (from, worst) = breach(&series, 10.0, 3, 100_000).unwrap();
2524 assert_eq!(from, "10-14");
2525 assert!((worst + 20.0).abs() < 1e-9);
2526 // A good day in the window clears it.
2527 let mended = vec![s("10-14", 1_050_000, 1_000_000), s("10-15", 1_300_000, 1_000_000), s("10-16", 1_000_000, 1_200_000)];
2528 assert!(breach(&mended, 10.0, 3, 100_000).is_none());
2529 // Cost with no revenue at all is the worst margin there is.
2530 assert_eq!(breach(&[s("10-16", 0, 500_000)], 10.0, 1, 100_000).unwrap().1, -100.0);
2531 // Too little cost to judge.
2532 assert!(breach(&[s("10-16", 0, 5_000)], 10.0, 1, 100_000).is_none());
2533 assert!(breach(&series, 10.0, 9, 100_000).is_none());
2534 }
2535
2536 #[test]
2537 fn shared_costs_add_up_to_the_bill() {
2538 let w = |k: &str, v: f64| (k.to_string(), v);
2539 assert_eq!(attribute(100, &[w("a", 1.0), w("b", 1.0), w("c", 1.0)]), vec![("a".into(), 34), ("b".into(), 33), ("c".into(), 33)]);
2540 assert_eq!(attribute(10, &[w("a", 3.0), w("b", 1.0), w("a", 0.0)]), vec![("a".into(), 8), ("b".into(), 2)]);
2541 assert!(attribute(10, &[w("a", 0.0)]).is_empty());
2542 assert!(attribute(0, &[w("a", 1.0)]).is_empty());
2543 }
2544
2545 #[test]
2546 fn counts_are_compared_from_the_day_g1t_started_counting() {
2547 let on = |day: &str, cf: f64, own: f64| ProductDay { day: day.into(), bucket: "git".into(), cf_quantity: cf, own_quantity: own, ..ProductDay::default() };
2548 // Five days of Cloudflare's count before g1t's meter, then two that match.
2549 let days = vec![on("2026-10-01", 500.0, 0.0), on("2026-10-05", 300.0, 0.0), on("2026-10-06", 210.0, 231.0), on("2026-10-07", 450.0, 458.0)];
2550 assert!(drifts("git", &days, 10.0, true, 0, None).iter().all(|d| d.kind != DriftKind::Count));
2551 // A real gap on the days both counted still shows.
2552 let days = vec![on("2026-10-01", 500.0, 0.0), on("2026-10-06", 400.0, 231.0), on("2026-10-07", 600.0, 300.0)];
2553 let found = drifts("git", &days, 10.0, true, 0, None);
2554 let count = found.iter().find(|d| d.kind == DriftKind::Count).unwrap();
2555 assert_eq!((count.ours, count.cloudflare), (531.0, 1000.0));
2556 // A meter that never counted is compared over every day.
2557 let days = vec![on("2026-10-06", 400.0, 0.0)];
2558 assert!(drifts("git", &days, 10.0, true, 0, None).iter().any(|d| d.kind == DriftKind::Count));
2559 }
2560
2561 #[test]
2562 fn what_g1t_gives_away_is_kept_apart_from_what_it_sells() {
2563 let map = BTreeMap::new();
2564 // A comped workspace (all of it given), one in its trial (half paid
2565 // by the trial) and one paying in cash, all on models.
2566 let comped = usage("2026-10-15", "flagon", "agent", 1_200_000, 0, 1_000_000);
2567 let mut trial = usage("2026-10-15", "acme", "agent", 1_200_000, 600_000, 1_000_000);
2568 trial.given = Given { trial: 600_000, ..Given::default() };
2569 let paying = usage("2026-10-15", "beta", "agent", 1_200_000, 1_200_000, 1_000_000);
2570 // Nothing priced that day: free use.
2571 let free = usage("2026-10-15", "gamma", "agent", 0, 0, 1_000_000);
2572 let internal = BTreeSet::from(["flagon".to_string()]);
2573 let (days, workspaces) = fold(&[], &map, &[], &[], &[comped, trial, paying, free], &internal);
2574 let models = days.iter().find(|d| d.bucket == "models").unwrap();
2575 assert_eq!(models.cost(), 4_000_000);
2576 assert_eq!(models.given, Given { comped: 1_000_000, free: 1_000_000, trial: 500_000, ..Given::default() });
2577 let given = |w: &str| workspaces.iter().find(|x| x.workspace == w).unwrap().given.total();
2578 assert_eq!((given("flagon"), given("acme"), given("beta"), given("gamma")), (1_000_000, 500_000, 0, 1_000_000));
2579 }
2580
2581 #[test]
2582 fn a_discounted_sale_keeps_its_margin_and_counts_the_discount_as_given() {
2583 // $1 of model cost at 20%, sold to an account with 30% off: charged
2584 // $0.84, and $0.36 below cost plus the margin given (as usage_rows
2585 // reads the ledger: value at price, the discount part given).
2586 let mut sale = usage("2026-10-15", "acme", "agent", 1_200_000, 840_000, 1_000_000);
2587 sale.given = Given { discount: 360_000, ..Given::default() };
2588 let (days, _) = fold(&[], &BTreeMap::new(), &[], &[], &[sale], &BTreeSet::new());
2589 let models = days.iter().find(|d| d.bucket == "models").unwrap();
2590 assert_eq!(models.value_micros, 1_200_000);
2591 assert_eq!(models.given, Given { discount: 300_000, ..Given::default() });
2592 // What was sold (cost less given) still makes the margin.
2593 let sold = models.cost() - models.given.total();
2594 assert_eq!(margin_percent(models.cash_micros, sold).map(|m| m.round()), Some(17.0));
2595 }
2596
2597 fn draw(kind: CreditKind, reference: &str, task: Option<&str>, at: &str, micros: i64) -> (String, crate::grants::Draw) {
2598 let draw = crate::grants::Draw { grant: "crd_a".into(), kind, reference: reference.into(), task: task.map(Into::into), at: at.into(), micros };
2599 ("acme".to_owned(), draw)
2600 }
2601
2602 #[test]
2603 fn the_overview_and_the_costs_page_add_up_the_same_days() {
2604 // Models: $1 of cost, $0.40 of it on promotional credit (given), $0.60 sold for $0.72.
2605 let models = ProductDay {
2606 day: "2026-10-14".into(),
2607 bucket: "models".into(),
2608 own_cost_micros: 1_000_000,
2609 value_micros: 1_200_000,
2610 cash_micros: 720_000,
2611 given: Given { credit_promotional: 400_000, ..Given::default() },
2612 ..ProductDay::default()
2613 };
2614 // A comped workspace's Cloudflare share: all of it given.
2615 let comped = ProductDay {
2616 day: "2026-10-15".into(),
2617 bucket: "workers".into(),
2618 cf_cost_micros: 300_000,
2619 given: Given { comped: 300_000, ..Given::default() },
2620 ..ProductDay::default()
2621 };
2622 let overall = overall_of(&[models, comped]);
2623 assert_eq!((overall.cost_micros, overall.given_micros), (1_300_000, 700_000));
2624 assert_eq!((overall.given_credit_promotional_micros, overall.given_comped_micros), (400_000, 300_000));
2625 assert_eq!(overall.usage_micros, 720_000);
2626 // Margin on what was sold: $0.72 in against the $0.60 not given.
2627 assert_eq!(overall.sold_margin_micros, 120_000);
2628 assert_eq!(overall.margin_micros, 720_000 - 1_300_000);
2629 assert_eq!(overall_of(&[]).sold_margin_percent, None);
2630 }
2631
2632 #[test]
2633 fn usage_paid_for_with_credit_is_given_not_money_in() {
2634 // $1.20 of usage on $1 of cost, all of it paid with promotional credit.
2635 let mut rows = vec![usage("2026-10-15", "acme", "implement", 1_200_000, 1_200_000, 1_000_000)];
2636 apply_credits(&mut rows, &[draw(CreditKind::Promotional, "run_1", Some("implement"), "2026-10-15T10:00:00Z", 1_200_000)], &[]);
2637 assert_eq!(rows[0].cash, 0);
2638 assert_eq!(rows[0].given, Given { credit_promotional: 1_200_000, ..Given::default() });
2639 let (days, workspaces) = fold(&[], &BTreeMap::new(), &[], &[], &rows, &BTreeSet::new());
2640 let models = days.iter().find(|d| d.bucket == "models").unwrap();
2641 // Valued at its price, none of it money in, all of its cost given:
2642 // the margin on what was sold is untouched by it.
2643 assert_eq!((models.value_micros, models.cash_micros), (1_200_000, 0));
2644 assert_eq!(models.given, Given { credit_promotional: 1_000_000, ..Given::default() });
2645 assert_eq!(models.cost() - models.given.total(), 0);
2646 assert_eq!(workspaces[0].given.total(), 1_000_000);
2647 // Half paid with goodwill credit: half the cost given, half sold.
2648 let mut rows = vec![usage("2026-10-15", "acme", "implement", 1_200_000, 1_200_000, 1_000_000)];
2649 apply_credits(&mut rows, &[draw(CreditKind::Goodwill, "run_1", Some("implement"), "2026-10-15T10:00:00Z", 600_000)], &[]);
2650 let (days, _) = fold(&[], &BTreeMap::new(), &[], &[], &rows, &BTreeSet::new());
2651 let models = days.iter().find(|d| d.bucket == "models").unwrap();
2652 assert_eq!(models.cash_micros, 600_000);
2653 assert_eq!(models.given, Given { credit_goodwill: 500_000, ..Given::default() });
2654 let sold = models.cost() - models.given.total();
2655 assert_eq!(margin_percent(models.cash_micros, sold).map(|m| m.round()), Some(17.0));
2656 }
2657
2658 #[test]
2659 fn what_a_refund_pays_for_is_paid_for_and_the_refund_comes_off_its_day() {
2660 // A refund's credit pays for usage: still money in, nothing given.
2661 let mut rows = vec![usage("2026-10-15", "acme", "implement", 1_200_000, 1_200_000, 1_000_000)];
2662 apply_credits(&mut rows, &[draw(CreditKind::Refund, "run_9", Some("implement"), "2026-10-15T10:00:00Z", 1_200_000)], &[]);
2663 assert_eq!((rows[0].cash, rows[0].given), (1_200_000, Given::default()));
2664 // The $3 refunded for Oct 2 comes off that day's money in, shared
2665 // over what was paid that day.
2666 let mut rows = vec![
2667 usage("2026-10-02", "acme", "implement", 4_000_000, 4_000_000, 3_000_000),
2668 usage("2026-10-02", "acme", "sandbox", 2_000_000, 2_000_000, 1_500_000),
2669 usage("2026-10-02", "beta", "implement", 9_000_000, 9_000_000, 7_000_000),
2670 ];
2671 let refund = crate::grants::Refunded { workspace: "acme".into(), day: "2026-10-02".into(), micros: 3_000_000 };
2672 apply_credits(&mut rows, &[], std::slice::from_ref(&refund));
2673 assert_eq!((rows[0].cash, rows[1].cash, rows[2].cash), (2_000_000, 1_000_000, 9_000_000));
2674 assert!(rows.iter().all(|r| r.given == Given::default()));
2675 // Nothing paid that day: a line of its own, money in less than nothing.
2676 let mut rows = vec![];
2677 apply_credits(&mut rows, &[], &[refund]);
2678 assert_eq!((rows[0].key.as_str(), rows[0].cash, rows[0].value), ("other", -3_000_000, 0));
2679 }
2680
2681 #[test]
2682 fn credit_spent_on_month_end_meters_is_a_line_of_its_own() {
2683 // Storage is reconciled from snapshots, not its ledger line: what
2684 // credit paid of it is its own row on the day it was charged.
2685 let mut rows = vec![usage("2026-10-01", "acme", "implement", 1_000, 1_000, 800)];
2686 apply_credits(&mut rows, &[draw(CreditKind::Goodwill, "storage/2026-09", Some("storage"), "2026-10-01T00:05:00Z", 2_000_000)], &[]);
2687 assert_eq!(rows.len(), 2);
2688 assert_eq!((rows[1].key.as_str(), rows[1].cash, rows[1].value), ("storage", -2_000_000, 0));
2689 assert_eq!(rows[1].given.credit_goodwill, 2_000_000);
2690 assert_eq!(rows[0].cash, 1_000);
2691 }
2692
2693 #[test]
2694 fn a_workspace_that_costs_more_than_it_pays_is_flagged() {
2695 let rows = vec![("acme".to_string(), 5_000_000, 1_000_000), ("beta".to_string(), 900_000, 0), ("gamma".to_string(), 2_000_000, 3_000_000)];
2696 let found = anomalies(&rows, 1.0, 1_000_000);
2697 assert_eq!(found, vec![("acme".to_string(), 5_000_000, 1_000_000)]);
2698 // At twice its revenue as the threshold, $5 against $3 is fine.
2699 assert!(anomalies(&[("acme".to_string(), 5_000_000, 3_000_000)], 2.0, 1_000_000).is_empty());
2700 }
2701
2702 #[test]
2703 fn a_git_operation_costs_what_cloudflare_counts_for_it() {
2704 // $0.15 per 1,000 of Cloudflare's operations, on the charged days.
2705 let rate = billed_rate(&[(10_000.0, 0.0), (20_000.0, 3.0), (30_000.0, 4.5), (5_000.0, 0.75)]).unwrap();
2706 assert!((rate - 0.000_15).abs() < 1e-12);
2707 // Cloudflare counted 3 for every 1 g1t did: binding reads count.
2708 let per_op = derived_unit_cost(rate, 300_000.0, 100_000.0).unwrap();
2709 let per_thousand_micros = per_op * unit_size("1,000 operations") * 1e6;
2710 assert!((per_thousand_micros - 450_000.0).abs() < 1e-6, "{per_thousand_micros}");
2711 // Too few of g1t's units to say.
2712 assert!(derived_unit_cost(rate, 3_000.0, 500.0).is_none());
2713 assert!(billed_rate(&[(10_000.0, 0.0)]).is_none());
2714 assert_eq!(unit_size("million requests"), 1e6);
2715 assert_eq!(unit_size("second"), 1.0);
2716 }
2717
2718 /// The case that started it: syntaqx's ~$8.62 of model usage was wiped
2719 /// by a testing reset, AI Gateway still priced all $11.11, and the
2720 /// ledger had $2.49 left.
2721 fn gateway_and_ledger(kept: bool) -> (Vec<ProductDay>, Vec<WorkspaceDay>) {
2722 let rules = vec![rule("ai_gateway_requests", "*", "models", None), rule("containers", "*", "sandboxes", None)];
2723 let lines = vec![
2724 line("2026-10-05", costs::SOURCE_GATEWAY, "ai_gateway_requests", "anthropic_claude_opus_5_5", 1.0, 11.11),
2725 line("2026-10-05", SOURCE_BILLABLE, "containers", "container_memory", 10.0, 0.30),
2726 ];
2727 let mut usage = vec![usage("2026-10-05", "acme", "implement", 2_988_000, 2_988_000, 2_490_000), usage("2026-10-05", "acme", "sandbox", 120_000, 120_000, 100_000)];
2728 if kept {
2729 // What the reset kept (reset_costs), read back for the day.
2730 usage.extend(reset_usage(&[
2731 ("2026-10-05".into(), "syntaqx".into(), "models".into(), 8_620_000, 10_344_000),
2732 ("2026-10-05".into(), "syntaqx".into(), "sandboxes".into(), 100_000, 120_000),
2733 // The reset's own row is not usage.
2734 ("2026-10-07".into(), "syntaqx".into(), String::new(), 0, 0),
2735 ]));
2736 }
2737 fold(&rules, &revenue_map(), &lines, &[], &usage, &BTreeSet::new())
2738 }
2739
2740 #[test]
2741 fn what_a_reset_kept_is_on_the_ledgers_side_of_model_cost() {
2742 let models = |days: &[ProductDay]| days.iter().find(|d| d.bucket == "models").cloned().unwrap();
2743 // Without it: the ledger's $2.49 of model cost.
2744 let (days, _) = gateway_and_ledger(false);
2745 assert_eq!(models(&days).own_cost_micros, 2_490_000);
2746 // With it: the ledger's model cost and the reset's add up to what
2747 // AI Gateway priced.
2748 let (days, _) = gateway_and_ledger(true);
2749 let m = models(&days);
2750 assert_eq!(m.own_cost_micros, 11_110_000);
2751 // Models are no longer compared with anything: no drift either way.
2752 assert!(drifts("models", &[m], 10.0, false, 100_000, None).is_empty());
2753 assert!(drifts("models", &[models(&gateway_and_ledger(false).0)], 10.0, false, 100_000, None).is_empty());
2754 // The reset's own row makes no bucket of its own.
2755 assert!(!days.iter().any(|d| d.bucket.is_empty()));
2756 }
2757
2758 #[test]
2759 fn what_a_reset_kept_is_given_away_as_testing_resets() {
2760 let (days, workspaces) = gateway_and_ledger(true);
2761 let models = days.iter().find(|d| d.bucket == "models").unwrap();
2762 // All of syntaqx's model cost is given, none of it money in.
2763 assert_eq!(models.given, Given { reset: 8_620_000, ..Given::default() });
2764 assert_eq!(models.cash_micros, 2_988_000);
2765 // Cloudflare's sandbox cost is shared by what each workspace's usage
2766 // cost: syntaqx's half is given too.
2767 let sandboxes = days.iter().find(|d| d.bucket == "sandboxes").unwrap();
2768 assert_eq!((sandboxes.cost(), sandboxes.given.reset), (300_000, 150_000));
2769 // Who g1t paid: syntaqx is still on it, all of its cost given.
2770 let syntaqx: Vec<&WorkspaceDay> = workspaces.iter().filter(|w| w.workspace == "syntaqx").collect();
2771 assert_eq!(syntaqx.iter().map(|w| w.cost).sum::<i64>(), 8_770_000);
2772 assert!(syntaqx.iter().all(|w| w.given.reset == w.cost && w.given.total() == w.cost && w.revenue == 0));
2773 // The statement reads it back from margin_days by why.
2774 let row = MarginRow {
2775 day: models.day.clone(),
2776 bucket: models.bucket.clone(),
2777 cf_cost_micros: models.cf_cost_micros,
2778 own_cost_micros: models.own_cost_micros,
2779 value_micros: models.value_micros,
2780 cash_micros: models.cash_micros,
2781 cf_quantity: 0.0,
2782 own_quantity: 0.0,
2783 given_comped_micros: Some(0),
2784 given_free_micros: Some(0),
2785 given_trial_micros: Some(0),
2786 given_pool_micros: Some(0),
2787 given_discount_micros: Some(0),
2788 given_credit_promotional_micros: Some(0),
2789 given_credit_goodwill_micros: Some(0),
2790 given_reset_micros: Some(models.given.reset),
2791 given_unpaid_micros: Some(0),
2792 };
2793 assert_eq!(ProductDay::from(row).given, models.given);
2794 }
2795
2796 #[test]
2797 fn test_mode_charges_are_never_money_in() {
2798 // A $12 sandbox charge and the $20 plan, both while payments were in
2799 // Stripe's test mode: valued as before, given as unpaid, no cash.
2800 let mut rows = vec![
2801 usage("2026-10-06", "acme", "sandbox", 12_000_000, 12_000_000, 10_000_000),
2802 UsageRow { day: "2026-10-06".into(), workspace: "acme".into(), key: "plan".into(), value: 666_667, cash: 666_667, ..UsageRow::default() },
2803 ];
2804 without_real_money(&mut rows, None);
2805 assert!(rows.iter().all(|r| r.cash == 0));
2806 // A comped workspace's charge is given once, as comped.
2807 let mut comped = vec![UsageRow { given: Given { comped: 500, ..Given::default() }, ..usage("2026-10-06", "flagon-io", "sandbox", 500, 500, 400) }];
2808 without_real_money(&mut comped, None);
2809 assert_eq!((comped[0].cash, comped[0].given.total()), (0, 500));
2810 assert_eq!(rows[0].given.unpaid, 12_000_000);
2811 assert_eq!(rows[0].value, 12_000_000);
2812 assert_eq!(rows[1].given.unpaid, 666_667);
2813 let (days, workspaces) = fold(&rules(), &revenue_map(), &[], &[], &rows, &BTreeSet::new());
2814 assert_eq!(days.iter().map(|d| d.cash_micros).sum::<i64>(), 0);
2815 let sandboxes = days.iter().find(|d| d.bucket == "sandboxes").unwrap();
2816 assert_eq!(sandboxes.given.unpaid, sandboxes.cost());
2817 assert!(workspaces.iter().all(|w| w.revenue == 0));
2818 // Once live: from that day on, it is money.
2819 let mut rows = vec![
2820 usage("2026-10-06", "acme", "sandbox", 1_000_000, 1_000_000, 800_000),
2821 usage("2026-10-07", "acme", "sandbox", 1_000_000, 1_000_000, 800_000),
2822 ];
2823 without_real_money(&mut rows, Some("2026-10-07"));
2824 assert_eq!((rows[0].cash, rows[0].given.unpaid), (0, 1_000_000));
2825 assert_eq!((rows[1].cash, rows[1].given.unpaid), (1_000_000, 0));
2826 // When payments went live is kept from the first time it is seen.
2827 assert_eq!(live_since(false, Some("2026-10-07"), "2026-10-09"), None);
2828 assert_eq!(live_since(true, None, "2026-10-09").as_deref(), Some("2026-10-09"));
2829 assert_eq!(live_since(true, Some("2026-10-07"), "2026-10-09").as_deref(), Some("2026-10-07"));
2830 }
2831
2832 #[test]
2833 fn workspaces_and_running_g1t_add_up_to_the_bill() {
2834 // Running g1t on a day with usage is shared; on a day with none it
2835 // is no one's, and the report says so.
2836 let lines = vec![
2837 line("2026-10-06", SOURCE_BILLABLE, "workers", "workers_cpu_ms", 1.0, 0.10),
2838 line("2026-10-07", SOURCE_BILLABLE, "workers", "workers_cpu_ms", 1.0, 0.20),
2839 line("2026-10-07", SOURCE_BILLABLE, "containers", "container_memory_per_gib_second", 1.0, 0.09),
2840 ];
2841 let usage = vec![usage("2026-10-07", "acme", "sandbox", 100, 100, 80), usage("2026-10-07", "beta", "sandbox", 300, 300, 240)];
2842 let (days, workspaces) = fold(&rules(), &revenue_map(), &lines, &[], &usage, &BTreeSet::new());
2843 let total: i64 = days.iter().map(ProductDay::cost).sum();
2844 let shared: i64 = workspaces.iter().map(|w| w.cost).sum();
2845 assert_eq!(total, 390_000);
2846 assert_eq!(unattributed(total, shared), 100_000);
2847 assert_eq!(shared + unattributed(total, shared), total);
2848 }
2849
2850 #[test]
2851 fn the_cycle_report_projects_as_cloudflare_does() {
2852 let cycle = crate::cycle::cycle_of("2026-10-09", 28);
2853 let meter = |meter: &str, cost: i64, quantity: f64| CycleMeter { meter: meter.into(), cost_micros: cost, quantity, ..CycleMeter::default() };
2854 let report = cycle_report(&cycle, 12, vec![meter("container_memory", 92_175, 126_870.0), meter("workers_cpu_ms", 200_000, 39_160_000.0), meter("d1_rows_read", 0, 61_820_000.0)], 30_000_000);
2855 assert_eq!((report.start.as_str(), report.end.as_str(), report.days, report.days_elapsed), ("2026-09-28", "2026-10-27", 30, 12));
2856 assert_eq!(report.usage_micros, 292_175);
2857 assert_eq!(report.projected_micros, 730_438);
2858 assert_eq!(report.average_daily_micros, 24_347);
2859 assert_eq!(report.meters[0].meter, "workers_cpu_ms");
2860 assert_eq!(report.meters[2].meter, "d1_rows_read");
2861 }
2862
2863 #[test]
2864 fn reconciling_again_gives_the_same_answer() {
2865 assert_eq!(gateway_and_ledger(true), gateway_and_ledger(true));
2866 // A reset's kept rows are read back exactly as kept: running it
2867 // again cannot count them twice.
2868 let kept = [("2026-10-05".to_string(), "syntaqx".to_string(), "models".to_string(), 8_620_000, 10_344_000)];
2869 assert_eq!(reset_usage(&kept), reset_usage(&kept));
2870 assert_eq!(reset_usage(&kept).len(), 1);
2871 }
2872}