{
  "name": "UAT 頁面健康監控 (關鍵頁面檢查)",
  "nodes": [
    {
      "parameters": {
        "rule": { "interval": [{ "field": "minutes", "minutesInterval": 5 }] }
      },
      "id": "schedule-trigger",
      "name": "每 5 分鐘檢查",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [250, 300]
    },
    {
      "parameters": {
        "jsCode": "// 定義要檢查的關鍵頁面\nconst pages = [\n  { name: '首頁', url: 'https://mo.wooo.work/' },\n  { name: '每日銷售', url: 'https://mo.wooo.work/daily_sales' },\n  { name: '廠商缺貨', url: 'https://mo.wooo.work/vendor-stockout/' },\n  { name: 'EDM 看板', url: 'https://mo.wooo.work/edm' },\n  { name: '健康檢查', url: 'https://mo.wooo.work/health' }\n];\n\nreturn pages.map(p => ({ json: p }));"
      },
      "id": "define-pages",
      "name": "定義檢查頁面",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [470, 300]
    },
    {
      "parameters": {
        "url": "={{ $json.url }}",
        "options": {
          "timeout": 15000,
          "redirect": { "redirect": { "followRedirects": true } }
        }
      },
      "id": "http-check",
      "name": "HTTP 檢查",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [690, 300],
      "continueOnFail": true
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all();\nconst errors = [];\n\nfor (const item of items) {\n  const data = item.json;\n  const statusCode = data.statusCode || data.status || 0;\n  const hasError = data.error || statusCode >= 400 || statusCode === 0;\n  \n  if (hasError) {\n    errors.push({\n      name: data.name || '未知頁面',\n      url: data.url || '未知',\n      status: statusCode,\n      error: data.error?.message || data.message || '連線失敗'\n    });\n  }\n}\n\nif (errors.length === 0) {\n  return [{ json: { hasErrors: false, message: '所有頁面正常' } }];\n}\n\nconst errorText = errors.map(e => `❌ ${e.name}: ${e.status} - ${e.error}`).join('\\n');\nconst alertText = `🔴 *【UAT】MOMO Pro 頁面異常*\\n\\n🏢 環境: 🟦 *測試環境* (mo.wooo.work)\\n\\n${errorText}\\n\\n🔧 嘗試重啟服務...`;\n\nreturn [{ json: { \n  hasErrors: true, \n  errors: errors,\n  telegramBody: {\n    chat_id: '5619078117',\n    text: alertText,\n    parse_mode: 'Markdown'\n  }\n} }];"
      },
      "id": "analyze-results",
      "name": "分析結果",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [910, 300]
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true },
          "conditions": [{
            "id": "has-errors",
            "leftValue": "={{ $json.hasErrors }}",
            "rightValue": true,
            "operator": { "type": "boolean", "operation": "equals" }
          }],
          "combinator": "and"
        }
      },
      "id": "if-errors",
      "name": "有錯誤?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [1130, 300]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.telegram.org/bot<TELEGRAM_BOT_TOKEN>/sendMessage",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.telegramBody) }}"
      },
      "id": "telegram-alert",
      "name": "發送告警",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [1350, 200]
    },
    {
      "parameters": {
        "command": "ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 wooo@192.168.0.110 'echo <LOGIN_PASSWORD> | sudo -S kubectl rollout restart deployment/momo-app -n momo 2>/dev/null && echo RESTART_SUCCESS || echo RESTART_FAILED'"
      },
      "id": "restart-app",
      "name": "重啟 K8s App",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [1350, 400]
    },
    {
      "parameters": {
        "jsCode": "const stdout = $input.first().json.stdout || '';\nconst success = stdout.includes('RESTART_SUCCESS');\nconst text = success ? \n  '✅ *momo-app 已重啟*\\n\\n等待 30 秒後服務應恢復正常' :\n  '❌ *重啟失敗*\\n\\n請手動檢查 K8s 狀態';\n\nreturn [{ json: { \n  telegramBody: {\n    chat_id: '5619078117',\n    text: text,\n    parse_mode: 'Markdown'\n  }\n} }];"
      },
      "id": "restart-result",
      "name": "重啟結果",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1570, 400]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.telegram.org/bot<TELEGRAM_BOT_TOKEN>/sendMessage",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.telegramBody) }}"
      },
      "id": "telegram-result",
      "name": "通知結果",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [1790, 400]
    },
    {
      "parameters": {},
      "id": "healthy-noop",
      "name": "正常 (不處理)",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [1350, 500]
    }
  ],
  "connections": {
    "每 5 分鐘檢查": { "main": [[{ "node": "定義檢查頁面", "type": "main", "index": 0 }]] },
    "定義檢查頁面": { "main": [[{ "node": "HTTP 檢查", "type": "main", "index": 0 }]] },
    "HTTP 檢查": { "main": [[{ "node": "分析結果", "type": "main", "index": 0 }]] },
    "分析結果": { "main": [[{ "node": "有錯誤?", "type": "main", "index": 0 }]] },
    "有錯誤?": {
      "main": [
        [{ "node": "發送告警", "type": "main", "index": 0 }],
        [{ "node": "正常 (不處理)", "type": "main", "index": 0 }]
      ]
    },
    "發送告警": { "main": [[{ "node": "重啟 K8s App", "type": "main", "index": 0 }]] },
    "重啟 K8s App": { "main": [[{ "node": "重啟結果", "type": "main", "index": 0 }]] },
    "重啟結果": { "main": [[{ "node": "通知結果", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" },
  "staticData": null,
  "tags": [
    { "name": "監控", "id": "monitoring" },
    { "name": "自動修復", "id": "auto-repair" },
    { "name": "UAT", "id": "uat" }
  ],
  "triggerCount": 1,
  "meta": { "instanceId": "momo-n8n" }
}
