"""自报计数快照落库。一次事务:插 1 条快照头 + N 条 event 行,返回快照 id。""" from __future__ import annotations from sqlalchemy.orm import Session from app.models.analytics_selfstat import AnalyticsSelfStat, AnalyticsSelfStatEvent from app.schemas.analytics_selfstat import SelfStatBatchIn def record_selfstat(db: Session, batch: SelfStatBatchIn) -> int: snap = AnalyticsSelfStat( device_id=batch.device_id, epoch_id=batch.epoch_id, app_ver=batch.app_ver, oem=batch.oem, os=batch.os, batches_attempted=batch.batches_attempted, batches_ok=batch.batches_ok, batches_fail=batch.batches_fail, retries=batch.retries, queue_depth=batch.queue_depth, sent_at=batch.sent_at, ) db.add(snap) db.flush() # 拿到 snap.id db.add_all([ AnalyticsSelfStatEvent( snapshot_id=snap.id, event=e.event, attempted=e.attempted, drop_capture=e.drop_capture, delivered=e.delivered, drop_undelivered=e.drop_undelivered, ) for e in batch.events ]) db.commit() return snap.id