{
  "id": "wfcb-circuit-breaker-reset-00000",
  "name": "WF-CB-circuit-breaker-reset",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "*/15 * * * *"
            }
          ]
        }
      },
      "id": "ccb00000-0000-0000-0000-000000000001",
      "name": "Schedule 15min",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE providers SET health = 'healthy', failure_count = 0, updated_at = NOW() WHERE health = 'degraded' AND updated_at < NOW() - INTERVAL '15 minutes'",
        "options": {}
      },
      "id": "ccb00000-0000-0000-0000-000000000002",
      "name": "Reset Degraded Providers",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        460,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE providers SET rpm_used = 0, last_rpm_reset = NOW() WHERE last_rpm_reset < NOW() - INTERVAL '1 minute'",
        "options": {}
      },
      "id": "ccb00000-0000-0000-0000-000000000003",
      "name": "Reset RPM Counters",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        680,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO provider_metrics (provider_id, window_start, window_end, window_minutes, total_requests, successful, rate_limited, errors, timeouts, avg_latency_ms, p95_latency_ms, failover_count, total_cost_usd, total_input_tokens, total_output_tokens, json_parse_failures) SELECT provider_id, NOW() - INTERVAL '15 minutes' AS window_start, NOW() AS window_end, 15 AS window_minutes, COUNT(*) AS total_requests, COUNT(*) FILTER (WHERE status = 'success') AS successful, COUNT(*) FILTER (WHERE status = 'rate_limited') AS rate_limited, COUNT(*) FILTER (WHERE status = 'error') AS errors, COUNT(*) FILTER (WHERE status = 'timeout') AS timeouts, AVG(latency_ms)::INTEGER AS avg_latency_ms, PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY latency_ms)::INTEGER AS p95_latency_ms, 0 AS failover_count, SUM(cost_usd) AS total_cost_usd, SUM(input_tokens) AS total_input_tokens, SUM(output_tokens) AS total_output_tokens, 0 AS json_parse_failures FROM provider_usage_logs WHERE created_at >= NOW() - INTERVAL '15 minutes' GROUP BY provider_id",
        "options": {}
      },
      "id": "ccb00000-0000-0000-0000-000000000004",
      "name": "Emit Provider Metrics",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        900,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 0 * * *"
            }
          ]
        }
      },
      "id": "ccb00000-0000-0000-0000-000000000005",
      "name": "Schedule Daily Midnight UTC",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        240,
        500
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE providers SET daily_used = 0, last_day_reset = NOW()",
        "options": {}
      },
      "id": "ccb00000-0000-0000-0000-000000000006",
      "name": "Reset Daily Usage",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        460,
        500
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Schedule 15min": {
      "main": [
        [
          {
            "node": "Reset Degraded Providers",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Reset Degraded Providers": {
      "main": [
        [
          {
            "node": "Reset RPM Counters",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Reset RPM Counters": {
      "main": [
        [
          {
            "node": "Emit Provider Metrics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Daily Midnight UTC": {
      "main": [
        [
          {
            "node": "Reset Daily Usage",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}