Files
Aether/crates/aether-data/runtime/schema/generated/mysql/baseline/006_usage.sql
T

283 lines
12 KiB
SQL
Raw Normal View History

-- Generated by aether-data-schema from crates/aether-data/runtime/schema/logical/*.toml.
2026-05-05 18:27:36 +08:00
-- Do not edit generated files directly; edit logical schema or explicit overrides instead.
CREATE TABLE IF NOT EXISTS `usage` (
`request_id` VARCHAR(128) NOT NULL,
`id` VARCHAR(128),
`user_id` VARCHAR(64),
`api_key_id` VARCHAR(64),
`provider_name` VARCHAR(255) NOT NULL DEFAULT 'unknown',
`model` VARCHAR(255) NOT NULL DEFAULT 'unknown',
`target_model` VARCHAR(255),
`provider_id` VARCHAR(64),
`provider_endpoint_id` VARCHAR(64),
`provider_api_key_id` VARCHAR(64),
`request_type` VARCHAR(64),
`api_format` VARCHAR(64),
`api_family` VARCHAR(64),
`endpoint_kind` VARCHAR(64),
`endpoint_api_format` VARCHAR(64),
`provider_api_family` VARCHAR(64),
`provider_endpoint_kind` VARCHAR(64),
`has_format_conversion` TINYINT(1) NOT NULL DEFAULT 0,
`is_stream` TINYINT(1) NOT NULL DEFAULT 0,
`upstream_is_stream` TINYINT(1),
2026-05-05 18:27:36 +08:00
`input_tokens` BIGINT NOT NULL DEFAULT 0,
`output_tokens` BIGINT NOT NULL DEFAULT 0,
`input_output_total_tokens` BIGINT NOT NULL DEFAULT 0,
`total_tokens` BIGINT NOT NULL DEFAULT 0,
`cache_creation_input_tokens` BIGINT NOT NULL DEFAULT 0,
`cache_creation_input_tokens_5m` BIGINT NOT NULL DEFAULT 0,
`cache_creation_input_tokens_1h` BIGINT NOT NULL DEFAULT 0,
`cache_creation_ephemeral_5m_input_tokens` BIGINT NOT NULL DEFAULT 0,
`cache_creation_ephemeral_1h_input_tokens` BIGINT NOT NULL DEFAULT 0,
`cache_read_input_tokens` BIGINT NOT NULL DEFAULT 0,
`input_context_tokens` BIGINT NOT NULL DEFAULT 0,
`input_cost_usd` DOUBLE NOT NULL DEFAULT 0,
`output_cost_usd` DOUBLE NOT NULL DEFAULT 0,
`cache_cost_usd` DOUBLE NOT NULL DEFAULT 0,
`cache_creation_cost_usd` DOUBLE NOT NULL DEFAULT 0,
`cache_creation_cost_usd_5m` DOUBLE NOT NULL DEFAULT 0,
`cache_creation_cost_usd_1h` DOUBLE NOT NULL DEFAULT 0,
`cache_read_cost_usd` DOUBLE NOT NULL DEFAULT 0,
`request_cost_usd` DOUBLE NOT NULL DEFAULT 0,
`actual_input_cost_usd` DOUBLE NOT NULL DEFAULT 0,
`actual_output_cost_usd` DOUBLE NOT NULL DEFAULT 0,
`actual_cache_cost_usd` DOUBLE NOT NULL DEFAULT 0,
`actual_cache_creation_cost_usd` DOUBLE NOT NULL DEFAULT 0,
`actual_cache_creation_cost_usd_5m` DOUBLE NOT NULL DEFAULT 0,
`actual_cache_creation_cost_usd_1h` DOUBLE NOT NULL DEFAULT 0,
`actual_cache_read_cost_usd` DOUBLE NOT NULL DEFAULT 0,
`actual_request_cost_usd` DOUBLE NOT NULL DEFAULT 0,
`rate_multiplier` DOUBLE NOT NULL DEFAULT 1,
`input_price_per_1m` DOUBLE,
`output_price_per_1m` DOUBLE,
`cache_creation_price_per_1m` DOUBLE,
`cache_creation_price_per_1m_5m` DOUBLE,
`cache_creation_price_per_1m_1h` DOUBLE,
`cache_read_price_per_1m` DOUBLE,
`price_per_request` DOUBLE,
`status_code` INT,
`error_message` LONGTEXT,
`error_category` VARCHAR(255),
`response_time_ms` BIGINT,
`first_byte_time_ms` BIGINT,
`wallet_id` VARCHAR(64),
`status` VARCHAR(64) NOT NULL DEFAULT 'completed',
`billing_status` VARCHAR(64) NOT NULL DEFAULT 'pending',
`total_cost_usd` DOUBLE NOT NULL DEFAULT 0,
`actual_total_cost_usd` DOUBLE NOT NULL DEFAULT 0,
`request_headers` JSON,
`request_body` JSON,
`provider_request_headers` JSON,
`provider_request_body` JSON,
`response_headers` JSON,
`response_body` JSON,
`client_response_headers` JSON,
`client_response_body` JSON,
`request_body_compressed` LONGBLOB,
`provider_request_body_compressed` LONGBLOB,
`response_body_compressed` LONGBLOB,
`client_response_body_compressed` LONGBLOB,
`request_metadata` JSON,
`created_at` BIGINT,
`candidate_id` VARCHAR(128),
`candidate_index` BIGINT,
`key_name` VARCHAR(255),
`username` VARCHAR(255),
`api_key_name` VARCHAR(255),
`planner_kind` VARCHAR(64),
`route_family` VARCHAR(128),
`route_kind` VARCHAR(128),
`execution_path` VARCHAR(128),
`local_execution_runtime_miss_reason` VARCHAR(255),
`wallet_balance_before` DOUBLE,
`wallet_balance_after` DOUBLE,
`wallet_recharge_balance_before` DOUBLE,
`wallet_recharge_balance_after` DOUBLE,
`wallet_gift_balance_before` DOUBLE,
`wallet_gift_balance_after` DOUBLE,
`finalized_at` BIGINT,
`created_at_unix_ms` BIGINT NOT NULL DEFAULT 0,
`updated_at_unix_secs` BIGINT NOT NULL DEFAULT 0,
PRIMARY KEY (`request_id`),
KEY usage_api_key_id_idx (`api_key_id`),
KEY usage_billing_status_idx (`billing_status`),
KEY usage_created_at_idx (`created_at_unix_ms`),
KEY usage_provider_api_key_id_idx (`provider_api_key_id`),
KEY usage_provider_id_idx (`provider_id`),
KEY usage_request_id_idx (`request_id`),
KEY usage_user_id_idx (`user_id`),
KEY usage_wallet_id_idx (`wallet_id`)
);
CREATE TABLE IF NOT EXISTS usage_body_blobs (
`body_ref` VARCHAR(160) NOT NULL,
`request_id` VARCHAR(128) NOT NULL,
`body_field` VARCHAR(50) NOT NULL,
`payload_gzip` LONGBLOB NOT NULL,
`created_at` BIGINT NOT NULL,
`updated_at` BIGINT NOT NULL,
PRIMARY KEY (`body_ref`),
UNIQUE KEY usage_body_blobs_request_id_field_key (`request_id`, `body_field`),
KEY ix_usage_body_blobs_request_id (`request_id`),
CONSTRAINT usage_body_blobs_request_id_fkey FOREIGN KEY (`request_id`) REFERENCES usage (`request_id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS usage_http_audits (
`request_id` VARCHAR(128) NOT NULL,
`request_headers` JSON,
`provider_request_headers` JSON,
`response_headers` JSON,
`client_response_headers` JSON,
`request_body_ref` VARCHAR(160),
`provider_request_body_ref` VARCHAR(160),
`response_body_ref` VARCHAR(160),
`client_response_body_ref` VARCHAR(160),
`request_body_state` VARCHAR(32),
`provider_request_body_state` VARCHAR(32),
`response_body_state` VARCHAR(32),
`client_response_body_state` VARCHAR(32),
`body_capture_mode` VARCHAR(32) NOT NULL DEFAULT 'none',
`created_at` BIGINT NOT NULL,
`updated_at` BIGINT NOT NULL,
PRIMARY KEY (`request_id`),
KEY ix_usage_http_audits_updated_at (`updated_at`),
CONSTRAINT usage_http_audits_request_id_fkey FOREIGN KEY (`request_id`) REFERENCES usage (`request_id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS usage_routing_snapshots (
`request_id` VARCHAR(128) NOT NULL,
`candidate_id` VARCHAR(160),
`candidate_index` BIGINT,
`key_name` VARCHAR(255),
`planner_kind` VARCHAR(120),
`route_family` VARCHAR(80),
`route_kind` VARCHAR(80),
`execution_path` VARCHAR(80),
`local_execution_runtime_miss_reason` VARCHAR(255),
`selected_provider_id` VARCHAR(100),
`selected_endpoint_id` VARCHAR(100),
`selected_provider_api_key_id` VARCHAR(100),
`has_format_conversion` TINYINT(1),
`created_at` BIGINT NOT NULL,
`updated_at` BIGINT NOT NULL,
PRIMARY KEY (`request_id`),
KEY ix_usage_routing_snapshots_route_family_kind (`route_family`, `route_kind`),
KEY ix_usage_routing_snapshots_candidate_id (`candidate_id`),
CONSTRAINT usage_routing_snapshots_request_id_fkey FOREIGN KEY (`request_id`) REFERENCES usage (`request_id`) ON DELETE CASCADE
);
2026-05-19 00:30:24 +08:00
CREATE TABLE IF NOT EXISTS usage_counter_deltas (
`id` VARCHAR(36) NOT NULL,
`request_id` VARCHAR(128) NOT NULL,
`kind` VARCHAR(64) NOT NULL,
`target_id` TEXT NOT NULL,
`target_tunnel_generation` VARCHAR(64),
2026-05-19 00:30:24 +08:00
`request_count_delta` BIGINT NOT NULL DEFAULT 0,
`total_requests_delta` BIGINT NOT NULL DEFAULT 0,
`success_count_delta` BIGINT NOT NULL DEFAULT 0,
`error_count_delta` BIGINT NOT NULL DEFAULT 0,
`dns_failures_delta` BIGINT NOT NULL DEFAULT 0,
`stream_errors_delta` BIGINT NOT NULL DEFAULT 0,
`total_tokens_delta` BIGINT NOT NULL DEFAULT 0,
`total_cost_usd_delta` DOUBLE NOT NULL DEFAULT 0,
`total_response_time_ms_delta` BIGINT NOT NULL DEFAULT 0,
`last_used_at_unix_secs` BIGINT,
`last_used_ip` TEXT,
`candidate_last_used_at_unix_secs` BIGINT,
`removed_last_used_at_unix_secs` BIGINT,
`usage_created_at_unix_secs` BIGINT,
`created_at` BIGINT NOT NULL,
`processed_at` BIGINT,
PRIMARY KEY (`id`),
KEY ix_usage_counter_deltas_unprocessed (`created_at`, `id`),
KEY ix_usage_counter_deltas_processed (`processed_at`, `created_at`, `id`),
KEY ix_usage_counter_deltas_request_kind (`request_id`, `kind`, `target_id`)
);
2026-05-05 18:27:36 +08:00
CREATE TABLE IF NOT EXISTS usage_settlement_snapshots (
`request_id` VARCHAR(128) NOT NULL,
`billing_status` VARCHAR(64) NOT NULL,
`wallet_id` VARCHAR(64),
`wallet_balance_before` DOUBLE,
`wallet_balance_after` DOUBLE,
`wallet_recharge_balance_before` DOUBLE,
`wallet_recharge_balance_after` DOUBLE,
`wallet_gift_balance_before` DOUBLE,
`wallet_gift_balance_after` DOUBLE,
`provider_monthly_used_usd` DOUBLE,
`billing_snapshot_schema_version` VARCHAR(20),
`billing_snapshot_status` VARCHAR(20),
`rate_multiplier` DOUBLE,
`is_free_tier` TINYINT(1),
`input_price_per_1m` DOUBLE,
`output_price_per_1m` DOUBLE,
`cache_creation_price_per_1m` DOUBLE,
`cache_read_price_per_1m` DOUBLE,
`price_per_request` DOUBLE,
`settlement_snapshot_schema_version` VARCHAR(20),
`settlement_snapshot` JSON,
`billing_dimensions` JSON,
`billing_input_tokens` BIGINT,
`billing_effective_input_tokens` BIGINT,
`billing_output_tokens` BIGINT,
`billing_cache_creation_tokens` BIGINT,
`billing_cache_creation_5m_tokens` BIGINT,
`billing_cache_creation_1h_tokens` BIGINT,
`billing_cache_read_tokens` BIGINT,
`billing_total_input_context` BIGINT,
`billing_cache_creation_cost_usd` DOUBLE,
`billing_cache_read_cost_usd` DOUBLE,
`billing_total_cost_usd` DOUBLE,
`billing_actual_total_cost_usd` DOUBLE,
`billing_pricing_source` VARCHAR(50),
`billing_rule_id` VARCHAR(100),
`billing_rule_version` VARCHAR(50),
2026-05-05 18:27:36 +08:00
`finalized_at` BIGINT,
`created_at` BIGINT NOT NULL,
`updated_at` BIGINT NOT NULL,
PRIMARY KEY (`request_id`),
KEY usage_settlement_snapshots_billing_status_idx (`billing_status`),
KEY usage_settlement_snapshots_wallet_id_idx (`wallet_id`),
KEY ix_usage_settlement_snapshots_schema_version (`settlement_snapshot_schema_version`),
KEY ix_usage_settlement_snapshots_pricing_source (`billing_pricing_source`)
2026-05-05 18:27:36 +08:00
);
CREATE TABLE IF NOT EXISTS usage_cost_reservations (
`request_id` VARCHAR(128) NOT NULL,
`subject_id` VARCHAR(128) NOT NULL,
`reservation_token` VARCHAR(128) NOT NULL,
`admitted_at` BIGINT NOT NULL,
`reserved_cost_units` BIGINT NOT NULL,
`actual_cost_units` BIGINT,
`state` VARCHAR(20) NOT NULL,
`reservation_expires_at` BIGINT NOT NULL,
`retain_until` BIGINT NOT NULL,
`finalized_at` BIGINT,
`created_at` BIGINT NOT NULL,
`updated_at` BIGINT NOT NULL,
PRIMARY KEY (`reservation_token`),
KEY usage_cost_reservations_request_id_idx (`request_id`),
KEY usage_cost_reservations_subject_admitted_at_idx (`subject_id`, `admitted_at`),
KEY usage_cost_reservations_reservation_expires_at_idx (`reservation_expires_at`),
KEY usage_cost_reservations_retain_until_token_idx (`retain_until`, `reservation_token`),
CONSTRAINT usage_cost_reservations_subject_id_fkey FOREIGN KEY (`subject_id`) REFERENCES users (`id`) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS usage_request_admissions (
`request_id` VARCHAR(128) NOT NULL,
`subject_id` VARCHAR(128) NOT NULL,
`event_token` VARCHAR(128) NOT NULL,
`admitted_at` BIGINT NOT NULL,
`retain_until` BIGINT NOT NULL,
`state` VARCHAR(20) NOT NULL,
`released_at` BIGINT,
`created_at` BIGINT NOT NULL,
PRIMARY KEY (`event_token`),
KEY usage_request_admissions_subject_admitted_at_idx (`subject_id`, `admitted_at`),
KEY usage_request_admissions_retain_until_token_idx (`retain_until`, `event_token`),
CONSTRAINT usage_request_admissions_subject_id_fkey FOREIGN KEY (`subject_id`) REFERENCES users (`id`) ON DELETE CASCADE
);