|
| 1 | +// GitHub billing collector core (#1051; hub design §2.2/§5): maps the |
| 2 | +// unified billing usage endpoint's items to §4 snapshot records. |
| 3 | +// |
| 4 | +// GET /users/{user}/settings/billing/usage?year=YYYY&month=M |
| 5 | +// |
| 6 | +// answers with the account's existing gh OAuth token — the old |
| 7 | +// per-product billing endpoints are gone (410, "This endpoint has been |
| 8 | +// moved"), and no billing-scoped token is needed for the own-account |
| 9 | +// usage read (verified live 2026-10-29, triage task 2315). Items are |
| 10 | +// per-day, per-SKU rows: |
| 11 | +// { date, product, sku, unitType, quantity, pricePerUnit, |
| 12 | +// grossAmount, discountAmount, netAmount, repositoryName } |
| 13 | +// |
| 14 | +// Aggregation is a group-by on date: Actions minutes, Codespaces |
| 15 | +// core-hours, storage GB-hours (the endpoint's own unit — GB-hours is |
| 16 | +// NOT GB; the snapshot unit keeps them distinct), plus gross/net USD |
| 17 | +// across all products. Records are stamped at the usage day (midday |
| 18 | +// UTC, the store's day convention), never at collection time, so a |
| 19 | +// re-run re-derives identical records and the store dedupes them. |
| 20 | +// |
| 21 | +// Pure functions, no I/O — scripts/collect-github-billing.ts owns the |
| 22 | +// gh call and the outbox append; tests feed literals. |
| 23 | +import type { SnapshotRecord } from "../model.ts"; |
| 24 | +import { dayTs } from "../snapshot-records.ts"; |
| 25 | + |
| 26 | +export interface BillingUsageItem { |
| 27 | + date?: string; |
| 28 | + product?: string; |
| 29 | + sku?: string; |
| 30 | + unitType?: string; |
| 31 | + quantity?: number; |
| 32 | + grossAmount?: number; |
| 33 | + discountAmount?: number; |
| 34 | + netAmount?: number; |
| 35 | + [key: string]: unknown; |
| 36 | +} |
| 37 | + |
| 38 | +export interface BillingDayAggregate { |
| 39 | + date: string; |
| 40 | + actionsMinutes: number; |
| 41 | + codespacesHours: number; |
| 42 | + actionsStorageGbHours: number; |
| 43 | + codespacesStorageGbHours: number; |
| 44 | + grossUsd: number; |
| 45 | + netUsd: number; |
| 46 | +} |
| 47 | + |
| 48 | +function round(n: number, dp: number): number { |
| 49 | + const f = 10 ** dp; |
| 50 | + return Math.round(n * f) / f; |
| 51 | +} |
| 52 | + |
| 53 | +function emptyDay(date: string): BillingDayAggregate { |
| 54 | + return { |
| 55 | + date, |
| 56 | + actionsMinutes: 0, |
| 57 | + codespacesHours: 0, |
| 58 | + actionsStorageGbHours: 0, |
| 59 | + codespacesStorageGbHours: 0, |
| 60 | + grossUsd: 0, |
| 61 | + netUsd: 0, |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +/** Codespaces compute rows are machine-hours, but the hub's |
| 66 | + * `codespaces-hours` metric is core-hours (src/saas-extract.ts): |
| 67 | + * one hour on an N-core machine consumes N core-hours (GitHub's |
| 68 | + * Codespaces pricing multiplier). The core count is parsed from the |
| 69 | + * SKU ("Codespaces 2-core"); a SKU with no parseable core count is |
| 70 | + * taken at face value (x1) rather than dropped. */ |
| 71 | +function codespacesCoreMultiplier(sku: unknown): number { |
| 72 | + if (typeof sku !== "string") return 1; |
| 73 | + const m = /(\d+)\s*-core/i.exec(sku); |
| 74 | + return m ? Number(m[1]) : 1; |
| 75 | +} |
| 76 | + |
| 77 | +/** Per-day aggregates from raw endpoint items, oldest day first. */ |
| 78 | +export function aggregateBillingUsage(items: BillingUsageItem[]): BillingDayAggregate[] { |
| 79 | + const byDate = new Map<string, BillingDayAggregate>(); |
| 80 | + for (const item of items) { |
| 81 | + if (typeof item.date !== "string" || item.date.length < 10) continue; |
| 82 | + const date = item.date.slice(0, 10); |
| 83 | + const day = byDate.get(date) ?? emptyDay(date); |
| 84 | + const qty = typeof item.quantity === "number" ? item.quantity : 0; |
| 85 | + // Classification is case-insensitive: the endpoint's documented |
| 86 | + // response example spells product/unitType "Actions"/"minutes" |
| 87 | + // while live rows arrive as "actions"/"Minutes" — both must |
| 88 | + // classify identically, never silently drop to money-only. |
| 89 | + const product = typeof item.product === "string" ? item.product.toLowerCase() : ""; |
| 90 | + const unit = typeof item.unitType === "string" ? item.unitType.toLowerCase() : ""; |
| 91 | + if (product === "actions" && unit === "minutes") day.actionsMinutes += qty; |
| 92 | + else if (product === "actions" && unit === "gigabytehours") |
| 93 | + day.actionsStorageGbHours += qty; |
| 94 | + else if (product === "codespaces" && unit === "hours") |
| 95 | + day.codespacesHours += qty * codespacesCoreMultiplier(item.sku); |
| 96 | + else if (product === "codespaces" && unit === "gigabytehours") |
| 97 | + day.codespacesStorageGbHours += qty; |
| 98 | + // Gross/net span every product the endpoint reports, known or not. |
| 99 | + if (typeof item.grossAmount === "number") day.grossUsd += item.grossAmount; |
| 100 | + if (typeof item.netAmount === "number") day.netUsd += item.netAmount; |
| 101 | + byDate.set(date, day); |
| 102 | + } |
| 103 | + return [...byDate.values()] |
| 104 | + .sort((a, b) => a.date.localeCompare(b.date)) |
| 105 | + .map((d) => ({ |
| 106 | + date: d.date, |
| 107 | + actionsMinutes: round(d.actionsMinutes, 3), |
| 108 | + codespacesHours: round(d.codespacesHours, 3), |
| 109 | + actionsStorageGbHours: round(d.actionsStorageGbHours, 3), |
| 110 | + codespacesStorageGbHours: round(d.codespacesStorageGbHours, 3), |
| 111 | + grossUsd: round(d.grossUsd, 2), |
| 112 | + netUsd: round(d.netUsd, 2), |
| 113 | + })); |
| 114 | +} |
| 115 | + |
| 116 | +/** Month totals over per-day aggregates — the collector's test target |
| 117 | + * ("reproduces the endpoint's aggregates within accrual"). */ |
| 118 | +export function summarizeBillingDays(days: BillingDayAggregate[]): Omit<BillingDayAggregate, "date"> { |
| 119 | + const total = { |
| 120 | + actionsMinutes: 0, |
| 121 | + codespacesHours: 0, |
| 122 | + actionsStorageGbHours: 0, |
| 123 | + codespacesStorageGbHours: 0, |
| 124 | + grossUsd: 0, |
| 125 | + netUsd: 0, |
| 126 | + }; |
| 127 | + for (const d of days) { |
| 128 | + total.actionsMinutes += d.actionsMinutes; |
| 129 | + total.codespacesHours += d.codespacesHours; |
| 130 | + total.actionsStorageGbHours += d.actionsStorageGbHours; |
| 131 | + total.codespacesStorageGbHours += d.codespacesStorageGbHours; |
| 132 | + total.grossUsd += d.grossUsd; |
| 133 | + total.netUsd += d.netUsd; |
| 134 | + } |
| 135 | + return { |
| 136 | + actionsMinutes: round(total.actionsMinutes, 3), |
| 137 | + codespacesHours: round(total.codespacesHours, 3), |
| 138 | + actionsStorageGbHours: round(total.actionsStorageGbHours, 3), |
| 139 | + codespacesStorageGbHours: round(total.codespacesStorageGbHours, 3), |
| 140 | + grossUsd: round(total.grossUsd, 2), |
| 141 | + netUsd: round(total.netUsd, 2), |
| 142 | + }; |
| 143 | +} |
| 144 | + |
| 145 | +/** §4 snapshot records for one month of endpoint items, under the |
| 146 | + * hub's existing `github` service and card metric ids where they |
| 147 | + * exist (`codespaces-hours`). Zero aggregates emit no record — a day |
| 148 | + * with no Codespaces use is an absence, not a 0 observation. */ |
| 149 | +export function recordsFromBillingUsage( |
| 150 | + items: BillingUsageItem[], |
| 151 | + observedBy: string, |
| 152 | +): SnapshotRecord[] { |
| 153 | + const out: SnapshotRecord[] = []; |
| 154 | + for (const day of aggregateBillingUsage(items)) { |
| 155 | + const ts = dayTs(day.date); |
| 156 | + const push = (metricId: string, value: number, unit: SnapshotRecord["unit"]) => { |
| 157 | + if (value === 0) return; |
| 158 | + out.push({ ts, serviceId: "github", metricId, value, unit, source: "api", observedBy }); |
| 159 | + }; |
| 160 | + push("actions-minutes", day.actionsMinutes, "minutes"); |
| 161 | + push("codespaces-hours", day.codespacesHours, "hours"); |
| 162 | + push("actions-storage-gb-hours", day.actionsStorageGbHours, "gb-hours"); |
| 163 | + push("codespaces-storage-gb-hours", day.codespacesStorageGbHours, "gb-hours"); |
| 164 | + push("billing-gross", day.grossUsd, "usd"); |
| 165 | + push("billing-net", day.netUsd, "usd"); |
| 166 | + } |
| 167 | + return out; |
| 168 | +} |