groups:
  # Real-time recording rules — evaluated every 60s (matches scrape interval)
  - name: recording_realtime
    interval: 60s
    rules:
      # Docker host resource ratios (used by DockerHostDiskHigh, DockerHostMemoryHigh alerts)
      - record: host:docker_host_disk_usage_ratio:ratio
        expr: docker_host_disk_used_bytes / docker_host_disk_total_bytes

      - record: host:docker_host_memory_usage_ratio:ratio
        expr: >
          docker_host_memory_used_bytes / docker_host_memory_total_bytes
          and docker_host_memory_total_bytes > 0

      # Docker container memory ratio (used by DockerContainerHighMemory alert)
      - record: container:docker_container_memory_usage_ratio:ratio
        expr: >
          docker_container_memory_usage_bytes / docker_container_memory_limit_bytes
          and docker_container_memory_limit_bytes > 0

      # PVE node disk ratio (used by PVENodeDiskHigh alert)
      - record: node:pve_node_disk_usage_ratio:ratio
        expr: pve_node_disk_used_bytes / pve_node_disk_total_bytes

      # PBS backup age in seconds (used by PBSBackupStale, PBSBackupMissing alerts)
      - record: job:pbs_backup_age_seconds:latest
        expr: >
          (time() - pbs_backup_last_success_timestamp)
          and pbs_backup_last_success_timestamp > 0

      # Exporter health summary — fraction of collectors reporting up
      - record: job:exporter_collectors_healthy_ratio:ratio
        expr: >
          sum(exporter_collector_up == 1) / count(exporter_collector_up)

      # PVE guest memory usage ratio
      - record: host:pve_guest_memory_usage_ratio:ratio
        expr: >
          pve_guest_mem_bytes / pve_guest_maxmem_bytes
          and pve_guest_maxmem_bytes > 0

      # PVE node memory usage ratio
      - record: host:pve_node_memory_usage_ratio:ratio
        expr: >
          pve_node_memory_used_bytes / pve_node_memory_total_bytes
          and pve_node_memory_total_bytes > 0

      # NAS ZFS pool usage ratio
      - record: host:nas_pool_usage_ratio:ratio
        expr: >
          nas_zpool_allocated_bytes / nas_zpool_size_bytes
          and nas_zpool_size_bytes > 0

  # Aggregation rules — evaluated every 5m for dashboard summaries
  - name: recording_aggregations
    interval: 5m
    rules:
      # Per-host container counts
      - record: host:docker_containers_running_ratio:ratio
        expr: >
          docker_host_containers_running / docker_host_containers_total
          and docker_host_containers_total > 0

      # Total containers across all hosts
      - record: cluster:docker_containers_total:sum
        expr: sum(docker_host_containers_total)

      - record: cluster:docker_containers_running:sum
        expr: sum(docker_host_containers_running)

      # PVE cluster-wide resource usage
      - record: cluster:pve_node_cpu_usage_ratio:avg
        expr: avg(pve_node_cpu_usage_ratio)

      - record: cluster:pve_node_memory_usage_ratio:avg
        expr: avg(pve_node_memory_usage_ratio)

      # NAS pool average temperature
      - record: nas:disk_temperature_celsius:avg
        expr: avg(nas_disk_temperature_celsius)

      - record: nas:disk_temperature_celsius:max
        expr: max(nas_disk_temperature_celsius)

      # Total Docker image disk usage across all hosts
      - record: cluster:docker_host_images_size_bytes:sum
        expr: sum(docker_host_images_size_bytes)

      # TLS certificate health (used by dashboard cert panel)
      - record: job:cert_expiry_days_min:min
        expr: min(cert_expiry_days)

      - record: job:cert_invalid_count:count
        expr: count(cert_valid == 0)

      # Media service availability (used by dashboard media panel)
      - record: cluster:media_service_up:sum
        expr: sum(media_service_up)

      - record: cluster:media_service_total:count
        expr: count(media_service_up)

      # App health aggregations (used by application-health dashboard)
      - record: job:app_health_up_ratio:ratio
        expr: >
          sum(app_health_up) / count(app_health_up)
          and count(app_health_up) > 0

      - record: job:app_health_response_time_avg:seconds
        expr: avg(app_health_response_time_seconds)

      # Docker container health aggregations (used by docker-hosts dashboard)
      - record: cluster:docker_containers_healthy:sum
        expr: sum(docker_container_health_status{status="healthy"})

      - record: cluster:docker_containers_unhealthy:sum
        expr: sum(docker_container_health_status{status="unhealthy"})

      # Proxy health summary (used by application-health dashboard)
      - record: job:proxy_health_wan:status
        expr: proxy_health_status{path="wan"}

      - record: job:proxy_health_tailscale:status
        expr: proxy_health_status{path="tailscale"}
