feat: 工参每周一一份并提前缓存,登录后自动补缺;评估按周一取工参,指标等平台同步三天后再查

- 工参从 2024-01-01 起每周一一份,排队任务一次补一份,新的周先补;一个地市同时只取一份
- 大数据平台账号登录成功后排补取,排队进程每 10 分钟检查缺的周;排队进程重启时放开没取完的快照
- 拆除前工参用拆除日上一周的周一,拆除后用当周的周一;待拆小区不参与周边站和指标匹配
- 一周的指标等周日过后第 3 天才查,没到时间在结果说明里写明哪天之后有数据
- AI 工作区没就绪时开通接口返回 200,改为等待重试,最多 150 秒
- 排任务加锁,避免多个线程同时排时用到已关闭的连接池
- 运行面板显示各地市工参缓存进度
This commit is contained in:
2026-11-12 12:31:03 +08:00
parent 452b01498f
commit 3a7a5b39b4
20 changed files with 814 additions and 180 deletions
+33 -14
View File
@@ -1,7 +1,7 @@
"""评估取数。
工参:本系统的工参快照(CellData_4G/5G),拆除前后按 pick_before / pick_after 选日期,库里没有的日期
用提交人的大数据平台凭证经 OpenCode 补取;本系统账号没有凭证时借号池里的账号。
工参:本系统的工参快照(CellData_4G/5G),每周一一份。拆除前用拆除日上一周的周一,拆除后用拆除日当周的周一;
库里还没有的周一用提交人的大数据平台凭证经 OpenCode 补取,本系统账号没有凭证时借号池里的账号。
指标按周(周一到周日)取。4G 小区的流量、质量、扇区和频段取即席查询「[管理视图]重要场景-小区周粒度」;
MR 覆盖、竞对和 5G 小区还没接,先用模拟数据,同一小区同一时间段每次结果相同。
环境变量 DECOM_EVAL_SOURCE=mock 时工参和指标都用模拟数据,便于本地演示。
@@ -11,23 +11,35 @@ from __future__ import annotations
import asyncio
import hashlib
import logging
import math
import os
from collections.abc import Callable, Iterator
from contextlib import contextmanager
from datetime import date, datetime, time, timedelta
from datetime import date, datetime, time
from typing import Protocol
from sqlalchemy import select
from app.collect.accounts import borrow_account, release_account
from app.collect.gongcan import AfterPick, GongcanError, GongcanStore, Target, fetch_snapshot, month_start, pick_after, pick_before
from app.collect.gongcan import (
AfterPick,
GongcanError,
GongcanStore,
Target,
after_monday,
before_monday,
fetch_snapshot,
pick_after,
pick_before,
)
from app.db import engine, session_scope
from app.models import NqiAccount
from app.nqi import adhoc
from app.nqi.client import NqiAuthError, NqiClient, NqiError
from app.nqi.opencode import OpenCode
logger = logging.getLogger("decom")
METRIC_KEYS = ("traffic_gb", "rrc_max", "high_load", "mr_weak_pct", "access_rate", "drop_rate", "util_peak")
MR_PROFILE_KEYS = ("mr_total", "mr_good_cnt", "mr_cover_pct", "unicom_cov", "telecom_cov", "weak_cell", "worse_unicom", "worse_telecom")
WEEK_TABLE = "appdbv3.a_overview_ispm_lte_cell_w"
@@ -44,9 +56,11 @@ class MetricError(Exception):
class GongcanSource(Protocol):
def before_day(self, city: str, target: Target, removal: date) -> date: ...
def before_day(self, city: str, target: Target, removal: date, today: date) -> date:
"""拆除前工参的日期。里面必须有目标,找不到时抛 GongcanMissing。"""
def after_pick(self, city: str, target: Target, removal: date, today: date) -> AfterPick | None: ...
def after_pick(self, city: str, target: Target, removal: date, today: date) -> AfterPick | None:
"""拆除后工参的日期,以及目标是否还在里面;还没有这份工参时返回 None。"""
def cells(self, city: str, day: date) -> list[dict]: ...
@@ -123,11 +137,16 @@ class StoreGongcan:
finally:
await client.aclose()
def before_day(self, city: str, target: Target, removal: date) -> date:
return asyncio.run(pick_before(self.store, city, target, removal))
def before_day(self, city: str, target: Target, removal: date, today: date) -> date:
return asyncio.run(pick_before(self.store, city, target, removal, today))
def after_pick(self, city: str, target: Target, removal: date, today: date) -> AfterPick | None:
return asyncio.run(pick_after(self.store, city, target, removal, today))
"""拆除后工参取不到不算失败,周边站改用拆除前工参。"""
try:
return asyncio.run(pick_after(self.store, city, target, removal, today))
except GongcanError as exc:
logger.info("%s 拆除后工参没取到,改用拆除前工参:%s", city, exc)
return None
def cells(self, city: str, day: date) -> list[dict]:
return self.store.cells(city, day)
@@ -143,23 +162,23 @@ def _between(low: float, high: float, *parts: object) -> float:
class MockGongcan:
"""演示用工参:待拆小区放在一处,周围散布模拟站点,拆除前取上个月 1 号、拆除后取昨天。"""
"""演示用工参:待拆小区放在一处,周围散布模拟站点。日期和实际规则一样取周一。"""
SITES = 24
def __init__(self):
self.targets: dict[str, str] = {}
def before_day(self, city: str, target: Target, removal: date) -> date:
def before_day(self, city: str, target: Target, removal: date, today: date) -> date:
for cgi in target.cgis:
self.targets.setdefault(cgi, target.site)
if target.site:
self.targets.setdefault(f"site:{target.site}", target.site)
return month_start(removal, -1)
return before_monday(removal, today)
def after_pick(self, city: str, target: Target, removal: date, today: date) -> AfterPick | None:
yesterday = today - timedelta(days=1)
return None if yesterday <= removal else AfterPick(yesterday, False)
day = after_monday(removal, today)
return None if day is None else AfterPick(day, True)
def cells(self, city: str, day: date) -> list[dict]:
seed = sorted(self.targets)[0] if self.targets else city