370 lines
9.5 KiB
Python
370 lines
9.5 KiB
Python
"""账号同步状态、运行记录和官网结果投影。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
import time
|
|
from dataclasses import dataclass
|
|
from datetime import UTC, datetime, timedelta
|
|
from typing import Any
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ..models import Account, AccountSyncRun, AccountSyncState
|
|
from .account_capability_service import (
|
|
capability_ttl_seconds,
|
|
sync_account_goods_states,
|
|
)
|
|
from .douyu_read_gateway import StorehomeSnapshot
|
|
from .high_value_service import (
|
|
DEFAULT_HIGH_VALUE_GOODS,
|
|
apply_high_value_goods,
|
|
extract_high_value_goods,
|
|
)
|
|
|
|
DOMAIN_STOREHOME = "storehome"
|
|
DOMAIN_EXCHANGE_RECORDS = "exchange_records"
|
|
DOMAIN_SESSION = "session"
|
|
DOMAIN_POINTS = "points"
|
|
DOMAIN_GOLD = "gold"
|
|
DOMAIN_ELITE = "elite"
|
|
DOMAIN_ESPORTS = "esports"
|
|
DOMAIN_ROLE = "role"
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class StorehomeSyncResult:
|
|
item_count: int
|
|
changed_count: int
|
|
observed_at: datetime
|
|
|
|
|
|
def _payload_hash(raw_payload: dict[str, Any]) -> str:
|
|
payload = json.dumps(
|
|
raw_payload,
|
|
ensure_ascii=False,
|
|
sort_keys=True,
|
|
separators=(",", ":"),
|
|
default=str,
|
|
)
|
|
return hashlib.sha256(payload.encode("utf-8")).hexdigest()
|
|
|
|
|
|
def _json_safe(value: Any) -> Any:
|
|
if isinstance(value, datetime):
|
|
return value.isoformat()
|
|
if isinstance(value, dict):
|
|
return {str(key): _json_safe(item) for key, item in value.items()}
|
|
if isinstance(value, (list, tuple, set, frozenset)):
|
|
return [_json_safe(item) for item in value]
|
|
if isinstance(value, (str, int, float, bool)) or value is None:
|
|
return value
|
|
return str(value)
|
|
|
|
|
|
def get_sync_state(
|
|
db: Session,
|
|
*,
|
|
account_id: int,
|
|
domain: str,
|
|
platform: str = "douyu",
|
|
) -> AccountSyncState | None:
|
|
return (
|
|
db.query(AccountSyncState)
|
|
.filter(
|
|
AccountSyncState.account_id == account_id,
|
|
AccountSyncState.platform == platform,
|
|
AccountSyncState.domain == domain,
|
|
)
|
|
.first()
|
|
)
|
|
|
|
|
|
def is_domain_fresh(
|
|
db: Session,
|
|
*,
|
|
account_id: int,
|
|
domain: str,
|
|
now: datetime | None = None,
|
|
) -> bool:
|
|
moment = now or datetime.now(UTC)
|
|
state = get_sync_state(db, account_id=account_id, domain=domain)
|
|
return bool(
|
|
state
|
|
and state.status == "success"
|
|
and state.valid_until
|
|
and state.valid_until > moment
|
|
)
|
|
|
|
|
|
def _get_or_create_state(
|
|
db: Session,
|
|
*,
|
|
account_id: int,
|
|
domain: str,
|
|
platform: str = "douyu",
|
|
) -> AccountSyncState:
|
|
state = get_sync_state(
|
|
db,
|
|
account_id=account_id,
|
|
domain=domain,
|
|
platform=platform,
|
|
)
|
|
if state is None:
|
|
state = AccountSyncState(
|
|
account_id=account_id,
|
|
platform=platform,
|
|
domain=domain,
|
|
)
|
|
db.add(state)
|
|
db.flush()
|
|
return state
|
|
|
|
|
|
def mark_domain_dirty(
|
|
db: Session,
|
|
*,
|
|
account_id: int,
|
|
domain: str,
|
|
reason: str = "",
|
|
) -> AccountSyncState:
|
|
state = _get_or_create_state(db, account_id=account_id, domain=domain)
|
|
state.status = "dirty"
|
|
state.valid_until = None
|
|
state.next_retry_at = datetime.now(UTC)
|
|
state.error_code = "dirty"
|
|
state.error_message = reason[:2000]
|
|
state.state_version = int(state.state_version or 0) + 1
|
|
db.flush()
|
|
return state
|
|
|
|
|
|
def start_sync_run(
|
|
db: Session,
|
|
*,
|
|
account_id: int,
|
|
domain: str,
|
|
source: str,
|
|
task_id: int | None = None,
|
|
) -> AccountSyncRun:
|
|
moment = datetime.now(UTC)
|
|
state = _get_or_create_state(db, account_id=account_id, domain=domain)
|
|
state.last_attempt_at = moment
|
|
state.status = "running"
|
|
state.error_code = ""
|
|
state.error_message = ""
|
|
state.source = source[:32]
|
|
run = AccountSyncRun(
|
|
account_id=account_id,
|
|
platform="douyu",
|
|
domain=domain,
|
|
task_id=task_id,
|
|
started_at=moment,
|
|
status="running",
|
|
source=source[:32],
|
|
)
|
|
db.add(run)
|
|
db.flush()
|
|
return run
|
|
|
|
|
|
def finish_sync_success(
|
|
db: Session,
|
|
run: AccountSyncRun,
|
|
*,
|
|
item_count: int,
|
|
changed_count: int,
|
|
observed_at: datetime,
|
|
valid_for_seconds: int | None = None,
|
|
raw_payload: dict[str, Any] | None = None,
|
|
) -> None:
|
|
moment = datetime.now(UTC)
|
|
duration_ms = max(0, int((moment - run.started_at).total_seconds() * 1000))
|
|
run.status = "success"
|
|
run.finished_at = moment
|
|
run.item_count = int(item_count)
|
|
run.changed_count = int(changed_count)
|
|
run.duration_ms = duration_ms
|
|
run.official_observed_at = observed_at
|
|
state = _get_or_create_state(
|
|
db,
|
|
account_id=int(run.account_id),
|
|
domain=run.domain,
|
|
platform=run.platform,
|
|
)
|
|
state.status = "success"
|
|
state.last_success_at = moment
|
|
state.valid_until = moment + timedelta(
|
|
seconds=max(
|
|
1,
|
|
int(
|
|
valid_for_seconds
|
|
if valid_for_seconds is not None
|
|
else capability_ttl_seconds()
|
|
),
|
|
)
|
|
)
|
|
state.next_retry_at = state.valid_until
|
|
state.error_code = ""
|
|
state.error_message = ""
|
|
if raw_payload is not None:
|
|
safe_payload = _json_safe(raw_payload)
|
|
state.raw_payload_json = safe_payload
|
|
state.payload_hash = _payload_hash(safe_payload)
|
|
state.state_version = int(state.state_version or 0) + 1
|
|
db.flush()
|
|
|
|
|
|
def finish_sync_failure(
|
|
db: Session,
|
|
run: AccountSyncRun,
|
|
*,
|
|
error: BaseException,
|
|
retry_after_seconds: int = 300,
|
|
error_code: str = "sync_failed",
|
|
raw_payload: dict[str, Any] | None = None,
|
|
) -> None:
|
|
moment = datetime.now(UTC)
|
|
duration_ms = max(0, int((moment - run.started_at).total_seconds() * 1000))
|
|
message = f"{type(error).__name__}: {error}"[:2000]
|
|
run.status = "failed"
|
|
run.finished_at = moment
|
|
run.duration_ms = duration_ms
|
|
run.error_code = error_code[:64]
|
|
run.error_message = message
|
|
state = _get_or_create_state(
|
|
db,
|
|
account_id=int(run.account_id),
|
|
domain=run.domain,
|
|
platform=run.platform,
|
|
)
|
|
state.status = "failed"
|
|
state.next_retry_at = moment + timedelta(seconds=max(1, retry_after_seconds))
|
|
state.error_code = error_code[:64]
|
|
state.error_message = message
|
|
if raw_payload is not None:
|
|
safe_payload = _json_safe(raw_payload)
|
|
state.raw_payload_json = safe_payload
|
|
state.payload_hash = _payload_hash(safe_payload)
|
|
state.state_version = int(state.state_version or 0) + 1
|
|
db.flush()
|
|
|
|
|
|
def sync_storehome_snapshot(
|
|
db: Session,
|
|
account: Account,
|
|
snapshot: StorehomeSnapshot,
|
|
*,
|
|
source: str,
|
|
task_id: int | None = None,
|
|
) -> StorehomeSyncResult:
|
|
"""把一次完整 storehome 快照投影成账号全部商品状态。"""
|
|
start = time.monotonic()
|
|
run = start_sync_run(
|
|
db,
|
|
account_id=int(account.id),
|
|
domain=DOMAIN_STOREHOME,
|
|
source=source,
|
|
task_id=task_id,
|
|
)
|
|
try:
|
|
items = list(snapshot.items)
|
|
changed = sync_account_goods_states(
|
|
db,
|
|
account,
|
|
items,
|
|
scope="elite",
|
|
observed_at=snapshot.observed_at,
|
|
source="storehome_sync",
|
|
)
|
|
high_value = extract_high_value_goods(
|
|
items,
|
|
DEFAULT_HIGH_VALUE_GOODS,
|
|
[],
|
|
)
|
|
apply_high_value_goods(account, high_value, checked_at=snapshot.observed_at)
|
|
account.updated_at = snapshot.observed_at
|
|
finish_sync_success(
|
|
db,
|
|
run,
|
|
item_count=len(items),
|
|
changed_count=changed,
|
|
observed_at=snapshot.observed_at,
|
|
raw_payload=snapshot.raw,
|
|
)
|
|
run.duration_ms = max(
|
|
int(run.duration_ms or 0),
|
|
int((time.monotonic() - start) * 1000),
|
|
)
|
|
return StorehomeSyncResult(
|
|
item_count=len(items),
|
|
changed_count=changed,
|
|
observed_at=snapshot.observed_at,
|
|
)
|
|
except Exception as exc:
|
|
finish_sync_failure(db, run, error=exc, raw_payload=snapshot.raw)
|
|
raise
|
|
|
|
|
|
def record_sync_success(
|
|
db: Session,
|
|
*,
|
|
account_id: int,
|
|
domain: str,
|
|
source: str,
|
|
observed_at: datetime | None = None,
|
|
item_count: int = 1,
|
|
changed_count: int = 1,
|
|
raw_payload: dict[str, Any] | None = None,
|
|
task_id: int | None = None,
|
|
valid_for_seconds: int | None = None,
|
|
) -> StorehomeSyncResult:
|
|
"""登记一次已经完成的官方查询投影。
|
|
|
|
适用于探针已经拿到并完成归一化的结果,避免调用方为了同步记录再重复请求。
|
|
"""
|
|
moment = observed_at or datetime.now(UTC)
|
|
run = start_sync_run(
|
|
db,
|
|
account_id=account_id,
|
|
domain=domain,
|
|
source=source,
|
|
task_id=task_id,
|
|
)
|
|
finish_sync_success(
|
|
db,
|
|
run,
|
|
item_count=item_count,
|
|
changed_count=changed_count,
|
|
observed_at=moment,
|
|
raw_payload=raw_payload,
|
|
valid_for_seconds=valid_for_seconds,
|
|
)
|
|
return StorehomeSyncResult(
|
|
item_count=int(item_count),
|
|
changed_count=int(changed_count),
|
|
observed_at=moment,
|
|
)
|
|
|
|
|
|
__all__ = [
|
|
"DOMAIN_ELITE",
|
|
"DOMAIN_ESPORTS",
|
|
"DOMAIN_EXCHANGE_RECORDS",
|
|
"DOMAIN_GOLD",
|
|
"DOMAIN_POINTS",
|
|
"DOMAIN_ROLE",
|
|
"DOMAIN_SESSION",
|
|
"DOMAIN_STOREHOME",
|
|
"StorehomeSyncResult",
|
|
"finish_sync_failure",
|
|
"finish_sync_success",
|
|
"get_sync_state",
|
|
"is_domain_fresh",
|
|
"mark_domain_dirty",
|
|
"record_sync_success",
|
|
"start_sync_run",
|
|
"sync_storehome_snapshot",
|
|
]
|