Billing and Usage reconcile: own-provider runs leave Billing's at-price total, and Usage's not-charged part is at price less charged
Billing's total marked up runs billed to the workspace's own provider, which g1t never charges. And what syntaxqx was not charged came mostly from a free period, which no funding column records: covered_micros is now g1t's usage at price less what was charged, so the two pages always add up.
6 files+20−130/6 viewed
| 118 | 118 | **This month's usage**: what each kind of usage has come to so far, in | |
| 119 | 119 | dollars and in what was used (*Agents & sandboxes*, *Builds*, *Requests & | |
| 120 | 120 | CPU*, *Custom domains*, *Git operations & storage*, *Search & security | |
| 121 | − | scans*). Its **Total at price** is before the included usage, the trial or | |
| 122 | − | a pool paid their part, so it can be more than what was charged; the | |
| 123 | − | workspace's **Usage** page shows what was charged as **Spent**, and how much | |
| 124 | − | of the total those paid. App traffic, custom domains, storage and git | |
| 121 | + | scans*). Its **Total at price** is g1t's usage at cost plus 20%, before the | |
| 122 | + | included usage, the trial, a pool or a free period paid their part, so it | |
| 123 | + | can be more than what was charged. Runs on your own model provider are not | |
| 124 | + | in it: your provider bills those. The workspace's **Usage** page shows what | |
| 125 | + | was charged as **Spent**, and how much of the total was not charged. App traffic, custom domains, storage and git | |
| 125 | 126 | operations are counted through the month and charged when it closes. **Manage on Stripe** opens the card, invoices and | |
| 126 | 127 | billing details. **End at the end of the period** ends the plan then, with | |
| 127 | 128 | nothing more charged after; **Keep the plan** takes that back until then. |
| 229 | 229 | value={dollars(usage.spentMicros)} | |
| 230 | 230 | note={ | |
| 231 | 231 | (usage.coveredMicros ?? 0) > 0 | |
| 232 | − | ? `Of ${dollars(usage.spentMicros + (usage.coveredMicros ?? 0))} at price: ${dollars(usage.coveredMicros ?? 0)} of it was paid by the included usage, the trial or a pool` | |
| 232 | + | ? `Of ${dollars(usage.spentMicros + (usage.coveredMicros ?? 0))} at price: ${dollars(usage.coveredMicros ?? 0)} of it was not charged (the included usage, the trial, a pool, or a free period)` | |
| 233 | 233 | : usage.providerMicros > 0 | |
| 234 | 234 | ? `Plus about ${dollars(usage.providerMicros)} billed by your own model provider` | |
| 235 | 235 | : `${dollars(usage.costMicros)} of it the model provider's` |
| 324 | 324 | pub since: String, | |
| 325 | 325 | /// Charged, including g1t's margin. | |
| 326 | 326 | pub spent_micros: i64, | |
| 327 | − | /// What paid for usage before it was charged, at price: the plan's | |
| 328 | − | /// included usage, the trial, the open-source pool and what g1t | |
| 329 | − | /// covered. Usage at price is `spent_micros` plus this. | |
| 327 | + | /// What g1t's usage came to at price, less what was charged: the plan's | |
| 328 | + | /// included usage, the trial, a pool or a free period paid it. Usage at | |
| 329 | + | /// price is `spent_micros` plus this. | |
| 330 | 330 | #[serde(default)] | |
| 331 | 331 | pub covered_micros: i64, | |
| 332 | 332 | /// What g1t's model provider charged, before the margin. |
| 1014 | 1014 | since: string; | |
| 1015 | 1015 | /** Charged, including g1t's margin. */ | |
| 1016 | 1016 | spentMicros: number; | |
| 1017 | − | /** What paid for usage before it was charged, at price: the plan's included usage, the trial, the open-source pool, what g1t covered. Usage at price is `spentMicros` plus this. */ | |
| 1017 | + | /** What g1t's usage came to at price, less what was charged: the plan's included usage, the trial, a pool or a free period paid it. Usage at price is `spentMicros` plus this. */ | |
| 1018 | 1018 | coveredMicros?: number; | |
| 1019 | 1019 | /** What g1t's model provider charged, before the margin. */ | |
| 1020 | 1020 | costMicros: number; |
| 428 | 428 | #[derive(serde::Deserialize)] | |
| 429 | 429 | struct Totals { | |
| 430 | 430 | spent: Option<i64>, | |
| 431 | − | covered: Option<i64>, | |
| 431 | + | g1t_cost: Option<i64>, | |
| 432 | 432 | cost: Option<i64>, | |
| 433 | 433 | provider: Option<i64>, | |
| 434 | 434 | runs: Option<u32>, | |
| 439 | 439 | .prepare( | |
| 440 | 440 | "SELECT | |
| 441 | 441 | -SUM(CASE WHEN kind = 'usage' THEN amount_micros END) AS spent, | |
| 442 | − | SUM(CASE WHEN kind = 'usage' THEN credit_micros + trial_micros + oss_micros + given_micros END) AS covered, | |
| 442 | + | SUM(CASE WHEN kind = 'usage' AND COALESCE(billed_to, 'g1t') = 'g1t' THEN cost_micros END) AS g1t_cost, | |
| 443 | 443 | SUM(CASE WHEN kind = 'usage' AND COALESCE(billed_to, 'g1t') = 'g1t' THEN cost_micros END) AS cost, | |
| 444 | 444 | SUM(CASE WHEN kind = 'usage' AND billed_to = 'workspace' THEN cost_micros END) AS provider, | |
| 445 | 445 | SUM(CASE WHEN kind = 'usage' THEN 1 ELSE 0 END) AS runs, | |
| 465 | 465 | .await?; | |
| 466 | 466 | let totals = totals.unwrap_or(Totals { | |
| 467 | 467 | spent: None, | |
| 468 | − | covered: None, | |
| 468 | + | g1t_cost: None, | |
| 469 | 469 | cost: None, | |
| 470 | 470 | provider: None, | |
| 471 | 471 | runs: None, | |
| 473 | 473 | }); | |
| 474 | 474 | Ok(Outcome::Ok(Usage { | |
| 475 | 475 | spent_micros: totals.spent.unwrap_or_default(), | |
| 476 | − | covered_micros: totals.covered.unwrap_or_default(), | |
| 476 | + | // At price, what g1t's usage came to, less what was charged: the | |
| 477 | + | // part the included usage, the trial, a pool, or a free period paid. | |
| 478 | + | covered_micros: (crate::credits::with_margin(totals.g1t_cost.unwrap_or_default(), self.margin_percent) | |
| 479 | + | - totals.spent.unwrap_or_default()) | |
| 480 | + | .max(0), | |
| 477 | 481 | cost_micros: totals.cost.unwrap_or_default(), | |
| 478 | 482 | provider_micros: totals.provider.unwrap_or_default(), | |
| 479 | 483 | used_micros: totals.cost.unwrap_or_default() + totals.provider.unwrap_or_default(), |
| 269 | 269 | "SELECT {METER_SQL} AS meter, COUNT(*) AS count, SUM(cost_micros) AS cost | |
| 270 | 270 | FROM ledger WHERE workspace = ?1 AND kind = 'usage' AND created_at >= ?2 AND created_at < ?3 | |
| 271 | 271 | AND COALESCE(reference, '') NOT LIKE 'deployments/%' | |
| 272 | + | -- Runs on the workspace's own model provider are its provider's to bill, never g1t's. | |
| 273 | + | AND COALESCE(billed_to, 'g1t') = 'g1t' | |
| 272 | 274 | GROUP BY 1" | |
| 273 | 275 | )) | |
| 274 | 276 | .bind(&[workspace.as_str().into(), from.as_str().into(), until.as_str().into()])? |