
resource "datadog_monitor" "assert_data_quality_monitor_failures" {
  for_each = local.data_quality_monitors_notifications
  name     = "[${var.environment}] Neo4j Data Integrity errors on ${each.key}"
  type     = "query alert"
  message  = "Cypher Scheduler detected Data Integrity issues in Neo4j for ${each.key} \n Notify: ${each.value} ."

  query = "sum(last_1d):sum:neo4j_cypher_scheduler.assert_data_quality.failure{environment:${var.environment}, query_file:${each.key}} by {query_file}.as_count() >= 1"

  monitor_thresholds {
    critical = 1
  }

  include_tags        = true
  notify_audit        = false
  notify_no_data      = false
  new_group_delay     = 60
  require_full_window = false
  renotify_interval   = 0
  timeout_h           = 24
}

resource "datadog_dashboard" "assert_data_quality_monitors_dashboard" {
  title       = "[${upper(var.environment)}] Neo4j Data Quality checks status"
  description = "All active monitors for the Data Quality check monitors for neo4j-cypher-scheduler."
  layout_type = "ordered"

  widget {
    manage_status_definition {
      title               = "Neo4j Data Integrity monitors"
      query               = "NOT status:ok Neo4j Data Integrity"
      summary_type        = "monitors"
      show_priority       = true
      show_last_triggered = true
      color_preference    = "background"
      display_format      = "countsAndList"
      hide_zero_counts    = true
      sort                = "status,asc"
    }
    widget_layout {
      x      = 0
      y      = 0
      height = 11
      width  = 10
    }
  }
}
