59 lines
4.9 KiB
JSON
59 lines
4.9 KiB
JSON
{
|
|
"name": "Google Drive 自動匯入監控",
|
|
"nodes": [
|
|
{
|
|
"parameters": { "rule": { "interval": [{ "field": "hours", "hoursInterval": 1 }] } },
|
|
"id": "schedule", "name": "每小時檢查", "type": "n8n-nodes-base.scheduleTrigger", "typeVersion": 1.1, "position": [0, 0]
|
|
},
|
|
{
|
|
"parameters": {
|
|
"url": "http://192.168.0.110:5001/api/import_jobs",
|
|
"options": { "timeout": 15000 }
|
|
},
|
|
"id": "http-jobs", "name": "取得匯入任務", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.1, "position": [220, 0],
|
|
"onError": "continueRegularOutput"
|
|
},
|
|
{
|
|
"parameters": {
|
|
"jsCode": "const response = $input.first().json;\nconst jobs = response.data || response.jobs || [];\n\n// 取得最新的任務\nconst latestJob = jobs[0];\nif (!latestJob) {\n return [{ json: { hasIssue: false, message: '無匯入任務記錄' } }];\n}\n\nconst now = new Date();\nconst jobTime = new Date(latestJob.created_at);\nconst hoursSinceLastJob = (now - jobTime) / (1000 * 60 * 60);\n\nlet hasIssue = false;\nlet issueType = '';\nlet message = '';\n\n// 檢查 1: 任務失敗\nif (latestJob.status === 'failed') {\n hasIssue = true;\n issueType = 'failed';\n message = `最新匯入任務失敗\\n錯誤: ${latestJob.error_message || '未知'}`;\n}\n// 檢查 2: 任務卡住 (超過 30 分鐘還在 importing)\nelse if (latestJob.status === 'importing') {\n const startTime = new Date(latestJob.started_at);\n const minsSinceStart = (now - startTime) / (1000 * 60);\n if (minsSinceStart > 30) {\n hasIssue = true;\n issueType = 'stuck';\n message = `匯入任務卡住 (${Math.round(minsSinceStart)} 分鐘)\\n進度: ${latestJob.progress_percent}%`;\n }\n}\n// 檢查 3: 超過 2 小時沒有新任務\nelse if (hoursSinceLastJob > 2 && latestJob.status === 'completed') {\n // 這可能是正常的,因為沒有新檔案\n // 只有在工作日 9:00-18:00 才告警\n const hour = now.getHours();\n const day = now.getDay();\n if (day >= 1 && day <= 5 && hour >= 9 && hour <= 18) {\n // 不告警,只記錄\n hasIssue = false;\n }\n}\n\nreturn [{ json: { \n hasIssue, \n issueType,\n message,\n latestJob: {\n id: latestJob.id,\n status: latestJob.status,\n file: latestJob.drive_file_name,\n progress: latestJob.progress_percent,\n created: latestJob.created_at\n }\n} }];"
|
|
},
|
|
"id": "check-status", "name": "檢查狀態", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [440, 0]
|
|
},
|
|
{
|
|
"parameters": {
|
|
"conditions": {
|
|
"options": { "caseSensitive": true },
|
|
"conditions": [
|
|
{ "id": "c1", "leftValue": "={{ $json.hasIssue }}", "rightValue": true, "operator": { "type": "boolean", "operation": "equals" } }
|
|
],
|
|
"combinator": "and"
|
|
}
|
|
},
|
|
"id": "if-issue", "name": "有問題?", "type": "n8n-nodes-base.if", "typeVersion": 2, "position": [660, 0]
|
|
},
|
|
{
|
|
"parameters": {
|
|
"jsCode": "const data = $input.first().json;\nconst time = new Date().toLocaleString('zh-TW', {timeZone: 'Asia/Taipei'});\n\nlet emoji = '⚠️';\nif (data.issueType === 'failed') emoji = '❌';\nif (data.issueType === 'stuck') emoji = '🔄';\n\nconst text = emoji + ' *Google Drive 自動匯入異常*\\n\\n' +\n '⏰ 時間: ' + time + '\\n' +\n '📄 檔案: ' + (data.latestJob?.file || 'N/A') + '\\n' +\n '📊 狀態: ' + (data.latestJob?.status || 'N/A') + '\\n' +\n '📈 進度: ' + (data.latestJob?.progress || 0) + '%\\n\\n' +\n '💬 ' + data.message + '\\n\\n' +\n '🔗 查看: http://192.168.0.110:5001/auto_import';\n\nreturn [{ json: { telegramBody: { chat_id: '5619078117', text: text, parse_mode: 'Markdown' } } }];"
|
|
},
|
|
"id": "code-alert", "name": "組合告警訊息", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [880, 100]
|
|
},
|
|
{
|
|
"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": [1100, 100]
|
|
},
|
|
{ "parameters": {}, "id": "no-op", "name": "匯入正常", "type": "n8n-nodes-base.noOp", "typeVersion": 1, "position": [880, -100] }
|
|
],
|
|
"connections": {
|
|
"每小時檢查": { "main": [[{ "node": "取得匯入任務", "type": "main", "index": 0 }]] },
|
|
"取得匯入任務": { "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": "發送告警", "type": "main", "index": 0 }]] }
|
|
},
|
|
"settings": { "executionOrder": "v1" }
|
|
}
|