CREATE TABLE `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,
	`model` varchar(256) NOT NULL,
	`provider_model` varchar(256) 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,
	`rank_by_tokens` int,
	`rank_by_requests` 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_model_period` UNIQUE INDEX(`grain`,`period_start`,`dataset`,`tier`,`client`,`source`,`provider`,`model`)
);
--> statement-breakpoint
CREATE INDEX `idx_leaderboard_tokens` ON `stat` (`grain`,`period_start`,`dataset`,`tier`,`total_tokens`);--> statement-breakpoint
CREATE INDEX `idx_model` ON `stat` (`model`,`grain`,`period_start`);