# datadog dashboard.
resource "datadog_dashboard_json" "ar_neo4j_dashboard_json" {
  dashboard = templatefile("dashboard.json", {
    environment = var.environment
  })
}

# monitors
# Current value is 1302. So alert if it increases from this number.
resource "datadog_monitor" "vend_contact_ar_only" {
  name    = "Count of LabelProfiles in AR DB that are missing in Neo4j has increased"
  type    = "query alert"
  message = "There are #{{value}} LabelProfiles in AR DB that are missing in Neo4j. This can be a code issue. \nNotify: ${var.notification_endpoints}"
  query   = "avg(last_5m):avg:snowflake.snowflake.custom.ar_to_neo_sync.vend_contact_ar_only{*} > 1313"

  monitor_thresholds {
    ok       = 0
    warning  = 1303
    critical = 1313
  }

  include_tags        = true
  notify_no_data      = false
  notify_audit        = false
  timeout_h           = 2
  require_full_window = false

  tags = [
    "environment:${var.environment}",
    "service_name:${var.service_name}",
    "application_family:${var.application_family}"
  ]
}

# Current value is 2. So alert if it increases from this number.
resource "datadog_monitor" "vend_contact_neo4j_only" {
  name    = "Count of extra LabelProfiles in Neo4j has increased"
  type    = "query alert"
  message = "There are #{{value}} LabelProfiles in Neo4j that are not in AR DB vend_contact table. This can be due to hard deletes via DB PRs. \nNotify: ${var.notification_endpoints}"
  query   = "avg(last_5m):avg:snowflake.snowflake.custom.ar_to_neo_sync.vend_contact_neo4j_only{*} > 13"

  monitor_thresholds {
    ok       = 0
    warning  = 3
    critical = 13
  }

  include_tags        = true
  notify_no_data      = false
  notify_audit        = false
  timeout_h           = 2
  require_full_window = false

  tags = [
    "environment:${var.environment}",
    "service_name:${var.service_name}",
    "application_family:${var.application_family}"
  ]
}

# Current value is 747. So alert if it increases from this number.
resource "datadog_monitor" "vend_contact_different_roles" {
  name    = "Count of LabelProfiles with mismatch roles in Neo4j vs AR DB has increased"
  type    = "query alert"
  message = "There are #{{value}} LabelProfiles in Neo4j that have mismatch roles in AR DB. This can be due to DB PRs or code issue. \nNotify: ${var.notification_endpoints}"
  query   = "avg(last_5m):avg:snowflake.snowflake.custom.ar_to_neo_sync.vend_contact_different_roles{*} > 758"

  monitor_thresholds {
    ok       = 0
    warning  = 748
    critical = 758
  }

  include_tags        = true
  notify_no_data      = false
  notify_audit        = false
  timeout_h           = 2
  require_full_window = false

  tags = [
    "environment:${var.environment}",
    "service_name:${var.service_name}",
    "application_family:${var.application_family}"
  ]
}
