mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-12 18:22:48 +03:00
feat(db): track API endpoint dimension on usage_history
Add an `endpoint` column to `usage_history` (migration 103) so the proxy records which API entry point a request came through — typically `/v1/chat/completions`, `/v1/messages`, or `/v1/responses`. Every `saveRequestUsage(...)` call site in `open-sse/handlers/chatCore.ts` (success, stream, failure) and the codex-responses-ws route now plumbs the endpoint through, and a new `getEndpointUsageRows(...)` analytics query in `src/lib/db/usageAnalytics.ts` aggregates per endpoint × provider × model with NULL → 'unknown' folding for backward compatibility with rows from before the migration. The aggregation reads directly from `usage_history` (matching the `getAutoRoutingVariantBreakdown` pattern) so the existing unified-source CTE — and the `daily_usage_summary` rollup it joins — stay untouched. TDD: tests/unit/usage-endpoint-dimension.test.ts covers persistence round-trip, NULL fallback, and the sinceIso filter (3/3 passing). Co-authored-by: toanalien <toanalien@gmail.com> Inspired-by: https://github.com/decolua/9router/pull/152
This commit is contained in:
@@ -433,6 +433,7 @@ export async function handleChatCore({
|
||||
statusCode,
|
||||
errorCode,
|
||||
latencyMs: Date.now() - startTime,
|
||||
endpoint: endpointPath,
|
||||
})
|
||||
).catch(() => {});
|
||||
};
|
||||
@@ -3422,6 +3423,7 @@ export async function handleChatCore({
|
||||
effectiveServiceTier,
|
||||
isCombo,
|
||||
comboStrategy,
|
||||
endpoint: endpointPath,
|
||||
});
|
||||
|
||||
// Translate response to client's expected format (usually OpenAI)
|
||||
@@ -3806,6 +3808,7 @@ export async function handleChatCore({
|
||||
effectiveServiceTier,
|
||||
isCombo,
|
||||
comboStrategy,
|
||||
endpoint: endpointPath,
|
||||
});
|
||||
|
||||
persistAttemptLogs({
|
||||
|
||||
@@ -19,6 +19,7 @@ export function buildFailureUsageRecord(opts: {
|
||||
statusCode: number;
|
||||
errorCode: string | null | undefined;
|
||||
latencyMs: number;
|
||||
endpoint?: string | null | undefined;
|
||||
}) {
|
||||
return {
|
||||
provider: opts.provider || "unknown",
|
||||
@@ -35,5 +36,6 @@ export function buildFailureUsageRecord(opts: {
|
||||
apiKeyName: opts.apiKeyInfo?.name || undefined,
|
||||
serviceTier: opts.effectiveServiceTier,
|
||||
comboStrategy: opts.isCombo ? opts.comboStrategy || undefined : undefined,
|
||||
endpoint: opts.endpoint || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ export type RecordNonStreamingUsageStatsContext = {
|
||||
effectiveServiceTier: EffectiveServiceTier;
|
||||
isCombo: boolean;
|
||||
comboStrategy: string | null | undefined;
|
||||
endpoint?: string | null | undefined;
|
||||
};
|
||||
|
||||
function logUsageTrace(
|
||||
@@ -55,6 +56,7 @@ function persistUsageRow(usage: object, ctx: RecordNonStreamingUsageStatsContext
|
||||
apiKeyName: apiKeyInfo?.name || undefined,
|
||||
serviceTier: effectiveServiceTier,
|
||||
comboStrategy: ctx.isCombo ? ctx.comboStrategy || undefined : undefined,
|
||||
endpoint: ctx.endpoint || undefined,
|
||||
}).catch((err) => {
|
||||
console.error("Failed to save usage stats:", err.message);
|
||||
});
|
||||
|
||||
@@ -27,6 +27,7 @@ export type RecordStreamingUsageStatsContext = {
|
||||
effectiveServiceTier: EffectiveServiceTier;
|
||||
isCombo: boolean;
|
||||
comboStrategy: string | null | undefined;
|
||||
endpoint?: string | null | undefined;
|
||||
};
|
||||
|
||||
function persistStreamingUsageRow(usage: object, ctx: RecordStreamingUsageStatsContext): void {
|
||||
@@ -46,6 +47,7 @@ function persistStreamingUsageRow(usage: object, ctx: RecordStreamingUsageStatsC
|
||||
apiKeyName: ctx.apiKeyInfo?.name || undefined,
|
||||
serviceTier: ctx.effectiveServiceTier,
|
||||
comboStrategy: ctx.isCombo ? ctx.comboStrategy || undefined : undefined,
|
||||
endpoint: ctx.endpoint || undefined,
|
||||
}).catch((err) => {
|
||||
console.error("Failed to save usage stats:", err.message);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user