perf(automation): 持久队列驱动并优化履约热路径
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
"""add account supply snapshots.
|
||||
|
||||
Revision ID: 20261123_0119
|
||||
Revises: 20261123_0118
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261123_0119"
|
||||
down_revision: str | None = "20261123_0118"
|
||||
branch_labels: str | None = None
|
||||
depends_on: str | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
if sa.inspect(op.get_bind()).has_table("account_supply_snapshots"):
|
||||
return
|
||||
op.create_table(
|
||||
"account_supply_snapshots",
|
||||
sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
|
||||
sa.Column(
|
||||
"target_id",
|
||||
sa.Integer(),
|
||||
sa.ForeignKey("account_supply_targets.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("covered", sa.Integer(), nullable=False, server_default="0"),
|
||||
sa.Column("available", sa.Integer(), nullable=False, server_default="0"),
|
||||
sa.Column("allocatable", sa.Integer(), nullable=False, server_default="0"),
|
||||
sa.Column("checked_at", sa.DateTime(), nullable=False),
|
||||
sa.Column(
|
||||
"updated_at",
|
||||
sa.DateTime(),
|
||||
nullable=False,
|
||||
server_default=sa.text("CURRENT_TIMESTAMP"),
|
||||
),
|
||||
sa.UniqueConstraint("target_id", name="uq_account_supply_snapshot_target"),
|
||||
)
|
||||
op.create_index(
|
||||
"ix_account_supply_snapshot_checked",
|
||||
"account_supply_snapshots",
|
||||
["checked_at"],
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
if sa.inspect(op.get_bind()).has_table("account_supply_snapshots"):
|
||||
op.drop_table("account_supply_snapshots")
|
||||
@@ -0,0 +1,85 @@
|
||||
"""add account value tier projection.
|
||||
|
||||
Revision ID: 20261123_0120
|
||||
Revises: 20261123_0119
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261123_0120"
|
||||
down_revision: str | None = "20261123_0119"
|
||||
branch_labels: str | None = None
|
||||
depends_on: str | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
columns = {
|
||||
str(column["name"])
|
||||
for column in sa.inspect(op.get_bind()).get_columns("accounts")
|
||||
}
|
||||
if "value_tier" not in columns:
|
||||
op.add_column(
|
||||
"accounts",
|
||||
sa.Column("value_tier", sa.Integer(), nullable=True),
|
||||
)
|
||||
indexes = {
|
||||
str(index["name"])
|
||||
for index in sa.inspect(op.get_bind()).get_indexes("accounts")
|
||||
if index.get("name")
|
||||
}
|
||||
if "ix_accounts_value_tier" not in indexes:
|
||||
op.create_index(
|
||||
"ix_accounts_value_tier",
|
||||
"accounts",
|
||||
["value_tier", "id"],
|
||||
)
|
||||
cutoff = datetime.now(UTC) - timedelta(days=7)
|
||||
op.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE accounts
|
||||
SET value_tier = CASE
|
||||
WHEN high_value_checked_at >= :cutoff
|
||||
AND high_value_bairimeng_status = 'available'
|
||||
AND high_value_mengtu_status = 'available'
|
||||
THEN 1
|
||||
WHEN (
|
||||
high_value_checked_at >= :cutoff
|
||||
AND (
|
||||
high_value_bairimeng_status = 'available'
|
||||
OR high_value_mengtu_status = 'available'
|
||||
)
|
||||
)
|
||||
OR COALESCE(points, 0) >= 300
|
||||
THEN 2
|
||||
WHEN COALESCE(gold_balance, 0) > 0
|
||||
OR elite_opened = 1
|
||||
THEN 3
|
||||
WHEN overview_queried_at IS NOT NULL
|
||||
THEN 4
|
||||
ELSE 5
|
||||
END
|
||||
"""
|
||||
).bindparams(cutoff=cutoff)
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
indexes = {
|
||||
str(index["name"])
|
||||
for index in sa.inspect(op.get_bind()).get_indexes("accounts")
|
||||
if index.get("name")
|
||||
}
|
||||
if "ix_accounts_value_tier" in indexes:
|
||||
op.drop_index("ix_accounts_value_tier", table_name="accounts")
|
||||
columns = {
|
||||
str(column["name"])
|
||||
for column in sa.inspect(op.get_bind()).get_columns("accounts")
|
||||
}
|
||||
if "value_tier" in columns:
|
||||
op.drop_column("accounts", "value_tier")
|
||||
@@ -0,0 +1,41 @@
|
||||
"""add douyu task retention scan index.
|
||||
|
||||
Revision ID: 20261123_0121
|
||||
Revises: 20261123_0120
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261123_0121"
|
||||
down_revision: str | None = "20261123_0120"
|
||||
branch_labels: str | None = None
|
||||
depends_on: str | None = None
|
||||
|
||||
INDEX_NAME = "ix_douyu_tasks_retention_scan"
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
indexes = {
|
||||
str(index["name"])
|
||||
for index in sa.inspect(op.get_bind()).get_indexes("douyu_tasks")
|
||||
if index.get("name")
|
||||
}
|
||||
if INDEX_NAME not in indexes:
|
||||
op.create_index(
|
||||
INDEX_NAME,
|
||||
"douyu_tasks",
|
||||
["task_type", "status", "created_at", "id"],
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
indexes = {
|
||||
str(index["name"])
|
||||
for index in sa.inspect(op.get_bind()).get_indexes("douyu_tasks")
|
||||
if index.get("name")
|
||||
}
|
||||
if INDEX_NAME in indexes:
|
||||
op.drop_index(INDEX_NAME, table_name="douyu_tasks")
|
||||
Reference in New Issue
Block a user