数据源容错: 单源失败不中断+页面告警; 记录source_status

This commit is contained in:
2026-09-14 15:44:39 +08:00
parent 804e85ea00
commit fb7deeac12
2 changed files with 37 additions and 2 deletions
+25 -2
View File
@@ -171,8 +171,30 @@ def fetch_report(ymd):
d=json.load(urllib.request.urlopen(req,timeout=180,context=ctx))
return d['data']['list']
print('[fetch] Superset...'); sup=fetch_superset()
print('[fetch] report/v2/list T-1...'); rep=fetch_report(PREV_YMD)
# ---- 各源独立容错:一个失败不影响另一个 ----
src_status={'superset':'ok','report':'ok'}
sup=[]; rep=[]
try:
print('[fetch] Superset...'); sup=fetch_superset()
except Exception as e:
src_status['superset']='fail: '+str(e)[:120]
print('[ERROR] Superset 拉取失败:', str(e)[:200])
try:
print('[fetch] report/v2/list T-1...'); rep=fetch_report(PREV_YMD)
except Exception as e:
src_status['report']='fail: '+str(e)[:120]
print('[ERROR] report/v2/list 拉取失败(可能MS token过期):', str(e)[:200])
# 两个源都失败 -> 放弃本次更新,不写 data.json
if src_status['superset'].startswith('fail') and src_status['report'].startswith('fail'):
print('[ABORT] 两个数据源都失败,本次不更新 data.json。')
print('!!! 需检查凭证: Superset cookie / MS token 可能均已过期 !!!')
import sys as _s; _s.exit(1)
# 单源失败:告警但继续(用另一个源的数据更新)
if src_status['report'].startswith('fail'):
print('!!!!! 告警: MS(report/v2/list) 数据源失效,本次 MS/带_API account 数据将缺失。请尽快更新 MS_TOKEN !!!!!')
if src_status['superset'].startswith('fail'):
print('!!!!! 告警: Superset 数据源失效,本次 Midas/无_API account 数据将缺失。请尽快更新 SUPERSET_COOKIE !!!!!')
# Superset: 只取 T-1 那天(dt格式20260910)
sup_prev=[r for r in sup if str(r.get('dt'))==PREV_YMD]
@@ -277,6 +299,7 @@ for key in ['pkg_midas','pkg_ms','cust_daily','cust_detail']:
old[key]=keep_recent(old[key])
old['updated_at']=datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=8))).strftime('%Y-%m-%d %H:%M:%S UTC+8')
old['source_status']=src_status
json.dump(old,open(DATA_FILE,'w',encoding='utf-8'),ensure_ascii=False,separators=(',',':'))
print(f"[done] overall={len(old['overall'])}天 CN合计={cn_row['DSP CN revenue']} Intl合计={intl_row['DSP international']}")
print(f" pkg_midas={len(pkg_midas)}个pkg pkg_ms={len(pkg_ms)}个pkg cust分类={cust_cat_today}")