feat(douyu): 完善账号同步域与供给目标配置
This commit is contained in:
@@ -11,14 +11,14 @@
|
||||
|
||||
更新时间:2026-11-21
|
||||
|
||||
整体完成度约 **55%**。
|
||||
整体完成度约 **60%**。
|
||||
|
||||
| 阶段 | 完成度 | 已落地 | 主要缺口 |
|
||||
| --- | ---: | --- | --- |
|
||||
| 阶段 0:止乱 | 100% | 登录批次上限、任务槽隔离、连接及时释放 | 无 |
|
||||
| 阶段 1:同步基础 | 85% | 新表、迁移、storehome 网关、全量快照、同步运行记录、内部工作队列执行器 | points/gold/role/exchange 域尚未全部统一 |
|
||||
| 阶段 1:同步基础 | 95% | 新表、迁移、storehome 网关、全量快照、同步运行记录、内部工作队列执行器、session/points/gold/elite/esports/role/exchange 同步状态 | 少量旁路查询仍需继续收口 |
|
||||
| 阶段 2:账号与 CK | 80% | CK 周期字段、invalid 分流、登录流水、日/小时/分类/单账号预算 | 配置管理 API、更多自动重登灰度指标 |
|
||||
| 阶段 3:商品与供给 | 45% | 全量商品能力、限时窗口、原始响应、供给库存和目标缺口、定向补货入队 | 默认供给目标、供给看板、覆盖率告警 |
|
||||
| 阶段 3:商品与供给 | 55% | 全量商品能力、限时窗口、原始响应、供给库存和目标缺口、定向补货入队、供给目标配置 API | 默认供给目标、供给看板、覆盖率告警 |
|
||||
| 阶段 4:安全恢复自动化 | 20% | account_sync_queue 与供给补货作业默认小批量开启 | 自动重登灰度参数、成功率熔断和动态预算 |
|
||||
| 阶段 5:清理旧逻辑 | 20% | 履约补货不再直接发官网请求 | 旧资料刷新、旁路查询和旧候选兜底仍需逐步下线 |
|
||||
|
||||
|
||||
@@ -12,10 +12,13 @@ from web.backend.models import (
|
||||
AccountWorkItem,
|
||||
User,
|
||||
)
|
||||
from web.backend.routers.douyu import create_supply_target, list_supply_targets
|
||||
from web.backend.schemas import DouyuSupplyTargetUpsert
|
||||
from web.backend.services.account_supply_service import (
|
||||
VALUE_HIGH,
|
||||
enqueue_supply_replenishment,
|
||||
evaluate_supply_target,
|
||||
upsert_supply_target,
|
||||
)
|
||||
|
||||
NOW = datetime.now(UTC).replace(microsecond=0)
|
||||
@@ -122,3 +125,69 @@ def test_supply_shortage_enqueues_missing_coverage(db):
|
||||
assert work.account_id == account.id
|
||||
assert work.work_type == "storehome_sync"
|
||||
assert work.priority == 35
|
||||
|
||||
|
||||
def test_supply_target_configuration_is_idempotent(db):
|
||||
first = upsert_supply_target(
|
||||
db,
|
||||
scope="elite",
|
||||
commodity_id="C",
|
||||
value_class=VALUE_HIGH,
|
||||
target_covered=100,
|
||||
target_available=80,
|
||||
target_allocatable=50,
|
||||
safety_buffer=10,
|
||||
enabled=True,
|
||||
updated_by=None,
|
||||
)
|
||||
second = upsert_supply_target(
|
||||
db,
|
||||
scope="elite",
|
||||
commodity_id="C",
|
||||
value_class=VALUE_HIGH,
|
||||
target_covered=120,
|
||||
target_available=90,
|
||||
target_allocatable=60,
|
||||
safety_buffer=15,
|
||||
enabled=False,
|
||||
updated_by=None,
|
||||
)
|
||||
|
||||
assert second.id == first.id
|
||||
assert db.query(AccountSupplyTarget).count() == 1
|
||||
assert second.target_covered == 120
|
||||
assert second.target_available == 90
|
||||
assert second.target_allocatable == 60
|
||||
assert second.safety_buffer == 15
|
||||
assert second.enabled is False
|
||||
|
||||
|
||||
def test_supply_target_configuration_route_returns_inventory(db):
|
||||
user = User(
|
||||
username="supply-config-admin",
|
||||
password_hash="h",
|
||||
role="super_admin",
|
||||
is_active=True,
|
||||
)
|
||||
db.add(user)
|
||||
db.commit()
|
||||
|
||||
created = create_supply_target(
|
||||
DouyuSupplyTargetUpsert(
|
||||
scope="elite",
|
||||
commodity_id="C",
|
||||
value_class=VALUE_HIGH,
|
||||
target_covered=10,
|
||||
target_available=8,
|
||||
target_allocatable=5,
|
||||
safety_buffer=2,
|
||||
),
|
||||
db=db,
|
||||
current=user,
|
||||
)
|
||||
listed = list_supply_targets(db=db, current=user)
|
||||
|
||||
assert created.id > 0
|
||||
assert created.covered_shortage == 12
|
||||
assert listed[0].id == created.id
|
||||
assert listed[0].needs_replenishment is True
|
||||
|
||||
@@ -6,7 +6,7 @@ from datetime import UTC, datetime
|
||||
from typing import cast
|
||||
|
||||
from core.douyu import DouyuActivityClient
|
||||
from web.backend.models import Account
|
||||
from web.backend.models import Account, AccountSyncState
|
||||
from web.backend.services.douyu_account_state import refresh_douyu_account_state
|
||||
|
||||
|
||||
@@ -94,6 +94,13 @@ def test_refresh_writes_ck_elite_points_limited_and_bind_lifecycle(db):
|
||||
assert (
|
||||
account.overview_snapshot["limited_goods"][0]["commodityId"] == "limited-goods"
|
||||
)
|
||||
sync_domains = {
|
||||
row.domain
|
||||
for row in db.query(AccountSyncState)
|
||||
.filter(AccountSyncState.account_id == account.id)
|
||||
.all()
|
||||
}
|
||||
assert {"session", "points", "elite", "role"} <= sync_domains
|
||||
|
||||
|
||||
def test_invalid_ck_writes_lifecycle_without_querying_activity(db):
|
||||
|
||||
@@ -34,6 +34,7 @@ from ..database import WorkerSessionLocal, get_db
|
||||
from ..deps import authenticate_websocket, get_current_user, require_permission
|
||||
from ..models import (
|
||||
Account,
|
||||
AccountSupplyTarget,
|
||||
DouyuConfig,
|
||||
DouyuEsportsGoodsSnapshot,
|
||||
DouyuGoodsSnapshot,
|
||||
@@ -51,6 +52,9 @@ from ..schemas import (
|
||||
DouyuConfigUpdate,
|
||||
DouyuExchangeResultImageOut,
|
||||
DouyuGoodsOut,
|
||||
DouyuSupplyTargetOut,
|
||||
DouyuSupplyTargetUpdate,
|
||||
DouyuSupplyTargetUpsert,
|
||||
DouyuTaskAccountOut,
|
||||
DouyuTaskBatchRequest,
|
||||
DouyuTaskOut,
|
||||
@@ -58,6 +62,11 @@ from ..schemas import (
|
||||
DouyuXpdGoodsOut,
|
||||
)
|
||||
from ..services.account_binding import bind_payload
|
||||
from ..services.account_supply_service import (
|
||||
evaluate_supply_target,
|
||||
update_supply_target,
|
||||
upsert_supply_target,
|
||||
)
|
||||
from ..services.audit_service import record_audit
|
||||
from ..services.business_events import record_recharge
|
||||
from ..services.douyu_activity_query import activity_client, query_activity_goods
|
||||
@@ -681,6 +690,34 @@ def _config_out(config: DouyuConfig) -> DouyuConfigOut:
|
||||
)
|
||||
|
||||
|
||||
def _supply_target_out(
|
||||
db: Session,
|
||||
target: AccountSupplyTarget,
|
||||
) -> DouyuSupplyTargetOut:
|
||||
inventory = evaluate_supply_target(db, target)
|
||||
return DouyuSupplyTargetOut(
|
||||
id=int(target.id),
|
||||
platform=target.platform,
|
||||
scope=target.scope,
|
||||
commodity_id=target.commodity_id,
|
||||
value_class=target.value_class,
|
||||
target_covered=inventory.target_covered,
|
||||
target_available=inventory.target_available,
|
||||
target_allocatable=inventory.target_allocatable,
|
||||
safety_buffer=inventory.safety_buffer,
|
||||
enabled=bool(target.enabled),
|
||||
covered=inventory.covered,
|
||||
available=inventory.available,
|
||||
allocatable=inventory.allocatable,
|
||||
covered_shortage=inventory.covered_shortage,
|
||||
available_shortage=inventory.available_shortage,
|
||||
allocatable_shortage=inventory.allocatable_shortage,
|
||||
needs_replenishment=inventory.needs_replenishment,
|
||||
updated_by=target.updated_by,
|
||||
updated_at=target.updated_at,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/task-types")
|
||||
def task_types(current: User = Depends(require_permission("douyu:task"))):
|
||||
"""返回斗鱼任务类型。"""
|
||||
@@ -937,6 +974,104 @@ def update_config(
|
||||
return _config_out(config)
|
||||
|
||||
|
||||
@router.get("/supply-targets", response_model=list[DouyuSupplyTargetOut])
|
||||
def list_supply_targets(
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("douyu:config")),
|
||||
):
|
||||
"""返回账号供给目标、当前库存和缺口。"""
|
||||
rows = (
|
||||
db.query(AccountSupplyTarget)
|
||||
.order_by(
|
||||
AccountSupplyTarget.enabled.desc(),
|
||||
AccountSupplyTarget.scope.asc(),
|
||||
AccountSupplyTarget.value_class.asc(),
|
||||
AccountSupplyTarget.commodity_id.asc(),
|
||||
AccountSupplyTarget.id.asc(),
|
||||
)
|
||||
.all()
|
||||
)
|
||||
return [_supply_target_out(db, row) for row in rows]
|
||||
|
||||
|
||||
@router.post("/supply-targets", response_model=DouyuSupplyTargetOut)
|
||||
def create_supply_target(
|
||||
req: DouyuSupplyTargetUpsert,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("douyu:config")),
|
||||
):
|
||||
"""新增或覆盖一条账号供给目标。"""
|
||||
target = upsert_supply_target(
|
||||
db,
|
||||
platform=req.platform,
|
||||
scope=req.scope,
|
||||
commodity_id=req.commodity_id.strip(),
|
||||
value_class=req.value_class,
|
||||
target_covered=req.target_covered,
|
||||
target_available=req.target_available,
|
||||
target_allocatable=req.target_allocatable,
|
||||
safety_buffer=req.safety_buffer,
|
||||
enabled=req.enabled,
|
||||
updated_by=current.id,
|
||||
)
|
||||
record_audit(
|
||||
db,
|
||||
current,
|
||||
action="douyu:supply-target:upsert",
|
||||
target=f"account_supply_target:{target.id}",
|
||||
detail={
|
||||
"scope": target.scope,
|
||||
"commodity_id": target.commodity_id,
|
||||
"value_class": target.value_class,
|
||||
"target_covered": target.target_covered,
|
||||
"target_available": target.target_available,
|
||||
"target_allocatable": target.target_allocatable,
|
||||
"safety_buffer": target.safety_buffer,
|
||||
"enabled": target.enabled,
|
||||
},
|
||||
)
|
||||
db.commit()
|
||||
return _supply_target_out(db, target)
|
||||
|
||||
|
||||
@router.put("/supply-targets/{target_id}", response_model=DouyuSupplyTargetOut)
|
||||
def edit_supply_target(
|
||||
target_id: int,
|
||||
req: DouyuSupplyTargetUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("douyu:config")),
|
||||
):
|
||||
"""修改供给目标数量或启停状态。"""
|
||||
target = db.get(AccountSupplyTarget, target_id)
|
||||
if target is None:
|
||||
raise HTTPException(status_code=404, detail="供给目标不存在")
|
||||
target = update_supply_target(
|
||||
db,
|
||||
target,
|
||||
target_covered=req.target_covered,
|
||||
target_available=req.target_available,
|
||||
target_allocatable=req.target_allocatable,
|
||||
safety_buffer=req.safety_buffer,
|
||||
enabled=req.enabled,
|
||||
updated_by=current.id,
|
||||
)
|
||||
record_audit(
|
||||
db,
|
||||
current,
|
||||
action="douyu:supply-target:update",
|
||||
target=f"account_supply_target:{target.id}",
|
||||
detail={
|
||||
"target_covered": target.target_covered,
|
||||
"target_available": target.target_available,
|
||||
"target_allocatable": target.target_allocatable,
|
||||
"safety_buffer": target.safety_buffer,
|
||||
"enabled": target.enabled,
|
||||
},
|
||||
)
|
||||
db.commit()
|
||||
return _supply_target_out(db, target)
|
||||
|
||||
|
||||
@router.get("/goods", response_model=list[DouyuGoodsOut])
|
||||
def list_goods(
|
||||
db: Session = Depends(get_db),
|
||||
|
||||
@@ -1581,6 +1581,48 @@ class DouyuConfigUpdate(BaseModel):
|
||||
skin_id: str | None = None
|
||||
|
||||
|
||||
class DouyuSupplyTargetUpsert(BaseModel):
|
||||
platform: str = Field("douyu", pattern="^douyu$")
|
||||
scope: str = Field("elite", pattern="^[a-z_]{1,16}$")
|
||||
commodity_id: str = Field("", max_length=64)
|
||||
value_class: str = Field(..., pattern="^(high_value|ordinary)$")
|
||||
target_covered: int = Field(0, ge=0, le=1_000_000)
|
||||
target_available: int = Field(0, ge=0, le=1_000_000)
|
||||
target_allocatable: int = Field(0, ge=0, le=1_000_000)
|
||||
safety_buffer: int = Field(0, ge=0, le=100_000)
|
||||
enabled: bool = True
|
||||
|
||||
|
||||
class DouyuSupplyTargetUpdate(BaseModel):
|
||||
target_covered: int = Field(0, ge=0, le=1_000_000)
|
||||
target_available: int = Field(0, ge=0, le=1_000_000)
|
||||
target_allocatable: int = Field(0, ge=0, le=1_000_000)
|
||||
safety_buffer: int = Field(0, ge=0, le=100_000)
|
||||
enabled: bool = True
|
||||
|
||||
|
||||
class DouyuSupplyTargetOut(BaseModel):
|
||||
id: int
|
||||
platform: str
|
||||
scope: str
|
||||
commodity_id: str
|
||||
value_class: str
|
||||
target_covered: int
|
||||
target_available: int
|
||||
target_allocatable: int
|
||||
safety_buffer: int
|
||||
enabled: bool
|
||||
covered: int
|
||||
available: int
|
||||
allocatable: int
|
||||
covered_shortage: int
|
||||
available_shortage: int
|
||||
allocatable_shortage: int
|
||||
needs_replenishment: bool
|
||||
updated_by: int | None = None
|
||||
updated_at: datetime | None = None
|
||||
|
||||
|
||||
class PlatformFulfillmentMappingUpsert(BaseModel):
|
||||
internal_sku_key: str = Field(..., min_length=1, max_length=255)
|
||||
campaign_key: str = Field(default="douyu-current", min_length=1, max_length=128)
|
||||
|
||||
@@ -6,6 +6,7 @@ from dataclasses import dataclass
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
from sqlalchemy import and_, or_, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models import (
|
||||
@@ -286,6 +287,101 @@ def evaluate_supply_targets(
|
||||
]
|
||||
|
||||
|
||||
def upsert_supply_target(
|
||||
db: Session,
|
||||
*,
|
||||
platform: str = "douyu",
|
||||
scope: str,
|
||||
commodity_id: str,
|
||||
value_class: str,
|
||||
target_covered: int,
|
||||
target_available: int,
|
||||
target_allocatable: int,
|
||||
safety_buffer: int,
|
||||
enabled: bool,
|
||||
updated_by: int | None,
|
||||
) -> AccountSupplyTarget:
|
||||
"""新增或更新一条供给目标。"""
|
||||
if value_class not in VALID_VALUES:
|
||||
raise ValueError(f"不支持的账号价值分类: {value_class}")
|
||||
for name, value in (
|
||||
("target_covered", target_covered),
|
||||
("target_available", target_available),
|
||||
("target_allocatable", target_allocatable),
|
||||
("safety_buffer", safety_buffer),
|
||||
):
|
||||
if int(value) < 0:
|
||||
raise ValueError(f"{name} 不能为负数")
|
||||
|
||||
target = (
|
||||
db.query(AccountSupplyTarget)
|
||||
.filter(
|
||||
AccountSupplyTarget.platform == platform,
|
||||
AccountSupplyTarget.scope == scope,
|
||||
AccountSupplyTarget.commodity_id == commodity_id,
|
||||
AccountSupplyTarget.value_class == value_class,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if target is None:
|
||||
target = AccountSupplyTarget(
|
||||
platform=platform,
|
||||
scope=scope,
|
||||
commodity_id=commodity_id,
|
||||
value_class=value_class,
|
||||
)
|
||||
db.add(target)
|
||||
target.target_covered = int(target_covered)
|
||||
target.target_available = int(target_available)
|
||||
target.target_allocatable = int(target_allocatable)
|
||||
target.safety_buffer = int(safety_buffer)
|
||||
target.enabled = bool(enabled)
|
||||
target.updated_by = updated_by
|
||||
target.updated_at = datetime.now(UTC)
|
||||
try:
|
||||
with db.begin_nested():
|
||||
db.flush()
|
||||
except IntegrityError:
|
||||
target = (
|
||||
db.query(AccountSupplyTarget)
|
||||
.filter(
|
||||
AccountSupplyTarget.platform == platform,
|
||||
AccountSupplyTarget.scope == scope,
|
||||
AccountSupplyTarget.commodity_id == commodity_id,
|
||||
AccountSupplyTarget.value_class == value_class,
|
||||
)
|
||||
.one()
|
||||
)
|
||||
db.commit()
|
||||
return target
|
||||
|
||||
|
||||
def update_supply_target(
|
||||
db: Session,
|
||||
target: AccountSupplyTarget,
|
||||
*,
|
||||
target_covered: int,
|
||||
target_available: int,
|
||||
target_allocatable: int,
|
||||
safety_buffer: int,
|
||||
enabled: bool,
|
||||
updated_by: int | None,
|
||||
) -> AccountSupplyTarget:
|
||||
return upsert_supply_target(
|
||||
db,
|
||||
platform=target.platform,
|
||||
scope=target.scope,
|
||||
commodity_id=target.commodity_id,
|
||||
value_class=target.value_class,
|
||||
target_covered=target_covered,
|
||||
target_available=target_available,
|
||||
target_allocatable=target_allocatable,
|
||||
safety_buffer=safety_buffer,
|
||||
enabled=enabled,
|
||||
updated_by=updated_by,
|
||||
)
|
||||
|
||||
|
||||
def _sync_candidate_query(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -389,4 +485,6 @@ __all__ = [
|
||||
"enqueue_supply_replenishment",
|
||||
"evaluate_supply_target",
|
||||
"evaluate_supply_targets",
|
||||
"update_supply_target",
|
||||
"upsert_supply_target",
|
||||
]
|
||||
|
||||
@@ -25,6 +25,12 @@ from .high_value_service import (
|
||||
|
||||
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)
|
||||
@@ -45,6 +51,18 @@ def _payload_hash(raw_payload: dict[str, Any]) -> 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,
|
||||
*,
|
||||
@@ -191,8 +209,9 @@ def finish_sync_success(
|
||||
state.error_code = ""
|
||||
state.error_message = ""
|
||||
if raw_payload is not None:
|
||||
state.raw_payload_json = raw_payload
|
||||
state.payload_hash = _payload_hash(raw_payload)
|
||||
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()
|
||||
|
||||
@@ -225,8 +244,9 @@ def finish_sync_failure(
|
||||
state.error_code = error_code[:64]
|
||||
state.error_message = message
|
||||
if raw_payload is not None:
|
||||
state.raw_payload_json = raw_payload
|
||||
state.payload_hash = _payload_hash(raw_payload)
|
||||
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()
|
||||
|
||||
@@ -287,8 +307,55 @@ def sync_storehome_snapshot(
|
||||
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",
|
||||
@@ -296,6 +363,7 @@ __all__ = [
|
||||
"get_sync_state",
|
||||
"is_domain_fresh",
|
||||
"mark_domain_dirty",
|
||||
"record_sync_success",
|
||||
"start_sync_run",
|
||||
"sync_storehome_snapshot",
|
||||
]
|
||||
|
||||
@@ -38,6 +38,14 @@ from .account_binding import (
|
||||
)
|
||||
from .account_events_service import CATEGORY_OPS, emit
|
||||
from .account_lifecycle import on_ck_verdict
|
||||
from .account_sync_service import (
|
||||
DOMAIN_ELITE,
|
||||
DOMAIN_GOLD,
|
||||
DOMAIN_POINTS,
|
||||
DOMAIN_ROLE,
|
||||
DOMAIN_SESSION,
|
||||
record_sync_success,
|
||||
)
|
||||
from .cookie_check_service import check_douyu_cookie, classify_douyu_cookie_result
|
||||
from .douyu_activity_query import (
|
||||
activity_client,
|
||||
@@ -973,6 +981,19 @@ def persist_probe_result(
|
||||
actor_type="user" if current else "system",
|
||||
actor_id=int(current.id) if current else None,
|
||||
)
|
||||
if session_status in {SESSION_VALID, SESSION_INVALID}:
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_SESSION,
|
||||
source=source,
|
||||
observed_at=now,
|
||||
raw_payload={
|
||||
"status": session_status,
|
||||
"message": session.get("message") or "",
|
||||
"detail": detail,
|
||||
},
|
||||
)
|
||||
|
||||
elite = probe.get("elite") or {}
|
||||
if elite.get("status") == CAPABILITY_OK:
|
||||
@@ -980,10 +1001,27 @@ def persist_probe_result(
|
||||
account.elite_expire_time = _to_int(elite.get("expire_time"))
|
||||
if _to_int(elite.get("manual_score")) is not None:
|
||||
account.points = _to_int(elite.get("manual_score"))
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_ELITE,
|
||||
source=source,
|
||||
observed_at=now,
|
||||
raw_payload=dict(elite),
|
||||
)
|
||||
|
||||
eligibility = probe.get("eligibility") or {}
|
||||
if _to_int(eligibility.get("points")) is not None:
|
||||
account.points = _to_int(eligibility.get("points"))
|
||||
if isinstance(eligibility.get("points_response"), dict):
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_POINTS,
|
||||
source=source,
|
||||
observed_at=now,
|
||||
raw_payload=eligibility["points_response"],
|
||||
)
|
||||
points_expire_at = eligibility.get("points_expire_at")
|
||||
if points_expire_at:
|
||||
try:
|
||||
@@ -996,10 +1034,28 @@ def persist_probe_result(
|
||||
role = roles.get(scope)
|
||||
if isinstance(role, dict):
|
||||
_apply_role_result(account, scope, role)
|
||||
if role.get("status") not in {ROLE_ERROR, ROLE_UNKNOWN}:
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_ROLE,
|
||||
source=source,
|
||||
observed_at=now,
|
||||
raw_payload=role,
|
||||
)
|
||||
|
||||
gold = probe.get("gold") or {}
|
||||
if gold.get("status") == CAPABILITY_OK and _to_int(gold.get("balance")) is not None:
|
||||
account.gold_balance = _to_int(gold.get("balance"))
|
||||
if gold.get("status") == CAPABILITY_OK:
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_GOLD,
|
||||
source=source,
|
||||
observed_at=now,
|
||||
raw_payload=gold,
|
||||
)
|
||||
|
||||
eligibility = probe.get("eligibility") or {}
|
||||
storehome_goods = [
|
||||
|
||||
@@ -17,6 +17,7 @@ from core.douyu import (
|
||||
)
|
||||
|
||||
from ..models import Account, DouyuTask
|
||||
from .account_sync_service import DOMAIN_GOLD, record_sync_success
|
||||
from .business_events import record_recharge
|
||||
from .douyu_activity_query import query_exchange_balance, query_gold_account
|
||||
from .douyu_service import update_account_profile_from_cookie
|
||||
@@ -613,6 +614,15 @@ class GoldMixin:
|
||||
):
|
||||
client = self._client(cookie)
|
||||
result = self._refresh_account_gold_balance(client, account)
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_GOLD,
|
||||
source="douyu_task.query_gold_balance",
|
||||
observed_at=datetime.now(UTC),
|
||||
raw_payload=result,
|
||||
task_id=task.id,
|
||||
)
|
||||
account.updated_at = datetime.now(UTC)
|
||||
self._mark_task(
|
||||
db,
|
||||
|
||||
@@ -11,6 +11,11 @@ from core.douyu import DouyuActivityClient, DouyuActivityError
|
||||
|
||||
from ..models import Account, DouyuTask
|
||||
from .account_events_service import EVENT_ELITE_OPENED, EVENT_ESPORTS_OPENED, emit
|
||||
from .account_sync_service import (
|
||||
DOMAIN_ELITE,
|
||||
DOMAIN_POINTS,
|
||||
record_sync_success,
|
||||
)
|
||||
from .douyu_activity_query import query_csrf, query_elite_user_info, query_points
|
||||
from .douyu_elite_status import (
|
||||
elite_is_currently_open,
|
||||
@@ -121,6 +126,25 @@ class ManualMixin:
|
||||
manual_id=manual_id,
|
||||
rid=str(config.get("rid") or ""),
|
||||
)
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_ELITE,
|
||||
source="douyu_task.query_elite_status",
|
||||
observed_at=datetime.now(UTC),
|
||||
raw_payload=result,
|
||||
task_id=task.id,
|
||||
)
|
||||
if result.get("elite_manual_score") is not None:
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_POINTS,
|
||||
source="douyu_task.query_elite_status",
|
||||
observed_at=datetime.now(UTC),
|
||||
raw_payload=result,
|
||||
task_id=task.id,
|
||||
)
|
||||
account.updated_at = datetime.now(UTC)
|
||||
state = (
|
||||
"已过期"
|
||||
@@ -552,6 +576,17 @@ class ManualMixin:
|
||||
client = self._client(cookie)
|
||||
ctn = query_csrf(client)
|
||||
result = self._refresh_account_points(client, account, cookie, ctn=ctn)
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_POINTS,
|
||||
source="douyu_task.query_points",
|
||||
observed_at=datetime.now(UTC),
|
||||
raw_payload=result.get("points_query")
|
||||
if isinstance(result.get("points_query"), dict)
|
||||
else result,
|
||||
task_id=task.id,
|
||||
)
|
||||
account.updated_at = datetime.now(UTC)
|
||||
points = result["points"]
|
||||
self._mark_task(
|
||||
|
||||
@@ -33,7 +33,15 @@ from sqlalchemy.orm import Session
|
||||
from core.douyu import DouyuActivityClient
|
||||
|
||||
from ..models import Account, DouyuTask
|
||||
from .account_sync_service import sync_storehome_snapshot
|
||||
from .account_sync_service import (
|
||||
DOMAIN_ELITE,
|
||||
DOMAIN_ESPORTS,
|
||||
DOMAIN_EXCHANGE_RECORDS,
|
||||
DOMAIN_GOLD,
|
||||
DOMAIN_POINTS,
|
||||
record_sync_success,
|
||||
sync_storehome_snapshot,
|
||||
)
|
||||
from .business_events import record_official_exchange
|
||||
from .douyu_account_probe import (
|
||||
ROLE_BOUND,
|
||||
@@ -301,6 +309,15 @@ class OverviewMixin:
|
||||
gold = self._refresh_account_gold_balance(client, account)
|
||||
overview["assets"]["gold_balance"] = gold.get("gold_balance")
|
||||
overview["assets"]["exchange_balance"] = gold.get("exchange_balance")
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_GOLD,
|
||||
source="douyu_task.query_account_overview",
|
||||
observed_at=queried_at,
|
||||
raw_payload=gold,
|
||||
task_id=task.id,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 外部接口边界需要宽泛兜底
|
||||
errors["gold"] = _err(exc)
|
||||
|
||||
@@ -317,6 +334,17 @@ class OverviewMixin:
|
||||
# 在账号详情里展示 —— 查询只是一次「刷新」,不是另开一份副本。
|
||||
expire_dt = _parse_expire_at(expire_raw)
|
||||
account.points_expire_at = expire_dt
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_POINTS,
|
||||
source="douyu_task.query_account_overview",
|
||||
observed_at=queried_at,
|
||||
raw_payload=points.get("points_query")
|
||||
if isinstance(points.get("points_query"), dict)
|
||||
else points,
|
||||
task_id=task.id,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors["points"] = _err(exc)
|
||||
|
||||
@@ -333,6 +361,15 @@ class OverviewMixin:
|
||||
"expire_time": _ts(elite.get("elite_expire_time")),
|
||||
"points": elite.get("elite_manual_score"),
|
||||
}
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_ELITE,
|
||||
source="douyu_task.query_account_overview",
|
||||
observed_at=queried_at,
|
||||
raw_payload=elite,
|
||||
task_id=task.id,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors["elite"] = _err(exc)
|
||||
|
||||
@@ -358,6 +395,15 @@ class OverviewMixin:
|
||||
"points": esports.get("esports_points"),
|
||||
"expire_time": _ts(esports.get("esports_expire_time")),
|
||||
}
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_ESPORTS,
|
||||
source="douyu_task.query_account_overview",
|
||||
observed_at=queried_at,
|
||||
raw_payload=esports,
|
||||
task_id=task.id,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors["esports"] = _err(exc)
|
||||
|
||||
@@ -512,6 +558,17 @@ class OverviewMixin:
|
||||
overview["exchange_events_added"] = record_official_exchange_records(
|
||||
db, account, all_records
|
||||
)
|
||||
record_sync_success(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
domain=DOMAIN_EXCHANGE_RECORDS,
|
||||
source="douyu_task.query_account_overview",
|
||||
observed_at=queried_at,
|
||||
item_count=len(all_records),
|
||||
changed_count=int(overview["exchange_events_added"] or 0),
|
||||
raw_payload=_group_by_scope(all_records),
|
||||
task_id=task.id,
|
||||
)
|
||||
overview["exchange_records"] = {
|
||||
scope: rows[:EXCHANGE_RECORD_DISPLAY_LIMIT]
|
||||
for scope, rows in _group_by_scope(all_records).items()
|
||||
|
||||
Reference in New Issue
Block a user