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
+98
View File
@@ -0,0 +1,98 @@
from datetime import date, datetime, timedelta, timezone
from sqlalchemy import create_engine
from app.collect import gongcan_sync
from app.collect.gongcan import GongcanError, GongcanStore, claim_snapshot, save_snapshot
from app.models import Base, CellData4G, CellData5G, GongcanSnapshot
CITY = "江门"
TODAY = date(2024, 1, 25) # 可取的周一:01-01、01-08、01-15、01-22
CELLS = {
"4G": [{"cgi": "c1", "cell_name": "A", "station_name": "S", "latitude": 22.0, "longitude": 113.0}],
"5G": [{"ncgi": "n1", "nrcell_name": "N", "station_name": "S", "latitude": 22.0, "longitude": 113.0}],
}
class Clock:
def __init__(self):
self.now = datetime(2024, 1, 25, 2, 0, tzinfo=timezone.utc)
def __call__(self):
return self.now
class Platform:
def __init__(self, broken=()):
self.broken = set(broken)
self.fetched = []
async def __call__(self, city, day):
self.fetched.append(day)
if day in self.broken:
raise GongcanError("平台出错")
return CELLS
def make_store(platform, stored=(), clock=None):
db = create_engine("sqlite://")
Base.metadata.create_all(db, tables=[GongcanSnapshot.__table__, CellData4G.__table__, CellData5G.__table__])
clock = clock or Clock()
for day in stored:
save_snapshot(db, claim_snapshot(db, CITY, day, clock()), CELLS, clock())
return GongcanStore(db, platform, poll_seconds=0.01, clock=clock)
def test_sync_fills_missing_mondays_newest_first_one_per_run():
platform = Platform()
store = make_store(platform, stored=[date(2024, 1, 15)])
runs = [gongcan_sync.sync_once(CITY, today=TODAY, store=store) for _ in range(4)]
assert runs == [(date(2024, 1, 22), 2), (date(2024, 1, 8), 1), (date(2024, 1, 1), 0), (None, 0)]
assert platform.fetched == [date(2024, 1, 22), date(2024, 1, 8), date(2024, 1, 1)]
def test_sync_stops_while_another_snapshot_is_loading_or_just_failed():
clock = Clock()
platform = Platform(broken=[date(2024, 1, 22)])
store = make_store(platform, clock=clock)
# 评估任务正在取 01-08:补取任务不再开第二个。
claim_snapshot(store.engine, CITY, date(2024, 1, 8), clock())
assert gongcan_sync.sync_once(CITY, today=TODAY, store=store) == (None, 4)
save_snapshot(store.engine, 1, CELLS, clock())
try:
gongcan_sync.sync_once(CITY, today=TODAY, store=store)
except GongcanError as exc:
assert "平台出错" in str(exc)
else:
raise AssertionError("失败应当抛出")
assert gongcan_sync.sync_once(CITY, today=TODAY, store=store) == (None, 3)
clock.now += timedelta(minutes=11)
# 冷却后先补别的周,失败过的 01-22 留到最后再试。
assert gongcan_sync.sync_once(CITY, today=TODAY, store=store) == (date(2024, 1, 15), 2)
assert gongcan_sync.sync_once(CITY, today=TODAY, store=store) == (date(2024, 1, 1), 1)
assert platform.fetched == [date(2024, 1, 22), date(2024, 1, 15), date(2024, 1, 1)]
def test_cache_status_counts_mondays_only(monkeypatch):
clock = Clock()
store = make_store(Platform(broken=[date(2024, 1, 22)]), stored=[date(2024, 1, 8), date(2024, 1, 15), date(2024, 1, 10)], clock=clock)
try:
gongcan_sync.sync_once(CITY, today=TODAY, store=store)
except GongcanError:
pass
monkeypatch.setattr(gongcan_sync, "engine", lambda: store.engine)
assert gongcan_sync.sync_cities() == [CITY]
status = gongcan_sync.cache_status(TODAY)
assert len(status) == 1
item = status[0]
assert (item["city"], item["expected"], item["ready"], item["earliest"], item["latest"], item["loading"]) == (CITY, 4, 2, "2024-01-08", "2024-01-15", None)
assert item["error"].startswith("2024-01-22 GongcanError: 平台出错") and item["errorAt"]
def test_request_sync_does_nothing_without_a_queue(monkeypatch):
monkeypatch.setattr(gongcan_sync, "postgres_conninfo", lambda: None)
assert gongcan_sync.request_sync("江门市") is False
monkeypatch.setattr(gongcan_sync, "postgres_conninfo", lambda: "postgresql://x")
monkeypatch.setenv("DECOM_EVAL_SOURCE", "mock")
assert gongcan_sync.request_sync("江门市") is False
assert gongcan_sync.request_sync("") is False