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

291 lines
15 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 public.usage (
request_id character varying(128) NOT NULL,
id character varying(128),
user_id character varying(64),
api_key_id character varying(64),
provider_name character varying(255) DEFAULT 'unknown' NOT NULL,
model character varying(255) DEFAULT 'unknown' NOT NULL,
target_model character varying(255),
provider_id character varying(64),
provider_endpoint_id character varying(64),
provider_api_key_id character varying(64),
request_type character varying(64),
api_format character varying(64),
api_family character varying(64),
endpoint_kind character varying(64),
endpoint_api_format character varying(64),
provider_api_family character varying(64),
provider_endpoint_kind character varying(64),
has_format_conversion boolean DEFAULT false NOT NULL,
is_stream boolean DEFAULT false NOT NULL,
upstream_is_stream boolean,
2026-05-05 18:27:36 +08:00
input_tokens bigint DEFAULT 0 NOT NULL,
output_tokens bigint DEFAULT 0 NOT NULL,
input_output_total_tokens bigint DEFAULT 0 NOT NULL,
total_tokens bigint DEFAULT 0 NOT NULL,
cache_creation_input_tokens bigint DEFAULT 0 NOT NULL,
cache_creation_input_tokens_5m bigint DEFAULT 0 NOT NULL,
cache_creation_input_tokens_1h bigint DEFAULT 0 NOT NULL,
cache_creation_ephemeral_5m_input_tokens bigint DEFAULT 0 NOT NULL,
cache_creation_ephemeral_1h_input_tokens bigint DEFAULT 0 NOT NULL,
cache_read_input_tokens bigint DEFAULT 0 NOT NULL,
input_context_tokens bigint DEFAULT 0 NOT NULL,
input_cost_usd double precision DEFAULT 0 NOT NULL,
output_cost_usd double precision DEFAULT 0 NOT NULL,
cache_cost_usd double precision DEFAULT 0 NOT NULL,
cache_creation_cost_usd double precision DEFAULT 0 NOT NULL,
cache_creation_cost_usd_5m double precision DEFAULT 0 NOT NULL,
cache_creation_cost_usd_1h double precision DEFAULT 0 NOT NULL,
cache_read_cost_usd double precision DEFAULT 0 NOT NULL,
request_cost_usd double precision DEFAULT 0 NOT NULL,
actual_input_cost_usd double precision DEFAULT 0 NOT NULL,
actual_output_cost_usd double precision DEFAULT 0 NOT NULL,
actual_cache_cost_usd double precision DEFAULT 0 NOT NULL,
actual_cache_creation_cost_usd double precision DEFAULT 0 NOT NULL,
actual_cache_creation_cost_usd_5m double precision DEFAULT 0 NOT NULL,
actual_cache_creation_cost_usd_1h double precision DEFAULT 0 NOT NULL,
actual_cache_read_cost_usd double precision DEFAULT 0 NOT NULL,
actual_request_cost_usd double precision DEFAULT 0 NOT NULL,
rate_multiplier double precision DEFAULT 1 NOT NULL,
input_price_per_1m double precision,
output_price_per_1m double precision,
cache_creation_price_per_1m double precision,
cache_creation_price_per_1m_5m double precision,
cache_creation_price_per_1m_1h double precision,
cache_read_price_per_1m double precision,
price_per_request double precision,
status_code integer,
error_message text,
error_category character varying(255),
response_time_ms bigint,
first_byte_time_ms bigint,
wallet_id character varying(64),
status character varying(64) DEFAULT 'completed' NOT NULL,
billing_status character varying(64) DEFAULT 'pending' NOT NULL,
total_cost_usd double precision DEFAULT 0 NOT NULL,
actual_total_cost_usd double precision DEFAULT 0 NOT NULL,
request_headers jsonb,
request_body jsonb,
provider_request_headers jsonb,
provider_request_body jsonb,
response_headers jsonb,
response_body jsonb,
client_response_headers jsonb,
client_response_body jsonb,
request_body_compressed bytea,
provider_request_body_compressed bytea,
response_body_compressed bytea,
client_response_body_compressed bytea,
request_metadata jsonb,
created_at bigint,
candidate_id character varying(128),
candidate_index bigint,
key_name character varying(255),
username character varying(255),
api_key_name character varying(255),
planner_kind character varying(64),
route_family character varying(128),
route_kind character varying(128),
execution_path character varying(128),
local_execution_runtime_miss_reason character varying(255),
wallet_balance_before double precision,
wallet_balance_after double precision,
wallet_recharge_balance_before double precision,
wallet_recharge_balance_after double precision,
wallet_gift_balance_before double precision,
wallet_gift_balance_after double precision,
finalized_at bigint,
created_at_unix_ms bigint DEFAULT 0 NOT NULL,
updated_at_unix_secs bigint DEFAULT 0 NOT NULL
);
ALTER TABLE ONLY public.usage ADD CONSTRAINT usage_pkey PRIMARY KEY (request_id);
CREATE INDEX IF NOT EXISTS usage_api_key_id_idx ON public.usage USING btree (api_key_id);
CREATE INDEX IF NOT EXISTS usage_billing_status_idx ON public.usage USING btree (billing_status);
CREATE INDEX IF NOT EXISTS usage_created_at_idx ON public.usage USING btree (created_at_unix_ms);
CREATE INDEX IF NOT EXISTS usage_provider_api_key_id_idx ON public.usage USING btree (provider_api_key_id);
CREATE INDEX IF NOT EXISTS usage_provider_id_idx ON public.usage USING btree (provider_id);
CREATE INDEX IF NOT EXISTS usage_request_id_idx ON public.usage USING btree (request_id);
CREATE INDEX IF NOT EXISTS usage_user_id_idx ON public.usage USING btree (user_id);
CREATE INDEX IF NOT EXISTS usage_wallet_id_idx ON public.usage USING btree (wallet_id);
CREATE TABLE IF NOT EXISTS public.usage_body_blobs (
body_ref character varying(160) NOT NULL,
request_id character varying(128) NOT NULL,
body_field character varying(50) NOT NULL,
payload_gzip bytea NOT NULL,
created_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone NOT NULL
);
ALTER TABLE ONLY public.usage_body_blobs ADD CONSTRAINT usage_body_blobs_pkey PRIMARY KEY (body_ref);
ALTER TABLE ONLY public.usage_body_blobs ADD CONSTRAINT usage_body_blobs_request_id_field_key UNIQUE (request_id, body_field);
CREATE INDEX IF NOT EXISTS ix_usage_body_blobs_request_id ON public.usage_body_blobs USING btree (request_id);
ALTER TABLE ONLY public.usage_body_blobs ADD CONSTRAINT usage_body_blobs_request_id_fkey FOREIGN KEY (request_id) REFERENCES public.usage(request_id) ON DELETE CASCADE;
CREATE TABLE IF NOT EXISTS public.usage_http_audits (
request_id character varying(128) NOT NULL,
request_headers jsonb,
provider_request_headers jsonb,
response_headers jsonb,
client_response_headers jsonb,
request_body_ref character varying(160),
provider_request_body_ref character varying(160),
response_body_ref character varying(160),
client_response_body_ref character varying(160),
request_body_state character varying(32),
provider_request_body_state character varying(32),
response_body_state character varying(32),
client_response_body_state character varying(32),
body_capture_mode character varying(32) DEFAULT 'none' NOT NULL,
created_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone NOT NULL
);
ALTER TABLE ONLY public.usage_http_audits ADD CONSTRAINT usage_http_audits_pkey PRIMARY KEY (request_id);
CREATE INDEX IF NOT EXISTS ix_usage_http_audits_updated_at ON public.usage_http_audits USING btree (updated_at);
ALTER TABLE ONLY public.usage_http_audits ADD CONSTRAINT usage_http_audits_request_id_fkey FOREIGN KEY (request_id) REFERENCES public.usage(request_id) ON DELETE CASCADE;
CREATE TABLE IF NOT EXISTS public.usage_routing_snapshots (
request_id character varying(128) NOT NULL,
candidate_id character varying(160),
candidate_index bigint,
key_name character varying(255),
planner_kind character varying(120),
route_family character varying(80),
route_kind character varying(80),
execution_path character varying(80),
local_execution_runtime_miss_reason character varying(255),
selected_provider_id character varying(100),
selected_endpoint_id character varying(100),
selected_provider_api_key_id character varying(100),
has_format_conversion boolean,
created_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone NOT NULL
);
ALTER TABLE ONLY public.usage_routing_snapshots ADD CONSTRAINT usage_routing_snapshots_pkey PRIMARY KEY (request_id);
CREATE INDEX IF NOT EXISTS ix_usage_routing_snapshots_route_family_kind ON public.usage_routing_snapshots USING btree (route_family, route_kind);
CREATE INDEX IF NOT EXISTS ix_usage_routing_snapshots_candidate_id ON public.usage_routing_snapshots USING btree (candidate_id);
ALTER TABLE ONLY public.usage_routing_snapshots ADD CONSTRAINT usage_routing_snapshots_request_id_fkey FOREIGN KEY (request_id) REFERENCES public.usage(request_id) ON DELETE CASCADE;
2026-05-19 00:30:24 +08:00
CREATE TABLE IF NOT EXISTS public.usage_counter_deltas (
id character varying(36) NOT NULL,
request_id character varying(128) NOT NULL,
kind character varying(64) NOT NULL,
target_id text NOT NULL,
target_tunnel_generation character varying(64),
2026-05-19 00:30:24 +08:00
request_count_delta bigint DEFAULT 0 NOT NULL,
total_requests_delta bigint DEFAULT 0 NOT NULL,
success_count_delta bigint DEFAULT 0 NOT NULL,
error_count_delta bigint DEFAULT 0 NOT NULL,
dns_failures_delta bigint DEFAULT 0 NOT NULL,
stream_errors_delta bigint DEFAULT 0 NOT NULL,
total_tokens_delta bigint DEFAULT 0 NOT NULL,
total_cost_usd_delta double precision DEFAULT 0 NOT NULL,
total_response_time_ms_delta bigint DEFAULT 0 NOT NULL,
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 timestamp with time zone NOT NULL,
processed_at timestamp with time zone
);
ALTER TABLE ONLY public.usage_counter_deltas ADD CONSTRAINT usage_counter_deltas_pkey PRIMARY KEY (id);
CREATE INDEX IF NOT EXISTS ix_usage_counter_deltas_unprocessed ON public.usage_counter_deltas USING btree (created_at, id);
CREATE INDEX IF NOT EXISTS ix_usage_counter_deltas_processed ON public.usage_counter_deltas USING btree (processed_at, created_at, id);
CREATE INDEX IF NOT EXISTS ix_usage_counter_deltas_request_kind ON public.usage_counter_deltas USING btree (request_id, kind, target_id);
2026-05-05 18:27:36 +08:00
CREATE TABLE IF NOT EXISTS public.usage_settlement_snapshots (
request_id character varying(128) NOT NULL,
billing_status character varying(64) NOT NULL,
wallet_id character varying(64),
wallet_balance_before double precision,
wallet_balance_after double precision,
wallet_recharge_balance_before double precision,
wallet_recharge_balance_after double precision,
wallet_gift_balance_before double precision,
wallet_gift_balance_after double precision,
provider_monthly_used_usd double precision,
billing_snapshot_schema_version character varying(20),
billing_snapshot_status character varying(20),
rate_multiplier double precision,
is_free_tier boolean,
input_price_per_1m double precision,
output_price_per_1m double precision,
cache_creation_price_per_1m double precision,
cache_read_price_per_1m double precision,
price_per_request double precision,
settlement_snapshot_schema_version character varying(20),
settlement_snapshot jsonb,
billing_dimensions jsonb,
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 precision,
billing_cache_read_cost_usd double precision,
billing_total_cost_usd double precision,
billing_actual_total_cost_usd double precision,
billing_pricing_source character varying(50),
billing_rule_id character varying(100),
billing_rule_version character varying(50),
2026-05-05 18:27:36 +08:00
finalized_at bigint,
created_at bigint NOT NULL,
updated_at bigint NOT NULL
);
ALTER TABLE ONLY public.usage_settlement_snapshots ADD CONSTRAINT usage_settlement_snapshots_pkey PRIMARY KEY (request_id);
CREATE INDEX IF NOT EXISTS usage_settlement_snapshots_billing_status_idx ON public.usage_settlement_snapshots USING btree (billing_status);
CREATE INDEX IF NOT EXISTS usage_settlement_snapshots_wallet_id_idx ON public.usage_settlement_snapshots USING btree (wallet_id);
CREATE INDEX IF NOT EXISTS ix_usage_settlement_snapshots_schema_version ON public.usage_settlement_snapshots USING btree (settlement_snapshot_schema_version);
CREATE INDEX IF NOT EXISTS ix_usage_settlement_snapshots_pricing_source ON public.usage_settlement_snapshots USING btree (billing_pricing_source);
2026-05-05 18:27:36 +08:00
CREATE TABLE IF NOT EXISTS public.usage_cost_reservations (
request_id character varying(128) NOT NULL,
subject_id character varying(128) NOT NULL,
reservation_token character varying(128) NOT NULL,
admitted_at timestamp with time zone NOT NULL,
reserved_cost_units bigint NOT NULL,
actual_cost_units bigint,
state character varying(20) NOT NULL,
reservation_expires_at timestamp with time zone NOT NULL,
retain_until timestamp with time zone NOT NULL,
finalized_at timestamp with time zone,
created_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone NOT NULL
);
ALTER TABLE ONLY public.usage_cost_reservations ADD CONSTRAINT usage_cost_reservations_pkey PRIMARY KEY (reservation_token);
CREATE INDEX IF NOT EXISTS usage_cost_reservations_request_id_idx ON public.usage_cost_reservations USING btree (request_id);
CREATE INDEX IF NOT EXISTS usage_cost_reservations_subject_admitted_at_idx ON public.usage_cost_reservations USING btree (subject_id, admitted_at);
CREATE INDEX IF NOT EXISTS usage_cost_reservations_reservation_expires_at_idx ON public.usage_cost_reservations USING btree (reservation_expires_at);
CREATE INDEX IF NOT EXISTS usage_cost_reservations_retain_until_token_idx ON public.usage_cost_reservations USING btree (retain_until, reservation_token);
ALTER TABLE ONLY public.usage_cost_reservations ADD CONSTRAINT usage_cost_reservations_subject_id_fkey FOREIGN KEY (subject_id) REFERENCES public.users(id) ON DELETE CASCADE;
CREATE TABLE IF NOT EXISTS public.usage_request_admissions (
request_id character varying(128) NOT NULL,
subject_id character varying(128) NOT NULL,
event_token character varying(128) NOT NULL,
admitted_at timestamp with time zone NOT NULL,
retain_until timestamp with time zone NOT NULL,
state character varying(20) NOT NULL,
released_at timestamp with time zone,
created_at timestamp with time zone NOT NULL
);
ALTER TABLE ONLY public.usage_request_admissions ADD CONSTRAINT usage_request_admissions_pkey PRIMARY KEY (event_token);
CREATE INDEX IF NOT EXISTS usage_request_admissions_subject_admitted_at_idx ON public.usage_request_admissions USING btree (subject_id, admitted_at);
CREATE INDEX IF NOT EXISTS usage_request_admissions_retain_until_token_idx ON public.usage_request_admissions USING btree (retain_until, event_token);
ALTER TABLE ONLY public.usage_request_admissions ADD CONSTRAINT usage_request_admissions_subject_id_fkey FOREIGN KEY (subject_id) REFERENCES public.users(id) ON DELETE CASCADE;