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.

466 linesCodeBlameRaw
1import { type ReactNode } from "react";
2
3import type { CostsReport, PriceProposal } from "@g1t/contracts";
4
5import type { Route } from "./+types/costs-bill";
6import { CostsHeader } from "~/components/costs-header";
7import { Badge, Button, EmptyState, Field, Input, Notice, Section, When } from "~/components/ui";
8import {
9 basisLabel,
10 billReadNote,
11 countLabel,
12 cycleHeadline,
13 driftLabel,
14 percentLabel,
15 proposalOutcome,
16 unitDollars,
17 unpricedMeters,
18 versionCells,
19} from "~/lib/costs";
20import { type CostsActionResult, costsAction, costsLoader } from "~/lib/costs-route.server";
21import { dollarsField, usd } from "~/lib/money";
22
23export const meta: Route.MetaFunction = () => [{ title: "Bill & pricing · sudo" }, { name: "robots", content: "noindex, nofollow" }];
24
25export const loader = ({ request, context }: Route.LoaderArgs) => costsLoader(request, context);
26export const action = ({ request, context }: Route.ActionArgs) => costsAction(request, context);
27
28/**
29 * Cloudflare's bill as read, what does not add up against it, and the
30 * price book: proposals, versions, mappings and the guardrails on them.
31 */
32export default function CostsBill({ loaderData, actionData }: Route.ComponentProps) {
33 const { range, report, error, done } = loaderData;
34 const failed = actionData && "error" in actionData ? (actionData as CostsActionResult) : null;
35 const description =
36 "Cloudflare's bill as read, line by line, what does not add up against g1t's own counts and price book, and the prices themselves: proposals waiting for a decision, every version, the mappings and the guardrails.";
37 if (!report) {
38 return (
39 <main className="mx-auto max-w-6xl px-4 py-8 sm:py-10">
40 <CostsHeader page="bill" range={range} report={null} done={null} runError={null} description={description} />
41 <div className="mt-5">
42 <Notice tone="warn">Billing did not answer for costs: {error}</Notice>
43 </div>
44 </main>
45 );
46 }
47 return (
48 <main className="mx-auto max-w-6xl px-4 py-8 sm:py-10">
49 <CostsHeader page="bill" range={range} report={report} done={done} runError={failed?.section === "run" ? failed.error : null} description={description} />
50
51 <CycleSection report={report} />
52
53 <Section
54 className="mt-6"
55 id="drift"
56 title="Drift"
57 description="Over the last 7 days: counts g1t and Cloudflare disagree on past a mapping's threshold, a bill far from the price book's cost of the same usage, and leaks (cost nothing charges for, or a Cloudflare meter no one mapped)."
58 >
59 {report.drift.length === 0 ? (
60 <p className="text-sm text-muted">Nothing has drifted.</p>
61 ) : (
62 <div className="-mx-4 overflow-x-auto sm:-mx-5">
63 <table className="w-full min-w-176 text-sm">
64 <thead>
65 <tr className="border-b border-line text-left text-xs text-muted">
66 <th className="px-4 py-2 font-medium sm:px-5">Product</th>
67 <th className="px-4 py-2 font-medium">Kind</th>
68 <th className="px-4 py-2 text-right font-medium">g1t</th>
69 <th className="px-4 py-2 text-right font-medium">Cloudflare</th>
70 <th className="px-4 py-2 text-right font-medium sm:pr-5">Delta</th>
71 </tr>
72 </thead>
73 <tbody>
74 {report.drift.map((d) => {
75 const money = d.kind !== "count";
76 return (
77 <tr key={`${d.bucket}-${d.kind}`} className="border-b border-line align-top last:border-0">
78 <td className="px-4 py-2.5 sm:px-5">
79 {d.title}
80 <span className="mt-0.5 block max-w-md text-xs text-faint">{d.detail.replace(`${d.title}: `, "")}</span>
81 </td>
82 <td className="px-4 py-2.5">
83 <Badge tone={d.kind === "leak" ? "danger" : "warn"}>{driftLabel(d.kind)}</Badge>
84 </td>
85 <td className="tabular px-4 py-2.5 text-right">{money ? usd(d.ours) : countLabel(d.ours)}</td>
86 <td className="tabular px-4 py-2.5 text-right">{money ? usd(d.cloudflare) : countLabel(d.cloudflare)}</td>
87 <td className="tabular px-4 py-2.5 text-right sm:pr-5">{percentLabel(d.deltaPercent, { signed: true })}</td>
88 </tr>
89 );
90 })}
91 </tbody>
92 </table>
93 </div>
94 )}
95 </Section>
96
97 <Section
98 className="mt-6"
99 id="proposals"
100 title="Price proposals"
101 description={`What the keeper and the reconciler measured. Moves within ${report.settings.autoApplyPercent}% apply themselves${report.settings.autoApply ? "" : " (off now)"}; larger ones wait here. A fall applies at once; a rise after ${report.settings.noticeDays} days' notice, and a monthly meter's from the start of the next month.`}
102 >
103 {report.proposals.length === 0 ? (
104 <p className="text-sm text-muted">No proposals yet.</p>
105 ) : (
106 <ul className="space-y-3">
107 {report.proposals.map((p) => (
108 <ProposalRow key={p.id} proposal={p} error={failed?.section === `proposal-${p.id}` ? failed.error : null} />
109 ))}
110 </ul>
111 )}
112 </Section>
113
114 <Section
115 className="mt-6"
116 title="Price versions"
117 description="Every price as it was and will be. A version never changes once written; each charge records the version it was made at."
118 >
119 <div className="-mx-4 overflow-x-auto sm:-mx-5">
120 <table className="w-full min-w-176 text-sm">
121 <thead>
122 <tr className="border-b border-line text-left text-xs text-muted">
123 <th className="px-4 py-2 font-medium sm:px-5">Meter</th>
124 <th className="px-4 py-2 text-right font-medium">Cost</th>
125 <th className="px-4 py-2 text-right font-medium">Price</th>
126 <th className="px-4 py-2 font-medium">From</th>
127 <th className="px-4 py-2 font-medium sm:pr-5">Why</th>
128 </tr>
129 </thead>
130 <tbody>
131 {report.versions.map((v) => {
132 const cells = versionCells(v);
133 return (
134 <tr key={v.id} className="border-b border-line align-top last:border-0">
135 <td className="px-4 py-2.5 sm:px-5">
136 <span className="font-mono text-xs">{v.meter}</span> <span className="text-faint">v{v.version}</span>
137 {!v.appliedAt && (
138 <span className="ml-2">
139 <Badge tone="info">Coming</Badge>
140 </span>
141 )}
142 {cells.note && <span className="block text-xs text-faint">{cells.note}</span>}
143 </td>
144 <td className="tabular px-4 py-2.5 text-right">{cells.cost === "—" ? <span className="text-faint">—</span> : cells.cost}</td>
145 <td className="tabular px-4 py-2.5 text-right">{cells.price}</td>
146 <td className="px-4 py-2.5 text-xs text-muted">
147 <When at={v.effectiveAt} time />
148 </td>
149 <td className="px-4 py-2.5 text-xs text-faint sm:pr-5">
150 {v.reason} · {v.createdBy}
151 </td>
152 </tr>
153 );
154 })}
155 </tbody>
156 </table>
157 </div>
158 </Section>
159
160 <Section
161 className="mt-6"
162 title="Cloudflare's lines"
163 description="Every meter Cloudflare billed or counted in the range, as it named it, and which of g1t's products it is a cost of. Not mapped means no one decided what pays for it."
164 >
165 {report.lines.length === 0 ? (
166 <EmptyState title="No lines yet">The bill is read once a day at 04:17 UTC, or now with “Run the analysis now”.</EmptyState>
167 ) : (
168 <div className="-mx-4 overflow-x-auto sm:-mx-5">
169 <table className="w-full min-w-3xl text-sm">
170 <thead>
171 <tr className="border-b border-line text-left text-xs text-muted">
172 <th className="px-4 py-2 font-medium sm:px-5">Product / meter</th>
173 <th className="px-4 py-2 text-right font-medium">Quantity</th>
174 <th className="px-4 py-2 text-right font-medium">Cost</th>
175 <th className="px-4 py-2 font-medium sm:pr-5">Cost of</th>
176 </tr>
177 </thead>
178 <tbody>
179 {report.lines.map((l) => (
180 <tr key={`${l.source}-${l.product}-${l.meter}`} className="border-b border-line align-top last:border-0">
181 <td className="px-4 py-2.5 sm:px-5">
182 <span className="font-mono text-xs">
183 {l.product} / {l.meter}
184 </span>
185 <span className="block text-xs text-faint">
186 {l.rawName}
187 {l.source === "artifacts_events" ? " (Artifacts' own count)" : ""}
188 </span>
189 </td>
190 <td className="tabular px-4 py-2.5 text-right">
191 {countLabel(l.quantity)} <span className="text-xs text-faint">{l.unit}</span>
192 </td>
193 <td className="tabular px-4 py-2.5 text-right">{usd(l.costMicros)}</td>
194 <td className="px-4 py-2.5 sm:pr-5">
195 {l.bucket ? <span className="text-fg-soft">{l.bucket}</span> : <Badge tone="danger">Not mapped</Badge>}
196 </td>
197 </tr>
198 ))}
199 </tbody>
200 </table>
201 </div>
202 )}
203 </Section>
204
205 <Section
206 className="mt-6"
207 id="mappings"
208 title="Mappings"
209 description="Which of g1t's products each Cloudflare meter is a cost of. The longest matching meter prefix wins; * is the rest of the product. A price meter is measured from it; an own meter is g1t's count to compare; scaling prices one of g1t's units at as many of Cloudflare's as it took."
210 >
211 <MappingsTable report={report} />
212 <form method="post" action="#mappings" className="mt-5 grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
213 <input type="hidden" name="intent" value="mapping" />
214 <Field label="Cloudflare product">
215 <Input name="product" placeholder="artifacts" defaultValue={failed?.section === "mapping" ? failed.values?.product : ""} required />
216 </Field>
217 <Field label="Meter prefix" hint="Or * for all of it">
218 <Input name="meter" placeholder="*" defaultValue={failed?.section === "mapping" ? failed.values?.meter : ""} />
219 </Field>
220 <Field label="g1t product">
221 <Input name="bucket" placeholder="git" defaultValue={failed?.section === "mapping" ? failed.values?.bucket : ""} />
222 </Field>
223 <Field label="Drift threshold, %">
224 <Input name="driftPercent" inputMode="decimal" placeholder="10" defaultValue={failed?.section === "mapping" ? failed.values?.driftPercent : ""} />
225 </Field>
226 <Field label="Price meter" hint="Optional">
227 <Input name="priceMeter" placeholder="git_operations" defaultValue={failed?.section === "mapping" ? failed.values?.priceMeter : ""} />
228 </Field>
229 <Field label="Own meter" hint="Optional">
230 <Input name="ownMeter" placeholder="git_operations" defaultValue={failed?.section === "mapping" ? failed.values?.ownMeter : ""} />
231 </Field>
232 <Field label="Note">
233 <Input name="note" defaultValue={failed?.section === "mapping" ? failed.values?.note : ""} />
234 </Field>
235 <div className="flex flex-col justify-end gap-2">
236 <label className="flex items-center gap-2 text-sm text-muted">
237 <input type="checkbox" name="scaleToOwn" className="accent-(--g1t-accent)" /> Scale to g1t's count
238 </label>
239 <div className="flex gap-2">
240 <Button type="submit">Save mapping</Button>
241 <Button type="submit" name="remove" value="1" variant="danger">
242 Remove
243 </Button>
244 </div>
245 </div>
246 {failed?.section === "mapping" && (
247 <div className="sm:col-span-2 lg:col-span-4">
248 <Notice tone="error">{failed.error}</Notice>
249 </div>
250 )}
251 </form>
252 </Section>
253
254 <Section className="mt-6" id="guardrails" title="Guardrails" description="What may change prices without a person, and when staff hear about margin.">
255 <form method="post" action="#guardrails" className="grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
256 <input type="hidden" name="intent" value="settings" />
257 <Field label="Apply small moves on their own">
258 <label className="flex h-9.5 items-center gap-2 text-sm text-fg-soft">
259 <input type="checkbox" name="autoApply" defaultChecked={report.settings.autoApply} className="accent-(--g1t-accent)" /> On
260 </label>
261 </Field>
262 <Field label="Up to, either way, %">
263 <Input name="autoApplyPercent" inputMode="decimal" defaultValue={String(report.settings.autoApplyPercent)} />
264 </Field>
265 <Field label="Notice for a rise, days">
266 <Input name="noticeDays" inputMode="numeric" defaultValue={String(report.settings.noticeDays)} />
267 </Field>
268 <Field label="Margin floor, %">
269 <Input name="marginFloorPercent" inputMode="decimal" defaultValue={String(report.settings.marginFloorPercent)} />
270 </Field>
271 <Field label="Alert after, days in a row">
272 <Input name="alertDays" inputMode="numeric" defaultValue={String(report.settings.alertDays)} />
273 </Field>
274 <Field label="Ignore days costing under, $">
275 <Input name="minDailyCost" inputMode="decimal" defaultValue={dollarsField(report.settings.minDailyCostMicros)} />
276 </Field>
277 <Field label="Flag a workspace costing more than its revenue ×">
278 <Input name="anomalyFactor" inputMode="decimal" defaultValue={String(report.settings.anomalyFactor)} />
279 </Field>
280 <Field label="And at least, $">
281 <Input name="anomalyFloor" inputMode="decimal" defaultValue={dollarsField(report.settings.anomalyFloorMicros)} />
282 </Field>
283 <div className="sm:col-span-2 lg:col-span-4">
284 {failed?.section === "settings" && (
285 <div className="mb-3">
286 <Notice tone="error">{failed.error}</Notice>
287 </div>
288 )}
289 <Button type="submit">Save guardrails</Button>
290 </div>
291 </form>
292 </Section>
293 </main>
294 );
295}
296
297function ProposalRow({ proposal: p, error }: { proposal: PriceProposal; error: string | null }) {
298 const rise = p.proposedCostMicros > p.currentCostMicros;
299 const outcome = proposalOutcome(p);
300 const statusTone = p.status === "open" ? "warn" : p.status === "rejected" || p.status === "superseded" ? "plain" : "mint";
301 return (
302 <li className="rounded-md border border-line px-4 py-3">
303 <div className="flex flex-wrap items-baseline justify-between gap-2">
304 <div className="min-w-0">
305 <span className="font-medium">{p.title}</span> <span className="font-mono text-xs text-faint">{p.meter}</span>
306 </div>
307 <div className="flex items-center gap-2">
308 {p.suspect && <Badge tone="danger">Far off: look first</Badge>}
309 <Badge tone={statusTone}>{p.status}</Badge>
310 </div>
311 </div>
312 <p className="tabular mt-1.5 text-sm">
313 Cost {unitDollars(p.currentCostMicros)} → <span className={rise ? "text-warn" : "text-success"}>{unitDollars(p.proposedCostMicros)}</span> per{" "}
314 {p.unit || "unit"} <span className="text-faint">({percentLabel(p.changePercent, { signed: true })})</span>
315 </p>
316 <p className="mt-1 text-xs text-muted">{p.reason}</p>
317 <p className="mt-1 text-xs text-faint">
318 From the {p.source}, <When at={p.createdAt} time />
319 {outcome ? ` · ${outcome}` : ""}
320 {p.effectiveAt && p.status !== "superseded" ? (
321 <>
322 {" "}
323 · in force from <When at={p.effectiveAt} time />
324 </>
325 ) : null}
326 {p.note ? ` · “${p.note}”` : ""}
327 </p>
328 {p.status === "open" && (
329 <form method="post" action="#proposals" className="mt-3 flex flex-col gap-2 sm:flex-row sm:items-center">
330 <input type="hidden" name="intent" value="decide" />
331 <input type="hidden" name="id" value={p.id} />
332 <Input name="note" placeholder="A note (needed to reject)" className="sm:max-w-sm" />
333 <div className="flex gap-2">
334 <Button type="submit" name="decision" value="approve" variant="lavender">
335 Approve
336 </Button>
337 <Button type="submit" name="decision" value="reject" variant="quiet">
338 Reject
339 </Button>
340 </div>
341 </form>
342 )}
343 {error && (
344 <div className="mt-2">
345 <Notice tone="error">{error}</Notice>
346 </div>
347 )}
348 </li>
349 );
350}
351
352/**
353 * Cloudflare's current billing cycle, the way its Billable usage page shows
354 * it: each meter's use, what the cycle includes, what is past it and what
355 * that costs, with the total so far and the projection.
356 */
357function CycleSection({ report }: { report: CostsReport }) {
358 const cycle = report.cycle;
359 const read = report.billRead ? billReadNote(report.billRead) : null;
360 const unpriced = cycle ? unpricedMeters(cycle.meters) : [];
361 const headline = cycle ? cycleHeadline(cycle) : null;
362 return (
363 <Section
364 className="mt-5"
365 id="cycle"
366 title="This billing cycle"
367 description="Cloudflare's usage bill for the cycle so far, meter by meter. Each meter's included amount is the account's, once a cycle; the cost is Cloudflare's own where its lines carry one, else the list price past the included amount (per-million meters in whole millions), on the days the cycle's total passed it. Subscriptions are apart."
368 >
369 {!cycle || !headline ? (
370 <EmptyState title="No cycle read yet">The bill is read once a day at 04:17 UTC, or now with “Run the analysis now”.</EmptyState>
371 ) : (
372 <>
373 <p className="text-sm">
374 <span className="font-medium text-fg">{headline.title}</span>
375 <span className="block text-muted">{headline.detail}.</span>
376 </p>
377 {unpriced.length > 0 && (
378 <div className="mt-3">
379 <Notice tone="warn">
380 Used with no list price, so counted at $0 until Cloudflare puts a cost on it or a price is added (src/cycle.rs):{" "}
381 {unpriced.map((m) => `${m.product} / ${m.meter}`).join(", ")}.
382 </Notice>
383 </div>
384 )}
385 <div className="-mx-4 mt-3 overflow-x-auto sm:-mx-5">
386 <table className="w-full min-w-3xl text-sm">
387 <thead>
388 <tr className="border-b border-line text-left text-xs text-muted">
389 <th className="px-4 py-2 font-medium sm:px-5">Meter</th>
390 <th className="px-4 py-2 text-right font-medium">Used</th>
391 <th className="px-4 py-2 text-right font-medium">Included</th>
392 <th className="px-4 py-2 text-right font-medium">Billable</th>
393 <th className="px-4 py-2 text-right font-medium sm:pr-5">Cost</th>
394 </tr>
395 </thead>
396 <tbody>
397 {cycle.meters.map((m) => (
398 <tr key={`${m.product}-${m.meter}`} className="border-b border-line align-top last:border-0">
399 <td className="px-4 py-2.5 sm:px-5">
400 {m.rawName}
401 <span className="block text-xs text-faint">{basisLabel(m.basis)}</span>
402 </td>
403 <td className="tabular px-4 py-2.5 text-right">
404 {countLabel(m.quantity)} <span className="text-xs text-faint">{m.unit}</span>
405 </td>
406 <td className="tabular px-4 py-2.5 text-right text-muted">{m.included == null ? "—" : countLabel(m.included)}</td>
407 <td className="tabular px-4 py-2.5 text-right">{countLabel(m.billableQuantity)}</td>
408 <td className="tabular px-4 py-2.5 text-right sm:pr-5">{usd(m.costMicros)}</td>
409 </tr>
410 ))}
411 <tr>
412 <td className="px-4 py-2.5 font-medium sm:px-5" colSpan={4}>
413 Usage so far
414 </td>
415 <td className="tabular px-4 py-2.5 text-right font-medium sm:pr-5">{usd(cycle.usageMicros, { cents: true })}</td>
416 </tr>
417 </tbody>
418 </table>
419 </div>
420 </>
421 )}
422 {read && (
423 <p className={`mt-3 text-xs ${read.warn ? "text-warn" : "text-faint"}`}>
424 Last read <When at={report.billRead?.readAt ?? null} time />: {read.text}
425 </p>
426 )}
427 </Section>
428 );
429}
430
431function MappingsTable({ report }: { report: CostsReport }) {
432 return (
433 <div className="-mx-4 overflow-x-auto sm:-mx-5">
434 <table className="w-full min-w-176 text-sm">
435 <thead>
436 <tr className="border-b border-line text-left text-xs text-muted">
437 <th className="px-4 py-2 font-medium sm:px-5">Cloudflare</th>
438 <th className="px-4 py-2 font-medium">g1t product</th>
439 <th className="px-4 py-2 font-medium">Price meter</th>
440 <th className="px-4 py-2 font-medium">Own meter</th>
441 <th className="px-4 py-2 text-right font-medium sm:pr-5">Drift at</th>
442 </tr>
443 </thead>
444 <tbody>
445 {report.mappings.map((m) => (
446 <tr key={`${m.product}-${m.meter}`} className="border-b border-line align-top last:border-0">
447 <td className="px-4 py-2 sm:px-5">
448 <span className="font-mono text-xs">
449 {m.product} / {m.meter}
450 </span>
451 {m.note && <span className="block text-xs text-faint">{m.note}</span>}
452 </td>
453 <td className="px-4 py-2 text-fg-soft">{m.bucket}</td>
454 <td className="px-4 py-2 font-mono text-xs text-muted">{m.priceMeter ?? "—"}</td>
455 <td className="px-4 py-2 font-mono text-xs text-muted">
456 {m.ownMeter ?? "—"}
457 {m.scaleToOwn && <span className="block font-sans text-faint">scaled</span>}
458 </td>
459 <td className="tabular px-4 py-2 text-right text-muted sm:pr-5">{m.driftPercent}%</td>
460 </tr>
461 ))}
462 </tbody>
463 </table>
464 </div>
465 );
466}