From 5894ce441aeb1218db8fb6357653b1ab2dbe052c Mon Sep 17 00:00:00 2001 From: Adam <2363879+adamdotdevin@users.noreply.github.com> Date: Sat, 23 May 2026 06:15:41 -0500 Subject: [PATCH] wip: stats --- packages/stats/app/src/routes/rankings.tsx | 23 +- .../migration.sql | 94 + .../snapshot.json | 2041 +++++++++++++++++ packages/stats/core/src/cron/stat.ts | 456 +++- packages/stats/core/src/database/schema.ts | 149 +- packages/stats/core/src/domain/ranking.ts | 36 +- 6 files changed, 2655 insertions(+), 144 deletions(-) create mode 100644 packages/stats/core/migrations/20260523110335_cool_vin_gonzales/migration.sql create mode 100644 packages/stats/core/migrations/20260523110335_cool_vin_gonzales/snapshot.json diff --git a/packages/stats/app/src/routes/rankings.tsx b/packages/stats/app/src/routes/rankings.tsx index 5d21808db7..2e06210569 100644 --- a/packages/stats/app/src/routes/rankings.tsx +++ b/packages/stats/app/src/routes/rankings.tsx @@ -59,11 +59,11 @@ export default function Rankings() { @@ -105,7 +105,7 @@ function RankingsLoading() { <> - + ) @@ -157,7 +157,7 @@ function UsageSection(props: { data: RankingsData["usage"] }) { usageTotal(item) > 0)} - fallback={} + fallback={} > @@ -401,7 +401,9 @@ function LeaderboardSection(props: { data: RankingsData["leaderboard"] }) { > 0} - fallback={} + fallback={ + + } > @@ -479,7 +481,7 @@ function MarketShareSection(props: { data: RankingsData["market"] }) { } + fallback={} > {(day) => ( <> @@ -576,7 +578,7 @@ function TokenCostSection(props: { data: RankingsData["tokenCost"] }) { 0} fallback={ - + } > @@ -665,7 +667,10 @@ function SessionCostSection(props: { data: RankingsData["sessionCost"] }) { 0} fallback={ - + } > diff --git a/packages/stats/core/migrations/20260523110335_cool_vin_gonzales/migration.sql b/packages/stats/core/migrations/20260523110335_cool_vin_gonzales/migration.sql new file mode 100644 index 0000000000..4d5c0abce6 --- /dev/null +++ b/packages/stats/core/migrations/20260523110335_cool_vin_gonzales/migration.sql @@ -0,0 +1,94 @@ +CREATE TABLE `geo_stat` ( + `id` bigint AUTO_INCREMENT PRIMARY KEY, + `grain` varchar(16) NOT NULL, + `period_start` datetime NOT NULL, + `period_end` datetime NOT NULL, + `dataset` varchar(64) NOT NULL DEFAULT 'all', + `tier` varchar(64) NOT NULL DEFAULT 'all', + `client` varchar(64) NOT NULL DEFAULT 'all', + `source` varchar(64) NOT NULL DEFAULT 'all', + `country` char(2) NOT NULL, + `continent` varchar(8) NOT NULL DEFAULT '', + `sessions` bigint NOT NULL DEFAULT 0, + `requests` bigint NOT NULL DEFAULT 0, + `input_tokens` bigint NOT NULL DEFAULT 0, + `output_tokens` bigint NOT NULL DEFAULT 0, + `reasoning_tokens` bigint NOT NULL DEFAULT 0, + `cache_read_tokens` bigint NOT NULL DEFAULT 0, + `total_tokens` bigint NOT NULL DEFAULT 0, + `input_cost_microcents` bigint NOT NULL DEFAULT 0, + `output_cost_microcents` bigint NOT NULL DEFAULT 0, + `total_cost_microcents` bigint NOT NULL DEFAULT 0, + `avg_duration_ms` decimal(12,2), + `p50_duration_ms` int, + `p95_duration_ms` int, + `avg_ttfb_ms` decimal(12,2), + `p50_ttfb_ms` int, + `p95_ttfb_ms` int, + `avg_output_tps` decimal(12,4), + `success_count` bigint NOT NULL DEFAULT 0, + `error_count` bigint NOT NULL DEFAULT 0, + `sample_count` bigint NOT NULL DEFAULT 0, + `market_share_tokens` decimal(10,6), + `market_share_requests` decimal(10,6), + `market_share_sessions` decimal(10,6), + `rank_by_tokens` int, + `rank_by_requests` int, + `rank_by_sessions` int, + `rank_by_cost` int, + `created_at` datetime NOT NULL DEFAULT (now()), + `updated_at` datetime NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP, + CONSTRAINT `uniq_country_period` UNIQUE INDEX(`grain`,`period_start`,`dataset`,`tier`,`client`,`source`,`country`) +); +--> statement-breakpoint +CREATE TABLE `provider_stat` ( + `id` bigint AUTO_INCREMENT PRIMARY KEY, + `grain` varchar(16) NOT NULL, + `period_start` datetime NOT NULL, + `period_end` datetime NOT NULL, + `dataset` varchar(64) NOT NULL DEFAULT 'all', + `tier` varchar(64) NOT NULL DEFAULT 'all', + `client` varchar(64) NOT NULL DEFAULT 'all', + `source` varchar(64) NOT NULL DEFAULT 'all', + `provider` varchar(128) NOT NULL, + `sessions` bigint NOT NULL DEFAULT 0, + `requests` bigint NOT NULL DEFAULT 0, + `input_tokens` bigint NOT NULL DEFAULT 0, + `output_tokens` bigint NOT NULL DEFAULT 0, + `reasoning_tokens` bigint NOT NULL DEFAULT 0, + `cache_read_tokens` bigint NOT NULL DEFAULT 0, + `total_tokens` bigint NOT NULL DEFAULT 0, + `input_cost_microcents` bigint NOT NULL DEFAULT 0, + `output_cost_microcents` bigint NOT NULL DEFAULT 0, + `total_cost_microcents` bigint NOT NULL DEFAULT 0, + `avg_duration_ms` decimal(12,2), + `p50_duration_ms` int, + `p95_duration_ms` int, + `avg_ttfb_ms` decimal(12,2), + `p50_ttfb_ms` int, + `p95_ttfb_ms` int, + `avg_output_tps` decimal(12,4), + `success_count` bigint NOT NULL DEFAULT 0, + `error_count` bigint NOT NULL DEFAULT 0, + `sample_count` bigint NOT NULL DEFAULT 0, + `market_share_tokens` decimal(10,6), + `market_share_requests` decimal(10,6), + `market_share_sessions` decimal(10,6), + `rank_by_tokens` int, + `rank_by_requests` int, + `rank_by_sessions` int, + `rank_by_cost` int, + `created_at` datetime NOT NULL DEFAULT (now()), + `updated_at` datetime NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP, + CONSTRAINT `uniq_provider_period` UNIQUE INDEX(`grain`,`period_start`,`dataset`,`tier`,`client`,`source`,`provider`) +); +--> statement-breakpoint +RENAME TABLE `stat` TO `model_stat`;--> statement-breakpoint +CREATE INDEX `idx_country_map_tokens` ON `geo_stat` (`grain`,`period_start`,`dataset`,`tier`,`total_tokens`);--> statement-breakpoint +CREATE INDEX `idx_country_rank` ON `geo_stat` (`grain`,`period_start`,`dataset`,`tier`,`rank_by_tokens`);--> statement-breakpoint +CREATE INDEX `idx_country` ON `geo_stat` (`country`,`grain`,`period_start`);--> statement-breakpoint +CREATE INDEX `idx_continent` ON `geo_stat` (`continent`,`grain`,`period_start`);--> statement-breakpoint +CREATE INDEX `idx_provider_leaderboard_tokens` ON `provider_stat` (`grain`,`period_start`,`dataset`,`tier`,`total_tokens`);--> statement-breakpoint +CREATE INDEX `idx_provider_market_share` ON `provider_stat` (`grain`,`period_start`,`dataset`,`tier`,`market_share_tokens`);--> statement-breakpoint +CREATE INDEX `idx_provider_rank` ON `provider_stat` (`grain`,`period_start`,`dataset`,`tier`,`rank_by_tokens`);--> statement-breakpoint +CREATE INDEX `idx_provider` ON `provider_stat` (`provider`,`grain`,`period_start`); \ No newline at end of file diff --git a/packages/stats/core/migrations/20260523110335_cool_vin_gonzales/snapshot.json b/packages/stats/core/migrations/20260523110335_cool_vin_gonzales/snapshot.json new file mode 100644 index 0000000000..02ce4b4d18 --- /dev/null +++ b/packages/stats/core/migrations/20260523110335_cool_vin_gonzales/snapshot.json @@ -0,0 +1,2041 @@ +{ + "version": "6", + "dialect": "mysql", + "id": "e246639a-0da0-4fbd-b7bb-f1781d407780", + "prevIds": [ + "72655266-65da-408e-bfd8-9f3a4ad817a5" + ], + "ddl": [ + { + "name": "geo_stat", + "entityType": "tables" + }, + { + "name": "model_stat", + "entityType": "tables" + }, + { + "name": "provider_stat", + "entityType": "tables" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": true, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "id", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "varchar(16)", + "notNull": true, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "grain", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "datetime", + "notNull": true, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "period_start", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "datetime", + "notNull": true, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "period_end", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "varchar(64)", + "notNull": true, + "autoIncrement": false, + "default": "'all'", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "dataset", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "varchar(64)", + "notNull": true, + "autoIncrement": false, + "default": "'all'", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "tier", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "varchar(64)", + "notNull": true, + "autoIncrement": false, + "default": "'all'", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "client", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "varchar(64)", + "notNull": true, + "autoIncrement": false, + "default": "'all'", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "source", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "char(2)", + "notNull": true, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "country", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "varchar(8)", + "notNull": true, + "autoIncrement": false, + "default": "''", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "continent", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "sessions", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "requests", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "input_tokens", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "output_tokens", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "reasoning_tokens", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "cache_read_tokens", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "total_tokens", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "input_cost_microcents", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "output_cost_microcents", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "total_cost_microcents", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "decimal(12,2)", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "avg_duration_ms", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "p50_duration_ms", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "p95_duration_ms", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "decimal(12,2)", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "avg_ttfb_ms", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "p50_ttfb_ms", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "p95_ttfb_ms", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "decimal(12,4)", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "avg_output_tps", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "success_count", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "error_count", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "sample_count", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "decimal(10,6)", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "market_share_tokens", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "decimal(10,6)", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "market_share_requests", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "decimal(10,6)", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "market_share_sessions", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "rank_by_tokens", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "rank_by_requests", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "rank_by_sessions", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "rank_by_cost", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "datetime", + "notNull": true, + "autoIncrement": false, + "default": "(now())", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "created_at", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "datetime", + "notNull": true, + "autoIncrement": false, + "default": "(now())", + "onUpdateNow": true, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "updated_at", + "entityType": "columns", + "table": "geo_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": true, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "id", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "varchar(16)", + "notNull": true, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "grain", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "datetime", + "notNull": true, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "period_start", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "datetime", + "notNull": true, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "period_end", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "varchar(64)", + "notNull": true, + "autoIncrement": false, + "default": "'all'", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "dataset", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "varchar(64)", + "notNull": true, + "autoIncrement": false, + "default": "'all'", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "tier", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "varchar(64)", + "notNull": true, + "autoIncrement": false, + "default": "'all'", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "client", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "varchar(64)", + "notNull": true, + "autoIncrement": false, + "default": "'all'", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "source", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "varchar(128)", + "notNull": true, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "provider", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "varchar(256)", + "notNull": true, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "model", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "varchar(256)", + "notNull": true, + "autoIncrement": false, + "default": "''", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "provider_model", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "sessions", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "requests", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "input_tokens", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "output_tokens", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "reasoning_tokens", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "cache_read_tokens", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "total_tokens", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "input_cost_microcents", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "output_cost_microcents", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "total_cost_microcents", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "decimal(12,2)", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "avg_duration_ms", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "p50_duration_ms", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "p95_duration_ms", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "decimal(12,2)", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "avg_ttfb_ms", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "p50_ttfb_ms", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "p95_ttfb_ms", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "decimal(12,4)", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "avg_output_tps", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "success_count", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "error_count", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "sample_count", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "rank_by_tokens", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "rank_by_requests", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "rank_by_cost", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "datetime", + "notNull": true, + "autoIncrement": false, + "default": "(now())", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "created_at", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "datetime", + "notNull": true, + "autoIncrement": false, + "default": "(now())", + "onUpdateNow": true, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "updated_at", + "entityType": "columns", + "table": "model_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": true, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "id", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "varchar(16)", + "notNull": true, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "grain", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "datetime", + "notNull": true, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "period_start", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "datetime", + "notNull": true, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "period_end", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "varchar(64)", + "notNull": true, + "autoIncrement": false, + "default": "'all'", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "dataset", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "varchar(64)", + "notNull": true, + "autoIncrement": false, + "default": "'all'", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "tier", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "varchar(64)", + "notNull": true, + "autoIncrement": false, + "default": "'all'", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "client", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "varchar(64)", + "notNull": true, + "autoIncrement": false, + "default": "'all'", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "source", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "varchar(128)", + "notNull": true, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "provider", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "sessions", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "requests", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "input_tokens", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "output_tokens", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "reasoning_tokens", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "cache_read_tokens", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "total_tokens", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "input_cost_microcents", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "output_cost_microcents", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "total_cost_microcents", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "decimal(12,2)", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "avg_duration_ms", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "p50_duration_ms", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "p95_duration_ms", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "decimal(12,2)", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "avg_ttfb_ms", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "p50_ttfb_ms", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "p95_ttfb_ms", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "decimal(12,4)", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "avg_output_tps", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "success_count", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "error_count", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "bigint", + "notNull": true, + "autoIncrement": false, + "default": "0", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "sample_count", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "decimal(10,6)", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "market_share_tokens", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "decimal(10,6)", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "market_share_requests", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "decimal(10,6)", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "market_share_sessions", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "rank_by_tokens", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "rank_by_requests", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "rank_by_sessions", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "int", + "notNull": false, + "autoIncrement": false, + "default": null, + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "rank_by_cost", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "datetime", + "notNull": true, + "autoIncrement": false, + "default": "(now())", + "onUpdateNow": false, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "created_at", + "entityType": "columns", + "table": "provider_stat" + }, + { + "type": "datetime", + "notNull": true, + "autoIncrement": false, + "default": "(now())", + "onUpdateNow": true, + "onUpdateNowFsp": null, + "charSet": null, + "collation": null, + "generated": null, + "name": "updated_at", + "entityType": "columns", + "table": "provider_stat" + }, + { + "columns": [ + "id" + ], + "name": "PRIMARY", + "table": "geo_stat", + "entityType": "pks" + }, + { + "columns": [ + "id" + ], + "name": "PRIMARY", + "table": "model_stat", + "entityType": "pks" + }, + { + "columns": [ + "id" + ], + "name": "PRIMARY", + "table": "provider_stat", + "entityType": "pks" + }, + { + "columns": [ + { + "value": "grain", + "isExpression": false + }, + { + "value": "period_start", + "isExpression": false + }, + { + "value": "dataset", + "isExpression": false + }, + { + "value": "tier", + "isExpression": false + }, + { + "value": "client", + "isExpression": false + }, + { + "value": "source", + "isExpression": false + }, + { + "value": "country", + "isExpression": false + } + ], + "isUnique": true, + "using": null, + "algorithm": null, + "lock": null, + "nameExplicit": true, + "name": "uniq_country_period", + "entityType": "indexes", + "table": "geo_stat" + }, + { + "columns": [ + { + "value": "grain", + "isExpression": false + }, + { + "value": "period_start", + "isExpression": false + }, + { + "value": "dataset", + "isExpression": false + }, + { + "value": "tier", + "isExpression": false + }, + { + "value": "total_tokens", + "isExpression": false + } + ], + "isUnique": false, + "using": null, + "algorithm": null, + "lock": null, + "nameExplicit": true, + "name": "idx_country_map_tokens", + "entityType": "indexes", + "table": "geo_stat" + }, + { + "columns": [ + { + "value": "grain", + "isExpression": false + }, + { + "value": "period_start", + "isExpression": false + }, + { + "value": "dataset", + "isExpression": false + }, + { + "value": "tier", + "isExpression": false + }, + { + "value": "rank_by_tokens", + "isExpression": false + } + ], + "isUnique": false, + "using": null, + "algorithm": null, + "lock": null, + "nameExplicit": true, + "name": "idx_country_rank", + "entityType": "indexes", + "table": "geo_stat" + }, + { + "columns": [ + { + "value": "country", + "isExpression": false + }, + { + "value": "grain", + "isExpression": false + }, + { + "value": "period_start", + "isExpression": false + } + ], + "isUnique": false, + "using": null, + "algorithm": null, + "lock": null, + "nameExplicit": true, + "name": "idx_country", + "entityType": "indexes", + "table": "geo_stat" + }, + { + "columns": [ + { + "value": "continent", + "isExpression": false + }, + { + "value": "grain", + "isExpression": false + }, + { + "value": "period_start", + "isExpression": false + } + ], + "isUnique": false, + "using": null, + "algorithm": null, + "lock": null, + "nameExplicit": true, + "name": "idx_continent", + "entityType": "indexes", + "table": "geo_stat" + }, + { + "columns": [ + { + "value": "grain", + "isExpression": false + }, + { + "value": "period_start", + "isExpression": false + }, + { + "value": "dataset", + "isExpression": false + }, + { + "value": "tier", + "isExpression": false + }, + { + "value": "client", + "isExpression": false + }, + { + "value": "source", + "isExpression": false + }, + { + "value": "provider", + "isExpression": false + }, + { + "value": "model", + "isExpression": false + } + ], + "isUnique": true, + "using": null, + "algorithm": null, + "lock": null, + "nameExplicit": true, + "name": "uniq_model_period", + "entityType": "indexes", + "table": "model_stat" + }, + { + "columns": [ + { + "value": "grain", + "isExpression": false + }, + { + "value": "period_start", + "isExpression": false + }, + { + "value": "dataset", + "isExpression": false + }, + { + "value": "tier", + "isExpression": false + }, + { + "value": "total_tokens", + "isExpression": false + } + ], + "isUnique": false, + "using": null, + "algorithm": null, + "lock": null, + "nameExplicit": true, + "name": "idx_leaderboard_tokens", + "entityType": "indexes", + "table": "model_stat" + }, + { + "columns": [ + { + "value": "model", + "isExpression": false + }, + { + "value": "grain", + "isExpression": false + }, + { + "value": "period_start", + "isExpression": false + } + ], + "isUnique": false, + "using": null, + "algorithm": null, + "lock": null, + "nameExplicit": true, + "name": "idx_model", + "entityType": "indexes", + "table": "model_stat" + }, + { + "columns": [ + { + "value": "grain", + "isExpression": false + }, + { + "value": "period_start", + "isExpression": false + }, + { + "value": "dataset", + "isExpression": false + }, + { + "value": "tier", + "isExpression": false + }, + { + "value": "client", + "isExpression": false + }, + { + "value": "source", + "isExpression": false + }, + { + "value": "provider", + "isExpression": false + } + ], + "isUnique": true, + "using": null, + "algorithm": null, + "lock": null, + "nameExplicit": true, + "name": "uniq_provider_period", + "entityType": "indexes", + "table": "provider_stat" + }, + { + "columns": [ + { + "value": "grain", + "isExpression": false + }, + { + "value": "period_start", + "isExpression": false + }, + { + "value": "dataset", + "isExpression": false + }, + { + "value": "tier", + "isExpression": false + }, + { + "value": "total_tokens", + "isExpression": false + } + ], + "isUnique": false, + "using": null, + "algorithm": null, + "lock": null, + "nameExplicit": true, + "name": "idx_provider_leaderboard_tokens", + "entityType": "indexes", + "table": "provider_stat" + }, + { + "columns": [ + { + "value": "grain", + "isExpression": false + }, + { + "value": "period_start", + "isExpression": false + }, + { + "value": "dataset", + "isExpression": false + }, + { + "value": "tier", + "isExpression": false + }, + { + "value": "market_share_tokens", + "isExpression": false + } + ], + "isUnique": false, + "using": null, + "algorithm": null, + "lock": null, + "nameExplicit": true, + "name": "idx_provider_market_share", + "entityType": "indexes", + "table": "provider_stat" + }, + { + "columns": [ + { + "value": "grain", + "isExpression": false + }, + { + "value": "period_start", + "isExpression": false + }, + { + "value": "dataset", + "isExpression": false + }, + { + "value": "tier", + "isExpression": false + }, + { + "value": "rank_by_tokens", + "isExpression": false + } + ], + "isUnique": false, + "using": null, + "algorithm": null, + "lock": null, + "nameExplicit": true, + "name": "idx_provider_rank", + "entityType": "indexes", + "table": "provider_stat" + }, + { + "columns": [ + { + "value": "provider", + "isExpression": false + }, + { + "value": "grain", + "isExpression": false + }, + { + "value": "period_start", + "isExpression": false + } + ], + "isUnique": false, + "using": null, + "algorithm": null, + "lock": null, + "nameExplicit": true, + "name": "idx_provider", + "entityType": "indexes", + "table": "provider_stat" + } + ], + "renames": [] +} \ No newline at end of file diff --git a/packages/stats/core/src/cron/stat.ts b/packages/stats/core/src/cron/stat.ts index ca56211423..002fbf8495 100644 --- a/packages/stats/core/src/cron/stat.ts +++ b/packages/stats/core/src/cron/stat.ts @@ -10,7 +10,7 @@ import { sql } from "drizzle-orm" import { drizzle } from "drizzle-orm/planetscale-serverless" import { DateTime, Effect, Schema } from "effect" import { Resource } from "sst" -import { stat } from "../database/schema" +import { geoStat, modelStat, providerStat } from "../database/schema" const ATHENA_MAX_POLL_ATTEMPTS = 60 const ATHENA_PAGE_SIZE = 1000 @@ -18,15 +18,15 @@ const DATALAKE_INGESTION_LAG_MS = 5 * 60_000 const UPSERT_CHUNK_SIZE = 500 type AthenaData = Record -type StatRow = typeof stat.$inferInsert -type StatAggregate = { +type ModelStatRow = typeof modelStat.$inferInsert +type ProviderStatRow = typeof providerStat.$inferInsert +type GeoStatRow = typeof geoStat.$inferInsert +type StatBaseAggregate = { grain: "day" | "week" period_start: Date period_end: Date dataset: string tier: string - provider: string - model: string sessions: number requests: number input_tokens: number @@ -48,6 +48,38 @@ type StatAggregate = { error_count: number sample_count: number } +type ModelStatAggregate = StatBaseAggregate & { provider: string; model: string; provider_model: string } +type ProviderStatAggregate = StatBaseAggregate & { provider: string } +type GeoStatAggregate = StatBaseAggregate & { country: string; continent: string } +type StatBaseRow = { + grain: string + period_start: Date + period_end: Date + dataset?: string + tier?: string + client?: string + source?: string + sessions?: number + requests?: number + input_tokens?: number + output_tokens?: number + reasoning_tokens?: number + cache_read_tokens?: number + total_tokens?: number + input_cost_microcents?: number + output_cost_microcents?: number + total_cost_microcents?: number + avg_duration_ms?: number | null + p50_duration_ms?: number | null + p95_duration_ms?: number | null + avg_ttfb_ms?: number | null + p50_ttfb_ms?: number | null + p95_ttfb_ms?: number | null + avg_output_tps?: number | null + success_count?: number + error_count?: number + sample_count?: number +} type SyncResult = { ok: true; rows: number; startedAt: string; periodStart: string; periodEnd: string } type SyncError = AthenaQueryError | AthenaQueryTimeoutError | StatDatabaseError @@ -80,27 +112,63 @@ const syncStats: () => Effect.Effect = Effect.fn(" yield* logAthenaRuntimeCheck() - const aggregates = (yield* runAthenaQuery(buildStatsQuery(periodStart, periodEnd))).flatMap(toStatAggregate) - const rows = rankRows([ - ...synthesizeAllTierRows(collapseRows(aggregates.filter((item) => item.grain === "week").map(toStatRow))), - ...synthesizeAllTierRows(collapseRows(aggregates.filter((item) => item.grain === "day").map(toStatRow))), + const modelAggregates = (yield* runAthenaQuery(buildStatsQuery(periodStart, periodEnd, "model"))).flatMap( + toModelStatAggregate, + ) + const providerAggregates = (yield* runAthenaQuery(buildStatsQuery(periodStart, periodEnd, "provider"))).flatMap( + toProviderStatAggregate, + ) + const geoAggregates = (yield* runAthenaQuery(buildStatsQuery(periodStart, periodEnd, "geo"))).flatMap( + toGeoStatAggregate, + ) + const modelRows = rankModelRows([ + ...synthesizeAllTierRows( + collapseRows(modelAggregates.filter((item) => item.grain === "week").map(toModelStatRow), modelStatKey), + modelStatKey, + ), + ...synthesizeAllTierRows( + collapseRows(modelAggregates.filter((item) => item.grain === "day").map(toModelStatRow), modelStatKey), + modelStatKey, + ), + ]) + const providerRows = rankRowsWithMarketShare([ + ...synthesizeAllTierRows( + collapseRows(providerAggregates.filter((item) => item.grain === "week").map(toProviderStatRow), providerStatKey), + providerStatKey, + ), + ...synthesizeAllTierRows( + collapseRows(providerAggregates.filter((item) => item.grain === "day").map(toProviderStatRow), providerStatKey), + providerStatKey, + ), + ]) + const geoRows = rankRowsWithMarketShare([ + ...synthesizeAllTierRows( + collapseRows(geoAggregates.filter((item) => item.grain === "week").map(toGeoStatRow), geoStatKey), + geoStatKey, + ), + ...synthesizeAllTierRows( + collapseRows(geoAggregates.filter((item) => item.grain === "day").map(toGeoStatRow), geoStatKey), + geoStatKey, + ), ]) - yield* saveRows(rows) + yield* saveRows({ modelRows, providerRows, geoRows }) yield* Effect.logInfo("stats sync complete").pipe( Effect.annotateLogs({ startedAt: startedAt.toISOString(), periodStart: periodStart.toISOString(), periodEnd: periodEnd.toISOString(), - rows: rows.length, + rows: modelRows.length, + providerRows: providerRows.length, + geoRows: geoRows.length, stage: Resource.App.stage, }), ) return { ok: true, - rows: rows.length, + rows: modelRows.length, startedAt: startedAt.toISOString(), periodStart: periodStart.toISOString(), periodEnd: periodEnd.toISOString(), @@ -192,66 +260,175 @@ const getAthenaResults: ( return [...rows, ...(yield* getAthenaResults(client, queryExecutionId, result.NextToken))] }) -const saveRows: (rows: StatRow[]) => Effect.Effect = Effect.fn("StatsCron.saveRows")( - function* (rows: StatRow[]) { - const db = drizzle({ - client: new Client({ - host: Resource.StatsDatabase.host, - username: Resource.StatsDatabase.username, - password: Resource.StatsDatabase.password, +const saveRows: (rows: { + modelRows: ModelStatRow[] + providerRows: ProviderStatRow[] + geoRows: GeoStatRow[] +}) => Effect.Effect = Effect.fn("StatsCron.saveRows")(function* (rows: { + modelRows: ModelStatRow[] + providerRows: ProviderStatRow[] + geoRows: GeoStatRow[] +}) { + const db = drizzle({ + client: new Client({ + host: Resource.StatsDatabase.host, + username: Resource.StatsDatabase.username, + password: Resource.StatsDatabase.password, + }), + }) + + yield* Effect.forEach( + chunks(rows.modelRows, UPSERT_CHUNK_SIZE), + (chunk) => + Effect.tryPromise({ + try: () => + db + .insert(modelStat) + .values(chunk) + .onDuplicateKeyUpdate({ + set: { + period_end: inserted("period_end"), + provider_model: inserted("provider_model"), + sessions: inserted("sessions"), + requests: inserted("requests"), + input_tokens: inserted("input_tokens"), + output_tokens: inserted("output_tokens"), + reasoning_tokens: inserted("reasoning_tokens"), + cache_read_tokens: inserted("cache_read_tokens"), + total_tokens: inserted("total_tokens"), + input_cost_microcents: inserted("input_cost_microcents"), + output_cost_microcents: inserted("output_cost_microcents"), + total_cost_microcents: inserted("total_cost_microcents"), + avg_duration_ms: inserted("avg_duration_ms"), + p50_duration_ms: inserted("p50_duration_ms"), + p95_duration_ms: inserted("p95_duration_ms"), + avg_ttfb_ms: inserted("avg_ttfb_ms"), + p50_ttfb_ms: inserted("p50_ttfb_ms"), + p95_ttfb_ms: inserted("p95_ttfb_ms"), + avg_output_tps: inserted("avg_output_tps"), + success_count: inserted("success_count"), + error_count: inserted("error_count"), + sample_count: inserted("sample_count"), + rank_by_tokens: inserted("rank_by_tokens"), + rank_by_requests: inserted("rank_by_requests"), + rank_by_cost: inserted("rank_by_cost"), + }, + }), + catch: (cause) => new StatDatabaseError({ message: "Failed to upsert model stats rows", cause }), }), - }) + { discard: true }, + ) - yield* Effect.forEach( - chunks(rows, UPSERT_CHUNK_SIZE), - (chunk) => - Effect.tryPromise({ - try: () => - db - .insert(stat) - .values(chunk) - .onDuplicateKeyUpdate({ - set: { - period_end: inserted("period_end"), - provider_model: inserted("provider_model"), - sessions: inserted("sessions"), - requests: inserted("requests"), - input_tokens: inserted("input_tokens"), - output_tokens: inserted("output_tokens"), - reasoning_tokens: inserted("reasoning_tokens"), - cache_read_tokens: inserted("cache_read_tokens"), - total_tokens: inserted("total_tokens"), - input_cost_microcents: inserted("input_cost_microcents"), - output_cost_microcents: inserted("output_cost_microcents"), - total_cost_microcents: inserted("total_cost_microcents"), - avg_duration_ms: inserted("avg_duration_ms"), - p50_duration_ms: inserted("p50_duration_ms"), - p95_duration_ms: inserted("p95_duration_ms"), - avg_ttfb_ms: inserted("avg_ttfb_ms"), - p50_ttfb_ms: inserted("p50_ttfb_ms"), - p95_ttfb_ms: inserted("p95_ttfb_ms"), - avg_output_tps: inserted("avg_output_tps"), - success_count: inserted("success_count"), - error_count: inserted("error_count"), - sample_count: inserted("sample_count"), - rank_by_tokens: inserted("rank_by_tokens"), - rank_by_requests: inserted("rank_by_requests"), - rank_by_cost: inserted("rank_by_cost"), - }, - }), - catch: (cause) => new StatDatabaseError({ message: "Failed to upsert stats rows", cause }), - }), - { discard: true }, - ) - }, -) + yield* Effect.forEach( + chunks(rows.providerRows, UPSERT_CHUNK_SIZE), + (chunk) => + Effect.tryPromise({ + try: () => + db + .insert(providerStat) + .values(chunk) + .onDuplicateKeyUpdate({ + set: { + period_end: inserted("period_end"), + sessions: inserted("sessions"), + requests: inserted("requests"), + input_tokens: inserted("input_tokens"), + output_tokens: inserted("output_tokens"), + reasoning_tokens: inserted("reasoning_tokens"), + cache_read_tokens: inserted("cache_read_tokens"), + total_tokens: inserted("total_tokens"), + input_cost_microcents: inserted("input_cost_microcents"), + output_cost_microcents: inserted("output_cost_microcents"), + total_cost_microcents: inserted("total_cost_microcents"), + avg_duration_ms: inserted("avg_duration_ms"), + p50_duration_ms: inserted("p50_duration_ms"), + p95_duration_ms: inserted("p95_duration_ms"), + avg_ttfb_ms: inserted("avg_ttfb_ms"), + p50_ttfb_ms: inserted("p50_ttfb_ms"), + p95_ttfb_ms: inserted("p95_ttfb_ms"), + avg_output_tps: inserted("avg_output_tps"), + success_count: inserted("success_count"), + error_count: inserted("error_count"), + sample_count: inserted("sample_count"), + market_share_tokens: inserted("market_share_tokens"), + market_share_requests: inserted("market_share_requests"), + market_share_sessions: inserted("market_share_sessions"), + rank_by_tokens: inserted("rank_by_tokens"), + rank_by_requests: inserted("rank_by_requests"), + rank_by_sessions: inserted("rank_by_sessions"), + rank_by_cost: inserted("rank_by_cost"), + }, + }), + catch: (cause) => new StatDatabaseError({ message: "Failed to upsert provider stats rows", cause }), + }), + { discard: true }, + ) -function buildStatsQuery(periodStart: Date, periodEnd: Date) { + yield* Effect.forEach( + chunks(rows.geoRows, UPSERT_CHUNK_SIZE), + (chunk) => + Effect.tryPromise({ + try: () => + db + .insert(geoStat) + .values(chunk) + .onDuplicateKeyUpdate({ + set: { + period_end: inserted("period_end"), + continent: inserted("continent"), + sessions: inserted("sessions"), + requests: inserted("requests"), + input_tokens: inserted("input_tokens"), + output_tokens: inserted("output_tokens"), + reasoning_tokens: inserted("reasoning_tokens"), + cache_read_tokens: inserted("cache_read_tokens"), + total_tokens: inserted("total_tokens"), + input_cost_microcents: inserted("input_cost_microcents"), + output_cost_microcents: inserted("output_cost_microcents"), + total_cost_microcents: inserted("total_cost_microcents"), + avg_duration_ms: inserted("avg_duration_ms"), + p50_duration_ms: inserted("p50_duration_ms"), + p95_duration_ms: inserted("p95_duration_ms"), + avg_ttfb_ms: inserted("avg_ttfb_ms"), + p50_ttfb_ms: inserted("p50_ttfb_ms"), + p95_ttfb_ms: inserted("p95_ttfb_ms"), + avg_output_tps: inserted("avg_output_tps"), + success_count: inserted("success_count"), + error_count: inserted("error_count"), + sample_count: inserted("sample_count"), + market_share_tokens: inserted("market_share_tokens"), + market_share_requests: inserted("market_share_requests"), + market_share_sessions: inserted("market_share_sessions"), + rank_by_tokens: inserted("rank_by_tokens"), + rank_by_requests: inserted("rank_by_requests"), + rank_by_sessions: inserted("rank_by_sessions"), + rank_by_cost: inserted("rank_by_cost"), + }, + }), + catch: (cause) => new StatDatabaseError({ message: "Failed to upsert geo stats rows", cause }), + }), + { discard: true }, + ) +}) + +function buildStatsQuery(periodStart: Date, periodEnd: Date, dimension: "model" | "provider" | "geo") { const periodStartValue = sqlString(periodStart.toISOString()) const periodEndValue = sqlString(periodEnd.toISOString()) const sourceTable = [Resource.StatsLake.catalog, Resource.StatsLake.database, Resource.StatsLake.table] .map(sqlIdentifier) .join(".") + const dimensionSql = (() => { + if (dimension === "model") + return { + select: "provider, model, COALESCE(MAX(NULLIF(provider_model, '')), '') AS provider_model", + groupBy: "provider, model", + } + if (dimension === "provider") return { select: "provider", groupBy: "provider" } + return { + select: "country, COALESCE(MAX(NULLIF(continent, '')), '') AS continent", + groupBy: "country", + } + })() const aggregateColumns = ` COUNT(DISTINCT session) AS sessions, COUNT(*) AS requests, @@ -280,7 +457,10 @@ WITH filtered AS ( from_iso8601_timestamp(event_timestamp) AS event_time, COALESCE(NULLIF(tier, ''), 'unknown') AS tier, COALESCE(NULLIF(provider, ''), 'unknown') AS provider, + COALESCE(NULLIF(provider_model, ''), '') AS provider_model, COALESCE(NULLIF(model, ''), 'unknown') AS model, + UPPER(COALESCE(NULLIF(cf_country, ''), 'ZZ')) AS country, + COALESCE(NULLIF(cf_continent, ''), '') AS continent, session, status, duration_ms, @@ -311,11 +491,10 @@ SELECT ${periodEndValue} AS period_end, ${sqlString(Resource.StatsLake.dataset)} AS dataset, tier, - provider, - model, + ${dimensionSql.select}, ${aggregateColumns} FROM filtered -GROUP BY tier, provider, model +GROUP BY tier, ${dimensionSql.groupBy} UNION ALL SELECT 'day' AS grain, @@ -323,11 +502,10 @@ SELECT to_iso8601(least(day + INTERVAL '1' DAY, from_iso8601_timestamp(${periodEndValue}))) AS period_end, ${sqlString(Resource.StatsLake.dataset)} AS dataset, tier, - provider, - model, + ${dimensionSql.select}, ${aggregateColumns} FROM daily -GROUP BY day, tier, provider, model +GROUP BY day, tier, ${dimensionSql.groupBy} ORDER BY grain, period_start, total_tokens DESC ` } @@ -349,7 +527,32 @@ function inserted(column: string) { return sql.raw(`values(\`${column}\`)`) } -function toStatAggregate(data: AthenaData): StatAggregate[] { +function toModelStatAggregate(data: AthenaData): ModelStatAggregate[] { + return toStatBaseAggregate(data).flatMap((base) => [ + { + ...base, + provider: data.provider || "unknown", + model: data.model || "unknown", + provider_model: data.provider_model || "", + }, + ]) +} + +function toProviderStatAggregate(data: AthenaData): ProviderStatAggregate[] { + return toStatBaseAggregate(data).flatMap((base) => [{ ...base, provider: data.provider || "unknown" }]) +} + +function toGeoStatAggregate(data: AthenaData): GeoStatAggregate[] { + return toStatBaseAggregate(data).flatMap((base) => [ + { + ...base, + country: normalizeCountry(data.country), + continent: data.continent || "", + }, + ]) +} + +function toStatBaseAggregate(data: AthenaData): StatBaseAggregate[] { const grain = data.grain === "day" || data.grain === "week" ? data.grain : undefined const periodStart = new Date(data.period_start ?? "") const periodEnd = new Date(data.period_end ?? "") @@ -362,8 +565,6 @@ function toStatAggregate(data: AthenaData): StatAggregate[] { period_end: periodEnd, dataset: data.dataset || Resource.StatsLake.dataset, tier: normalizeTier(data.tier || "unknown"), - provider: data.provider || "unknown", - model: data.model || "unknown", sessions: integer(data, "sessions"), requests: integer(data, "requests"), input_tokens: integer(data, "input_tokens"), @@ -388,7 +589,31 @@ function toStatAggregate(data: AthenaData): StatAggregate[] { ] } -function toStatRow(data: StatAggregate): StatRow { +function toModelStatRow(data: ModelStatAggregate): ModelStatRow { + return { + ...toStatBaseRow(data), + provider: data.provider, + model: data.model, + provider_model: data.provider_model, + } +} + +function toProviderStatRow(data: ProviderStatAggregate): ProviderStatRow { + return { + ...toStatBaseRow(data), + provider: data.provider, + } +} + +function toGeoStatRow(data: GeoStatAggregate): GeoStatRow { + return { + ...toStatBaseRow(data), + country: data.country, + continent: data.continent, + } +} + +function toStatBaseRow(data: StatBaseAggregate) { return { grain: data.grain, period_start: data.period_start, @@ -397,9 +622,6 @@ function toStatRow(data: StatAggregate): StatRow { tier: data.tier, client: "all", source: "all", - provider: data.provider, - model: data.model, - provider_model: "", sessions: data.sessions, requests: data.requests, input_tokens: data.input_tokens, @@ -423,19 +645,18 @@ function toStatRow(data: StatAggregate): StatRow { } } -function synthesizeAllTierRows(rows: StatRow[]) { +function synthesizeAllTierRows(rows: T[], dimensionKey: (row: T) => string) { return [ ...rows, ...Object.values( - rows.reduce>((result, row) => { + rows.reduce>((result, row) => { const key = [ row.grain, row.period_start.toISOString(), row.dataset, row.client, row.source, - row.provider, - row.model, + dimensionKey(row), ].join("\u0000") result[key] = result[key] ? combineRows(result[key], row) : { ...row, tier: "all" } return result @@ -444,9 +665,9 @@ function synthesizeAllTierRows(rows: StatRow[]) { ] } -function collapseRows(rows: StatRow[]) { +function collapseRows(rows: T[], dimensionKey: (row: T) => string) { return Object.values( - rows.reduce>((result, row) => { + rows.reduce>((result, row) => { const key = [ row.grain, row.period_start.toISOString(), @@ -454,8 +675,7 @@ function collapseRows(rows: StatRow[]) { row.tier, row.client, row.source, - row.provider, - row.model, + dimensionKey(row), ].join("\u0000") result[key] = result[key] ? combineRows(result[key], row) : row return result @@ -463,7 +683,7 @@ function collapseRows(rows: StatRow[]) { ) } -function combineRows(left: StatRow, right: StatRow): StatRow { +function combineRows(left: T, right: T): T { return { ...left, period_end: right.period_end > left.period_end ? right.period_end : left.period_end, @@ -490,12 +710,10 @@ function combineRows(left: StatRow, right: StatRow): StatRow { } } -function rankRows(rows: StatRow[]) { +function rankModelRows(rows: ModelStatRow[]) { return Object.values( - rows.reduce>((result, row) => { - const key = [row.grain, row.period_start.toISOString(), row.dataset, row.tier, row.client, row.source].join( - "\u0000", - ) + rows.reduce>((result, row) => { + const key = statPeriodKey(row) result[key] = [...(result[key] ?? []), row] return result }, {}), @@ -512,10 +730,59 @@ function rankRows(rows: StatRow[]) { }) } -function rankBy(rows: StatRow[], value: (row: StatRow) => number) { +function rankRowsWithMarketShare(rows: T[]) { + return Object.values( + rows.reduce>((result, row) => { + const key = statPeriodKey(row) + result[key] = [...(result[key] ?? []), row] + return result + }, {}), + ).flatMap((group) => { + const tokens = group.reduce((sum, row) => sum + (row.total_tokens ?? 0), 0) + const requests = group.reduce((sum, row) => sum + (row.requests ?? 0), 0) + const sessions = group.reduce((sum, row) => sum + (row.sessions ?? 0), 0) + const tokenRanks = rankBy(group, (row) => row.total_tokens ?? 0) + const requestRanks = rankBy(group, (row) => row.requests ?? 0) + const sessionRanks = rankBy(group, (row) => row.sessions ?? 0) + const costRanks = rankBy(group, (row) => row.total_cost_microcents ?? 0) + return group.map((row) => ({ + ...row, + market_share_tokens: share(row.total_tokens, tokens), + market_share_requests: share(row.requests, requests), + market_share_sessions: share(row.sessions, sessions), + rank_by_tokens: tokenRanks.get(row) ?? null, + rank_by_requests: requestRanks.get(row) ?? null, + rank_by_sessions: sessionRanks.get(row) ?? null, + rank_by_cost: costRanks.get(row) ?? null, + })) + }) +} + +function rankBy(rows: T[], value: (row: T) => number) { return new Map(rows.toSorted((a, b) => value(b) - value(a)).map((row, index) => [row, index + 1])) } +function statPeriodKey(row: StatBaseRow) { + return [row.grain, row.period_start.toISOString(), row.dataset, row.tier, row.client, row.source].join("\u0000") +} + +function modelStatKey(row: ModelStatRow) { + return [row.provider, row.model].join("\u0000") +} + +function providerStatKey(row: ProviderStatRow) { + return row.provider +} + +function geoStatKey(row: GeoStatRow) { + return row.country +} + +function share(value: number | null | undefined, total: number) { + if (total <= 0) return null + return Number(((value ?? 0) / total).toFixed(6)) +} + function chunks(items: T[], size: number) { return Array.from({ length: Math.ceil(items.length / size) }, (_, index) => items.slice(index * size, (index + 1) * size), @@ -539,6 +806,11 @@ function normalizeTier(value: string) { return value } +function normalizeCountry(value: string | undefined) { + if (!value || value.length !== 2) return "ZZ" + return value.toUpperCase() +} + function integer(data: AthenaData, key: string) { return Math.round(number(data, key)) } diff --git a/packages/stats/core/src/database/schema.ts b/packages/stats/core/src/database/schema.ts index 0d5fbeaf89..7fbfb632d7 100644 --- a/packages/stats/core/src/database/schema.ts +++ b/packages/stats/core/src/database/schema.ts @@ -1,8 +1,109 @@ -import { bigint, datetime, decimal, index, int, mysqlTable, uniqueIndex, varchar } from "drizzle-orm/mysql-core" +import { bigint, char, datetime, decimal, index, int, mysqlTable, uniqueIndex, varchar } from "drizzle-orm/mysql-core" -export const stat = mysqlTable( - "stat", +export const modelStat = mysqlTable( + "model_stat", { + ...periodColumns(), + provider: varchar({ length: 128 }).notNull(), + model: varchar({ length: 256 }).notNull(), + provider_model: varchar({ length: 256 }).notNull().default(""), + ...metricColumns(), + rank_by_tokens: int(), + rank_by_requests: int(), + rank_by_cost: int(), + ...timestampColumns(), + }, + (table) => [ + uniqueIndex("uniq_model_period").on( + table.grain, + table.period_start, + table.dataset, + table.tier, + table.client, + table.source, + table.provider, + table.model, + ), + index("idx_leaderboard_tokens").on(table.grain, table.period_start, table.dataset, table.tier, table.total_tokens), + index("idx_model").on(table.model, table.grain, table.period_start), + ], +) + +export const providerStat = mysqlTable( + "provider_stat", + { + ...periodColumns(), + provider: varchar({ length: 128 }).notNull(), + ...metricColumns(), + ...marketShareColumns(), + rank_by_tokens: int(), + rank_by_requests: int(), + rank_by_sessions: int(), + rank_by_cost: int(), + ...timestampColumns(), + }, + (table) => [ + uniqueIndex("uniq_provider_period").on( + table.grain, + table.period_start, + table.dataset, + table.tier, + table.client, + table.source, + table.provider, + ), + index("idx_provider_leaderboard_tokens").on( + table.grain, + table.period_start, + table.dataset, + table.tier, + table.total_tokens, + ), + index("idx_provider_market_share").on( + table.grain, + table.period_start, + table.dataset, + table.tier, + table.market_share_tokens, + ), + index("idx_provider_rank").on(table.grain, table.period_start, table.dataset, table.tier, table.rank_by_tokens), + index("idx_provider").on(table.provider, table.grain, table.period_start), + ], +) + +export const geoStat = mysqlTable( + "geo_stat", + { + ...periodColumns(), + country: char({ length: 2 }).notNull(), + continent: varchar({ length: 8 }).notNull().default(""), + ...metricColumns(), + ...marketShareColumns(), + rank_by_tokens: int(), + rank_by_requests: int(), + rank_by_sessions: int(), + rank_by_cost: int(), + ...timestampColumns(), + }, + (table) => [ + uniqueIndex("uniq_country_period").on( + table.grain, + table.period_start, + table.dataset, + table.tier, + table.client, + table.source, + table.country, + ), + index("idx_country_map_tokens").on(table.grain, table.period_start, table.dataset, table.tier, table.total_tokens), + index("idx_country_rank").on(table.grain, table.period_start, table.dataset, table.tier, table.rank_by_tokens), + index("idx_country").on(table.country, table.grain, table.period_start), + index("idx_continent").on(table.continent, table.grain, table.period_start), + ], +) + +function periodColumns() { + return { id: bigint({ mode: "number" }).autoincrement().primaryKey(), grain: varchar({ length: 16 }).notNull(), period_start: datetime({ mode: "date" }).notNull(), @@ -11,9 +112,11 @@ export const stat = mysqlTable( tier: varchar({ length: 64 }).notNull().default("all"), client: varchar({ length: 64 }).notNull().default("all"), source: varchar({ length: 64 }).notNull().default("all"), - provider: varchar({ length: 128 }).notNull(), - model: varchar({ length: 256 }).notNull(), - provider_model: varchar({ length: 256 }).notNull().default(""), + } +} + +function metricColumns() { + return { sessions: bigint({ mode: "number" }).notNull().default(0), requests: bigint({ mode: "number" }).notNull().default(0), input_tokens: bigint({ mode: "number" }).notNull().default(0), @@ -34,24 +137,20 @@ export const stat = mysqlTable( success_count: bigint({ mode: "number" }).notNull().default(0), error_count: bigint({ mode: "number" }).notNull().default(0), sample_count: bigint({ mode: "number" }).notNull().default(0), - rank_by_tokens: int(), - rank_by_requests: int(), - rank_by_cost: int(), + } +} + +function marketShareColumns() { + return { + market_share_tokens: decimal({ precision: 10, scale: 6, mode: "number" }), + market_share_requests: decimal({ precision: 10, scale: 6, mode: "number" }), + market_share_sessions: decimal({ precision: 10, scale: 6, mode: "number" }), + } +} + +function timestampColumns() { + return { created_at: datetime({ mode: "date" }).notNull().defaultNow(), updated_at: datetime({ mode: "date" }).notNull().defaultNow().onUpdateNow(), - }, - (table) => [ - uniqueIndex("uniq_model_period").on( - table.grain, - table.period_start, - table.dataset, - table.tier, - table.client, - table.source, - table.provider, - table.model, - ), - index("idx_leaderboard_tokens").on(table.grain, table.period_start, table.dataset, table.tier, table.total_tokens), - index("idx_model").on(table.model, table.grain, table.period_start), - ], -) + } +} diff --git a/packages/stats/core/src/domain/ranking.ts b/packages/stats/core/src/domain/ranking.ts index d813130a39..5b95ec8652 100644 --- a/packages/stats/core/src/domain/ranking.ts +++ b/packages/stats/core/src/domain/ranking.ts @@ -3,7 +3,7 @@ import { and, asc, eq } from "drizzle-orm" import { drizzle } from "drizzle-orm/planetscale-serverless" import { Effect, Schema } from "effect" import { DatabaseConfig } from "../database" -import { stat } from "../database/schema" +import { modelStat } from "../database/schema" export const RankingSnapshotId = Schema.String.check(Schema.isStartsWith("rank_"), Schema.isMaxLength(64)).pipe( Schema.brand("RankingSnapshotId"), @@ -98,24 +98,24 @@ export const getRankingsData = Effect.fn("Ranking.getRankingsData")(function* () try: () => db .select({ - periodStart: stat.period_start, - periodEnd: stat.period_end, - tier: stat.tier, - provider: stat.provider, - model: stat.model, - sessions: stat.sessions, - inputTokens: stat.input_tokens, - outputTokens: stat.output_tokens, - reasoningTokens: stat.reasoning_tokens, - cacheReadTokens: stat.cache_read_tokens, - totalTokens: stat.total_tokens, - inputCostMicrocents: stat.input_cost_microcents, - outputCostMicrocents: stat.output_cost_microcents, - totalCostMicrocents: stat.total_cost_microcents, + periodStart: modelStat.period_start, + periodEnd: modelStat.period_end, + tier: modelStat.tier, + provider: modelStat.provider, + model: modelStat.model, + sessions: modelStat.sessions, + inputTokens: modelStat.input_tokens, + outputTokens: modelStat.output_tokens, + reasoningTokens: modelStat.reasoning_tokens, + cacheReadTokens: modelStat.cache_read_tokens, + totalTokens: modelStat.total_tokens, + inputCostMicrocents: modelStat.input_cost_microcents, + outputCostMicrocents: modelStat.output_cost_microcents, + totalCostMicrocents: modelStat.total_cost_microcents, }) - .from(stat) - .where(and(eq(stat.grain, "day"), eq(stat.client, "all"), eq(stat.source, "all"))) - .orderBy(asc(stat.period_start)), + .from(modelStat) + .where(and(eq(modelStat.grain, "day"), eq(modelStat.client, "all"), eq(modelStat.source, "all"))) + .orderBy(asc(modelStat.period_start)), catch: (cause) => new RankingQueryError({ message: "Failed to load rankings stats", cause }), }) return buildRankingsData(rows)