# ==============================================================================
# ADTLAS PROMETHEUS RECORDING RULES
# ==============================================================================
# Pre-calculated metrics for improved dashboard performance and alerting

groups:
  # ==========================================================================
  # APPLICATION PERFORMANCE RECORDING RULES
  # ==========================================================================
  - name: adtlas_application_performance
    interval: 30s
    rules:
      # HTTP request rate by status code
      - record: adtlas:django_http_requests_per_second
        expr: rate(django_http_responses_total_by_status[1m])
        labels:
          service: django

      # HTTP error rate percentage
      - record: adtlas:django_http_error_rate
        expr: |
          (
            rate(django_http_responses_total_by_status{status=~"4..|5.."}[5m]) /
            rate(django_http_responses_total_by_status[5m])
          ) * 100
        labels:
          service: django

      # Average response time
      - record: adtlas:django_http_request_duration_avg
        expr: rate(django_http_request_duration_seconds_sum[5m]) / rate(django_http_request_duration_seconds_count[5m])
        labels:
          service: django

      # Request throughput (requests per minute)
      - record: adtlas:django_http_requests_per_minute
        expr: rate(django_http_responses_total_by_status[1m]) * 60
        labels:
          service: django

  # ==========================================================================
  # DATABASE PERFORMANCE RECORDING RULES
  # ==========================================================================
  - name: adtlas_database_performance
    interval: 30s
    rules:
      # Database connection utilization percentage
      - record: adtlas:postgresql_connection_utilization
        expr: |
          (
            pg_stat_database_numbackends /
            pg_settings_max_connections
          ) * 100
        labels:
          service: postgresql

      # Database query rate
      - record: adtlas:postgresql_queries_per_second
        expr: rate(pg_stat_database_xact_commit[1m]) + rate(pg_stat_database_xact_rollback[1m])
        labels:
          service: postgresql

      # Database cache hit ratio
      - record: adtlas:postgresql_cache_hit_ratio
        expr: |
          (
            pg_stat_database_blks_hit /
            (pg_stat_database_blks_hit + pg_stat_database_blks_read)
          ) * 100
        labels:
          service: postgresql

      # Database size growth rate (bytes per hour)
      - record: adtlas:postgresql_size_growth_rate
        expr: rate(pg_database_size_bytes[1h])
        labels:
          service: postgresql

  # ==========================================================================
  # REDIS PERFORMANCE RECORDING RULES
  # ==========================================================================
  - name: adtlas_redis_performance
    interval: 30s
    rules:
      # Redis memory utilization percentage
      - record: adtlas:redis_memory_utilization
        expr: |
          (
            redis_memory_used_bytes /
            redis_memory_max_bytes
          ) * 100
        labels:
          service: redis

      # Redis operations per second
      - record: adtlas:redis_ops_per_second
        expr: rate(redis_commands_processed_total[1m])
        labels:
          service: redis

      # Redis cache hit ratio
      - record: adtlas:redis_cache_hit_ratio
        expr: |
          (
            rate(redis_keyspace_hits_total[5m]) /
            (rate(redis_keyspace_hits_total[5m]) + rate(redis_keyspace_misses_total[5m]))
          ) * 100
        labels:
          service: redis

      # Redis evicted keys rate
      - record: adtlas:redis_evicted_keys_per_second
        expr: rate(redis_evicted_keys_total[1m])
        labels:
          service: redis

  # ==========================================================================
  # CELERY PERFORMANCE RECORDING RULES
  # ==========================================================================
  - name: adtlas_celery_performance
    interval: 30s
    rules:
      # Celery task processing rate
      - record: adtlas:celery_tasks_per_second
        expr: rate(celery_task_total[1m])
        labels:
          service: celery

      # Celery task failure rate percentage
      - record: adtlas:celery_task_failure_rate
        expr: |
          (
            rate(celery_task_failed_total[5m]) /
            rate(celery_task_total[5m])
          ) * 100
        labels:
          service: celery

      # Average task execution time
      - record: adtlas:celery_task_duration_avg
        expr: rate(celery_task_duration_seconds_sum[5m]) / rate(celery_task_duration_seconds_count[5m])
        labels:
          service: celery

      # Celery worker utilization
      - record: adtlas:celery_worker_utilization
        expr: |
          (
            celery_active_tasks /
            celery_worker_pool_size
          ) * 100
        labels:
          service: celery

  # ==========================================================================
  # INFRASTRUCTURE PERFORMANCE RECORDING RULES
  # ==========================================================================
  - name: adtlas_infrastructure_performance
    interval: 30s
    rules:
      # CPU utilization percentage
      - record: adtlas:node_cpu_utilization
        expr: |
          100 - (
            avg by (instance) (
              irate(node_cpu_seconds_total{mode="idle"}[5m])
            ) * 100
          )
        labels:
          service: system

      # Memory utilization percentage
      - record: adtlas:node_memory_utilization
        expr: |
          (
            (node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) /
            node_memory_MemTotal_bytes
          ) * 100
        labels:
          service: system

      # Disk utilization percentage
      - record: adtlas:node_disk_utilization
        expr: |
          (
            (node_filesystem_size_bytes - node_filesystem_avail_bytes) /
            node_filesystem_size_bytes
          ) * 100
        labels:
          service: system

      # Network I/O rate (bytes per second)
      - record: adtlas:node_network_io_bytes_per_second
        expr: rate(node_network_receive_bytes_total[1m]) + rate(node_network_transmit_bytes_total[1m])
        labels:
          service: system

      # Disk I/O rate (operations per second)
      - record: adtlas:node_disk_io_ops_per_second
        expr: rate(node_disk_reads_completed_total[1m]) + rate(node_disk_writes_completed_total[1m])
        labels:
          service: system

  # ==========================================================================
  # NGINX PERFORMANCE RECORDING RULES
  # ==========================================================================
  - name: adtlas_nginx_performance
    interval: 30s
    rules:
      # Nginx request rate
      - record: adtlas:nginx_requests_per_second
        expr: rate(nginx_http_requests_total[1m])
        labels:
          service: nginx

      # Nginx error rate percentage
      - record: adtlas:nginx_error_rate
        expr: |
          (
            rate(nginx_http_requests_total{status=~"4..|5.."}[5m]) /
            rate(nginx_http_requests_total[5m])
          ) * 100
        labels:
          service: nginx

      # Nginx active connections
      - record: adtlas:nginx_active_connections
        expr: nginx_connections_active
        labels:
          service: nginx

  # ==========================================================================
  # BUSINESS METRICS RECORDING RULES
  # ==========================================================================
  - name: adtlas_business_metrics
    interval: 60s
    rules:
      # Ad delivery success rate
      - record: adtlas:ad_delivery_success_rate
        expr: |
          (
            rate(adtlas_ad_delivery_success_total[5m]) /
            rate(adtlas_ad_delivery_total[5m])
          ) * 100
        labels:
          service: business

      # Revenue per minute
      - record: adtlas:revenue_per_minute
        expr: rate(adtlas_revenue_total[1m]) * 60
        labels:
          service: business

      # Active campaigns count
      - record: adtlas:active_campaigns_count
        expr: adtlas_campaigns_active_total
        labels:
          service: business

      # Average campaign performance score
      - record: adtlas:campaign_performance_avg
        expr: avg(adtlas_campaign_performance_score)
        labels:
          service: business

  # ==========================================================================
  # CONTAINER PERFORMANCE RECORDING RULES
  # ==========================================================================
  - name: adtlas_container_performance
    interval: 30s
    rules:
      # Container CPU utilization
      - record: adtlas:container_cpu_utilization
        expr: |
          (
            rate(container_cpu_usage_seconds_total[5m]) /
            container_spec_cpu_quota * container_spec_cpu_period
          ) * 100
        labels:
          service: docker

      # Container memory utilization
      - record: adtlas:container_memory_utilization
        expr: |
          (
            container_memory_usage_bytes /
            container_spec_memory_limit_bytes
          ) * 100
        labels:
          service: docker

      # Container network I/O rate
      - record: adtlas:container_network_io_bytes_per_second
        expr: rate(container_network_receive_bytes_total[1m]) + rate(container_network_transmit_bytes_total[1m])
        labels:
          service: docker