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
+85 -45
View File
@@ -7,6 +7,7 @@ from sqlalchemy import create_engine
from app.collect import gongcan
from app.collect.gongcan import (
HISTORY_START,
AfterPick,
GongcanError,
GongcanMissing,
@@ -14,13 +15,16 @@ from app.collect.gongcan import (
Target,
build_cells,
claim_snapshot,
expected_mondays,
fetch_snapshot,
month_start,
latest_monday,
nearest_sites,
pick_after,
pick_before,
platform_files,
release_loading,
save_snapshot,
short_city,
snapshot_task_name,
target_center,
)
@@ -84,10 +88,16 @@ def make_store(platform, stored=None, clock=None):
return GongcanStore(db, platform, poll_seconds=0.01, clock=clock)
def test_month_start_crosses_year():
assert month_start(date(2026, 1, 10), -1) == date(2025, 12, 1)
assert month_start(date(2026, 1, 10), -3) == date(2025, 10, 1)
assert month_start(date(2026, 11, 30)) == date(2026, 11, 1)
def test_snapshots_are_mondays_and_wait_three_days_for_the_platform():
# 2026-11-09 是周一,它的工参要到周四 11-12 才取。
assert latest_monday(date(2026, 11, 11)) == date(2026, 11, 2)
assert latest_monday(date(2026, 11, 12)) == date(2026, 11, 9)
assert latest_monday(date(2026, 11, 18)) == date(2026, 11, 9)
days = expected_mondays(date(2026, 11, 12))
assert days[0] == HISTORY_START == date(2024, 1, 1) and days[-1] == date(2026, 11, 9) and len(days) == 150
assert all(day.weekday() == 0 for day in days)
assert expected_mondays(date(2024, 1, 3)) == []
assert short_city("江门市") == "江门" and short_city("江门分公司") == "江门" and short_city(" 江门 ") == "江门" and short_city("市") == "市"
def test_platform_files_use_make_date_and_keep_commas_out_of_text_columns():
@@ -217,67 +227,97 @@ def test_stale_loading_is_taken_over():
assert claim_snapshot(store.engine, CITY, date(2026, 11, 1), clock() + timedelta(minutes=41)) is not None
REMOVAL = date(2026, 11, 15)
def test_release_loading_frees_snapshots_left_by_a_dead_worker():
clock = Clock()
store = make_store(Platform({date(2026, 11, 2): with_target()}), {date(2026, 10, 26): with_target()}, clock=clock)
assert claim_snapshot(store.engine, CITY, date(2026, 11, 2), clock()) is not None
assert store.next_to_fill(CITY, [date(2026, 11, 2)]) is None
assert release_loading(store.engine) == 1
assert store.next_to_fill(CITY, [date(2026, 11, 2)]) == date(2026, 11, 2)
assert store.ready_dates(CITY, date(2026, 10, 1), date(2026, 12, 1)) == [date(2026, 10, 26)]
def test_before_uses_latest_stored_snapshot_with_target():
def test_next_to_fill_takes_one_at_a_time_and_backs_off_after_a_failure():
clock = Clock()
newest_first = [date(2026, 11, 2), date(2026, 10, 26), date(2026, 10, 19)]
platform = Platform({date(2026, 10, 26): with_target(), date(2026, 10, 19): with_target()})
store = make_store(platform, {date(2026, 10, 19): with_target()}, clock=clock)
assert store.missing_dates(CITY, newest_first) == [date(2026, 11, 2), date(2026, 10, 26)]
assert store.missing_dates(CITY, []) == []
assert store.next_to_fill(CITY, newest_first) == date(2026, 11, 2)
with pytest.raises(GongcanError):
asyncio.run(store.fill(CITY, date(2026, 11, 2)))
# 刚失败:冷却期内整个地市都不再取。
assert store.next_to_fill(CITY, newest_first) is None
clock.now += timedelta(minutes=11)
# 冷却过后先补没失败过的,失败过的排到最后。
assert store.next_to_fill(CITY, newest_first) == date(2026, 10, 26)
assert asyncio.run(store.fill(CITY, date(2026, 10, 26))) is True
assert store.next_to_fill(CITY, newest_first) == date(2026, 11, 2)
assert asyncio.run(store.fill(CITY, date(2026, 10, 26))) is False
assert store.next_to_fill(CITY, [date(2026, 10, 26), date(2026, 10, 19)]) is None
assert platform.fetched == [date(2026, 11, 2), date(2026, 10, 26)]
# 拆除日 2026-10-15 是周四:上一周的周一是 10-05,当周的周一是 10-12。
REMOVAL = date(2026, 10, 15)
TODAY = date(2026, 11, 12)
def test_before_is_the_monday_of_the_week_before_removal():
platform = Platform({})
store = make_store(platform, {date(2026, 9, 1): with_target(), date(2026, 10, 20): with_target(), date(2026, 11, 14): without_target()})
assert asyncio.run(pick_before(store, CITY, SITE, REMOVAL)) == date(2026, 10, 20)
store = make_store(platform, {date(2026, 9, 28): with_target(), date(2026, 10, 5): with_target(), date(2026, 10, 12): with_target()})
assert asyncio.run(pick_before(store, CITY, SITE, REMOVAL, TODAY)) == date(2026, 10, 5)
assert platform.fetched == []
def test_before_fetches_previous_month_when_stored_data_is_too_old():
platform = Platform({date(2026, 10, 1): with_target()})
store = make_store(platform, {date(2026, 7, 1): with_target()})
assert asyncio.run(pick_before(store, CITY, SITE, REMOVAL)) == date(2026, 10, 1)
assert platform.fetched == [date(2026, 10, 1)]
def test_before_fetches_the_monday_when_it_is_not_cached():
platform = Platform({date(2026, 10, 5): with_target()})
store = make_store(platform, {date(2026, 9, 28): with_target()})
assert asyncio.run(pick_before(store, CITY, SITE, REMOVAL, TODAY)) == date(2026, 10, 5)
assert platform.fetched == [date(2026, 10, 5)]
def test_before_walks_back_month_by_month():
platform = Platform({date(2026, 10, 1): without_target(), date(2026, 9, 1): with_target()})
store = make_store(platform)
assert asyncio.run(pick_before(store, CITY, SITE, REMOVAL)) == date(2026, 9, 1)
assert platform.fetched == [date(2026, 10, 1), date(2026, 9, 1)]
def test_before_walks_back_week_by_week_when_target_is_missing():
platform = Platform({date(2026, 9, 28): without_target(), date(2026, 9, 21): with_target()})
store = make_store(platform, {date(2026, 10, 5): without_target()})
assert asyncio.run(pick_before(store, CITY, SITE, REMOVAL, TODAY)) == date(2026, 9, 21)
assert platform.fetched == [date(2026, 9, 28), date(2026, 9, 21)]
def test_before_fails_after_three_months_and_skips_stored_months():
platform = Platform({date(2026, 9, 1): without_target(), date(2026, 8, 1): without_target()})
store = make_store(platform, {date(2026, 10, 1): without_target()})
def test_before_fails_after_four_more_weeks():
mondays = [date(2026, 10, 5), date(2026, 9, 28), date(2026, 9, 21), date(2026, 9, 14), date(2026, 9, 7)]
platform = Platform({})
store = make_store(platform, {day: without_target() for day in mondays} | {date(2026, 8, 31): with_target()})
with pytest.raises(GongcanMissing, match="站点 T"):
asyncio.run(pick_before(store, CITY, SITE, REMOVAL))
assert platform.fetched == [date(2026, 9, 1), date(2026, 8, 1)]
AFTER_REMOVAL = date(2026, 11, 1)
TODAY = date(2026, 11, 11)
def test_after_prefers_latest_snapshot_without_target():
platform = Platform({})
stored = {date(2026, 11, 3): without_target(), date(2026, 11, 5): with_target(), date(2026, 11, 8): without_target(), date(2026, 10, 30): without_target()}
store = make_store(platform, stored)
assert asyncio.run(pick_after(store, CITY, SITE, AFTER_REMOVAL, TODAY)) == AfterPick(date(2026, 11, 8), False)
asyncio.run(pick_before(store, CITY, SITE, REMOVAL, TODAY))
assert platform.fetched == []
def test_after_uses_yesterday_when_target_is_still_there():
def test_before_uses_latest_available_monday_for_a_future_removal():
platform = Platform({})
store = make_store(platform, {date(2026, 11, 5): with_target(), date(2026, 11, 10): with_target()})
assert asyncio.run(pick_after(store, CITY, SITE, AFTER_REMOVAL, TODAY)) == AfterPick(date(2026, 11, 10), True)
store = make_store(platform, {date(2026, 11, 9): with_target()})
assert asyncio.run(pick_before(store, CITY, SITE, date(2026, 12, 20), TODAY)) == date(2026, 11, 9)
with pytest.raises(GongcanMissing, match="太早"):
asyncio.run(pick_before(store, CITY, SITE, date(2024, 1, 5), TODAY))
assert platform.fetched == []
def test_after_fetches_yesterday_when_stored_data_still_has_target():
platform = Platform({date(2026, 11, 10): without_target()})
store = make_store(platform, {date(2026, 11, 5): with_target(), date(2026, 10, 1): without_target()})
assert asyncio.run(pick_after(store, CITY, SITE, AFTER_REMOVAL, TODAY)) == AfterPick(date(2026, 11, 10), False)
assert platform.fetched == [date(2026, 11, 10)]
def test_after_is_the_monday_of_the_removal_week_and_target_may_still_be_there():
platform = Platform({date(2026, 10, 12): with_target()})
store = make_store(platform, {date(2026, 10, 19): without_target()})
assert asyncio.run(pick_after(store, CITY, SITE, REMOVAL, TODAY)) == AfterPick(date(2026, 10, 12), True)
assert asyncio.run(pick_after(store, CITY, SITE, date(2026, 10, 19), TODAY)) == AfterPick(date(2026, 10, 19), False)
assert platform.fetched == [date(2026, 10, 12)]
def test_after_is_empty_until_removal_day_has_passed():
def test_after_is_empty_until_that_monday_can_be_fetched():
store = make_store(Platform({}))
assert asyncio.run(pick_after(store, CITY, SITE, date(2026, 11, 10), TODAY)) is None
# 11-09 这个周一 11-12 起可取;11-16 的还不行。
assert asyncio.run(pick_after(store, CITY, SITE, date(2026, 11, 10), date(2026, 11, 11))) is None
assert asyncio.run(pick_after(store, CITY, SITE, date(2026, 11, 17), TODAY)) is None
with pytest.raises(GongcanError, match="平台没有这一天"):
asyncio.run(pick_after(store, CITY, SITE, date(2026, 11, 10), TODAY))
def test_target_needs_cgis_or_site():