feat: auto-regenerate matrix status daily in update.sh

This commit is contained in:
王源
2026-09-08 23:55:51 +08:00
parent 367eeda50c
commit 9306889af8
2 changed files with 48 additions and 1 deletions
+47
View File
@@ -279,4 +279,51 @@ json.dump(out, open("mtg_data.json","w"), ensure_ascii=False, indent=2)
print(f"[update] MediaGo 合并后共 {len(out)} 条,已写入 mtg_data.json")
PYEOF
# ============ 重新生成渠道投放矩阵状态(matrix_overrides.json)============
python3 - <<'PYEOF'
import json, os
from collections import defaultdict
if not os.path.exists("mtg_data.json"):
raise SystemExit(0)
d = json.load(open("mtg_data.json"))
orders = json.load(open("orders.json")) if os.path.exists("orders.json") else []
# 产品状态:按 pkg -> status("a / b" 合并形式拆开)
pkg_status = {}
for o in orders:
for p in str(o.get("pkg","")).replace(" ","").split("/"):
if p: pkg_status[p] = o.get("status","Pause")
# 聚合每个 (channel, pkg) 的每日展示
g = defaultdict(lambda: defaultdict(int))
for r in d:
g[(r["channel"], r["pkg"])][r["date"]] += r.get("impression", 0)
alldates = sorted(set(r["date"] for r in d))
if not alldates:
raise SystemExit(0)
lastday = alldates[-1]
last3 = alldates[-3:]
overrides = {}
for (ch, pkg), days in g.items():
prod_status = pkg_status.get(pkg, "Pause")
# 第一优先级:产品订单状态为 Pause -> 直接 Pause
if prod_status != "Active":
overrides[f"{ch}|{pkg}"] = "Pause"
continue
# 产品在投 -> 看数据判定该渠道
total = sum(days.values())
lastval = days.get(lastday, 0)
recent3 = sum(days.get(dt, 0) for dt in last3)
if total == 0:
overrides[f"{ch}|{pkg}"] = "none" # 零曝光=从未投放(前端留空)
elif lastval >= 10 and recent3 >= 30:
overrides[f"{ch}|{pkg}"] = "Active" # 末日两位数以上且近期持续
else:
overrides[f"{ch}|{pkg}"] = "Pause" # 掉到个位数/断更=已停
json.dump(overrides, open("matrix_overrides.json","w"), ensure_ascii=False, indent=2)
print(f"[update] 矩阵状态已刷新,共 {len(overrides)} 个渠道×产品组合")
PYEOF
echo "[update] $(date '+%Y-%m-%d %H:%M:%S') 完成"