2026-05-13 14:42:45 +08:00
#!/bin/bash
# run_daily.sh - 每日数据采集与报告生成流水线
# Sprint 3: 完整调度脚本(采集→质量检查→报告生成→归档→通知)
set -euo pipefail
PROJECT_DIR = "/home/long/project/llm-intelligence"
2026-05-13 20:13:02 +08:00
. " $PROJECT_DIR /scripts/report_utils.sh "
2026-05-13 14:42:45 +08:00
DB_URL = " ${ DATABASE_URL :- host =/var/run/postgresql dbname=llm_intelligence user=long sslmode=disable } "
2026-05-13 20:13:02 +08:00
REPORT_DATE = " $( report_date_value) "
2026-05-13 14:42:45 +08:00
LOG_FILE = " /tmp/llm_hub_daily_ ${ REPORT_DATE } .log "
FEISHU_WEBHOOK = " ${ FEISHU_WEBHOOK :- } "
2026-05-13 20:13:02 +08:00
MODEL_COUNT = ""
2026-05-13 14:42:45 +08:00
# 日志函数
log( ) {
echo " [ $( date '+%Y-%m-%d %H:%M:%S' ) ] $1 " | tee -a " $LOG_FILE "
}
# 错误处理
error_exit( ) {
2026-05-13 20:13:02 +08:00
local output_path = ""
2026-05-13 14:42:45 +08:00
log " ❌ 错误: $1 "
# 降级:复制昨日报告
fallback_report
2026-05-13 20:13:02 +08:00
if [ -f " $( report_markdown_path " $REPORT_DATE " ) " ] ; then
output_path = " $( report_markdown_path " $REPORT_DATE " ) "
fi
track_report_state " $DB_URL " " $REPORT_DATE " "failed" " ${ MODEL_COUNT :- } " "" " $output_path " " $1 " >> " $LOG_FILE " 2>& 1 || true
2026-05-13 14:42:45 +08:00
# 发送告警
if [ -n " $FEISHU_WEBHOOK " ] ; then
send_alert " $1 "
fi
exit 1
}
# 降级:复制昨日报告
fallback_report( ) {
2026-05-13 20:13:02 +08:00
local yesterday yesterday_md today_md yesterday_html today_html
yesterday = $( date -d "yesterday" +%Y-%m-%d)
yesterday_md = " ${ PROJECT_DIR } / $( report_markdown_path " $yesterday " ) "
today_md = " ${ PROJECT_DIR } / $( report_markdown_path " $REPORT_DATE " ) "
yesterday_html = " ${ PROJECT_DIR } / $( report_html_path " $yesterday " ) "
today_html = " ${ PROJECT_DIR } / $( report_html_path " $REPORT_DATE " ) "
2026-05-13 14:42:45 +08:00
if [ -f " $yesterday_md " ] ; then
cp " $yesterday_md " " $today_md "
sed -i " s/ ${ yesterday } / ${ REPORT_DATE } /g " " $today_md "
sed -i "1s/^/# [数据延迟] /" " $today_md "
2026-05-13 20:13:02 +08:00
if [ -f " $yesterday_html " ] ; then
cp " $yesterday_html " " $today_html "
sed -i " s/ ${ yesterday } / ${ REPORT_DATE } /g " " $today_html "
fi
if [ -f " $today_md " ] && [ -f " $today_html " ] ; then
archive_report_artifacts " $REPORT_DATE " >> " $LOG_FILE " 2>& 1 || true
fi
2026-05-13 14:42:45 +08:00
log "⚠️ 已复制昨日报告并标记[数据延迟]"
else
log "⚠️ 无昨日报告可供复制"
fi
}
# 发送飞书告警
send_alert( ) {
local msg = " $1 "
local payload = " {\"msg_type\":\"text\",\"content\":{\"text\":\"🚨 LLM Hub 日报失败\\n日期: ${ REPORT_DATE } \\n错误: ${ msg } \\n请检查日志: ${ LOG_FILE } \"}} "
curl -s -X POST -H "Content-Type: application/json" \
-d " $payload " \
" $FEISHU_WEBHOOK " > /dev/null || true
log "📢 飞书告警已发送"
}
# 主流程
log " 🚀 开始每日流水线: ${ REPORT_DATE } "
cd " $PROJECT_DIR "
# 1. 数据采集
log "1️ ⃣ 数据采集..."
if ! go run scripts/fetch_openrouter.go >> " $LOG_FILE " 2>& 1; then
error_exit "数据采集失败"
fi
log "✅ 数据采集完成"
# 2. 数据质量检查
log "2️ ⃣ 数据质量检查..."
MODEL_COUNT = $( psql " $DB_URL " -t -c "SELECT COUNT(*) FROM models WHERE deleted_at IS NULL" 2>/dev/null | tr -d ' ' )
if [ " $MODEL_COUNT " -lt 10 ] ; then
error_exit " 模型数量不足: ${ MODEL_COUNT } < 10 "
fi
log " ✅ 数据质量检查通过 (模型数: ${ MODEL_COUNT } ) "
# 3. 生成日报
log "3️ ⃣ 生成日报..."
export DATABASE_URL = " $DB_URL "
if ! go run scripts/generate_daily_report.go >> " $LOG_FILE " 2>& 1; then
error_exit "日报生成失败"
fi
log "✅ 日报生成完成"
2026-05-13 20:13:02 +08:00
# 4. 校验归档
log "4️ ⃣ 校验归档..."
if [ ! -f " $( report_archive_markdown_path " $REPORT_DATE " ) " ] || [ ! -f " $( report_archive_html_path " $REPORT_DATE " ) " ] ; then
error_exit "日报归档失败"
fi
2026-05-13 14:42:45 +08:00
log "✅ 归档完成"
2026-05-13 20:13:02 +08:00
# 5. 校验运行记录
log "5️ ⃣ 校验运行记录..."
if ! psql " $DB_URL " -Atqc " select count(*) from daily_report where report_date = DATE ' ${ REPORT_DATE } ' and status = 'generated'; " | awk '{ exit !($1 >= 1) }' ; then
error_exit "daily_report 未写入 generated 记录"
fi
if ! psql " $DB_URL " -Atqc " select count(*) from report_runs where report_date = DATE ' ${ REPORT_DATE } ' and status = 'generated'; " | awk '{ exit !($1 >= 1) }' ; then
error_exit "report_runs 未写入 generated 记录"
fi
2026-05-13 14:42:45 +08:00
log "✅ 日报记录更新完成"
log "🎉 每日流水线全部完成!"
2026-05-13 20:13:02 +08:00
log " 📄 Markdown: $( report_markdown_path " $REPORT_DATE " ) "
log " 🌐 HTML: $( report_html_path " $REPORT_DATE " ) "
2026-05-13 14:42:45 +08:00
exit 0