95 lines
5.3 KiB
Python
95 lines
5.3 KiB
Python
"""add model capability gap and cost tracking tables
|
|
|
|
Revision ID: add_model_capability_tables
|
|
Revises: add_ai_tables
|
|
Create Date: 2026-02-25
|
|
"""
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
revision = "add_model_capability_tables"
|
|
down_revision = "add_ai_tables"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def _table_exists(table_name: str) -> bool:
|
|
bind = op.get_bind()
|
|
inspector = sa.inspect(bind)
|
|
return table_name in inspector.get_table_names()
|
|
|
|
|
|
def upgrade():
|
|
if not _table_exists("model_capability_gaps"):
|
|
op.create_table(
|
|
"model_capability_gaps",
|
|
sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
|
|
sa.Column("tenant_id", sa.BigInteger(), nullable=False),
|
|
sa.Column("model_version_id", sa.BigInteger(), nullable=False),
|
|
sa.Column("truth_node_id", sa.BigInteger(), nullable=False),
|
|
sa.Column("gap_type", sa.String(length=50), nullable=False),
|
|
sa.Column("severity", sa.String(length=20), nullable=False),
|
|
sa.Column("description", sa.Text(), nullable=False),
|
|
sa.Column("ai_analysis", sa.JSON(), nullable=False),
|
|
sa.Column("status", sa.String(length=50), nullable=False, server_default="open"),
|
|
sa.Column("resolved_at", sa.TIMESTAMP(), nullable=True),
|
|
sa.Column("resolution_notes", sa.Text(), nullable=True),
|
|
sa.Column("created_at", sa.TIMESTAMP(), nullable=False, server_default=sa.func.now()),
|
|
sa.Column("updated_at", sa.TIMESTAMP(), nullable=False, server_default=sa.func.now()),
|
|
sa.ForeignKeyConstraint(["model_version_id"], ["model_versions.id"]),
|
|
sa.ForeignKeyConstraint(["truth_node_id"], ["truth_nodes.id"]),
|
|
)
|
|
op.create_index("ix_model_capability_gaps_tenant_id", "model_capability_gaps", ["tenant_id"])
|
|
op.create_index("ix_model_capability_gaps_model_version_id", "model_capability_gaps", ["model_version_id"])
|
|
op.create_index("ix_model_capability_gaps_truth_node_id", "model_capability_gaps", ["truth_node_id"])
|
|
op.create_index("ix_model_capability_gaps_gap_type", "model_capability_gaps", ["gap_type"])
|
|
op.create_index("ix_model_capability_gaps_severity", "model_capability_gaps", ["severity"])
|
|
op.create_index("ix_model_capability_gaps_status", "model_capability_gaps", ["status"])
|
|
|
|
if not _table_exists("cost_trackings"):
|
|
op.create_table(
|
|
"cost_trackings",
|
|
sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
|
|
sa.Column("tenant_id", sa.BigInteger(), nullable=False),
|
|
sa.Column("truth_node_id", sa.BigInteger(), nullable=True),
|
|
sa.Column("model_version_id", sa.BigInteger(), nullable=True),
|
|
sa.Column("task_id", sa.BigInteger(), nullable=True),
|
|
sa.Column("period", sa.String(length=20), nullable=False),
|
|
sa.Column("estimated_tokens", sa.Integer(), nullable=False, server_default="0"),
|
|
sa.Column("actual_tokens", sa.Integer(), nullable=False, server_default="0"),
|
|
sa.Column("estimated_cost_usd", sa.DECIMAL(10, 4), nullable=False, server_default="0"),
|
|
sa.Column("actual_cost_usd", sa.DECIMAL(10, 4), nullable=False, server_default="0"),
|
|
sa.Column("roi_score", sa.Float(), nullable=True),
|
|
sa.Column("roi_analysis", sa.JSON(), nullable=False),
|
|
sa.Column("optimization_suggestions", sa.JSON(), nullable=False),
|
|
sa.Column("created_at", sa.TIMESTAMP(), nullable=False, server_default=sa.func.now()),
|
|
sa.Column("updated_at", sa.TIMESTAMP(), nullable=False, server_default=sa.func.now()),
|
|
sa.ForeignKeyConstraint(["truth_node_id"], ["truth_nodes.id"]),
|
|
sa.ForeignKeyConstraint(["model_version_id"], ["model_versions.id"]),
|
|
sa.ForeignKeyConstraint(["task_id"], ["tasks.id"]),
|
|
)
|
|
op.create_index("ix_cost_trackings_tenant_id", "cost_trackings", ["tenant_id"])
|
|
op.create_index("ix_cost_trackings_truth_node_id", "cost_trackings", ["truth_node_id"])
|
|
op.create_index("ix_cost_trackings_task_id", "cost_trackings", ["task_id"])
|
|
op.create_index("ix_cost_trackings_period", "cost_trackings", ["period"])
|
|
|
|
|
|
def downgrade():
|
|
if _table_exists("cost_trackings"):
|
|
op.drop_index("ix_cost_trackings_period", table_name="cost_trackings")
|
|
op.drop_index("ix_cost_trackings_task_id", table_name="cost_trackings")
|
|
op.drop_index("ix_cost_trackings_truth_node_id", table_name="cost_trackings")
|
|
op.drop_index("ix_cost_trackings_tenant_id", table_name="cost_trackings")
|
|
op.drop_table("cost_trackings")
|
|
|
|
if _table_exists("model_capability_gaps"):
|
|
op.drop_index("ix_model_capability_gaps_status", table_name="model_capability_gaps")
|
|
op.drop_index("ix_model_capability_gaps_severity", table_name="model_capability_gaps")
|
|
op.drop_index("ix_model_capability_gaps_gap_type", table_name="model_capability_gaps")
|
|
op.drop_index("ix_model_capability_gaps_truth_node_id", table_name="model_capability_gaps")
|
|
op.drop_index("ix_model_capability_gaps_model_version_id", table_name="model_capability_gaps")
|
|
op.drop_index("ix_model_capability_gaps_tenant_id", table_name="model_capability_gaps")
|
|
op.drop_table("model_capability_gaps")
|
|
|