module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=1.0.0"
  environment        = var.environment
  application_family = var.application_family

}

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/swf-feed-ingestion/swf_datadog_monitoring.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

resource "datadog_monitor" "swf_incompleted_monitor" {
  for_each = var.swf_monitors
  name     = "Feed: ${each.value.feed} is not completed for expected_time"
  type     = "log alert"
  message  = "Feed: ${each.value.feed} is not completed for expected_time ${each.value.expected_to_completed_hour_utc}:${each.value.expected_to_completed_minute}  UTC Notify: ${var.notification_endpoint}"

  query = "logs(\"service:${each.value.service} environment:${var.environment} DATADOG_MONITOR ${each.value.ingest_completed} ${each.value.feed} \").index(\"*\").rollup(\"count\").last(\"${each.value.ingest_completed_validation_window_hours}h\") < 1"

  scheduling_options {
    custom_schedule {
      recurrence {
        rrule    = "FREQ=DAILY;BYHOUR=${each.value.expected_to_completed_hour_utc};BYMINUTE=${each.value.expected_to_completed_minute}"
        timezone = "UTC"
      }
    }
  }

  notify_no_data = true

  priority = 1

  tags = [
    "environment:${var.environment}",
    "service_name:${each.value.service}",
    "application_family:data-platform",
    "type:data-delivery-monitor"
  ]
}


resource "datadog_monitor" "Monitor_dbt-scheduler-analytics-pipeline" {
  include_tags        = false
  on_missing_data     = "default"
  require_full_window = false
  monitor_thresholds {
    critical = 2
  }
  scheduling_options {
    custom_schedule {
      recurrence {
        rrule    = "FREQ=DAILY;INTERVAL=1;BYHOUR=19;BYMINUTE=45"
        timezone = "UTC"
      }
    }
  }
  name    = "Monitor dbt-scheduler-analytics-pipeline"
  type    = "event-v2 alert"
  query   = <<EOT
events("source:jenkins status:ok job:dbt-scheduler-analytics-pipeline").rollup("count").last("8h") < 2
EOT
  message = <<EOT
{{#is_alert}}
🚨 **2 DBT runs are not completed by 19:45 UTC**

Apple or/and Spotify  jobs are not completed

${var.notification_endpoint}
{{/is_alert}}
EOT

  tags = [
    "environment:${var.environment}",
    "service_name:dbt-scheduler-analytics-pipeline",
    "application_family:data-platform",
    "type:data-delivery-monitor"
  ]
}

resource "datadog_monitor" "Spotify_Download_Warning" {
  name = "Spotify Download Warning"
  type = "log alert"
  query = <<EOT
logs("service:swf-spotify environment:prod \"Download Complete for\" (sme OR theorchard) -report").index("*").rollup("count").last("8h") < 2
EOT
  message = <<EOT
{{#is_alert}}
🚨 **Spotify Download Not Complete by 17:30 UTC**

**Report Date:** Previous day
**Expected:** Both SME and TheOrchard downloads complete
**Found:** {{value}} completion(s) (need 2)


**Query Used:**
```
service:swf-spotify environment:prod "Download Complete for" (sme OR theorchard) -report
```

@slack-data-platform-team-private
{{/is_alert}}

{{#is_recovery}}
✅ ** Spotify Download Completed**

Both licensors (SME and TheOrchard) have successfully downloaded data.
@slack-data-platform-team-private
{{/is_recovery}}
EOT
   tags = [
    "environment:${var.environment}",
    "application_family:data-platform",
    "type:data-delivery-monitor"
  ]
  priority = 1
  draft_status = "published"
  include_tags = false
  on_missing_data = "default"
  require_full_window = false
  monitor_thresholds {
    critical = 2
  }
  scheduling_options {
    custom_schedule {
      recurrence {
        rrule = "FREQ=DAILY;INTERVAL=1;BYHOUR=17;BYMINUTE=30"
        timezone = "UTC"
      }
    }
  }
}
