GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #3445 from developing-today/queue/5350-2493-billin… · developing-today/code@b09c9ce

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pm-stack/aggregator
    • src/sources
      • github-billing.ts
    • test
      • github-billing.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pm-stack/aggregator/src/sources/github-billing.ts‎

Copy file name to clipboardExpand all lines: pm-stack/aggregator/src/sources/github-billing.ts
+18-3Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
// gh call and the outbox append; tests feed literals.
2323
import type { SnapshotRecord } from "../model.ts";
2424
import { dayTs } from "../snapshot-records.ts";
25+
import { isIsoDate, isNonNegative } from "../validate.ts";
2526

2627
export interface BillingUsageItem {
2728
date?: string;
@@ -74,14 +75,28 @@ function codespacesCoreMultiplier(sku: unknown): number {
7475
return m ? Number(m[1]) : 1;
7576
}
7677

78+
/** A numeric endpoint field contributes only when it is a finite,
79+
* non-negative number (#2493): a negative or non-finite quantity or
80+
* amount is not usage, and a negative derived record would pass
81+
* isSnapshotRecord and be stored. Anything else contributes 0,
82+
* extending the existing non-number fallback. */
83+
function usageNumber(v: unknown): number {
84+
return isNonNegative(v) ? v : 0;
85+
}
86+
7787
/** Per-day aggregates from raw endpoint items, oldest day first. */
7888
export function aggregateBillingUsage(items: BillingUsageItem[]): BillingDayAggregate[] {
7989
const byDate = new Map<string, BillingDayAggregate>();
8090
for (const item of items) {
8191
if (typeof item.date !== "string" || item.date.length < 10) continue;
8292
const date = item.date.slice(0, 10);
93+
// The bucket date must be a real calendar date (#2493): an
94+
// impossible date (month 13, Feb 30) derives snapshot records
95+
// that fail isSnapshotRecord, and the collector throws on ANY
96+
// invalid record — discarding the whole month's collection.
97+
if (!isIsoDate(date)) continue;
8398
const day = byDate.get(date) ?? emptyDay(date);
84-
const qty = typeof item.quantity === "number" ? item.quantity : 0;
99+
const qty = usageNumber(item.quantity);
85100
// Classification is case-insensitive: the endpoint's documented
86101
// response example spells product/unitType "Actions"/"minutes"
87102
// while live rows arrive as "actions"/"Minutes" — both must
@@ -96,8 +111,8 @@ export function aggregateBillingUsage(items: BillingUsageItem[]): BillingDayAggr
96111
else if (product === "codespaces" && unit === "gigabytehours")
97112
day.codespacesStorageGbHours += qty;
98113
// 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;
114+
day.grossUsd += usageNumber(item.grossAmount);
115+
day.netUsd += usageNumber(item.netAmount);
101116
byDate.set(date, day);
102117
}
103118
return [...byDate.values()]
Collapse file

‎pm-stack/aggregator/test/github-billing.test.ts‎

Copy file name to clipboardExpand all lines: pm-stack/aggregator/test/github-billing.test.ts
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,45 @@ describe("aggregateBillingUsage", () => {
5454
});
5555
});
5656

57+
describe("aggregateBillingUsage validation (#2493)", () => {
58+
test("impossible dates never create a day bucket", () => {
59+
const days = aggregateBillingUsage([
60+
{ date: "2026-13-01", product: "actions", unitType: "Minutes", quantity: 10, grossAmount: 1, netAmount: 1 },
61+
{ date: "2026-02-30", product: "actions", unitType: "Minutes", quantity: 10, grossAmount: 1, netAmount: 1 },
62+
{ date: "2026-10-03", product: "actions", unitType: "Minutes", quantity: 7, grossAmount: 0.1, netAmount: 0.1 },
63+
]);
64+
expect(days.length).toBe(1);
65+
expect(days[0].date).toBe("2026-10-03");
66+
expect(days[0].actionsMinutes).toBe(7);
67+
});
68+
69+
test("negative or non-finite numbers contribute 0, never usage", () => {
70+
const days = aggregateBillingUsage([
71+
{ date: "2026-10-04", product: "actions", unitType: "Minutes", quantity: -50, grossAmount: -2, netAmount: -2 },
72+
{ date: "2026-10-04", product: "actions", unitType: "Minutes", quantity: Number.NaN, grossAmount: Number.POSITIVE_INFINITY, netAmount: 0.5 },
73+
{ date: "2026-10-04", product: "actions", unitType: "Minutes", quantity: 30, grossAmount: 0.25, netAmount: 0 },
74+
]);
75+
expect(days.length).toBe(1);
76+
expect(days[0].actionsMinutes).toBe(30);
77+
expect(days[0].grossUsd).toBe(0.25);
78+
expect(days[0].netUsd).toBe(0.5);
79+
});
80+
81+
test("records derived from hostile items all pass isSnapshotRecord", () => {
82+
const records = recordsFromBillingUsage(
83+
[
84+
{ date: "2026-13-40", product: "actions", unitType: "Minutes", quantity: 10, grossAmount: 1, netAmount: 1 },
85+
{ date: "2026-10-05", product: "actions", unitType: "Minutes", quantity: -10, grossAmount: -1, netAmount: -1 },
86+
{ date: "2026-10-05", product: "codespaces", sku: "Codespaces 4-core", unitType: "Hours", quantity: 1.5, grossAmount: 0.4, netAmount: 0.4 },
87+
],
88+
"test",
89+
);
90+
expect(records.length).toBeGreaterThan(0);
91+
for (const r of records) expect(isSnapshotRecord(r)).toBe(true);
92+
expect(records.every((r) => r.value > 0)).toBe(true);
93+
});
94+
});
95+
5796
describe("recordsFromBillingUsage", () => {
5897
const records = recordsFromBillingUsage(ITEMS, "test");
5998

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.