(periodStart: Date, periodEnd: Date, dimension: StatDimension)
| 15 | export type StatDimension = "model" | "provider" | "geo" | "geo_model" |
| 16 | |
| 17 | export function buildStatsQuery(periodStart: Date, periodEnd: Date, dimension: StatDimension) { |
| 18 | const periodStartValue = sqlString(periodStart.toISOString()) |
| 19 | const periodEndValue = sqlString(periodEnd.toISOString()) |
| 20 | const periodStartDateValue = sqlString(periodStart.toISOString().slice(0, 10)) |
| 21 | const periodEndDateValue = sqlString(periodEnd.toISOString().slice(0, 10)) |
| 22 | const sourceTable = [Resource.InferenceEvent.catalog, Resource.InferenceEvent.database, Resource.InferenceEvent.table] |
| 23 | .map(sqlIdentifier) |
| 24 | .join(".") |
| 25 | const dimensionSql = (() => { |
| 26 | if (dimension === "model") |
| 27 | return { |
| 28 | select: "provider, model, COALESCE(MAX(NULLIF(provider_model, '')), '') AS provider_model", |
| 29 | groupBy: "provider, model", |
| 30 | } |
| 31 | if (dimension === "provider") return { select: "provider", groupBy: "provider" } |
| 32 | if (dimension === "geo_model") |
| 33 | return { |
| 34 | select: "provider, model, country, COALESCE(MAX(NULLIF(continent, '')), '') AS continent", |
| 35 | groupBy: "provider, model, country", |
| 36 | } |
| 37 | return { |
| 38 | select: "'all' AS provider, 'all' AS model, country, COALESCE(MAX(NULLIF(continent, '')), '') AS continent", |
| 39 | groupBy: "country", |
| 40 | } |
| 41 | })() |
| 42 | const aggregateColumns = ` |
| 43 | COUNT(DISTINCT session) AS sessions, |
| 44 | COUNT(*) AS requests, |
| 45 | COUNT(DISTINCT user_key) AS unique_users, |
| 46 | COALESCE(SUM(tokens_input), 0) AS input_tokens, |
| 47 | COALESCE(SUM(tokens_output), 0) AS output_tokens, |
| 48 | COALESCE(SUM(tokens_reasoning), 0) AS reasoning_tokens, |
| 49 | COALESCE(SUM(tokens_cache_read), 0) AS cache_read_tokens, |
| 50 | COALESCE(SUM(tokens_total), 0) AS total_tokens, |
| 51 | COALESCE(SUM(cost_input_microcents), 0) AS input_cost_microcents, |
| 52 | COALESCE(SUM(cost_output_microcents), 0) AS output_cost_microcents, |
| 53 | COALESCE(SUM(cost_total_microcents), 0) AS total_cost_microcents, |
| 54 | AVG(duration_ms) AS avg_duration_ms, |
| 55 | approx_percentile(CAST(duration_ms AS double), 0.5) AS p50_duration_ms, |
| 56 | approx_percentile(CAST(duration_ms AS double), 0.95) AS p95_duration_ms, |
| 57 | AVG(ttfb_ms) AS avg_ttfb_ms, |
| 58 | approx_percentile(CAST(ttfb_ms AS double), 0.5) AS p50_ttfb_ms, |
| 59 | approx_percentile(CAST(ttfb_ms AS double), 0.95) AS p95_ttfb_ms, |
| 60 | AVG(output_tps) AS avg_output_tps, |
| 61 | SUM(CASE WHEN status >= 200 AND status < 400 THEN 1 ELSE 0 END) AS success_count, |
| 62 | SUM(CASE WHEN status >= 400 THEN 1 ELSE 0 END) AS error_count, |
| 63 | COUNT(*) AS sample_count` |
| 64 | |
| 65 | return ` |
| 66 | WITH normalized AS ( |
| 67 | SELECT |
| 68 | from_iso8601_timestamp(event_timestamp) AS event_time, |
| 69 | model AS raw_model, |
| 70 | ${statModelSql("model", "provider_model")} AS model, |
| 71 | COALESCE(NULLIF(provider_model, ''), '') AS provider_model, |
| 72 | COALESCE(NULLIF(provider, ''), '') AS raw_provider, |
| 73 | UPPER(COALESCE(NULLIF(cf_country, ''), 'ZZ')) AS country, |
| 74 | COALESCE(NULLIF(cf_continent, ''), '') AS continent, |
no test coverage detected