resource "datadog_dashboard_json" "aoma_core_elb_dashboard_json" {
  dashboard = jsonencode({
    title       = "AOMA Core ELB Dashboard"
    description = ""
    widgets = [
      {
        id = 0
        layout = {
          x      = 35
          y      = 32
          width  = 31
          height = 17
        }
        definition = {
          title       = "CPU Utilization - Top list"
          title_size  = "16"
          title_align = "left"
          type        = "toplist"
          requests = [
            {
              conditional_formats = [
                {
                  palette    = "white_on_red"
                  value      = 75
                  comparator = ">"
                },
                {
                  palette    = "white_on_green"
                  value      = 75
                  comparator = "<="
                }
              ]
              response_format = "scalar"
              queries = [
                {
                  data_source = "metrics"
                  name        = "query1"
                  query       = "avg:aws.ec2.cpuutilization{env:aomap} by {name}"
                  aggregator  = "avg"
                }
              ]
              formulas = [
                {
                  formula = "query1"
                }
              ]
              sort = {
                count = 5
                order_by = [
                  {
                    type  = "formula"
                    index = 0
                    order = "desc"
                  }
                ]
              }
            }
          ]
        }
      },
      {
        id = 1
        layout = {
          x      = 16
          y      = 1
          width  = 18
          height = 9
        }
        definition = {
          title       = "Un-Health Host Count"
          title_size  = "13"
          title_align = "left"
          time = {
            live_span = "1d"
          }
          type = "query_value"
          requests = [
            {
              conditional_formats = [
                {
                  palette    = "white_on_red"
                  value      = 1
                  comparator = ">="
                },
                {
                  palette    = "white_on_green"
                  value      = 0
                  comparator = "<="
                }
              ]
              response_format = "scalar"
              queries = [
                {
                  data_source = "metrics"
                  name        = "query1"
                  query       = "sum:aws.applicationelb.un_healthy_host_count.maximum{*} by {name}"
                  aggregator  = "max"
                }
              ]
              formulas = [
                {
                  formula = "query1"
                  limit = {
                    count = 10
                    order = "desc"
                  }
                }
              ]
            }
          ]
          autoscale  = true
          text_align = "left"
          precision  = 0
        }
      },
      {
        id = 2
        layout = {
          x      = 16
          y      = 11
          width  = 18
          height = 9
        }
        definition = {
          title       = "Healthy host count"
          title_size  = "13"
          title_align = "left"
          time = {
            live_span = "1d"
          }
          type = "query_value"
          requests = [
            {
              response_format = "scalar"
              queries = [
                {
                  data_source = "metrics"
                  name        = "query1"
                  query       = "avg:aws.applicationelb.healthy_host_count{*}"
                  aggregator  = "min"
                }
              ]
              formulas = [
                {
                  formula = "top(query1, 10, \"mean\", \"desc\") * 10"
                }
              ]
            }
          ]
          autoscale  = true
          text_align = "left"
          precision  = 0
        }
      },
      {
        id = 3
        layout = {
          x      = 16
          y      = 32
          width  = 18
          height = 11
        }
        definition = {
          title       = "Status Check Failed"
          title_size  = "16"
          title_align = "left"
          time = {
            live_span = "1d"
          }
          type = "query_value"
          requests = [
            {
              conditional_formats = [
                {
                  palette    = "white_on_red"
                  value      = 1
                  comparator = ">="
                },
                {
                  palette    = "white_on_green"
                  value      = 1
                  comparator = "<"
                }
              ]
              response_format = "scalar"
              queries = [
                {
                  data_source = "metrics"
                  name        = "query1"
                  query       = "sum:aws.ec2.status_check_failed{*}"
                  aggregator  = "sum"
                }
              ]
            }
          ]
          autoscale   = true
          custom_unit = "host"
          precision   = 0
        }
      },
      {
        id = 4
        layout = {
          x      = 16
          y      = 44
          width  = 18
          height = 11
        }
        definition = {
          title       = "Instance Status OK"
          title_size  = "13"
          title_align = "left"
          time = {
            live_span = "1d"
          }
          type = "query_value"
          requests = [
            {
              conditional_formats = [
                {
                  palette    = "white_on_green"
                  value      = 12
                  comparator = ">"
                }
              ]
              response_format = "scalar"
              queries = [
                {
                  data_source = "metrics"
                  name        = "query1"
                  query       = "sum:aws.ec2.host_ok{*}"
                  aggregator  = "avg"
                }
              ]
            }
          ]
          autoscale   = true
          custom_unit = "host"
          text_align  = "left"
          precision   = 0
        }
      },
      {
        id = 5
        layout = {
          x      = 121
          y      = 1
          width  = 21
          height = 14
        }
        definition = {
          title       = "HTTP 200"
          title_size  = "16"
          title_align = "left"
          time = {
            live_span = "1d"
          }
          type = "query_value"
          requests = [
            {
              response_format = "scalar"
              queries = [
                {
                  data_source = "metrics"
                  name        = "query1"
                  query       = "sum:aws.applicationelb.httpcode_target_2xx{*}.as_count()"
                  aggregator  = "avg"
                }
              ]
            }
          ]
          autoscale  = true
          text_align = "left"
          precision  = 0
        }
      },
      {
        id = 6
        layout = {
          x      = 35
          y      = 1
          width  = 85
          height = 14
        }
        definition = {
          title       = "Request Count"
          title_size  = "16"
          title_align = "left"
          show_legend = false
          legend_size = "0"
          time = {
            live_span = "1d"
          }
          type = "timeseries"
          requests = [
            {
              response_format = "timeseries"
              queries = [
                {
                  data_source = "metrics"
                  name        = "query1"
                  query       = "avg:aws.applicationelb.request_count{*} by {host}.as_count()"
                }
              ]
              style = {
                palette    = "dog_classic"
                line_type  = "solid"
                line_width = "normal"
              }
              display_type = "line"
            }
          ]
          yaxis = {
            include_zero = true
            scale        = "linear"
            label        = ""
            min          = "auto"
            max          = "auto"
          }
        }
      },
      {
        id = 7
        layout = {
          x      = 35
          y      = 16
          width  = 85
          height = 15
        }
        definition = {
          title       = "Latency"
          title_size  = "16"
          title_align = "left"
          show_legend = false
          legend_size = "0"
          time = {
            live_span = "1d"
          }
          type = "timeseries"
          requests = [
            {
              response_format = "timeseries"
              queries = [
                {
                  data_source = "metrics"
                  name        = "query1"
                  query       = "avg:aws.applicationelb.target_response_time.p95{*} by {targetgroup}"
                }
              ]
              style = {
                palette    = "dog_classic"
                line_type  = "solid"
                line_width = "normal"
              }
              display_type = "line"
            }
          ]
        }
      },
      {
        id = 8
        layout = {
          x      = 0
          y      = 2
          width  = 14
          height = 27
        }
        definition = {
          type             = "note"
          content          = "\n\n##  \n\n\nELB"
          background_color = "blue"
          font_size        = "56"
          text_align       = "center"
          show_tick        = true
          tick_pos         = "50%"
          tick_edge        = "right"
        }
      },
      {
        id = 9
        layout = {
          x      = 121
          y      = 16
          width  = 21
          height = 15
        }
        definition = {
          title       = "HTTP 400"
          title_size  = "16"
          title_align = "left"
          time = {
            live_span = "1d"
          }
          type = "query_value"
          requests = [
            {
              response_format = "scalar"
              queries = [
                {
                  data_source = "metrics"
                  name        = "query1"
                  query       = "avg:aws.applicationelb.httpcode_target_4xx{*}.as_count()"
                  aggregator  = "avg"
                }
              ]
            }
          ]
          autoscale = true
          precision = 0
        }
      },
      {
        id = 10
        layout = {
          x      = 16
          y      = 21
          width  = 18
          height = 9
        }
        definition = {
          title       = "Total no.of requests"
          title_size  = "16"
          title_align = "left"
          time = {
            live_span = "1d"
          }
          type = "query_value"
          requests = [
            {
              conditional_formats = [
                {
                  palette    = "white_on_green"
                  value      = 100
                  comparator = ">="
                }
              ]
              response_format = "scalar"
              queries = [
                {
                  data_source = "metrics"
                  name        = "query1"
                  query       = "sum:aws.applicationelb.request_count_per_target{*}.as_count()"
                  aggregator  = "sum"
                }
              ]
            }
          ]
          autoscale  = true
          text_align = "left"
          precision  = 0
        }
      },
      {
        id = 11
        layout = {
          x      = 0
          y      = 32
          width  = 14
          height = 23
        }
        definition = {
          type             = "note"
          content          = "##\n\n\n\n\n\nSystem"
          background_color = "gray"
          font_size        = "36"
          text_align       = "left"
          show_tick        = true
          tick_pos         = "50%"
          tick_edge        = "right"
        }
      }
    ]
    template_variables = []
    layout_type        = "free"
    notify_list        = []
    pause_auto_refresh = false
    tags = [
      "team:atf-team",
      "team:aoma-core-team"
    ]
  })
}