{
  "name": "50_542_STATUS_REPORT",
  "nodes": [
    {
      "id": "542-trigger",
      "name": "When Called by Another Workflow",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1,
      "position": [
        250,
        400
      ],
      "parameters": {}
    },
    {
      "id": "542-fetch-workflows",
      "name": "Fetch n8n Workflows",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        480,
        160
      ],
      "parameters": {
        "method": "GET",
        "url": "http://10.0.1.16:5678/api/v1/workflows",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-N8N-API-KEY",
              "value": "={{ $env.N8N_API_KEY }}"
            }
          ]
        },
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "limit",
              "value": "100"
            }
          ]
        },
        "options": {
          "timeout": 8000
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "542-fetch-executions",
      "name": "Fetch n8n Executions",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        480,
        300
      ],
      "parameters": {
        "method": "GET",
        "url": "http://10.0.1.16:5678/api/v1/executions",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-N8N-API-KEY",
              "value": "={{ $env.N8N_API_KEY }}"
            }
          ]
        },
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "limit",
              "value": "50"
            }
          ]
        },
        "options": {
          "timeout": 8000
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "542-fetch-alerts",
      "name": "Fetch Prometheus Alerts",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        480,
        440
      ],
      "parameters": {
        "method": "GET",
        "url": "http://10.0.1.15:9090/api/v1/alerts",
        "authentication": "none",
        "options": {
          "timeout": 8000
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "542-fetch-crews",
      "name": "Fetch Crew API",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        480,
        580
      ],
      "parameters": {
        "method": "GET",
        "url": "http://s0k444ck0w4cgc400skwkos0.<HETZNER_HOST>.sslip.io/crews/",
        "authentication": "none",
        "options": {
          "timeout": 10000
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "542-fetch-pieces",
      "name": "Fetch Content Pieces",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        480,
        720
      ],
      "parameters": {
        "method": "GET",
        "url": "https://directus.automation-plus-ki.de/items/330_knowledge_items",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer ={{ $env.DIRECTUS_TOKEN }}"
            }
          ]
        },
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "limit",
              "value": "20"
            },
            {
              "name": "sort",
              "value": "-created_at"
            }
          ]
        },
        "options": {
          "timeout": 8000
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "542-format",
      "name": "Format Status Report",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        820,
        440
      ],
      "parameters": {
        "jsCode": "const chatId = $('When Called by Another Workflow').first().json.chatId;\n\n// \u2500\u2500 n8n Workflows \u2500\u2500\nlet workflows = [];\ntry {\n  const wfData = $('Fetch n8n Workflows').first().json;\n  workflows = wfData?.data ?? wfData?.workflows ?? [];\n} catch(e) {}\nconst totalWF = workflows.length;\nconst activeWF = workflows.filter(w => w.active).length;\n\n// \u2500\u2500 n8n Executions \u2500\u2500\nlet executions = [];\ntry {\n  const exData = $('Fetch n8n Executions').first().json;\n  executions = exData?.data ?? exData?.executions ?? [];\n} catch(e) {}\nconst now = Date.now();\nconst recent24h = executions.filter(e => (now - new Date(e.startedAt ?? e.created_at ?? 0).getTime()) < 86400000).length;\nconst failedCount = executions.filter(e => e.status === 'error' || e.status === 'failed').length;\nconst successCount = executions.filter(e => e.status === 'success').length;\n\n// \u2500\u2500 Prometheus Alerts \u2500\u2500\nlet firingAlerts = 0;\nlet alertNames = [];\ntry {\n  const alertData = $('Fetch Prometheus Alerts').first().json;\n  const alerts = alertData?.data?.alerts ?? [];\n  const firing = alerts.filter(a => a.state === 'firing');\n  firingAlerts = firing.length;\n  alertNames = firing.map(a => a.labels?.alertname ?? 'Unknown').slice(0, 3);\n} catch(e) {}\n\n// \u2500\u2500 Crew API \u2500\u2500\nlet crews = [];\ntry {\n  const crewData = $('Fetch Crew API').first().json;\n  crews = Array.isArray(crewData) ? crewData : [];\n} catch(e) {}\nconst totalAgents = crews.reduce((s, c) => s + (c.agents ?? 0), 0);\n\n// \u2500\u2500 Content Pieces \u2500\u2500\nlet pieces = [];\ntry {\n  const pData = $('Fetch Content Pieces').first().json;\n  pieces = pData?.data ?? [];\n} catch(e) {}\nconst draftCount = pieces.filter(p => p.status === 'draft').length;\nconst publishedCount = pieces.filter(p => p.status === 'published').length;\n\n// \u2500\u2500 Status Icon \u2500\u2500\nconst statusIcon = firingAlerts > 0 ? '\ud83d\udd34' : failedCount > 3 ? '\ud83d\udfe1' : '\ud83d\udfe2';\nconst statusText = firingAlerts > 0 ? 'Alarm' : failedCount > 3 ? 'Warnung' : 'OK';\n\nconst ts = new Date().toLocaleString('de-DE', { timeZone: 'Europe/Berlin', hour: '2-digit', minute: '2-digit', day: '2-digit', month: '2-digit' });\n\nlet report = `${statusIcon} *AIOS System Status* \u2014 ${statusText}\\n`;\nreport += `_Stand: ${ts} Uhr_\\n\\n`;\n\nreport += `*n8n Workflows:*\\n`;\nreport += `\u2022 ${activeWF} aktiv | ${totalWF - activeWF} pausiert | ${totalWF} gesamt\\n\\n`;\n\nreport += `*Ausf\u00fchrungen (letzte 50):*\\n`;\nreport += `\u2022 24h: ${recent24h} | \u2705 ${successCount} | \u274c ${failedCount}\\n\\n`;\n\nreport += `*Crew AI:*\\n`;\nif (crews.length > 0) {\n  report += `\u2022 ${crews.length} Crews | ${totalAgents} Agents | ${pieces.length} Pieces\\n`;\n  report += `\u2022 Drafts: ${draftCount} | Published: ${publishedCount}\\n`;\n} else {\n  report += `\u2022 Crew API nicht erreichbar\\n`;\n}\nreport += `\\n`;\n\nreport += `*Prometheus:*\\n`;\nif (firingAlerts === 0) {\n  report += `\u2022 \u2705 Keine feuernden Alerts\\n`;\n} else {\n  report += `\u2022 \ud83d\udd34 ${firingAlerts} Alert(s):\\n`;\n  alertNames.forEach(name => { report += `  - ${name}\\n`; });\n}\n\nreturn [{ json: { chatId, response: report } }];"
      }
    }
  ],
  "connections": {
    "When Called by Another Workflow": {
      "main": [
        [
          {
            "node": "Fetch n8n Workflows",
            "type": "main",
            "index": 0
          },
          {
            "node": "Fetch n8n Executions",
            "type": "main",
            "index": 0
          },
          {
            "node": "Fetch Prometheus Alerts",
            "type": "main",
            "index": 0
          },
          {
            "node": "Fetch Crew API",
            "type": "main",
            "index": 0
          },
          {
            "node": "Fetch Content Pieces",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch n8n Workflows": {
      "main": [
        [
          {
            "node": "Format Status Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch n8n Executions": {
      "main": [
        [
          {
            "node": "Format Status Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Prometheus Alerts": {
      "main": [
        [
          {
            "node": "Format Status Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Crew API": {
      "main": [
        [
          {
            "node": "Format Status Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Content Pieces": {
      "main": [
        [
          {
            "node": "Format Status Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveDataErrorExecution": "all",
    "saveDataSuccessExecution": "none"
  },
  "staticData": null,
  "tags": [
    "telegram",
    "monitoring"
  ],
  "meta": {
    "description": "542 \u2014 Sub-Workflow: System-Status-Report f\u00fcr Telegram (n8n + Crew API + Content Pieces + Prometheus)"
  }
}