Files
llm-intelligence/scripts/run_daily.sh
2026-05-13 20:13:02 +08:00

123 lines
4.2 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# run_daily.sh - 每日数据采集与报告生成流水线
# Sprint 3: 完整调度脚本(采集→质量检查→报告生成→归档→通知)
set -euo pipefail
PROJECT_DIR="/home/long/project/llm-intelligence"
. "$PROJECT_DIR/scripts/report_utils.sh"
DB_URL="${DATABASE_URL:-host=/var/run/postgresql dbname=llm_intelligence user=long sslmode=disable}"
REPORT_DATE="$(report_date_value)"
LOG_FILE="/tmp/llm_hub_daily_${REPORT_DATE}.log"
FEISHU_WEBHOOK="${FEISHU_WEBHOOK:-}"
MODEL_COUNT=""
# 日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
# 错误处理
error_exit() {
local output_path=""
log "❌ 错误: $1"
# 降级:复制昨日报告
fallback_report
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
# 发送告警
if [ -n "$FEISHU_WEBHOOK" ]; then
send_alert "$1"
fi
exit 1
}
# 降级:复制昨日报告
fallback_report() {
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")"
if [ -f "$yesterday_md" ]; then
cp "$yesterday_md" "$today_md"
sed -i "s/${yesterday}/${REPORT_DATE}/g" "$today_md"
sed -i "1s/^/# [数据延迟] /" "$today_md"
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
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 "✅ 日报生成完成"
# 4. 校验归档
log "4⃣ 校验归档..."
if [ ! -f "$(report_archive_markdown_path "$REPORT_DATE")" ] || [ ! -f "$(report_archive_html_path "$REPORT_DATE")" ]; then
error_exit "日报归档失败"
fi
log "✅ 归档完成"
# 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
log "✅ 日报记录更新完成"
log "🎉 每日流水线全部完成!"
log "📄 Markdown: $(report_markdown_path "$REPORT_DATE")"
log "🌐 HTML: $(report_html_path "$REPORT_DATE")"
exit 0