业务日期端到端:状态库快照 + 下载写入 + 报告读库
- state_store: site_status 加 expected/actual/undelivered_business_date 列(建表+旧库迁移);set_data_state 加可选 business_date(None 时保留,心跳不覆盖下载快照) - runtime: dispatch_task 下载成功后快照业务日期(today−offset);4 站 undelivered 一并补写 expected/actual/undelivered 三列(_site_undelivered_handler 内部连下绕过 dispatch,否则应到/实到业务日期丢失) - expected_undelivered: 报告数据日期列改读状态库(_read_business_dates),退役 mtime 反推;KPI 卡 2-3-2-2 等宽对齐 + 柱状图图例 overlay 分行 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
39
runtime.py
39
runtime.py
@@ -16,7 +16,7 @@ import socket
|
||||
import subprocess
|
||||
import time
|
||||
import urllib.request
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import yaml
|
||||
from playwright.sync_api import sync_playwright
|
||||
@@ -490,6 +490,42 @@ TASK_HANDLERS = {
|
||||
}
|
||||
|
||||
|
||||
def _record_business_date(site, kind):
|
||||
"""下载成功后,把本次数据的业务日期快照写进状态库(供前端/报告显示「是哪天的数据」)。
|
||||
业务日期 = 下载当天 − 该数据对应的日期偏移。__compare__ 无数据概念,跳过。
|
||||
|
||||
kind → 写入:
|
||||
expected/actual:各写自己一列(偏移各取其列)。
|
||||
undelivered:百世直供(恒 0)写 undelivered;4 站未到由 _site_undelivered_handler
|
||||
内部连带下了 expected+actual(不经 dispatch,无业务日期写入),故此处一并补写
|
||||
expected/actual/undelivered 三列——actual 用 actual 偏移、未到跟随 expected 偏移。
|
||||
顺带置 ready=True,让前端不必等心跳即可反映下载成功;写入失败仅告警、不影响任务判定。"""
|
||||
if site == "__compare__":
|
||||
return
|
||||
today = datetime.now().date()
|
||||
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
def _write(k, off):
|
||||
biz = (today - timedelta(days=off)).strftime("%Y-%m-%d")
|
||||
try:
|
||||
state_store.set_data_state(
|
||||
site, k, ready=True, generated_at=now, business_date=biz
|
||||
)
|
||||
except Exception as e:
|
||||
print(f">> [状态] 写业务日期失败 {site}/{k}: {e}")
|
||||
|
||||
if kind == "expected":
|
||||
_write("expected", state_store.get_offset(site, "expected"))
|
||||
elif kind == "actual":
|
||||
_write("actual", state_store.get_offset(site, "actual"))
|
||||
elif site == "百世":
|
||||
_write("undelivered", 0)
|
||||
else: # 4 站 undelivered:连带补写 expected/actual/undelivered 三列
|
||||
_write("expected", state_store.get_offset(site, "expected"))
|
||||
_write("actual", state_store.get_offset(site, "actual"))
|
||||
_write("undelivered", state_store.get_offset(site, "expected"))
|
||||
|
||||
|
||||
def dispatch_task(ctx, task_spec):
|
||||
"""执行一条任务。task_spec = {"site", "kind"}。返回 (status, error)。
|
||||
|
||||
@@ -513,6 +549,7 @@ def dispatch_task(ctx, task_spec):
|
||||
ret = handler(ctx)
|
||||
if ret is False:
|
||||
return (state_store.TASK_FAILED, "任务执行失败(重试耗尽)")
|
||||
_record_business_date(site, kind)
|
||||
return (state_store.TASK_SUCCESS, None)
|
||||
except Exception as e:
|
||||
return (state_store.TASK_FAILED, str(e))
|
||||
|
||||
Reference in New Issue
Block a user