"""create instance real data snapshots table

Revision ID: 021_instance_real_data_snapshots
Revises: 020
Create Date: 2026-06-08
"""

from alembic import op
import sqlalchemy as sa

revision = "021_instance_real_data_snapshots"
down_revision = "020"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "instance_real_data_snapshots",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("instance_id", sa.Integer(), sa.ForeignKey("instances.id", ondelete="CASCADE"), nullable=False),
        sa.Column("upstream_id", sa.Integer(), nullable=True),
        sa.Column("kvm", sa.String(64), nullable=True),
        sa.Column("cpu_usage", sa.Float(), nullable=True),
        sa.Column("memory_usage", sa.Float(), nullable=True),
        sa.Column("memory_total", sa.String(64), nullable=True),
        sa.Column("memory_usable", sa.String(64), nullable=True),
        sa.Column("current_in_bw", sa.String(64), nullable=True),
        sa.Column("current_out_bw", sa.String(64), nullable=True),
        sa.Column("current_read_byte", sa.Float(), nullable=True),
        sa.Column("current_write_byte", sa.Float(), nullable=True),
        sa.Column("collected_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("raw_json", sa.Text(), nullable=True),
    )
    op.create_index(
        "ix_instance_real_data_snapshots_instance_id",
        "instance_real_data_snapshots",
        ["instance_id"],
    )
    op.create_index(
        "ix_instance_real_data_snapshots_upstream_id",
        "instance_real_data_snapshots",
        ["upstream_id"],
    )
    op.create_index(
        "ix_instance_real_data_snapshots_collected_at",
        "instance_real_data_snapshots",
        ["collected_at"],
    )


def downgrade() -> None:
    op.drop_index("ix_instance_real_data_snapshots_collected_at", table_name="instance_real_data_snapshots")
    op.drop_index("ix_instance_real_data_snapshots_upstream_id", table_name="instance_real_data_snapshots")
    op.drop_index("ix_instance_real_data_snapshots_instance_id", table_name="instance_real_data_snapshots")
    op.drop_table("instance_real_data_snapshots")
