Rootfix usage counter outbox

This commit is contained in:
fawney19
2026-05-19 00:30:24 +08:00
parent 19c8688eb1
commit 5eed329916
60 changed files with 4910 additions and 362 deletions

View File

@@ -168,7 +168,7 @@ CREATE TABLE IF NOT EXISTS public.api_keys (
name character varying(100),
key_prefix character varying(64),
status character varying(64) DEFAULT 'active'::character varying NOT NULL,
total_requests integer DEFAULT 0,
total_requests bigint DEFAULT 0,
total_tokens bigint DEFAULT '0'::bigint NOT NULL,
total_cost_usd numeric(20,8) DEFAULT '0'::double precision,
is_standalone boolean DEFAULT false NOT NULL,
@@ -288,7 +288,7 @@ CREATE TABLE IF NOT EXISTS public.global_models (
default_tiered_pricing json,
supported_capabilities json,
is_active boolean DEFAULT true NOT NULL,
usage_count integer DEFAULT 0 NOT NULL,
usage_count bigint DEFAULT 0 NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
config jsonb,
@@ -359,7 +359,7 @@ CREATE TABLE IF NOT EXISTS public.management_tokens (
expires_at timestamp with time zone,
last_used_at timestamp with time zone,
last_used_ip character varying(45),
usage_count integer DEFAULT 0 NOT NULL,
usage_count bigint DEFAULT 0 NOT NULL,
is_active boolean DEFAULT true NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
@@ -585,10 +585,10 @@ CREATE TABLE IF NOT EXISTS public.provider_api_keys (
last_probe_increase_at timestamp with time zone,
cache_ttl_minutes integer DEFAULT 5 NOT NULL,
max_probe_interval_minutes integer DEFAULT 32 NOT NULL,
request_count integer DEFAULT 0,
success_count integer DEFAULT 0,
error_count integer DEFAULT 0,
total_response_time_ms integer DEFAULT 0,
request_count bigint DEFAULT 0,
success_count bigint DEFAULT 0,
error_count bigint DEFAULT 0,
total_response_time_ms bigint DEFAULT 0,
last_used_at timestamp with time zone,
last_error_at timestamp with time zone,
last_error_msg text,
@@ -856,7 +856,7 @@ CREATE TABLE IF NOT EXISTS public.refund_requests (
CREATE TABLE IF NOT EXISTS public.request_candidates (
id character varying(36) NOT NULL,
request_id character varying(100) NOT NULL,
request_id character varying(128) NOT NULL,
user_id character varying(36),
api_key_id character varying(36),
candidate_index integer NOT NULL,
@@ -1263,6 +1263,39 @@ CREATE TABLE IF NOT EXISTS public.usage (
--
-- Name: usage_counter_deltas; Type: TABLE; Schema: public; Owner: -
--
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,
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 DEFAULT now() NOT NULL,
processed_at timestamp with time zone,
CONSTRAINT usage_counter_deltas_pkey PRIMARY KEY (id),
CONSTRAINT usage_counter_deltas_kind_check CHECK (
kind IN ('api_key', 'provider_api_key', 'model', 'provider_monthly', 'proxy_node', 'management_token', 'api_key_last_used')
)
);
--
-- Name: user_model_usage_counts; Type: TABLE; Schema: public; Owner: -
--

View File

@@ -173,6 +173,14 @@ CREATE INDEX IF NOT EXISTS idx_entitlement_usage_user_date ON public.entitlement
--
-- Name: idx_entitlement_usage_entitlement_date; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX IF NOT EXISTS idx_entitlement_usage_entitlement_date ON public.entitlement_usage_ledgers USING btree (user_entitlement_id, usage_date);
--
-- Name: idx_provider_api_keys_provider_active; Type: INDEX; Schema: public; Owner: -
--
@@ -549,6 +557,30 @@ CREATE INDEX IF NOT EXISTS idx_usage_wallet_finalized ON public.usage USING btre
--
-- Name: ix_usage_counter_deltas_request_kind; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX IF NOT EXISTS ix_usage_counter_deltas_request_kind ON public.usage_counter_deltas USING btree (request_id, kind, target_id);
--
-- Name: ix_usage_counter_deltas_processed; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX IF NOT EXISTS ix_usage_counter_deltas_processed ON public.usage_counter_deltas USING btree (processed_at, created_at, id) WHERE processed_at IS NOT NULL;
--
-- Name: ix_usage_counter_deltas_unprocessed; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX IF NOT EXISTS ix_usage_counter_deltas_unprocessed ON public.usage_counter_deltas USING btree (created_at, id) WHERE processed_at IS NULL;
--
-- Name: idx_user_model_usage_model; Type: INDEX; Schema: public; Owner: -
--
@@ -597,6 +629,14 @@ CREATE INDEX IF NOT EXISTS idx_video_tasks_next_poll ON public.video_tasks USING
--
-- Name: idx_video_tasks_due_poll; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX IF NOT EXISTS idx_video_tasks_due_poll ON public.video_tasks USING btree (status, next_poll_at, updated_at) WHERE next_poll_at IS NOT NULL;
--
-- Name: idx_video_tasks_request_id; Type: INDEX; Schema: public; Owner: -
--