Add period-over-period trend table at top of report

build_data.py now also computes each buyer's total for the preceding
3-day window under identical rules (exclusions, attribution), exposed as
prev_total plus meta.prev_range. index.html renders it as the first
panel, with green/red deltas and a total row; the panel stays hidden if
no prior period exists.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
FANGKK
2026-09-07 23:42:03 +08:00
co-authored by Claude Opus 5
parent b33cfcf6cb
commit 88084ce5ff
3 changed files with 47 additions and 0 deletions
+18
View File
@@ -130,6 +130,20 @@ by_buyer = collections.defaultdict(dict)
for (b, pkg), v in pkg_spend.items():
by_buyer[b][pkg] = v
# --- 上一周期:紧邻本窗口之前的同样天数,口径完全一致 ---
prev_end = start - datetime.timedelta(days=1)
prev_start = prev_end - datetime.timedelta(days=WINDOW_DAYS - 1)
prev = collections.defaultdict(float)
prev_has_data = any(prev_start <= d <= prev_end for d in by_date)
for d, adp, pkg, geo, s_ in rows:
if not (prev_start <= d <= prev_end):
continue
if pkg in EXCL or adp in dropped_ids:
continue
b = amap.get(adp)
if b is not None:
prev[b] += s_
out = []
for b, pk in by_buyer.items():
pkgs = []
@@ -150,6 +164,7 @@ for b, pk in by_buyer.items():
out.append({
"name": b, "@name": b,
"total": round(sum(pk.values()), 4),
"prev_total": round(prev.get(b, 0.0), 4),
"active_ids": len(ids[b]),
"owned_ids": owned[b],
"pkgs": pkgs,
@@ -163,6 +178,7 @@ data = {
"source": "萌奇熊日报",
"generated_at": datetime.date.today().isoformat(),
"date_range": f"{start:%Y/%m/%d}-{end:%Y/%m/%d}",
"prev_range": (f"{prev_start:%Y/%m/%d}-{prev_end:%Y/%m/%d}" if prev_has_data else ""),
"note": "投放人以「投手」表 Media Buyer 列为准,@某某 已与某某合并计算;"
"未填写投放人的 Adpingid 不计入;exclude.json 所列包名已排除。"
"Package 后缀为该 Package 花费最高的投放地区,Adpingid 为该地区内花费最高的 Adping。",
@@ -182,6 +198,8 @@ if extra_used:
print(f"窗口 {start} -> {end}")
print(f"排除包名命中 {excluded_rows} 行,剔除花费 {excluded_spend:.2f}")
print(f"合计 {data['grand_total']:.2f} / Adpingid {data['week34_id_count']}")
print(f"上一周期 {prev_start} -> {prev_end} 合计 {sum(prev.values()):.2f}"
+ ('' if prev_has_data else ' (无数据)'))
for b in out:
print(f" {b['name']:6s} {b['total']:10.2f} 在投{b['active_ids']:3d}/负责{b['owned_ids']:3d}")
for p in b['pkgs']: