# ==============================================================================
# ADTLAS DAI MANAGEMENT SYSTEM - BUSINESS LOGIC ALERTING RULES
# ==============================================================================
#
# This file contains Prometheus alerting rules specifically for monitoring
# business logic and domain-specific metrics of the Adtlas DAI Management System.
#
# Categories:
# - Campaign Management
# - Channel Operations
# - VAST Server Performance
# - Playlist Generation
# - Ad Serving Metrics
# - Revenue and Analytics
# - Content Management
# - User Activity
#
# Author: Adtlas Development Team
# Version: 2.0.0
# Last Updated: 2025-01-20
# ==============================================================================

groups:
  # ==============================================================================
  # CAMPAIGN MANAGEMENT MONITORING
  # ==============================================================================
  
  - name: adtlas_campaign_management
    interval: 60s
    rules:
      # High campaign creation failure rate
      - alert: AdtlasHighCampaignCreationFailureRate
        expr: |
          (
            rate(adtlas_campaign_operations_total{operation="create", status="failed"}[10m]) /
            rate(adtlas_campaign_operations_total{operation="create"}[10m])
          ) * 100 > 5
        for: 5m
        labels:
          severity: warning
          service: adtlas
          component: campaign-management
          business_impact: medium
        annotations:
          summary: "High campaign creation failure rate"
          description: "Campaign creation failure rate is {{ $value }}% (threshold: 5%)"
          runbook_url: "https://docs.adtlas.com/runbooks/campaign-creation-failures"
          business_impact: "New campaigns cannot be created, affecting advertiser onboarding"
      
      # Campaign activation delays
      - alert: AdtlasCampaignActivationDelays
        expr: |
          histogram_quantile(0.95,
            rate(adtlas_campaign_activation_duration_seconds_bucket[10m])
          ) > 300
        for: 5m
        labels:
          severity: warning
          service: adtlas
          component: campaign-management
          business_impact: medium
        annotations:
          summary: "Campaign activation taking too long"
          description: "95th percentile campaign activation time is {{ $value }}s (threshold: 300s)"
          runbook_url: "https://docs.adtlas.com/runbooks/campaign-activation-delays"
          business_impact: "Delayed campaign launches affecting advertiser satisfaction"
      
      # Low active campaigns
      - alert: AdtlasLowActiveCampaigns
        expr: adtlas_campaigns_active_total < 10
        for: 30m
        labels:
          severity: info
          service: adtlas
          component: campaign-management
          business_impact: low
        annotations:
          summary: "Low number of active campaigns"
          description: "Only {{ $value }} campaigns are currently active (threshold: 10)"
          runbook_url: "https://docs.adtlas.com/runbooks/low-active-campaigns"
          business_impact: "Reduced revenue potential due to low campaign activity"
      
      # Campaign budget exhaustion rate
      - alert: AdtlasHighCampaignBudgetExhaustionRate
        expr: |
          (
            rate(adtlas_campaign_budget_exhausted_total[1h]) /
            rate(adtlas_campaign_operations_total{operation="budget_check"}[1h])
          ) * 100 > 20
        for: 10m
        labels:
          severity: warning
          service: adtlas
          component: campaign-management
          business_impact: high
        annotations:
          summary: "High campaign budget exhaustion rate"
          description: "{{ $value }}% of campaigns are exhausting budgets quickly (threshold: 20%)"
          runbook_url: "https://docs.adtlas.com/runbooks/budget-exhaustion"
          business_impact: "Campaigns ending prematurely, reducing revenue and advertiser satisfaction"

  # ==============================================================================
  # CHANNEL OPERATIONS MONITORING
  # ==============================================================================
  
  - name: adtlas_channel_operations
    interval: 30s
    rules:
      # Channel offline detection
      - alert: AdtlasChannelOffline
        expr: adtlas_channel_status{status="offline"} == 1
        for: 2m
        labels:
          severity: critical
          service: adtlas
          component: channel-management
          business_impact: high
        annotations:
          summary: "Channel is offline"
          description: "Channel {{ $labels.channel_name }} (ID: {{ $labels.channel_id }}) is offline"
          runbook_url: "https://docs.adtlas.com/runbooks/channel-offline"
          business_impact: "Ad serving stopped for this channel, direct revenue loss"
      
      # High channel error rate
      - alert: AdtlasHighChannelErrorRate
        expr: |
          (
            rate(adtlas_channel_operations_total{status="error"}[5m]) /
            rate(adtlas_channel_operations_total[5m])
          ) * 100 > 10
        for: 5m
        labels:
          severity: warning
          service: adtlas
          component: channel-management
          business_impact: medium
        annotations:
          summary: "High channel error rate"
          description: "Channel {{ $labels.channel_name }} error rate is {{ $value }}% (threshold: 10%)"
          runbook_url: "https://docs.adtlas.com/runbooks/channel-errors"
          business_impact: "Reduced ad serving quality and potential revenue loss"
      
      # Channel stream quality issues
      - alert: AdtlasChannelStreamQualityIssues
        expr: adtlas_channel_stream_quality_score < 0.8
        for: 10m
        labels:
          severity: warning
          service: adtlas
          component: channel-management
          business_impact: medium
        annotations:
          summary: "Channel stream quality issues"
          description: "Channel {{ $labels.channel_name }} quality score is {{ $value }} (threshold: 0.8)"
          runbook_url: "https://docs.adtlas.com/runbooks/stream-quality-issues"
          business_impact: "Poor viewer experience may affect advertiser value"
      
      # Low channel utilization
      - alert: AdtlasLowChannelUtilization
        expr: |
          (
            adtlas_channel_ad_slots_filled / adtlas_channel_ad_slots_available
          ) * 100 < 70
        for: 30m
        labels:
          severity: info
          service: adtlas
          component: channel-management
          business_impact: medium
        annotations:
          summary: "Low channel ad slot utilization"
          description: "Channel {{ $labels.channel_name }} utilization is {{ $value }}% (threshold: 70%)"
          runbook_url: "https://docs.adtlas.com/runbooks/low-channel-utilization"
          business_impact: "Missed revenue opportunities due to unfilled ad slots"

  # ==============================================================================
  # VAST SERVER PERFORMANCE
  # ==============================================================================
  
  - name: adtlas_vast_server
    interval: 30s
    rules:
      # VAST response errors
      - alert: AdtlasVASTResponseErrors
        expr: rate(adtlas_vast_responses_total{status="error"}[5m]) > 0.1
        for: 2m
        labels:
          severity: critical
          service: adtlas
          component: vast-server
          business_impact: high
        annotations:
          summary: "VAST response errors detected"
          description: "VAST error rate is {{ $value }} errors/s (threshold: 0.1/s)"
          runbook_url: "https://docs.adtlas.com/runbooks/vast-errors"
          business_impact: "Ad serving failures causing direct revenue loss"
      
      # Slow VAST response times
      - alert: AdtlasSlowVASTResponses
        expr: |
          histogram_quantile(0.95,
            rate(adtlas_vast_response_duration_seconds_bucket[5m])
          ) > 2
        for: 5m
        labels:
          severity: warning
          service: adtlas
          component: vast-server
          business_impact: medium
        annotations:
          summary: "Slow VAST response times"
          description: "95th percentile VAST response time is {{ $value }}s (threshold: 2s)"
          runbook_url: "https://docs.adtlas.com/runbooks/slow-vast-responses"
          business_impact: "Slow ad loading may cause viewer abandonment"
      
      # Low VAST fill rate
      - alert: AdtlasLowVASTFillRate
        expr: |
          (
            rate(adtlas_vast_responses_total{status="filled"}[10m]) /
            rate(adtlas_vast_requests_total[10m])
          ) * 100 < 80
        for: 10m
        labels:
          severity: warning
          service: adtlas
          component: vast-server
          business_impact: high
        annotations:
          summary: "Low VAST fill rate"
          description: "VAST fill rate is {{ $value }}% (threshold: 80%)"
          runbook_url: "https://docs.adtlas.com/runbooks/low-vast-fill-rate"
          business_impact: "Unfilled ad requests causing revenue loss"
      
      # VAST XML validation errors
      - alert: AdtlasVASTXMLValidationErrors
        expr: rate(adtlas_vast_xml_validation_errors_total[5m]) > 0.01
        for: 5m
        labels:
          severity: warning
          service: adtlas
          component: vast-server
          business_impact: medium
        annotations:
          summary: "VAST XML validation errors"
          description: "VAST XML validation error rate is {{ $value }} errors/s (threshold: 0.01/s)"
          runbook_url: "https://docs.adtlas.com/runbooks/vast-xml-validation"
          business_impact: "Invalid VAST responses may cause ad player failures"

  # ==============================================================================
  # PLAYLIST GENERATION MONITORING
  # ==============================================================================
  
  - name: adtlas_playlist_generation
    interval: 60s
    rules:
      # Slow playlist generation
      - alert: AdtlasSlowPlaylistGeneration
        expr: |
          histogram_quantile(0.95,
            rate(adtlas_playlist_generation_duration_seconds_bucket[5m])
          ) > 30
        for: 5m
        labels:
          severity: warning
          service: adtlas
          component: playlist-generation
          business_impact: medium
        annotations:
          summary: "Slow playlist generation"
          description: "95th percentile playlist generation time is {{ $value }}s (threshold: 30s)"
          runbook_url: "https://docs.adtlas.com/runbooks/slow-playlist-generation"
          business_impact: "Delayed content updates affecting viewer experience"
      
      # Playlist generation failures
      - alert: AdtlasPlaylistGenerationFailures
        expr: |
          (
            rate(adtlas_playlist_generation_total{status="failed"}[10m]) /
            rate(adtlas_playlist_generation_total[10m])
          ) * 100 > 5
        for: 5m
        labels:
          severity: warning
          service: adtlas
          component: playlist-generation
          business_impact: medium
        annotations:
          summary: "High playlist generation failure rate"
          description: "Playlist generation failure rate is {{ $value }}% (threshold: 5%)"
          runbook_url: "https://docs.adtlas.com/runbooks/playlist-generation-failures"
          business_impact: "Content scheduling issues affecting ad insertion"
      
      # Stale playlists
      - alert: AdtlasStalePlaylists
        expr: (time() - adtlas_playlist_last_update_timestamp) / 3600 > 2
        for: 10m
        labels:
          severity: warning
          service: adtlas
          component: playlist-generation
          business_impact: medium
        annotations:
          summary: "Stale playlists detected"
          description: "Playlist {{ $labels.playlist_id }} hasn't been updated for {{ $value }} hours (threshold: 2h)"
          runbook_url: "https://docs.adtlas.com/runbooks/stale-playlists"
          business_impact: "Outdated content scheduling may affect ad targeting"

  # ==============================================================================
  # AD SERVING METRICS
  # ==============================================================================
  
  - name: adtlas_ad_serving
    interval: 30s
    rules:
      # Low ad serving rate
      - alert: AdtlasLowAdServingRate
        expr: rate(adtlas_ads_served_total[5m]) < 10
        for: 10m
        labels:
          severity: warning
          service: adtlas
          component: ad-serving
          business_impact: high
        annotations:
          summary: "Low ad serving rate"
          description: "Ad serving rate is {{ $value }} ads/s (threshold: 10 ads/s)"
          runbook_url: "https://docs.adtlas.com/runbooks/low-ad-serving"
          business_impact: "Reduced ad impressions causing revenue loss"
      
      # High ad serving error rate
      - alert: AdtlasHighAdServingErrorRate
        expr: |
          (
            rate(adtlas_ads_served_total{status="error"}[5m]) /
            rate(adtlas_ads_served_total[5m])
          ) * 100 > 5
        for: 5m
        labels:
          severity: critical
          service: adtlas
          component: ad-serving
          business_impact: high
        annotations:
          summary: "High ad serving error rate"
          description: "Ad serving error rate is {{ $value }}% (threshold: 5%)"
          runbook_url: "https://docs.adtlas.com/runbooks/ad-serving-errors"
          business_impact: "Failed ad deliveries causing revenue loss and advertiser dissatisfaction"
      
      # Low click-through rate
      - alert: AdtlasLowClickThroughRate
        expr: |
          (
            rate(adtlas_ad_clicks_total[1h]) /
            rate(adtlas_ad_impressions_total[1h])
          ) * 100 < 0.5
        for: 30m
        labels:
          severity: info
          service: adtlas
          component: ad-serving
          business_impact: medium
        annotations:
          summary: "Low click-through rate"
          description: "CTR is {{ $value }}% (threshold: 0.5%)"
          runbook_url: "https://docs.adtlas.com/runbooks/low-ctr"
          business_impact: "Poor ad performance may affect advertiser retention"
      
      # Ad inventory depletion
      - alert: AdtlasAdInventoryDepletion
        expr: adtlas_ad_inventory_available < 100
        for: 15m
        labels:
          severity: warning
          service: adtlas
          component: ad-serving
          business_impact: high
        annotations:
          summary: "Low ad inventory"
          description: "Available ad inventory is {{ $value }} (threshold: 100)"
          runbook_url: "https://docs.adtlas.com/runbooks/low-ad-inventory"
          business_impact: "Risk of unfilled ad slots leading to revenue loss"

  # ==============================================================================
  # REVENUE AND ANALYTICS
  # ==============================================================================
  
  - name: adtlas_revenue_analytics
    interval: 300s  # 5 minutes
    rules:
      # Revenue drop
      - alert: AdtlasRevenueDrop
        expr: |
          (
            rate(adtlas_revenue_total[1h]) < 
            rate(adtlas_revenue_total[1h] offset 24h) * 0.8
          )
        for: 30m
        labels:
          severity: critical
          service: adtlas
          component: revenue
          business_impact: critical
        annotations:
          summary: "Significant revenue drop detected"
          description: "Current revenue rate is 20% below same time yesterday"
          runbook_url: "https://docs.adtlas.com/runbooks/revenue-drop"
          business_impact: "Direct financial impact requiring immediate investigation"
      
      # Low impression volume
      - alert: AdtlasLowImpressionVolume
        expr: |
          (
            rate(adtlas_ad_impressions_total[1h]) < 
            rate(adtlas_ad_impressions_total[1h] offset 24h) * 0.7
          )
        for: 30m
        labels:
          severity: warning
          service: adtlas
          component: analytics
          business_impact: high
        annotations:
          summary: "Low impression volume"
          description: "Impression volume is 30% below same time yesterday"
          runbook_url: "https://docs.adtlas.com/runbooks/low-impression-volume"
          business_impact: "Reduced ad delivery affecting revenue potential"
      
      # Analytics processing delays
      - alert: AdtlasAnalyticsProcessingDelays
        expr: (time() - adtlas_analytics_last_processed_timestamp) / 60 > 30
        for: 10m
        labels:
          severity: warning
          service: adtlas
          component: analytics
          business_impact: medium
        annotations:
          summary: "Analytics processing delays"
          description: "Analytics haven't been processed for {{ $value }} minutes (threshold: 30m)"
          runbook_url: "https://docs.adtlas.com/runbooks/analytics-delays"
          business_impact: "Delayed reporting affecting business decision making"

  # ==============================================================================
  # CONTENT MANAGEMENT
  # ==============================================================================
  
  - name: adtlas_content_management
    interval: 60s
    rules:
      # Content upload failures
      - alert: AdtlasContentUploadFailures
        expr: |
          (
            rate(adtlas_content_uploads_total{status="failed"}[10m]) /
            rate(adtlas_content_uploads_total[10m])
          ) * 100 > 10
        for: 5m
        labels:
          severity: warning
          service: adtlas
          component: content-management
          business_impact: medium
        annotations:
          summary: "High content upload failure rate"
          description: "Content upload failure rate is {{ $value }}% (threshold: 10%)"
          runbook_url: "https://docs.adtlas.com/runbooks/content-upload-failures"
          business_impact: "Advertisers cannot upload new creative content"
      
      # Content processing delays
      - alert: AdtlasContentProcessingDelays
        expr: |
          histogram_quantile(0.95,
            rate(adtlas_content_processing_duration_seconds_bucket[10m])
          ) > 600
        for: 10m
        labels:
          severity: warning
          service: adtlas
          component: content-management
          business_impact: medium
        annotations:
          summary: "Content processing delays"
          description: "95th percentile content processing time is {{ $value }}s (threshold: 600s)"
          runbook_url: "https://docs.adtlas.com/runbooks/content-processing-delays"
          business_impact: "Delayed content availability affecting campaign launches"
      
      # Low content approval rate
      - alert: AdtlasLowContentApprovalRate
        expr: |
          (
            rate(adtlas_content_approvals_total{status="approved"}[1h]) /
            rate(adtlas_content_approvals_total[1h])
          ) * 100 < 80
        for: 30m
        labels:
          severity: info
          service: adtlas
          component: content-management
          business_impact: medium
        annotations:
          summary: "Low content approval rate"
          description: "Content approval rate is {{ $value }}% (threshold: 80%)"
          runbook_url: "https://docs.adtlas.com/runbooks/low-approval-rate"
          business_impact: "High rejection rate may indicate content quality issues"

  # ==============================================================================
  # USER ACTIVITY MONITORING
  # ==============================================================================
  
  - name: adtlas_user_activity
    interval: 300s  # 5 minutes
    rules:
      # Low user activity
      - alert: AdtlasLowUserActivity
        expr: rate(adtlas_user_sessions_total[1h]) < 5
        for: 30m
        labels:
          severity: info
          service: adtlas
          component: user-activity
          business_impact: low
        annotations:
          summary: "Low user activity"
          description: "User session rate is {{ $value }} sessions/s (threshold: 5/s)"
          runbook_url: "https://docs.adtlas.com/runbooks/low-user-activity"
          business_impact: "Reduced platform usage may indicate user experience issues"
      
      # High user error rate
      - alert: AdtlasHighUserErrorRate
        expr: |
          (
            rate(adtlas_user_operations_total{status="error"}[10m]) /
            rate(adtlas_user_operations_total[10m])
          ) * 100 > 15
        for: 10m
        labels:
          severity: warning
          service: adtlas
          component: user-activity
          business_impact: medium
        annotations:
          summary: "High user error rate"
          description: "User operation error rate is {{ $value }}% (threshold: 15%)"
          runbook_url: "https://docs.adtlas.com/runbooks/high-user-error-rate"
          business_impact: "Poor user experience may affect platform adoption"
      
      # Account creation issues
      - alert: AdtlasAccountCreationIssues
        expr: |
          (
            rate(adtlas_account_creation_total{status="failed"}[1h]) /
            rate(adtlas_account_creation_total[1h])
          ) * 100 > 20
        for: 15m
        labels:
          severity: warning
          service: adtlas
          component: user-management
          business_impact: medium
        annotations:
          summary: "High account creation failure rate"
          description: "Account creation failure rate is {{ $value }}% (threshold: 20%)"
          runbook_url: "https://docs.adtlas.com/runbooks/account-creation-issues"
          business_impact: "New user onboarding issues affecting growth"

# ==============================================================================
# BUSINESS RULE CONFIGURATION NOTES
# ==============================================================================
#
# These business logic alerting rules monitor:
# 1. Campaign lifecycle and performance metrics
# 2. Channel operations and stream quality
# 3. VAST server performance and ad delivery
# 4. Playlist generation and content scheduling
# 5. Ad serving rates and error conditions
# 6. Revenue tracking and analytics processing
# 7. Content management and approval workflows
# 8. User activity and platform engagement
#
# Business impact levels:
# - critical: Direct revenue impact, immediate action required
# - high: Significant business impact, urgent attention needed
# - medium: Moderate impact, attention needed within hours
# - low: Minor impact, can be addressed during business hours
#
# Key business metrics:
# - Revenue per hour/day
# - Ad fill rates and serving success
# - Campaign performance and activation times
# - Channel availability and quality
# - User engagement and error rates
# - Content processing and approval rates
#
# Alert customization:
# 1. Adjust thresholds based on business requirements
# 2. Add seasonal or time-based variations
# 3. Configure escalation based on business impact
# 4. Set up automated remediation for common issues
# 5. Create business-focused dashboards and reports
# ==============================================================================