Commit

Models' margin read -14%: usage nothing paid for is valued at price, not $0

A run in the free period was charged nothing and drawn from nothing, so the reconciler counted its cost and a value of $0: models showed $8.04 at price against $9.20 of cost. Such usage is now valued at price (a giveaway, not a sale for nothing); paid usage keeps what was paid, so discounts still show. Each daily run now reconciles the full 31-day window from kept data, so the correction reaches every day sudo shows.

syntaqxcommitted Parent7dd8455Browse files
2 files+42−20/2 viewed
+9−0
186186 (raw counts with the mapping) for `cost_operations`. Migration 0023 drops
187187 the `billable_units` table 0022 made for this, which was never written.
188188
189+**Charged at price** (a product's value) is what each day's usage was paid:
190+charged to a card or credit, or drawn from the plan's included usage, a
191+trial, a pool or a gift. Usage nothing paid for, as in a free period, is
192+valued at price (cost plus the margin), since it was given away at its price
193+rather than sold for nothing; so are g1t's own workspaces. Runs on a
194+workspace's own model provider have no cost to g1t. Every daily run
195+reconciles the whole 31-day window from what is already kept, so a change in
196+how a day is valued reaches every day sudo shows.
197+
189198 ## Alerts runbook
190199
191200 | Alert | Raised when | First steps |
+33−2
405405 /// Workspaces that cost g1t more than `factor` times what they paid, with
406406 /// at least `floor_micros` of cost: each (workspace, cost, revenue), the
407407 /// biggest gap first.
408+/// What a day's usage was worth at price. g1t's own workspaces are valued
409+/// at price. So is usage nothing paid for, neither charged nor drawn from
410+/// the plan, a trial, a pool or a gift (a free period): it was given away at
411+/// its price, not sold for nothing. Anything paid keeps what it was paid, so
412+/// a discount still shows as one.
413+pub(crate) fn usage_value(internal: bool, cost: i64, paid: i64, margin_percent: u32) -> i64 {
414+ if internal || (paid == 0 && cost > 0) {
415+ return crate::credits::with_margin(cost, margin_percent);
416+ }
417+ paid
418+}
419+
408420 /// What the overall alert says: the money as money, and a percentage only
409421 /// while there is enough coming in for one to mean something (a few cents
410422 /// against dollars of cost reads as -8000%).
597609 run.problems.push(format!("g1t's own counts could not be read: {error}"));
598610 }
599611 self.snapshot_pending(&until).await?;
600− run.days = self.reconcile_range(&since, &until).await?;
612+ // Reconciled over the whole window sudo shows, not only the days the
613+ // bill was read for: it reads only what is already kept, so a change
614+ // in how a day is valued reaches every day shown at the next run.
615+ let window = day_before(&until, costs::BACKFILL_DAYS - 1);
616+ let reconcile_from = if window < since { window } else { since.clone() };
617+ run.days = self.reconcile_range(&reconcile_from, &until).await?;
601618 let drift = self.find_drift(&until).await?;
602619 run.proposals = self.measure_units(&until).await?;
603620 self.apply_due_versions().await?;
668685 // to g1t. g1t's own workspaces are valued at price.
669686 let cost = if r.own_provider == 1 { 0 } else { r.cost.unwrap_or(0) };
670687 let cash = r.cash.unwrap_or(0);
671− let value = if r.internal == 1 { crate::credits::with_margin(cost, self.margin_percent) } else { cash + r.drawn.unwrap_or(0) };
688+ let paid = cash + r.drawn.unwrap_or(0);
689+ let value = usage_value(r.internal == 1, cost, paid, self.margin_percent);
672690 UsageRow { day: r.day, workspace: r.workspace, key: r.key, value, cash, cost }
673691 })
674692 .collect();
14731491 use super::*;
14741492
14751493 #[test]
1494+ fn usage_nothing_paid_for_is_valued_at_price_and_paid_usage_at_what_was_paid() {
1495+ // A free period: charged nothing, drawn from nothing.
1496+ assert_eq!(usage_value(false, 1_000_000, 0, 20), 1_200_000);
1497+ // Charged, or drawn from a trial: what was paid.
1498+ assert_eq!(usage_value(false, 1_000_000, 1_200_000, 20), 1_200_000);
1499+ assert_eq!(usage_value(false, 1_000_000, 900_000, 20), 900_000);
1500+ // g1t's own: at price.
1501+ assert_eq!(usage_value(true, 1_000_000, 0, 20), 1_200_000);
1502+ // No cost, nothing paid: nothing.
1503+ assert_eq!(usage_value(false, 0, 0, 20), 0);
1504+ }
1505+
1506+ #[test]
14761507 fn the_overall_alert_says_dollars_while_little_comes_in() {
14771508 let small = overall_detail(90_000, 7_500_000, 3, 10.0, -8239.7);
14781509 assert!(small.contains("took in $0.09 against $7.50"), "{small}");