feat: extend enterprise gateway policies billing protocols and operations

This commit is contained in:
wlx committed 2026-10-13 19:08:50 +08:00
1 parent 1528b71882
commit af43947b5d
67 files changed
+1822 -69

No files matched your search

@@ -27,6 +27,8 @@ server:
address: ${CONTROL_PLANE_HOST:0.0.0.0}
port: ${CONTROL_PLANE_PORT:8787}
shutdown: graceful
# Native Tomcat processing trusts only its configured internal proxies, never arbitrary clients.
forward-headers-strategy: ${MOYI_FORWARD_HEADERS_STRATEGY:none}
management:
endpoints:
@@ -0,0 +1,31 @@
ALTER TABLE model_catalog ADD COLUMN procurement_pricing jsonb;
CREATE TABLE model_price_history (
id bigserial PRIMARY KEY,
model_id varchar(64) NOT NULL,
pricing jsonb,
procurement_pricing jsonb,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX model_price_history_model_idx ON model_price_history(model_id,id DESC);
INSERT INTO model_price_history(model_id,pricing) SELECT id,pricing FROM model_catalog;
CREATE FUNCTION record_model_price_change() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
IF TG_OP='INSERT' THEN
INSERT INTO model_price_history(model_id,pricing,procurement_pricing) VALUES(NEW.id,NEW.pricing,NEW.procurement_pricing);
ELSIF NEW.pricing IS DISTINCT FROM OLD.pricing OR NEW.procurement_pricing IS DISTINCT FROM OLD.procurement_pricing THEN
INSERT INTO model_price_history(model_id,pricing,procurement_pricing) VALUES(NEW.id,NEW.pricing,NEW.procurement_pricing);
END IF;
RETURN NEW;
END $$;
CREATE TRIGGER model_price_version AFTER INSERT OR UPDATE ON model_catalog FOR EACH ROW EXECUTE FUNCTION record_model_price_change();
ALTER TABLE chat_billing_requests ADD COLUMN procurement_pricing jsonb;
ALTER TABLE chat_billing_requests ADD COLUMN price_version bigint REFERENCES model_price_history(id);
ALTER TABLE usage_events ADD COLUMN procurement_cost numeric(20,8) CHECK(procurement_cost>=0);
ALTER TABLE usage_events ADD COLUMN profit_amount numeric(20,8);
ALTER TABLE usage_events ADD COLUMN cost_status varchar(24) NOT NULL DEFAULT 'unknown'
CHECK(cost_status IN ('unknown','estimated','pending','reconciled'));
ALTER TABLE usage_events ADD COLUMN reconciliation_note text;
ALTER TABLE usage_events ADD COLUMN reconciled_by uuid REFERENCES users(id);
ALTER TABLE usage_events ADD COLUMN reconciled_at timestamptz;
CREATE INDEX usage_cost_pending_idx ON usage_events(created_at) WHERE cost_status IN ('pending','unknown');
@@ -0,0 +1,24 @@
CREATE TABLE gateway_user_policies (
user_id uuid PRIMARY KEY REFERENCES users(id),
requests_per_minute integer NOT NULL DEFAULT 120 CHECK(requests_per_minute BETWEEN 1 AND 100000),
max_concurrency integer NOT NULL DEFAULT 16 CHECK(max_concurrency BETWEEN 1 AND 10000),
monthly_budget numeric(20,8) CHECK(monthly_budget >= 0),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE gateway_key_policies (
key_id uuid PRIMARY KEY REFERENCES platform_api_keys(id),
expires_at timestamptz,
ip_allowlist jsonb NOT NULL DEFAULT '[]' CHECK(jsonb_typeof(ip_allowlist)='array'),
total_budget numeric(20,8) CHECK(total_budget >= 0),
daily_budget numeric(20,8) CHECK(daily_budget >= 0),
monthly_budget numeric(20,8) CHECK(monthly_budget >= 0),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE gateway_budget_holds (
request_id varchar(100) PRIMARY KEY,
user_id uuid NOT NULL REFERENCES users(id),
key_id uuid NOT NULL REFERENCES platform_api_keys(id),
amount numeric(20,8) NOT NULL CHECK(amount >= 0),
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX gateway_budget_holds_user ON gateway_budget_holds(user_id);
@@ -0,0 +1,16 @@
CREATE TABLE user_totp (
user_id uuid PRIMARY KEY REFERENCES users(id),
secret_ciphertext text NOT NULL,
enabled boolean NOT NULL DEFAULT false,
pending_until timestamptz,
last_step bigint NOT NULL DEFAULT -1,
attempts integer NOT NULL DEFAULT 0,
attempt_window timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE user_totp_recovery (
user_id uuid NOT NULL REFERENCES users(id),
code_hash varchar(64) NOT NULL,
used_at timestamptz,
PRIMARY KEY(user_id,code_hash)
);
@@ -0,0 +1,34 @@
CREATE TABLE upstream_account_health (
route_id varchar(64) NOT NULL,
account_fingerprint varchar(64) NOT NULL,
failures integer NOT NULL DEFAULT 0,
last_status integer,
latency_ms integer,
cooldown_until timestamptz,
updated_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY(route_id,account_fingerprint)
);
CREATE TABLE ops_alert_rules (
id varchar(64) PRIMARY KEY,
name varchar(120) NOT NULL,
enabled boolean NOT NULL DEFAULT true,
window_minutes integer NOT NULL DEFAULT 5 CHECK(window_minutes BETWEEN 1 AND 1440),
minimum_requests integer NOT NULL DEFAULT 10 CHECK(minimum_requests BETWEEN 1 AND 1000000),
error_percent numeric(5,2) NOT NULL DEFAULT 20 CHECK(error_percent BETWEEN 0 AND 100),
cooldown_minutes integer NOT NULL DEFAULT 30 CHECK(cooldown_minutes BETWEEN 1 AND 1440),
last_fired_at timestamptz,
updated_at timestamptz NOT NULL DEFAULT now()
);
INSERT INTO ops_alert_rules(id,name) VALUES ('gateway-errors','网关错误率');
CREATE TABLE ops_alert_events (
id uuid PRIMARY KEY,
rule_id varchar(64) NOT NULL REFERENCES ops_alert_rules(id),
total_requests bigint NOT NULL,
failed_requests bigint NOT NULL,
error_percent numeric(5,2) NOT NULL,
status varchar(16) NOT NULL DEFAULT 'open' CHECK(status IN ('open','acknowledged','resolved')),
created_at timestamptz NOT NULL DEFAULT now(),
acknowledged_by uuid REFERENCES users(id),
acknowledged_at timestamptz
);
CREATE INDEX ops_alert_events_recent ON ops_alert_events(created_at DESC);
@@ -0,0 +1,2 @@
ALTER TABLE usage_events ADD COLUMN actual_route_id varchar(64);
ALTER TABLE usage_events ADD COLUMN observed_usage jsonb;
@@ -0,0 +1,6 @@
ALTER TABLE ops_alert_rules ADD COLUMN notification_email varchar(254);
ALTER TABLE ops_alert_rules ADD COLUMN notification_enabled boolean NOT NULL DEFAULT false;
ALTER TABLE ops_alert_events ADD COLUMN notification_status varchar(24) NOT NULL DEFAULT 'disabled';
ALTER TABLE ops_alert_events ADD COLUMN notification_attempts integer NOT NULL DEFAULT 0;
ALTER TABLE ops_alert_events ADD COLUMN notification_next_at timestamptz;
ALTER TABLE ops_alert_events ADD COLUMN notification_email varchar(254);
@@ -5,5 +5,9 @@ if redis.call('ZCARD', KEYS[1]) >= tonumber(ARGV[1]) then
return 0
end
redis.call('ZADD', KEYS[1], now + tonumber(ARGV[2]), ARGV[3])
redis.call('PEXPIRE', KEYS[1], ARGV[2])
-- Long async leases and short chat leases share the user bucket.
-- A chat admission must never shorten a running async task's lease container.
if redis.call('PTTL', KEYS[1]) < tonumber(ARGV[2]) then
redis.call('PEXPIRE', KEYS[1], ARGV[2])
end
return 1