{
  "name": "Service Health Monitor",
  "version": "1.0.0",
  "description": "Monitor multiple services, aggregate health status, and alert on failures - demonstrates foreach loops, parallel execution, and conditional alerting",
  "input_schema": {
    "type": "object",
    "properties": {
      "service_registry_url": {
        "type": "string",
        "description": "URL of the service registry API"
      },
      "alert_threshold": {
        "type": "object",
        "properties": {
          "unhealthy_count": {
            "type": "integer",
            "default": 1
          },
          "healthy_percentage": {
            "type": "number",
            "default": 90
          }
        }
      }
    },
    "required": ["service_registry_url"]
  },
  "output_schema": {
    "type": "object",
    "properties": {
      "summary": { "type": "object" },
      "unhealthy_services": { "type": "array" },
      "alert_sent": { "type": "boolean" }
    }
  },
  "stages": [
    {
      "id": "fetch-services",
      "name": "Fetch Service Registry",
      "component_type": "http_request",
      "config": {
        "url": "{{input.service_registry_url}}/services",
        "method": "GET",
        "headers": {
          "Authorization": "Bearer {{secrets.REGISTRY_TOKEN}}"
        },
        "timeout": 10
      },
      "depends_on": [],
      "position": { "x": 100, "y": 100 }
    },
    {
      "id": "foreach-service",
      "name": "Check Each Service",
      "component_type": "foreach",
      "config": {
        "items": "{{upstream.fetch-services.body.services}}",
        "item_variable": "service",
        "stages": ["health-check", "evaluate-health"]
      },
      "depends_on": ["fetch-services"],
      "position": { "x": 100, "y": 200 }
    },
    {
      "id": "health-check",
      "name": "Health Check",
      "component_type": "http_request",
      "config": {
        "url": "{{service.health_endpoint}}",
        "method": "GET",
        "timeout": 5
      },
      "depends_on": ["foreach-service"],
      "position": { "x": 350, "y": 200 }
    },
    {
      "id": "evaluate-health",
      "name": "Evaluate Health Status",
      "component_type": "json_transform",
      "config": {
        "data": {
          "service_name": "{{service.name}}",
          "endpoint": "{{service.health_endpoint}}",
          "status_code": "{{upstream.health-check.status_code}}",
          "response_time_ms": "{{upstream.health-check.duration_ms}}",
          "body": "{{upstream.health-check.body}}"
        },
        "jmespath_expression": "{ service_name: service_name, endpoint: endpoint, status: (status_code == `200`) && 'healthy' || 'unhealthy', status_code: status_code, response_time_ms: response_time_ms }"
      },
      "depends_on": ["health-check"],
      "position": { "x": 350, "y": 300 }
    },
    {
      "id": "aggregate-results",
      "name": "Aggregate Health Results",
      "component_type": "json_transform",
      "config": {
        "data": "{{upstream.foreach-service.results}}",
        "jmespath_expression": "{ all_services: @, healthy: [?status == 'healthy'], unhealthy: [?status == 'unhealthy'], metrics: { total: length(@), healthy_count: length([?status == 'healthy']), unhealthy_count: length([?status == 'unhealthy']), avg_response_ms: avg([*].response_time_ms) } }"
      },
      "depends_on": ["foreach-service"],
      "position": { "x": 600, "y": 200 }
    },
    {
      "id": "analyze-health",
      "name": "AI Health Analysis",
      "component_type": "generator",
      "config": {
        "model": "gpt-4",
        "temperature": 0.3,
        "system_prompt": "You are an SRE analyzing service health data. Identify: 1) Critical issues, 2) Patterns suggesting systemic problems, 3) Recommended actions. Be concise.",
        "prompt": "Analyze this health report:\n\nTotal services: {{upstream.aggregate-results.result.metrics.total}}\nHealthy: {{upstream.aggregate-results.result.metrics.healthy_count}}\nUnhealthy: {{upstream.aggregate-results.result.metrics.unhealthy_count}}\n\nUnhealthy services:\n{{upstream.aggregate-results.result.unhealthy}}"
      },
      "depends_on": ["aggregate-results"],
      "position": { "x": 600, "y": 350 }
    },
    {
      "id": "check-alerts",
      "name": "Check Alert Threshold",
      "component_type": "conditional",
      "config": {
        "condition": "{{upstream.aggregate-results.result.metrics.unhealthy_count > 0}}",
        "if_true": ["send-alert"],
        "if_false": ["log-healthy"]
      },
      "depends_on": ["aggregate-results", "analyze-health"],
      "position": { "x": 600, "y": 450 }
    },
    {
      "id": "send-alert",
      "name": "Send Alert",
      "component_type": "http_request",
      "config": {
        "url": "{{secrets.PAGERDUTY_EVENTS_URL}}",
        "method": "POST",
        "body": {
          "routing_key": "{{secrets.PAGERDUTY_KEY}}",
          "event_action": "trigger",
          "payload": {
            "summary": "{{upstream.aggregate-results.result.metrics.unhealthy_count}} services unhealthy",
            "severity": "warning",
            "source": "flowmason-health-monitor",
            "custom_details": {
              "ai_analysis": "{{upstream.analyze-health.result}}",
              "unhealthy_services": "{{upstream.aggregate-results.result.unhealthy}}"
            }
          }
        }
      },
      "depends_on": ["check-alerts"],
      "position": { "x": 850, "y": 450 }
    },
    {
      "id": "log-healthy",
      "name": "Log All Healthy",
      "component_type": "logger",
      "config": {
        "message": "All services healthy",
        "level": "info",
        "data": "{{upstream.aggregate-results.result.metrics}}"
      },
      "depends_on": ["check-alerts"],
      "position": { "x": 350, "y": 450 }
    },
    {
      "id": "log-summary",
      "name": "Log Health Summary",
      "component_type": "logger",
      "config": {
        "message": "Health check complete",
        "level": "info",
        "data": {
          "healthy": "{{upstream.aggregate-results.result.metrics.healthy_count}}",
          "unhealthy": "{{upstream.aggregate-results.result.metrics.unhealthy_count}}",
          "total": "{{upstream.aggregate-results.result.metrics.total}}"
        }
      },
      "depends_on": ["check-alerts"],
      "position": { "x": 600, "y": 550 }
    }
  ],
  "output_stage_id": "aggregate-results"
}
