forked from xiaohei/taiji-AI-PAD
161 lines
6.0 KiB
PL/PgSQL
161 lines
6.0 KiB
PL/PgSQL
-- Migration: 006_add_agent_resource_management
|
|
-- Description: 添加 Agent 资源管理相关表(资源申请、平台Agent配额、Agent计费记录)
|
|
-- Date: 2026-01-05
|
|
|
|
-- 1. 资源申请表(统一的申请审批)
|
|
CREATE TABLE IF NOT EXISTS resource_applications (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
|
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
|
|
|
channel_id UUID NOT NULL REFERENCES channels(id),
|
|
|
|
-- 申请类型: platform_agent, provider, custom_agent_quota
|
|
resource_type VARCHAR(30) NOT NULL,
|
|
|
|
-- 平台 Agent 申请字段
|
|
template_name VARCHAR(100),
|
|
requested_pod_quota INTEGER,
|
|
|
|
-- 模型供应商申请字段
|
|
provider_id UUID REFERENCES model_providers(id),
|
|
requested_rpm INTEGER,
|
|
requested_tpm INTEGER,
|
|
|
|
-- 自定义 Agent 资源申请字段
|
|
requested_cpu_quota NUMERIC(12, 2),
|
|
requested_memory_quota NUMERIC(12, 2),
|
|
|
|
-- 申请信息
|
|
reason TEXT NOT NULL,
|
|
|
|
-- 审批状态: pending, approved, rejected
|
|
status VARCHAR(20) DEFAULT 'pending',
|
|
|
|
-- 审批结果
|
|
approved_pod_quota INTEGER,
|
|
approved_rpm INTEGER,
|
|
approved_tpm INTEGER,
|
|
approved_cpu_quota NUMERIC(12, 2),
|
|
approved_memory_quota NUMERIC(12, 2),
|
|
|
|
-- 审批信息
|
|
reviewed_by UUID REFERENCES users(id),
|
|
review_reason TEXT,
|
|
reviewed_at TIMESTAMP
|
|
);
|
|
|
|
-- 资源申请表索引
|
|
CREATE INDEX IF NOT EXISTS idx_resource_app_channel ON resource_applications(channel_id);
|
|
CREATE INDEX IF NOT EXISTS idx_resource_app_type ON resource_applications(resource_type);
|
|
CREATE INDEX IF NOT EXISTS idx_resource_app_status ON resource_applications(status);
|
|
CREATE INDEX IF NOT EXISTS idx_resource_app_created ON resource_applications(created_at);
|
|
|
|
-- 2. 平台 Agent 配额分配表
|
|
CREATE TABLE IF NOT EXISTS platform_agent_quotas (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
|
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
|
|
|
-- 分配目标
|
|
target_id UUID NOT NULL,
|
|
target_type VARCHAR(20) NOT NULL, -- channel, tenant
|
|
|
|
-- Agent 模板
|
|
template_name VARCHAR(100) NOT NULL,
|
|
|
|
-- 配额
|
|
pod_quota INTEGER NOT NULL DEFAULT 0,
|
|
pod_used INTEGER DEFAULT 0,
|
|
|
|
-- 分配信息
|
|
allocated_by UUID REFERENCES users(id),
|
|
allocated_at TIMESTAMP DEFAULT NOW(),
|
|
|
|
-- 唯一约束
|
|
CONSTRAINT uq_platform_agent_quota UNIQUE (target_id, target_type, template_name)
|
|
);
|
|
|
|
-- 平台 Agent 配额表索引
|
|
CREATE INDEX IF NOT EXISTS idx_platform_agent_quota_target ON platform_agent_quotas(target_id, target_type);
|
|
CREATE INDEX IF NOT EXISTS idx_platform_agent_quota_template ON platform_agent_quotas(template_name);
|
|
|
|
-- 3. Agent 计费记录表
|
|
CREATE TABLE IF NOT EXISTS agent_billing_records (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
|
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
|
|
|
user_id UUID NOT NULL REFERENCES users(id),
|
|
channel_id UUID REFERENCES channels(id),
|
|
|
|
-- Agent 信息
|
|
agent_name VARCHAR(100) NOT NULL,
|
|
agent_type VARCHAR(20) NOT NULL, -- platform, custom
|
|
template_name VARCHAR(100) NOT NULL,
|
|
|
|
-- 使用量
|
|
duration_seconds INTEGER NOT NULL,
|
|
cpu_seconds FLOAT DEFAULT 0,
|
|
memory_gb_seconds FLOAT DEFAULT 0,
|
|
request_count INTEGER DEFAULT 0,
|
|
|
|
-- 费用
|
|
cost NUMERIC(12, 4) NOT NULL,
|
|
currency VARCHAR(10) DEFAULT 'EU',
|
|
|
|
-- 时间范围
|
|
period_start TIMESTAMP NOT NULL,
|
|
period_end TIMESTAMP NOT NULL
|
|
);
|
|
|
|
-- Agent 计费记录表索引
|
|
CREATE INDEX IF NOT EXISTS idx_agent_billing_user ON agent_billing_records(user_id);
|
|
CREATE INDEX IF NOT EXISTS idx_agent_billing_channel ON agent_billing_records(channel_id);
|
|
CREATE INDEX IF NOT EXISTS idx_agent_billing_period ON agent_billing_records(period_start, period_end);
|
|
CREATE INDEX IF NOT EXISTS idx_agent_billing_type ON agent_billing_records(agent_type);
|
|
|
|
-- 4. 添加触发器自动更新 updated_at
|
|
CREATE OR REPLACE FUNCTION update_updated_at_column()
|
|
RETURNS TRIGGER AS $$
|
|
BEGIN
|
|
NEW.updated_at = NOW();
|
|
RETURN NEW;
|
|
END;
|
|
$$ language 'plpgsql';
|
|
|
|
-- 为新表添加触发器
|
|
DROP TRIGGER IF EXISTS update_resource_applications_updated_at ON resource_applications;
|
|
CREATE TRIGGER update_resource_applications_updated_at
|
|
BEFORE UPDATE ON resource_applications
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION update_updated_at_column();
|
|
|
|
DROP TRIGGER IF EXISTS update_platform_agent_quotas_updated_at ON platform_agent_quotas;
|
|
CREATE TRIGGER update_platform_agent_quotas_updated_at
|
|
BEFORE UPDATE ON platform_agent_quotas
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION update_updated_at_column();
|
|
|
|
DROP TRIGGER IF EXISTS update_agent_billing_records_updated_at ON agent_billing_records;
|
|
CREATE TRIGGER update_agent_billing_records_updated_at
|
|
BEFORE UPDATE ON agent_billing_records
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION update_updated_at_column();
|
|
|
|
-- 5. 添加注释
|
|
COMMENT ON TABLE resource_applications IS '资源申请表 - 渠道申请平台Agent、供应商、自定义Agent资源';
|
|
COMMENT ON TABLE platform_agent_quotas IS '平台Agent配额表 - 记录渠道和租户的平台Agent Pod配额';
|
|
COMMENT ON TABLE agent_billing_records IS 'Agent计费记录表 - 记录Agent使用的计费信息';
|
|
|
|
COMMENT ON COLUMN resource_applications.resource_type IS '申请类型: platform_agent, provider, custom_agent_quota';
|
|
COMMENT ON COLUMN resource_applications.template_name IS '平台Agent模板名称';
|
|
COMMENT ON COLUMN resource_applications.status IS '审批状态: pending, approved, rejected';
|
|
|
|
COMMENT ON COLUMN platform_agent_quotas.target_type IS '分配目标类型: channel, tenant';
|
|
COMMENT ON COLUMN platform_agent_quotas.pod_quota IS 'Pod数量配额';
|
|
COMMENT ON COLUMN platform_agent_quotas.pod_used IS '已使用Pod数量';
|
|
|
|
COMMENT ON COLUMN agent_billing_records.agent_type IS 'Agent类型: platform, custom';
|
|
COMMENT ON COLUMN agent_billing_records.duration_seconds IS '运行时长(秒)';
|