feat: 手术视频消耗、待确认与持久化改造
- 新增 Alembic 初始迁移、领域明细模型及归档持久化与重试链路\n- 拆分视频会话注册表、分类处理、推理时间窗聚合与流处理\n- 消耗日志:TSV/Markdown 含 top2/top3;item_id 优先产品编码;待确认记「待确认」行,语音确认后落正式行并更新汇总\n- 待确认时内存/DB 明细为占位行,确认后替换;拒绝时移除占位\n- 分类 probs 先 detach/cpu 再转 NumPy,修复 MPS/CUDA 上推理被静默跳过\n- 补充集成测试、归档与设备张量等单测 Made-with: Cursor
This commit is contained in:
66
alembic/env.py
Normal file
66
alembic/env.py
Normal file
@@ -0,0 +1,66 @@
|
||||
"""Alembic environment.
|
||||
|
||||
生产请用 `alembic upgrade head`;开发/测试可让 ``Settings.auto_create_schema`` 调用
|
||||
``init_db_schema()``。本文件读取 `app.config.settings`,把 asyncpg URL 转为同步
|
||||
`psycopg` URL 供 Alembic 使用(仅迁移期间)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from logging.config import fileConfig
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import engine_from_config, pool
|
||||
|
||||
from app.config import settings
|
||||
import app.db.models # noqa: F401 - register ORM tables on Base.metadata
|
||||
from app.db.base import Base
|
||||
|
||||
|
||||
alembic_config = context.config
|
||||
|
||||
if alembic_config.config_file_name is not None:
|
||||
fileConfig(alembic_config.config_file_name)
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def _sync_database_url() -> str:
|
||||
"""把 asyncpg URL 转为同步 psycopg2 URL,避免 Alembic 强依赖 async 驱动。"""
|
||||
url = settings.sqlalchemy_database_url
|
||||
return url.replace("postgresql+asyncpg://", "postgresql+psycopg://", 1)
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
context.configure(
|
||||
url=_sync_database_url(),
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
config_section = alembic_config.get_section(alembic_config.config_ini_section) or {}
|
||||
config_section["sqlalchemy.url"] = _sync_database_url()
|
||||
connectable = engine_from_config(
|
||||
config_section,
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
with connectable.connect() as connection:
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=target_metadata,
|
||||
compare_type=True,
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
27
alembic/script.py.mako
Normal file
27
alembic/script.py.mako
Normal file
@@ -0,0 +1,27 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: Union[str, None] = ${repr(down_revision)}
|
||||
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
119
alembic/versions/0001_initial.py
Normal file
119
alembic/versions/0001_initial.py
Normal file
@@ -0,0 +1,119 @@
|
||||
"""initial schema: surgery_final_results / surgery_result_details / voice_confirmation_audits
|
||||
|
||||
Revision ID: 0001_initial
|
||||
Revises:
|
||||
Create Date: 2026-04-23
|
||||
|
||||
对应 `app.db.models` 中的三张表,与 `init_db_schema()` 的 ``Base.metadata.create_all``
|
||||
等价,作为生产环境 `alembic upgrade head` 的初始版本。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision: str = "0001_initial"
|
||||
down_revision: Union[str, None] = None
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"surgery_final_results",
|
||||
sa.Column("surgery_id", sa.String(length=6), primary_key=True),
|
||||
sa.Column(
|
||||
"completed_at",
|
||||
sa.DateTime(timezone=True),
|
||||
server_default=sa.text("CURRENT_TIMESTAMP"),
|
||||
nullable=False,
|
||||
),
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"surgery_result_details",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column(
|
||||
"surgery_id",
|
||||
sa.String(length=6),
|
||||
sa.ForeignKey(
|
||||
"surgery_final_results.surgery_id", ondelete="CASCADE"
|
||||
),
|
||||
nullable=False,
|
||||
index=True,
|
||||
),
|
||||
sa.Column("item_id", sa.String(length=256), nullable=False),
|
||||
sa.Column("item_name", sa.String(length=256), nullable=False),
|
||||
sa.Column("quantity", sa.Integer(), nullable=False),
|
||||
sa.Column("doctor_id", sa.String(length=128), nullable=False),
|
||||
sa.Column("recorded_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column(
|
||||
"source", sa.String(length=32), nullable=False, server_default="vision"
|
||||
),
|
||||
)
|
||||
op.create_index(
|
||||
op.f("ix_surgery_result_details_surgery_id"),
|
||||
"surgery_result_details",
|
||||
["surgery_id"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"voice_confirmation_audits",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("surgery_id", sa.String(length=6), nullable=False, index=True),
|
||||
sa.Column(
|
||||
"confirmation_id", sa.String(length=128), nullable=False, index=True
|
||||
),
|
||||
sa.Column("status", sa.String(length=32), nullable=False),
|
||||
sa.Column("audio_object_key", sa.String(length=512), nullable=True),
|
||||
sa.Column("audio_content_type", sa.String(length=128), nullable=True),
|
||||
sa.Column("audio_size_bytes", sa.Integer(), nullable=True),
|
||||
sa.Column("audio_sha256", sa.String(length=64), nullable=True),
|
||||
sa.Column("asr_text", sa.String(length=2048), nullable=True),
|
||||
sa.Column("resolved_label", sa.String(length=256), nullable=True),
|
||||
sa.Column("options_snapshot_json", sa.Text(), nullable=True),
|
||||
sa.Column("error_message", sa.String(length=1024), nullable=True),
|
||||
sa.Column(
|
||||
"created_at",
|
||||
sa.DateTime(timezone=True),
|
||||
server_default=sa.text("CURRENT_TIMESTAMP"),
|
||||
nullable=False,
|
||||
),
|
||||
)
|
||||
op.create_index(
|
||||
op.f("ix_voice_confirmation_audits_surgery_id"),
|
||||
"voice_confirmation_audits",
|
||||
["surgery_id"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
op.f("ix_voice_confirmation_audits_confirmation_id"),
|
||||
"voice_confirmation_audits",
|
||||
["confirmation_id"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index(
|
||||
op.f("ix_voice_confirmation_audits_confirmation_id"),
|
||||
table_name="voice_confirmation_audits",
|
||||
)
|
||||
op.drop_index(
|
||||
op.f("ix_voice_confirmation_audits_surgery_id"),
|
||||
table_name="voice_confirmation_audits",
|
||||
)
|
||||
op.drop_table("voice_confirmation_audits")
|
||||
|
||||
op.drop_index(
|
||||
op.f("ix_surgery_result_details_surgery_id"),
|
||||
table_name="surgery_result_details",
|
||||
)
|
||||
op.drop_table("surgery_result_details")
|
||||
|
||||
op.drop_table("surgery_final_results")
|
||||
Reference in New Issue
Block a user