Skip to content

Commit 62ae125

Browse files
fix(stats): ignore requests rejected before routing (#59773)
Co-authored-by: adamdotdevin <[email protected]>
1 parent 587f2cb commit 62ae125

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

‎packages/stats/core/src/domain/inference.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,19 @@ describe("inference stat normalization", () => {
283283
queries.forEach((query) => expect(query).toContain("lower(model) NOT IN ('alpha-gpt-next', 'hidden-model')"))
284284
})
285285

286+
test("excludes requests rejected before routing from stats and retention queries", () => {
287+
const source = { namespace: "inference", table: "generation", dataset: "zen" }
288+
const queries = [
289+
...buildStatsQueries(new Date("2026-08-10T00:00:00.000Z"), new Date("2026-08-11T00:00:00.000Z"), source),
290+
...buildRetentionQueries(new Date("2026-08-10T00:00:00.000Z"), new Date("2026-08-24T00:00:00.000Z"), source).map(
291+
(item) => item.query,
292+
),
293+
]
294+
295+
expect(queries.length).toBeGreaterThan(0)
296+
queries.forEach((query) => expect(query).toContain("AND attempts > 0\n"))
297+
})
298+
286299
test("builds bounded R2 SQL queries for each day and week", () => {
287300
const queries = buildStatsQueries(new Date("2026-08-10T00:00:00.000Z"), new Date("2026-08-12T12:00:00.000Z"), {
288301
namespace: "inference",

‎packages/stats/core/src/domain/inference.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ const WEEK_MS = 7 * DAY_MS
4343
const LIVE_SOURCE_START = "2026-08-11T10:57:48.186Z"
4444
// Go Plus is a level of the Go subscription, but the lake records it as its own product.
4545
const GO_PRODUCTS = ["go", "go-plus"]
46+
// Requests rejected before routing (unknown model, auth, rate limit) never reach an upstream
47+
// and carry no usage. Counting them would publish any requested model name as a stats page.
48+
const ROUTED_ONLY = "attempts > 0"
4649

4750
// R2 SQL results are cursor-paginated after aggregation. Separate usage and geo
4851
// queries per day/week avoid combining costly distinct user/session aggregates
@@ -138,6 +141,7 @@ WITH normalized AS (
138141
OR (source = 'inference' AND started_at >= ${sqlString(LIVE_SOURCE_START)})
139142
)
140143
AND product IN (${GO_PRODUCTS.map(sqlString).join(", ")})
144+
AND ${ROUTED_ONLY}
141145
AND model_requested IS NOT NULL
142146
AND model_requested <> ''
143147
AND __ingest_ts >= ${scanStartValue}
@@ -302,6 +306,7 @@ WITH normalized AS (
302306
OR (source = 'inference' AND started_at >= ${sqlString(LIVE_SOURCE_START)})
303307
)
304308
AND (product IN (${GO_PRODUCTS.map(sqlString).join(", ")}) OR (${sourceFreeTier}))
309+
AND ${ROUTED_ONLY}
305310
AND model_requested IS NOT NULL
306311
AND model_requested <> ''
307312
AND __ingest_ts >= ${periodStartValue}

0 commit comments

Comments
 (0)