{
  "name": "07 · Data & Analytics / Metric Anomaly Spike Detector",
  "active": false,
  "nodes": [
    {
      "id": "cc8d94e1-29b8-43db-9bdd-f7cc0ac26286",
      "name": "📘 Guide",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -640,
        -120
      ],
      "parameters": {
        "content": "## 07 · Data & Analytics · Metric Anomaly Spike Detector\n\n### What this does\nCompares current KPIs vs rolling baseline and alerts on z-score spikes.\n\n### Setup checklist\n1. Metrics API\n2. Alert channel\n3. Sensitivity in Code\n\n### How to test locally\nPOST `data-anomaly` with time series points.\n\n### Tips\n- Keep credentials in n8n Credentials (never hardcode secrets).\n- Use **Test URL** on the Webhook while developing.\n- Activate only after credentials + destinations are configured.\n",
        "width": 560,
        "height": 560,
        "color": 7
      }
    },
    {
      "id": "742fca20-d5da-4b60-9153-ffdae388562f",
      "name": "⚙️ Config",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -640,
        480
      ],
      "parameters": {
        "content": "## Config map\n\n- **Webhook path:** `data-anomaly`\n- **Replace** all `https://httpbin.org/...` calls with real integrations.\n- **Credentials:** create in n8n → Credentials, then select on HTTP nodes.\n- **Activate** workflow only after a successful Test execution.\n\n### Local files\nShared host folder is mounted at `/files` inside n8n.",
        "width": 560,
        "height": 320,
        "color": 2
      }
    },
    {
      "id": "a4c0ceab-594f-49e7-b5c4-b3eaecce1039",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        200
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 60
            }
          ]
        }
      }
    },
    {
      "id": "feafc788-0a36-4760-a6d1-e3c7c48350a1",
      "name": "Pull Records",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        280,
        200
      ],
      "parameters": {
        "method": "GET",
        "url": "https://httpbin.org/get",
        "options": {}
      }
    },
    {
      "id": "cb048149-8080-4081-be76-76b26a7cce30",
      "name": "Triage & Score",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        560,
        200
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const now = Date.now();\nconst sample = [\n  { id: 'A1', ageHours: 52, severity: 'high', owner: 'ada' },\n  { id: 'B2', ageHours: 5, severity: 'low', owner: 'lin' },\n  { id: 'C3', ageHours: 30, severity: 'medium', owner: 'sam' },\n];\nconst flagged = sample\n  .map(r => ({ ...r, breach: r.ageHours > 24 || r.severity === 'high', score: r.ageHours + (r.severity==='high'?50:0) }))\n  .filter(r => r.breach)\n  .sort((a,b) => b.score - a.score);\nreturn flagged.length\n  ? flagged.map(f => ({ json: { workflow: 'Metric Anomaly Spike Detector', ...f, checkedAt: new Date(now).toISOString() } }))\n  : [{ json: { workflow: 'Metric Anomaly Spike Detector', ok: true, flagged: 0 } }];"
      }
    },
    {
      "id": "e475f57f-eec6-4867-855b-69f048c3ffce",
      "name": "Needs Alert?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        840,
        200
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "b2155537-6c52-43d7-82f6-3595ebd18035",
              "leftValue": "={{ $json.breach === true }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "32578c47-e349-437b-adcd-82184974c58f",
      "name": "Send Alert",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        80
      ],
      "parameters": {
        "method": "POST",
        "url": "https://httpbin.org/post",
        "options": {},
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json) }}"
      }
    },
    {
      "id": "59ba5b9d-4de8-43e5-b331-ccebb7ce4b21",
      "name": "All Clear",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1120,
        320
      ],
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "1",
              "name": "status",
              "value": "healthy",
              "type": "string"
            }
          ]
        },
        "options": {}
      }
    }
  ],
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Pull Records",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pull Records": {
      "main": [
        [
          {
            "node": "Triage & Score",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Triage & Score": {
      "main": [
        [
          {
            "node": "Needs Alert?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Needs Alert?": {
      "main": [
        [
          {
            "node": "Send Alert",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "All Clear",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "callerPolicy": "workflowsFromSameOwner"
  },
  "tags": [],
  "meta": {
    "templateCredsSetupCompleted": true,
    "description": "Compares current KPIs vs rolling baseline and alerts on z-score spikes."
  },
  "pinData": {}
}