resource "datadog_dashboard" "aws_ecs_dhaval_dashboard" {
  description = <<-EOT
      This dashboard provides high-level status and resource metrics for your ECS cluster, including task– and container-level granularity into cluster availability. Further reading on AWS ECS monitoring:

      - [Datadog's guide to key AWS ECS metrics](https://www.datadoghq.com/blog/amazon-ecs-metrics/)

      - [Tools for ECS monitoring](https://www.datadoghq.com/blog/ecs-monitoring-tools/)

      - [Monitoring ECS with Datadog](https://www.datadoghq.com/blog/monitoring-ecs-with-datadog/)

      - [Datadog's AWS ECS integration docs](https://docs.datadoghq.com/integrations/amazon_ecs/)

      Clone this template dashboard to make changes and add your own graph widgets.
        EOT
  layout_type = "free"
  title       = "AWS ECS (Dhaval)"
  url         = "/dashboard/su9-8s7-bbj/aws-ecs-dhaval"
  template_variable {
    defaults = ["prod-delphi-api"]
    name     = "ecs_service"
    prefix   = "servicename"
  }
  template_variable {
    defaults = ["*"]
    name     = "ecs_task"
  }
  template_variable {
    defaults = ["prod-delphi-clu"]
    name     = "ecs_cluster"
    prefix   = "cluster"
  }
  widget {
    image_definition {
      has_background = false
      has_border     = false
      sizing         = "center"
      url            = "/static/images/saas_logos/bot/amazon_ecs.png"
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 16
      x               = 0
      y               = 0
    }
  }
  widget {
    event_stream_definition {
      event_size     = "s"
      query          = "tags:\"cluster:prod-delphi-clu\" source:amazon_ecs"
      tags_execution = "and"
      title          = "ECS Events"
      title_align    = "left"
      title_size     = "16"
    }
    widget_layout {
      height          = 70
      is_column_break = false
      width           = 32
      x               = 113
      y               = 0
    }
  }
  widget {
    check_status_definition {
      check     = "aws.ecs.agent_connected"
      grouping  = "cluster"
      live_span = "30m"
      tags = [
        "$ecs_task",
      ]
      title       = "ECS Agent service status"
      title_align = "center"
      title_size  = "13"
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 21
      x               = 18
      y               = 0
    }
  }
  widget {
    note_definition {
      background_color = "gray"
      content          = "Overview"
      font_size        = "18"
      has_padding      = false
      show_tick        = true
      text_align       = "center"
      tick_edge        = "right"
      tick_pos         = "50%"
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 12
      x               = 41
      y               = 0
    }
  }
  widget {
    query_value_definition {
      autoscale   = true
      live_span   = "1h"
      precision   = 2
      title       = "Running services"
      title_align = "left"
      title_size  = "16"
      request {
        aggregator = "avg"
        q          = "sum:aws.ecs.services{$ecs_cluster}"
      }
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 16
      x               = 54
      y               = 0
    }
  }
  widget {
    query_value_definition {
      autoscale   = true
      custom_unit = "%"
      live_span   = "1h"
      precision   = 2
      title       = "...vs all"
      title_align = "left"
      title_size  = "16"
      request {
        aggregator = "avg"
        q          = "(sum:aws.ecs.service.running{$ecs_cluster,$ecs_cluster,$ecs_task}/(sum:aws.ecs.service.running{$ecs_cluster,$ecs_cluster,$ecs_task}+sum:aws.ecs.service.desired{$ecs_cluster,$ecs_cluster,$ecs_task}+sum:aws.ecs.service.pending{$ecs_cluster,$ecs_cluster,$ecs_task}))*100"
      }
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 14
      x               = 97
      y               = 23
    }
  }
  widget {
    query_value_definition {
      autoscale   = true
      custom_unit = "%"
      live_span   = "1h"
      precision   = 2
      title       = "...vs all"
      title_align = "left"
      title_size  = "16"
      request {
        aggregator = "avg"
        q          = "(sum:aws.ecs.service.desired{$ecs_cluster,$ecs_cluster,$ecs_task}/(sum:aws.ecs.service.running{$ecs_cluster,$ecs_cluster,$ecs_task}+sum:aws.ecs.service.desired{$ecs_cluster,$ecs_cluster,$ecs_task}+sum:aws.ecs.service.pending{$ecs_cluster,$ecs_cluster,$ecs_task}))*100"
      }
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 14
      x               = 97
      y               = 55
    }
  }
  widget {
    query_value_definition {
      autoscale   = true
      custom_unit = "%"
      live_span   = "1h"
      precision   = 2
      title       = "...vs all"
      title_align = "left"
      title_size  = "16"
      request {
        aggregator = "avg"
        q          = "(sum:aws.ecs.service.pending{$ecs_cluster,$ecs_cluster,$ecs_task}/(sum:aws.ecs.service.running{$ecs_cluster,$ecs_cluster,$ecs_task}+sum:aws.ecs.service.desired{$ecs_cluster,$ecs_cluster,$ecs_task}+sum:aws.ecs.service.pending{$ecs_cluster,$ecs_cluster,$ecs_task}))*100"
      }
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 14
      x               = 97
      y               = 39
    }
  }
  widget {
    note_definition {
      background_color = "gray"
      content          = "Container status"
      font_size        = "18"
      has_padding      = false
      show_tick        = true
      text_align       = "center"
      tick_edge        = "bottom"
      tick_pos         = "50%"
    }
    widget_layout {
      height          = 5
      is_column_break = false
      width           = 62
      x               = 49
      y               = 17
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      show_legend = false
      title       = "RUNNING"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "area"
        on_right_yaxis = false
        q              = "sum:aws.ecs.service.running{$ecs_task,$ecs_cluster,$ecs_cluster} by {clustername}"
        style {
          line_type  = "solid"
          line_width = "normal"
          palette    = "purple"
        }
      }
      yaxis {
        include_zero = true
        max          = "auto"
        min          = "auto"
        scale        = "linear"
      }
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 47
      x               = 49
      y               = 23
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      show_legend = false
      title       = "PENDING"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "area"
        on_right_yaxis = false
        q              = "sum:aws.ecs.service.pending{$ecs_task,$ecs_cluster,$ecs_cluster} by {clustername}"
        style {
          line_type  = "solid"
          line_width = "normal"
          palette    = "purple"
        }
      }
      yaxis {
        include_zero = true
        max          = "auto"
        min          = "auto"
        scale        = "linear"
      }
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 47
      x               = 49
      y               = 39
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      show_legend = false
      title       = "DESIRED"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "area"
        on_right_yaxis = false
        q              = "sum:aws.ecs.service.desired{$ecs_task,$ecs_cluster,$ecs_cluster} by {clustername}"
        style {
          line_type  = "solid"
          line_width = "normal"
          palette    = "purple"
        }
      }
      yaxis {
        include_zero = true
        max          = "auto"
        min          = "auto"
        scale        = "linear"
      }
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 47
      x               = 49
      y               = 55
    }
  }
  widget {
    toplist_definition {
      live_span   = "1h"
      title       = "% running containers per ECS service"
      title_align = "left"
      title_size  = "16"
      request {
        q = "top(avg:aws.ecs.service.running{$ecs_task,$ecs_cluster,$ecs_cluster} by {servicename}, 10, 'mean', 'desc')/(top(avg:aws.ecs.service.desired{$ecs_task,$ecs_cluster,$ecs_cluster} by {servicename}, 10, 'mean', 'desc')+top(avg:aws.ecs.service.pending{$ecs_task,$ecs_cluster,$ecs_cluster} by {servicename}, 10, 'mean', 'desc')+top(avg:aws.ecs.service.running{$ecs_task,$ecs_cluster,$ecs_cluster} by {servicename}, 10, 'mean', 'desc'))*100"
        style {
          palette = "dog_classic"
        }
      }
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 40
      x               = 71
      y               = 0
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      show_legend = true
      title       = "Memory"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "sum:aws.ecs.service.memory_utilization{clustername:prod-delphi-clu} by {servicename}"
        style {
          line_type  = "solid"
          line_width = "normal"
          palette    = "dog_classic"
        }
      }
      yaxis {
        include_zero = true
        max          = "auto"
        min          = "auto"
        scale        = "linear"
      }
    }
    widget_layout {
      height          = 23
      is_column_break = false
      width           = 48
      x               = 0
      y               = 23
    }
  }
  widget {
    note_definition {
      background_color = "gray"
      content          = "Service resource utilization"
      font_size        = "18"
      has_padding      = false
      show_tick        = true
      text_align       = "center"
      tick_edge        = "bottom"
      tick_pos         = "50%"
    }
    widget_layout {
      height          = 5
      is_column_break = false
      width           = 48
      x               = 0
      y               = 17
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      show_legend = true
      title       = "CPU"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "sum:aws.ecs.service.cpuutilization{clustername:prod-delphi-clu} by {clustername,servicename}"
        style {
          line_type  = "solid"
          line_width = "normal"
          palette    = "dog_classic"
        }
      }
      yaxis {
        include_zero = true
        max          = "auto"
        min          = "auto"
        scale        = "linear"
      }
    }
    widget_layout {
      height          = 23
      is_column_break = false
      width           = 48
      x               = 0
      y               = 47
    }
  }
}

resource "datadog_dashboard" "aws_ecs_prod_test_dashboard" {
  layout_type = "ordered"
  title       = "AWS-ECS-PROD-test"
  url         = "/dashboard/7x7-kgc-ew7/aws-ecs-prod-test"
  widget {
    timeseries_definition {
      show_legend = false
      title       = "aws.ecs.services"
      request {
        on_right_yaxis = false
        q              = "avg:aws.ecs.services{cluster:prod-delphi-clu}"
      }
    }
  }
  widget {
    timeseries_definition {
      show_legend = false
      title       = "Running Service aws.ecs.service.running with clustername:prod-delphi-clu"
      request {
        on_right_yaxis = false
        q              = "sum:aws.ecs.service.running{cluster:prod-delphi-clu,clustername:prod-delphi-clu}"
      }
    }
  }
}

