# SavePal Production Alert Rules
groups:
  - name: savepal-backend-alerts
    rules:
      # High Error Rate
      - alert: SavePalHighErrorRate
        expr: rate(http_requests_total{job="savepal-backend",status=~"5.."}[5m]) > 0.1
        for: 2m
        labels:
          severity: critical
          service: savepal-backend
        annotations:
          summary: "SavePal Backend high error rate"
          description: "SavePal Backend error rate is {{ $value | humanizePercentage }} for the last 5 minutes"

      # High Response Time
      - alert: SavePalHighResponseTime
        expr: histogram_quantile(0.95, rate(http_request_duration_seconds_bucket{job="savepal-backend"}[5m])) > 2
        for: 5m
        labels:
          severity: warning
          service: savepal-backend
        annotations:
          summary: "SavePal Backend high response time"
          description: "SavePal Backend 95th percentile response time is {{ $value }}s"

      # Service Down
      - alert: SavePalServiceDown
        expr: up{job="savepal-backend"} == 0
        for: 1m
        labels:
          severity: critical
          service: savepal-backend
        annotations:
          summary: "SavePal Backend service is down"
          description: "SavePal Backend service has been down for more than 1 minute"

      # High Memory Usage
      - alert: SavePalHighMemoryUsage
        expr: (container_memory_usage_bytes{pod=~"savepal-backend-.*"} / container_spec_memory_limit_bytes{pod=~"savepal-backend-.*"}) > 0.8
        for: 5m
        labels:
          severity: warning
          service: savepal-backend
        annotations:
          summary: "SavePal Backend high memory usage"
          description: "SavePal Backend memory usage is {{ $value | humanizePercentage }} of limit"

      # High CPU Usage
      - alert: SavePalHighCPUUsage
        expr: rate(container_cpu_usage_seconds_total{pod=~"savepal-backend-.*"}[5m]) > 0.8
        for: 5m
        labels:
          severity: warning
          service: savepal-backend
        annotations:
          summary: "SavePal Backend high CPU usage"
          description: "SavePal Backend CPU usage is {{ $value | humanizePercentage }}"

      # Database Connection Issues
      - alert: SavePalDatabaseConnectionIssues
        expr: mongodb_connections_current{job="mongodb"} > mongodb_connections_available{job="mongodb"} * 0.8
        for: 2m
        labels:
          severity: critical
          service: mongodb
        annotations:
          summary: "SavePal MongoDB connection pool exhaustion"
          description: "MongoDB connection pool is {{ $value | humanizePercentage }} full"

      # Redis Connection Issues
      - alert: SavePalRedisDown
        expr: up{job="redis"} == 0
        for: 1m
        labels:
          severity: critical
          service: redis
        annotations:
          summary: "SavePal Redis is down"
          description: "Redis service has been down for more than 1 minute"

      # High Redis Memory Usage
      - alert: SavePalRedisHighMemoryUsage
        expr: redis_memory_used_bytes{job="redis"} / redis_memory_max_bytes{job="redis"} > 0.8
        for: 5m
        labels:
          severity: warning
          service: redis
        annotations:
          summary: "SavePal Redis high memory usage"
          description: "Redis memory usage is {{ $value | humanizePercentage }} of maximum"

  - name: savepal-ai-alerts
    rules:
      # AI API Rate Limiting
      - alert: SavePalAIRateLimitHit
        expr: increase(ai_api_rate_limit_hits_total{job="savepal-backend"}[5m]) > 10
        for: 1m
        labels:
          severity: warning
          service: ai-apis
        annotations:
          summary: "SavePal AI API rate limits being hit"
          description: "AI API rate limits have been hit {{ $value }} times in the last 5 minutes"

      # AI API High Error Rate
      - alert: SavePalAIHighErrorRate
        expr: rate(ai_api_requests_total{job="savepal-backend",status="error"}[5m]) / rate(ai_api_requests_total{job="savepal-backend"}[5m]) > 0.1
        for: 3m
        labels:
          severity: critical
          service: ai-apis
        annotations:
          summary: "SavePal AI API high error rate"
          description: "AI API error rate is {{ $value | humanizePercentage }}"

      # AI Response Time
      - alert: SavePalAISlowResponse
        expr: histogram_quantile(0.95, rate(ai_api_duration_seconds_bucket{job="savepal-backend"}[5m])) > 10
        for: 5m
        labels:
          severity: warning
          service: ai-apis
        annotations:
          summary: "SavePal AI API slow response"
          description: "AI API 95th percentile response time is {{ $value }}s"

  - name: savepal-business-alerts
    rules:
      # Low User Registration Rate
      - alert: SavePalLowRegistrationRate
        expr: rate(user_registrations_total{job="savepal-backend"}[1h]) < 0.1
        for: 30m
        labels:
          severity: warning
          service: business-metrics
        annotations:
          summary: "SavePal low user registration rate"
          description: "User registration rate is {{ $value }} per hour"

      # High Cart Abandonment Rate
      - alert: SavePalHighCartAbandonmentRate
        expr: rate(cart_abandonments_total{job="savepal-backend"}[1h]) / rate(cart_creations_total{job="savepal-backend"}[1h]) > 0.8
        for: 15m
        labels:
          severity: warning
          service: business-metrics
        annotations:
          summary: "SavePal high cart abandonment rate"
          description: "Cart abandonment rate is {{ $value | humanizePercentage }}"

      # Low Conversion Rate
      - alert: SavePalLowConversionRate
        expr: rate(purchases_total{job="savepal-backend"}[1h]) / rate(product_views_total{job="savepal-backend"}[1h]) < 0.02
        for: 30m
        labels:
          severity: warning
          service: business-metrics
        annotations:
          summary: "SavePal low conversion rate"
          description: "Conversion rate is {{ $value | humanizePercentage }}"

  - name: savepal-infrastructure-alerts
    rules:
      # Kubernetes Pod Restart
      - alert: SavePalPodRestarting
        expr: increase(kube_pod_container_status_restarts_total{namespace="savepal-production"}[1h]) > 3
        for: 0m
        labels:
          severity: warning
          service: kubernetes
        annotations:
          summary: "SavePal pod restarting frequently"
          description: "Pod {{ $labels.pod }} in namespace {{ $labels.namespace }} has restarted {{ $value }} times in the last hour"

      # Kubernetes Node Not Ready
      - alert: SavePalNodeNotReady
        expr: kube_node_status_condition{condition="Ready",status="true"} == 0
        for: 5m
        labels:
          severity: critical
          service: kubernetes
        annotations:
          summary: "SavePal Kubernetes node not ready"
          description: "Node {{ $labels.node }} has been not ready for more than 5 minutes"

      # Disk Space Low
      - alert: SavePalDiskSpaceLow
        expr: (node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"}) < 0.1
        for: 5m
        labels:
          severity: critical
          service: infrastructure
        annotations:
          summary: "SavePal disk space low"
          description: "Disk space is {{ $value | humanizePercentage }} full on {{ $labels.instance }}"

      # Load Average High
      - alert: SavePalHighLoadAverage
        expr: node_load1 > 2
        for: 5m
        labels:
          severity: warning
          service: infrastructure
        annotations:
          summary: "SavePal high load average"
          description: "Load average is {{ $value }} on {{ $labels.instance }}"

  - name: savepal-security-alerts
    rules:
      # Too Many Failed Login Attempts
      - alert: SavePalTooManyFailedLogins
        expr: rate(auth_failed_attempts_total{job="savepal-backend"}[5m]) > 10
        for: 2m
        labels:
          severity: warning
          service: security
        annotations:
          summary: "SavePal too many failed login attempts"
          description: "{{ $value }} failed login attempts per second in the last 5 minutes"

      # Suspicious API Usage
      - alert: SavePalSuspiciousAPIUsage
        expr: rate(http_requests_total{job="savepal-backend",status="429"}[5m]) > 5
        for: 2m
        labels:
          severity: warning
          service: security
        annotations:
          summary: "SavePal suspicious API usage detected"
          description: "High rate of 429 responses: {{ $value }} per second"

      # SSL Certificate Expiry
      - alert: SavePalSSLCertificateExpiry
        expr: probe_ssl_earliest_cert_expiry - time() < 86400 * 7
        for: 0m
        labels:
          severity: critical
          service: security
        annotations:
          summary: "SavePal SSL certificate expiring soon"
          description: "SSL certificate for {{ $labels.instance }} expires in {{ $value | humanizeDuration }}"
