
resource "datadog_monitor" "ScraperSpotifyPlaylistProcessed_low" {
  name = "DAPD monitor: ScraperSpotifyPlaylistProcessed too low"
  type = "query alert"

  message = <<EOT
{{#is_alert}}
${local.alert_message_env_title} amount of ScraperSpotifyPlaylistProcessed is < 12000 for the last 6 hours.
{{/is_alert}}

{{#is_recovery}}
${local.alert_message_env_title} amount of ScraperSpotifyPlaylistProcessed is back to normal values.
{{/is_recovery}}
${local.alert_channels}
EOT

  query = "sum(last_6h):sum:dapd.ScraperSpotifyPlaylistProcessed.sum{*} by {alias} < 12000"


  evaluation_delay    = 600 // minimum recommended value for GCP metrics 300
  require_full_window = true
  notify_no_data      = true
  no_data_timeframe   = 30
  renotify_interval   = 1440
  include_tags        = true
  restricted_roles    = [data.datadog_role.datadog_admin_role.id]

  tags = local.common_tags
}

resource "datadog_monitor" "ScraperSpotifyPlaylistProcessed_high" {
  name = "DAPD monitor: ScraperSpotifyPlaylistProcessed too high"
  type = "query alert"

  message = <<EOT
{{#is_alert}}
${local.alert_message_env_title} amount of ScraperSpotifyPlaylistProcessed is > 15000 for the last 6 hours.
{{/is_alert}}

{{#is_recovery}}
${local.alert_message_env_title} amount of ScraperSpotifyPlaylistProcessed is back to normal values.
{{/is_recovery}}
${local.alert_channels}
EOT

  query = "sum(last_6h):sum:dapd.ScraperSpotifyPlaylistProcessed.sum{*} by {alias} > 15000"


  evaluation_delay    = 600 // minimum recommended value for GCP metrics 300
  require_full_window = true
  notify_no_data      = true
  no_data_timeframe   = 30
  renotify_interval   = 1440
  include_tags        = true
  restricted_roles    = [data.datadog_role.datadog_admin_role.id]

  tags = local.common_tags
}

resource "datadog_monitor" "SpotifyPlaylist_TransformService_to_Scraper_ratio_high" {
  name = "DAPD monitor: TransformServiceInputSpotifyPlaylists / ScraperSpotifyPlaylistProcessed ratio too high"
  type = "query alert"

  message = <<EOT
{{#is_alert}}
${local.alert_message_env_title} relation TransformServiceInputSpotifyPlaylists / ScraperSpotifyPlaylistProcessed is >1.5  for the last 6 hours.
{{/is_alert}}

{{#is_recovery}}
${local.alert_message_env_title} relation TransformServiceInputSpotifyPlaylists / ScraperSpotifyPlaylistProcessed is back to normal values.
{{/is_recovery}}
${local.alert_channels}
EOT

  query = "avg(last_10m):sum:dapd.TransformServiceInputSpotifyPlaylists.sum{*} by {alias}.rollup(sum, 21600)/sum:dapd.ScraperSpotifyPlaylistProcessed.sum{*} by {alias}.rollup(sum, 21600) > 1.5"


  evaluation_delay    = 21600 // minimum recommended value for GCP metrics 300
  require_full_window = true
  notify_no_data      = true
  no_data_timeframe   = 30
  renotify_interval   = 1440
  include_tags        = true
  restricted_roles    = [data.datadog_role.datadog_admin_role.id]

  tags = local.common_tags
}

resource "datadog_monitor" "SpotifyPlaylist_TransformService_to_Scraper_ratio_low" {
  name = "DAPD monitor: TransformServiceInputSpotifyPlaylists / ScraperSpotifyPlaylistProcessed ratio too low"
  type = "query alert"

  message = <<EOT
{{#is_alert}}
${local.alert_message_env_title} relation TransformServiceInputSpotifyPlaylists / ScraperSpotifyPlaylistProcessed is < 0.85  for the last 6 hours.
{{/is_alert}}

{{#is_recovery}}
${local.alert_message_env_title} relation TransformServiceInputSpotifyPlaylists / ScraperSpotifyPlaylistProcessed is back to normal values.
{{/is_recovery}}
${local.alert_channels}
EOT

  query = "max(last_10h):sum:dapd.TransformServiceInputSpotifyPlaylists.sum{*} by {alias}.rollup(sum, 21600)/sum:dapd.ScraperSpotifyPlaylistProcessed.sum{*} by {alias}.rollup(sum, 21600) < 0.85"


  evaluation_delay    = 21600 // minimum recommended value for GCP metrics 300
  require_full_window = true
  notify_no_data      = true
  no_data_timeframe   = 30
  renotify_interval   = 1440
  include_tags        = true
  restricted_roles    = [data.datadog_role.datadog_admin_role.id]

  tags = local.common_tags
}
