Skip to content

Commit e732d02

Browse files
committed
fix(analytics): valid RI-01 decisionIds in tests; simplify helpers and query validation (RI-03)
1 parent a91550c commit e732d02

3 files changed

Lines changed: 40 additions & 32 deletions

File tree

‎src/routing/analytics.ts‎

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,19 @@ function cooldownTriggering(entry: PersistedUsageEntry | null, status: number):
152152
return attempts.some(attempt => attempt.recoveryKinds.some(kind => COOLDOWN_RECOVERY_KINDS.has(kind)));
153153
}
154154

155-
function isSuccessStatus(status: number): boolean {
156-
return status >= 200 && status < 400;
155+
function successCostUsd(
156+
row: Pick<ScannedRow, "provider" | "model">,
157+
entry: PersistedUsageEntry,
158+
): number | null {
159+
if (!entry.usage) return null;
160+
const estimate = estimateRequestCost({
161+
provider: row.provider,
162+
model: row.model,
163+
usage: entry.usage,
164+
usageStatus: entry.usageStatus,
165+
serviceTier: serviceTierContext(entry),
166+
});
167+
return estimate ? estimate.cost.total : null;
157168
}
158169

159170
export async function computeRoutingAnalytics(
@@ -227,16 +238,11 @@ export async function computeRoutingAnalytics(
227238
const entry = kind === "success" || row.status >= 400 ? parseEntry(row.rowJson) : null;
228239
if (cooldownTriggering(entry, row.status)) cooldownFailures += 1;
229240

230-
if (kind === "success" && entry?.usage) {
231-
const estimate = estimateRequestCost({
232-
provider: row.provider,
233-
model: row.model,
234-
usage: entry.usage,
235-
usageStatus: entry.usageStatus,
236-
serviceTier: serviceTierContext(entry),
237-
});
238-
if (estimate) {
239-
costTotalUsd += estimate.cost.total;
241+
let rowCostUsd: number | null = null;
242+
if (kind === "success" && entry) {
243+
rowCostUsd = successCostUsd(row, entry);
244+
if (rowCostUsd !== null) {
245+
costTotalUsd += rowCostUsd;
240246
costCount += 1;
241247
}
242248
}
@@ -265,18 +271,9 @@ export async function computeRoutingAnalytics(
265271
else if (kind === "failure") bucket.failures += 1;
266272
else bucket.cancelled += 1;
267273
bucket.durations.push(row.durationMs);
268-
if (kind === "success" && entry?.usage) {
269-
const estimate = estimateRequestCost({
270-
provider: row.provider,
271-
model: row.model,
272-
usage: entry.usage,
273-
usageStatus: entry.usageStatus,
274-
serviceTier: serviceTierContext(entry),
275-
});
276-
if (estimate) {
277-
bucket.costUsdSum += estimate.cost.total;
278-
bucket.costRows += 1;
279-
}
274+
if (rowCostUsd !== null) {
275+
bucket.costUsdSum += rowCostUsd;
276+
bucket.costRows += 1;
280277
}
281278

282279
if (row.profileId) {
@@ -307,6 +304,7 @@ export async function computeRoutingAnalytics(
307304

308305
const breakdown: AnalyticsBreakdownRow[] = [...byKey.values()].map(bucket => {
309306
const sorted = bucket.durations.sort((a, b) => a - b);
307+
const p50DurationMs = percentile(sorted, 50);
310308
return {
311309
provider: bucket.provider,
312310
model: bucket.model,
@@ -317,7 +315,7 @@ export async function computeRoutingAnalytics(
317315
failures: bucket.failures,
318316
cancelled: bucket.cancelled,
319317
successRate: bucket.requests > 0 ? bucket.successes / bucket.requests : null,
320-
...(percentile(sorted, 50) !== undefined ? { p50DurationMs: percentile(sorted, 50) } : {}),
318+
...(p50DurationMs !== undefined ? { p50DurationMs } : {}),
321319
...(bucket.requests > 0
322320
? { estimatedCostUsdPerSuccessfulRequest: bucket.costRows > 0
323321
? bucket.costUsdSum / bucket.costRows

‎src/server/management/routing-analytics-routes.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,28 @@ import { computeRoutingAnalytics } from "../../routing/analytics";
99
import { jsonResponse } from "../auth-cors";
1010
import type { ManagementContext } from "./context";
1111

12-
function parseOptionalInt(raw: string | null): number | undefined {
12+
function parseQueryInt(raw: string | null): number | undefined | "invalid" {
1313
if (raw === null) return undefined;
14-
const value = Number(raw.trim());
15-
return Number.isInteger(value) ? value : undefined;
14+
const trimmed = raw.trim();
15+
if (trimmed.length === 0) return "invalid";
16+
const value = Number(trimmed);
17+
return Number.isInteger(value) ? value : "invalid";
1618
}
1719

1820
export async function handleRoutingAnalyticsRoutes(ctx: ManagementContext): Promise<Response | null> {
1921
const { url, req, config } = ctx;
2022
if (url.pathname !== "/api/routing-analytics" || req.method !== "GET") return null;
2123

22-
const from = parseOptionalInt(url.searchParams.get("from"));
23-
const to = parseOptionalInt(url.searchParams.get("to"));
24+
const fromParsed = parseQueryInt(url.searchParams.get("from"));
25+
if (fromParsed === "invalid") {
26+
return jsonResponse({ error: { code: "invalid_from", message: "from must be an integer timestamp" } }, 400, req, config);
27+
}
28+
const toParsed = parseQueryInt(url.searchParams.get("to"));
29+
if (toParsed === "invalid") {
30+
return jsonResponse({ error: { code: "invalid_to", message: "to must be an integer timestamp" } }, 400, req, config);
31+
}
32+
const from = fromParsed;
33+
const to = toParsed;
2434
if (from !== undefined && to !== undefined && from > to) {
2535
return jsonResponse({ error: { code: "invalid_range", message: "from must not be after to" } }, 400, req, config);
2636
}

‎tests/routing-analytics.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe("routing analytics (RI-03)", () => {
114114
apiKeyId: "key-a",
115115
routeDecision: {
116116
version: 1,
117-
decisionId: "d1",
117+
decisionId: "a00000000001",
118118
createdAt: 1,
119119
requestedModel: "policy/fast",
120120
routeKind: "policy",
@@ -131,7 +131,7 @@ describe("routing analytics (RI-03)", () => {
131131
apiKeyId: "key-a",
132132
routeDecision: {
133133
version: 1,
134-
decisionId: "d2",
134+
decisionId: "a00000000002",
135135
createdAt: 2,
136136
requestedModel: "policy/fast",
137137
routeKind: "policy",

0 commit comments

Comments
 (0)