add: daily auto-sync (launchd plist + one-click setup script)

This commit is contained in:
王源
2026-09-10 15:36:24 +08:00
parent 5b45702ee9
commit 6b85c3fb2b
4 changed files with 140 additions and 60 deletions
+37
View File
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.voypace.dailysync</string>
<!-- 每天运行的命令:用 ads-center 的 python 跑同步脚本 -->
<key>ProgramArguments</key>
<array>
<string>/Users/ads-center/py314env/bin/python</string>
<string>/Users/ads-center/voypace-report-sync/voypace-ad-report/adscenter_daily_sync.py</string>
</array>
<!-- 工作目录 -->
<key>WorkingDirectory</key>
<string>/Users/ads-center/voypace-report-sync/voypace-ad-report</string>
<!-- 每天 09:00 触发(取前一天数据) -->
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>9</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
<!-- 日志 -->
<key>StandardOutPath</key>
<string>/Users/ads-center/voypace-report-sync/voypace_sync.log</string>
<key>StandardErrorPath</key>
<string>/Users/ads-center/voypace-report-sync/voypace_sync.err.log</string>
<key>RunAtLoad</key>
<false/>
</dict>
</plist>
+33
View File
@@ -0,0 +1,33 @@
#!/bin/bash
# === Voypace Ad Report 一键部署脚本 ===
# 在 voypace-ad-report 目录下执行:bash deploy.sh
set -e
REPO_NAME="voypace-ad-report"
USER="wangyuan"
TOKEN="51fa1035659e6d267beffcd08af5ec7168efe4df"
GIT_HOST="git.ops.roasbloom.site"
echo ">>> Step 1: 建仓库..."
curl -s -X POST "https://${GIT_HOST}/api/v1/user/repos" \
-H "Authorization: token ${TOKEN}" \
-H "Content-Type: application/json" \
-d "{\"name\":\"${REPO_NAME}\",\"private\":true}" \
-o /tmp/create_repo_resp.json -w "\nHTTP %{http_code}\n"
echo "响应:"
cat /tmp/create_repo_resp.json | head -5
echo ""
echo ">>> Step 2: 初始化 git 并推送..."
git init
git add index.html mtg_data.json orders.json campaigns.json matrix_overrides.json \
db_fetch.py excel_fetch.py adscenter_daily_sync.py seed_from_sheet4.py
git commit -m "init: voypace ad report"
git remote add origin "https://${USER}:${TOKEN}@${GIT_HOST}/${USER}/${REPO_NAME}.git"
git push -u origin main || git push -u origin master
echo ""
echo "=== 部署完成 ==="
echo "报告网址:https://ops.roasbloom.site/${REPO_NAME}/"
-60
View File
@@ -1,60 +0,0 @@
#!/bin/bash
# =====================================================================
# 【在 ads-center 那台电脑上运行这一个脚本即可】
# 作用:把 Voypace 面板的数据用最新逻辑从 2026-07-15 起全量重查一遍并上线。
# 用法:把本文件拷到 ads-center,执行 bash run_on_adscenter.sh
# =====================================================================
set -e
# ads-center 上的 Python(与 Moboost 同步脚本一致)
PY="/Users/ads-center/py314env/bin/python"
[ -x "$PY" ] || PY="python3" # 找不到就退回系统 python3
# 同步仓库目录(不存在则自动 clone)
REPO_DIR="/Users/ads-center/voypace-report-sync/voypace-ad-report"
REPO_URL="https://wangyuan:51fa1035659e6d267beffcd08af5ec7168efe4df@git.ops.roasbloom.site/wangyuan/voypace-ad-report.git"
echo ">>> 1/4 准备代码仓库..."
if [ -d "$REPO_DIR/.git" ]; then
git -C "$REPO_DIR" pull --rebase -q
else
mkdir -p "$(dirname "$REPO_DIR")"
git clone -q "$REPO_URL" "$REPO_DIR"
fi
cd "$REPO_DIR"
echo ">>> 2/4 用新逻辑从 2026-07-15 全量重查(查数据库)..."
"$PY" db_fetch.py 2026-07-15 2026-12-31 > mtg_data.json
echo " 生成记录数:$("$PY" -c 'import json;print(len(json.load(open("mtg_data.json"))))')"
echo ">>> 3/4 刷新矩阵状态..."
"$PY" - <<'EOF'
import json
from collections import defaultdict
data=json.load(open("mtg_data.json")); orders=json.load(open("orders.json"))
pkg_status={}
for o in orders:
for p in str(o["pkg"]).replace(" ","").split("/"):
if p: pkg_status[p]=o.get("status","Pause")
g=defaultdict(lambda: defaultdict(int))
for r in data: g[(r["channel"],r["pkg"])][r["date"]]+=r["impression"]
alldates=sorted(set(r["date"] for r in data)); last3=alldates[-3:]
ov={}
for (ch,pkg),days in g.items():
ps=pkg_status.get(pkg,"Pause")
if ps!="Active": ov[f"{ch}|{pkg}"]="Pause"; continue
total=sum(days.values()); rmax=max([days.get(dt,0) for dt in last3]) if last3 else 0
ov[f"{ch}|{pkg}"]= "none" if total==0 else ("Active" if rmax>=100 else "Pause")
json.dump(ov, open("matrix_overrides.json","w"), ensure_ascii=False, indent=2)
EOF
echo ">>> 4/4 上传更新(面板会自动刷新)..."
git add mtg_data.json matrix_overrides.json
if git commit -m "data: full backfill from 2026-07-15" -q; then
git push -q origin main && echo " 已上线"
else
echo " 数据无变化,跳过"
fi
echo ""
echo "=== 完成!面板地址:https://ops.roasbloom.site/voypace-ad-report/ ==="
+70
View File
@@ -0,0 +1,70 @@
#!/bin/bash
# =====================================================================
# 【在 ads-center 那台电脑上运行一次即可】
# 作用:
# 1) 拉取 Voypace 面板代码
# 2) 首次从 2026-07-15 全量回填数据并上线
# 3) 安装“每天早上 9 点自动更新”的定时任务
# 用法:把本文件拷到 ads-center,执行 bash setup_on_adscenter.sh
# =====================================================================
set -e
PY="/Users/ads-center/py314env/bin/python"
[ -x "$PY" ] || PY="python3"
BASE="/Users/ads-center/voypace-report-sync"
REPO_DIR="$BASE/voypace-ad-report"
REPO_URL="https://wangyuan:51fa1035659e6d267beffcd08af5ec7168efe4df@git.ops.roasbloom.site/wangyuan/voypace-ad-report.git"
PLIST_SRC="$REPO_DIR/com.voypace.dailysync.plist"
PLIST_DST="$HOME/Library/LaunchAgents/com.voypace.dailysync.plist"
echo ">>> 1/4 准备代码仓库..."
if [ -d "$REPO_DIR/.git" ]; then
git -C "$REPO_DIR" pull --rebase -q
else
mkdir -p "$BASE"
git clone -q "$REPO_URL" "$REPO_DIR"
fi
cd "$REPO_DIR"
echo ">>> 2/4 首次全量回填(从 2026-07-15,查数据库)..."
"$PY" db_fetch.py 2026-07-15 2026-12-31 > mtg_data.json
echo " 记录数:$("$PY" -c 'import json;print(len(json.load(open("mtg_data.json"))))')"
# 刷新矩阵状态
"$PY" - <<'EOF'
import json
from collections import defaultdict
data=json.load(open("mtg_data.json")); orders=json.load(open("orders.json"))
pkg_status={}
for o in orders:
for p in str(o["pkg"]).replace(" ","").split("/"):
if p: pkg_status[p]=o.get("status","Pause")
g=defaultdict(lambda: defaultdict(int))
for r in data: g[(r["channel"],r["pkg"])][r["date"]]+=r["impression"]
alldates=sorted(set(r["date"] for r in data)); last3=alldates[-3:]
ov={}
for (ch,pkg),days in g.items():
ps=pkg_status.get(pkg,"Pause")
if ps!="Active": ov[f"{ch}|{pkg}"]="Pause"; continue
total=sum(days.values()); rmax=max([days.get(dt,0) for dt in last3]) if last3 else 0
ov[f"{ch}|{pkg}"]= "none" if total==0 else ("Active" if rmax>=100 else "Pause")
json.dump(ov, open("matrix_overrides.json","w"), ensure_ascii=False, indent=2)
EOF
git add mtg_data.json matrix_overrides.json
git commit -m "data: full backfill from 2026-07-15" -q && git push -q origin main && echo " 已上线" || echo " 数据无变化"
echo ">>> 3/4 安装每日定时任务(每天 09:00)..."
mkdir -p "$HOME/Library/LaunchAgents"
cp "$PLIST_SRC" "$PLIST_DST"
# 先卸载旧的(若存在),再加载
launchctl unload "$PLIST_DST" 2>/dev/null || true
launchctl load "$PLIST_DST"
echo " 定时任务已安装:com.voypace.dailysync"
echo ">>> 4/4 校验..."
launchctl list | grep voypace || echo " (未在列表看到,可执行 launchctl list | grep voypace 复查)"
echo ""
echo "=== 全部完成! ==="
echo "面板地址:https://ops.roasbloom.site/voypace-ad-report/"
echo "以后每天 09:00 自动更新,日志:$BASE/voypace_sync.log"