{
  "name": "Vorlux AI | Log Analyzer",
  "active": false,
  "nodes": [
    { "parameters": { "rule": { "interval": [{ "field": "hours", "hoursInterval": 4 }] } }, "id": "sched", "name": "Every 4 Hours", "type": "n8n-nodes-base.scheduleTrigger", "typeVersion": 1.2, "position": [220, 300] },
    { "parameters": { "url": "http://host.docker.internal:3010/api/logs", "method": "GET", "qs": { "level": "error", "period": "4h" }, "options": { "timeout": 15000 } }, "id": "fetch-logs", "name": "Fetch Error Logs", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [460, 300] },
    { "parameters": { "mode": "runOnceForAllItems", "jsCode": "const logs = $input.first().json.data || $input.first().json.logs || [];\nconst grouped = {};\nlogs.forEach(l => {\n  const key = l.message?.substring(0, 80) || 'unknown';\n  if (!grouped[key]) grouped[key] = { message: key, count: 0, firstSeen: l.timestamp, lastSeen: l.timestamp };\n  grouped[key].count++;\n  grouped[key].lastSeen = l.timestamp;\n});\nconst patterns = Object.values(grouped).sort((a, b) => b.count - a.count);\nconst critical = patterns.filter(p => p.count >= 5);\nreturn [{ json: { totalErrors: logs.length, uniquePatterns: patterns.length, criticalPatterns: critical.length, topErrors: patterns.slice(0, 10), needsIncident: critical.length > 0 } }];" }, "id": "analyze", "name": "Analyze Patterns", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [700, 300] },
    { "parameters": { "url": "http://host.docker.internal:3010/api/incidents", "method": "POST", "sendBody": true, "specifyBody": "json", "jsonBody": "={{ $json.needsIncident ? JSON.stringify({ type: 'log-analysis', severity: 'medium', title: 'Recurring errors detected', data: { patterns: $json.topErrors.slice(0, 3) } }) : '{}' }}", "options": { "timeout": 10000 } }, "id": "create-incident", "name": "Create Incident If Needed", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [960, 200] },
    { "parameters": { "method": "POST", "url": "={{$env.DISCORD_OPS_WEBHOOK}}", "sendBody": true, "specifyBody": "json", "jsonBody": "={{ JSON.stringify({ content: '📋 **Log Analysis (4h)**\\nErrors: ' + $json.totalErrors + '\\nUnique Patterns: ' + $json.uniquePatterns + '\\nCritical: ' + $json.criticalPatterns + ($json.needsIncident ? '\\n⚠️ Incident created!' : '') }) }}" }, "id": "notify", "name": "Discord Notify", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [960, 400] }
  ],
  "connections": {
    "Every 4 Hours": { "main": [[{ "node": "Fetch Error Logs", "type": "main", "index": 0 }]] },
    "Fetch Error Logs": { "main": [[{ "node": "Analyze Patterns", "type": "main", "index": 0 }]] },
    "Analyze Patterns": { "main": [[{ "node": "Create Incident If Needed", "type": "main", "index": 0 }, { "node": "Discord Notify", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" }
}
