{
  "name": "PostgreSQL 資料庫監控 (UAT + GCP)",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 15
            }
          ]
        }
      },
      "id": "schedule",
      "name": "每 15 分鐘執行",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [240, 300]
    },
    {
      "parameters": {
        "url": "https://mo.wooo.work/api/system/db/stats",
        "options": {
          "timeout": 15000
        }
      },
      "id": "uat-db",
      "name": "UAT DB 狀態",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [460, 200],
      "continueOnFail": true
    },
    {
      "parameters": {
        "url": "https://momo.wooo.work/api/system/db/stats",
        "options": {
          "timeout": 15000
        }
      },
      "id": "gcp-db",
      "name": "GCP DB 狀態",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [460, 400],
      "continueOnFail": true
    },
    {
      "parameters": {
        "mode": "combine",
        "combineBy": "combineAll",
        "options": {}
      },
      "id": "merge",
      "name": "合併結果",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3,
      "position": [680, 300]
    },
    {
      "parameters": {
        "jsCode": "const results = $input.all();\nconst now = new Date().toLocaleString('zh-TW', { timeZone: 'Asia/Taipei' });\n\nlet issues = [];\nlet envStatus = [];\n\n// 分析 UAT\nif (results[0]) {\n  const uat = results[0].json;\n  if (uat.error) {\n    issues.push('🟦 UAT DB: 無法連接');\n  } else {\n    const dbSize = uat.database_size_mb || 0;\n    const connCount = uat.connection_count || 0;\n    \n    if (dbSize > 8000) {\n      issues.push(`🟦 UAT DB: 空間嚴重不足 (${dbSize}MB)`);\n    } else if (dbSize > 5000) {\n      issues.push(`🟦 UAT DB: 空間警告 (${dbSize}MB)`);\n    }\n    \n    if (connCount > 80) {\n      issues.push(`🟦 UAT DB: 連線數過高 (${connCount})`);\n    }\n    \n    envStatus.push(`🟦 UAT: ${dbSize}MB, ${connCount} 連線`);\n  }\n}\n\n// 分析 GCP\nif (results[1]) {\n  const gcp = results[1].json;\n  if (gcp.error) {\n    issues.push('🟥 GCP DB: 無法連接');\n  } else {\n    const dbSize = gcp.database_size_mb || 0;\n    const connCount = gcp.connection_count || 0;\n    \n    if (dbSize > 8000) {\n      issues.push(`🟥 GCP DB: 空間嚴重不足 (${dbSize}MB)`);\n    } else if (dbSize > 5000) {\n      issues.push(`🟥 GCP DB: 空間警告 (${dbSize}MB)`);\n    }\n    \n    if (connCount > 80) {\n      issues.push(`🟥 GCP DB: 連線數過高 (${connCount})`);\n    }\n    \n    envStatus.push(`🟥 GCP: ${dbSize}MB, ${connCount} 連線`);\n  }\n}\n\nlet message = '';\nif (issues.length > 0) {\n  message = '🗄️ <b>資料庫健康告警</b>\\n\\n';\n  message += issues.join('\\n');\n  message += '\\n\\n📊 <b>狀態摘要</b>:\\n';\n  message += envStatus.join('\\n');\n  message += `\\n\\n⏰ <i>檢查時間: ${now}</i>`;\n}\n\nreturn [{\n  json: {\n    hasIssue: issues.length > 0,\n    message,\n    issues,\n    envStatus,\n    timestamp: now\n  }\n}];"
      },
      "id": "process",
      "name": "分析 DB 狀態",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [900, 300]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "check-issue",
              "leftValue": "={{ $json.hasIssue }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "if-issue",
      "name": "有問題?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [1120, 300]
    },
    {
      "parameters": {
        "url": "https://api.telegram.org/bot8075645931:AAH-EGKMo8ZC4QJs-Nc1_0s92xHrGdQvdpg/sendMessage",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"chat_id\": \"5619078117\",\n  \"parse_mode\": \"HTML\",\n  \"text\": \"{{ $json.message }}\"\n}",
        "options": {}
      },
      "id": "telegram",
      "name": "發送告警",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [1340, 200]
    }
  ],
  "connections": {
    "每 15 分鐘執行": {
      "main": [
        [
          { "node": "UAT DB 狀態", "type": "main", "index": 0 },
          { "node": "GCP DB 狀態", "type": "main", "index": 0 }
        ]
      ]
    },
    "UAT DB 狀態": {
      "main": [
        [{ "node": "合併結果", "type": "main", "index": 0 }]
      ]
    },
    "GCP DB 狀態": {
      "main": [
        [{ "node": "合併結果", "type": "main", "index": 1 }]
      ]
    },
    "合併結果": {
      "main": [
        [{ "node": "分析 DB 狀態", "type": "main", "index": 0 }]
      ]
    },
    "分析 DB 狀態": {
      "main": [
        [{ "node": "有問題?", "type": "main", "index": 0 }]
      ]
    },
    "有問題?": {
      "main": [
        [{ "node": "發送告警", "type": "main", "index": 0 }],
        []
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 0,
  "updatedAt": "2026-02-07T14:35:00.000Z",
  "versionId": "1"
}
