fix(stats): ignore requests rejected before routing (#59773)

Co-authored-by: adamdotdevin <2363879+adamdotdevin@users.noreply.github.com>
This commit is contained in:
opencode-agent[bot]
2026-10-28 04:38:10 -05:00
committed by GitHub
Unverified
parent 587f2cbb82
commit 62ae125d94
2 changed files with 18 additions and 0 deletions
@@ -283,6 +283,19 @@ describe("inference stat normalization", () => {
queries.forEach((query) => expect(query).toContain("lower(model) NOT IN ('alpha-gpt-next', 'hidden-model')"))
})
test("excludes requests rejected before routing from stats and retention queries", () => {
const source = { namespace: "inference", table: "generation", dataset: "zen" }
const queries = [
...buildStatsQueries(new Date("2026-08-10T00:00:00.000Z"), new Date("2026-08-11T00:00:00.000Z"), source),
...buildRetentionQueries(new Date("2026-08-10T00:00:00.000Z"), new Date("2026-08-24T00:00:00.000Z"), source).map(
(item) => item.query,
),
]
expect(queries.length).toBeGreaterThan(0)
queries.forEach((query) => expect(query).toContain("AND attempts > 0\n"))
})
test("builds bounded R2 SQL queries for each day and week", () => {
const queries = buildStatsQueries(new Date("2026-08-10T00:00:00.000Z"), new Date("2026-08-12T12:00:00.000Z"), {
namespace: "inference",
@@ -43,6 +43,9 @@ const WEEK_MS = 7 * DAY_MS
const LIVE_SOURCE_START = "2026-08-11T10:57:48.186Z"
// Go Plus is a level of the Go subscription, but the lake records it as its own product.
const GO_PRODUCTS = ["go", "go-plus"]
// Requests rejected before routing (unknown model, auth, rate limit) never reach an upstream
// and carry no usage. Counting them would publish any requested model name as a stats page.
const ROUTED_ONLY = "attempts > 0"
// R2 SQL results are cursor-paginated after aggregation. Separate usage and geo
// queries per day/week avoid combining costly distinct user/session aggregates
@@ -138,6 +141,7 @@ WITH normalized AS (
OR (source = 'inference' AND started_at >= ${sqlString(LIVE_SOURCE_START)})
)
AND product IN (${GO_PRODUCTS.map(sqlString).join(", ")})
AND ${ROUTED_ONLY}
AND model_requested IS NOT NULL
AND model_requested <> ''
AND __ingest_ts >= ${scanStartValue}
@@ -302,6 +306,7 @@ WITH normalized AS (
OR (source = 'inference' AND started_at >= ${sqlString(LIVE_SOURCE_START)})
)
AND (product IN (${GO_PRODUCTS.map(sqlString).join(", ")}) OR (${sourceFreeTier}))
AND ${ROUTED_ONLY}
AND model_requested IS NOT NULL
AND model_requested <> ''
AND __ingest_ts >= ${periodStartValue}