resource "datadog_monitor" "minimal_amount_of_pods" {
  for_each = var.create_monitors ? local.eks_applications : {}

  include_tags        = false
  on_missing_data     = "show_and_notify_no_data"
  require_full_window = false
  monitor_thresholds {
    critical = 1
  }
  name    = "AMPSV Team | ${var.name_prefix} | ${each.key} | Minimal amount of pods running"
  type    = "query alert"
  tags    =  concat(
    [local.managed_by_terraform_datadog_tag],
    [local.email_notifications_datadog_tag],
    [local.pagerduty_notifications_datadog_tag],
    ["team:aoma-core-team"],
    ["env:${var.environment}"]
  )
  query   = <<-EOT
    min(last_5m):sum:kubernetes_state.pod.status_phase{${each.value.application_type}:${each.value.application_name_prefix}*,pod_phase:running, kube_cluster_name:${var.cluster_name}, kube_namespace:${var.namespace}} < 1
    EOT
  message = <<-EOT
    {{#or is_alert is_no_data}}
    There are no pods running for ${each.key} or they are in unhealthy state for the last {{triggered_duration_sec}}
    {{/or}}

    {{#is_recovery}}
    Minimal amount of pods has met threshold - {{threshold}}: {{value}}
    {{/is_recovery}}
    EOT
}
