{
  "_comment": "Workflow: NGO.tools KB Status | n8n ID: 0NWL8G87JV7AylSf | Webhook: /webhook/ngo-tools-kb-status (GET)",
  "name": "NGO.tools KB Status",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "GET",
        "path": "ngo-tools-kb-status",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Webhook Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -200,
        0
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT (SELECT COUNT(*) FROM n8n_vectors_ngo_tools) as total_chunks, (SELECT COUNT(DISTINCT metadata->>'source') FROM n8n_vectors_ngo_tools) as unique_sources, (SELECT COUNT(DISTINCT metadata->>'doc_type') FROM n8n_vectors_ngo_tools) as doc_types"
      },
      "id": "summary-query",
      "name": "Summary Query",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        40,
        0
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT metadata->>'title' as title, metadata->>'source' as source, metadata->>'doc_type' as doc_type, MAX(metadata->>'ingested_at') as ingested_at, COUNT(*) as chunks FROM n8n_vectors_ngo_tools GROUP BY metadata->>'title', metadata->>'source', metadata->>'doc_type' ORDER BY MAX(metadata->>'ingested_at') DESC NULLS LAST"
      },
      "id": "detail-query",
      "name": "Detail Query",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        280,
        0
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const summary = $('Summary Query').first().json;\nconst details = $('Detail Query').all().map(item => item.json);\n\nconst byType = {};\nfor (const d of details) {\n  const type = d.doc_type || 'unknown';\n  if (!byType[type]) byType[type] = { count: 0, sources: [] };\n  byType[type].count += parseInt(d.chunks) || 0;\n  byType[type].sources.push({ title: d.title, source: d.source, chunks: parseInt(d.chunks) || 0, ingested_at: d.ingested_at || null });\n}\n\nreturn [{ json: { total_chunks: parseInt(summary.total_chunks) || 0, unique_sources: parseInt(summary.unique_sources) || 0, doc_types: parseInt(summary.doc_types) || 0, by_type: byType, details: details } }];"
      },
      "id": "combine-results",
      "name": "Combine Results",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        520,
        0
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify($json) }}"
      },
      "id": "respond",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        760,
        0
      ]
    }
  ],
  "connections": {
    "Webhook Trigger": {
      "main": [
        [
          {
            "node": "Summary Query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Summary Query": {
      "main": [
        [
          {
            "node": "Detail Query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Detail Query": {
      "main": [
        [
          {
            "node": "Combine Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Combine Results": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "callerPolicy": "workflowsFromSameOwner",
    "availableInMCP": false
  },
  "tags": []
}