resource "datadog_dashboard" "dev_delphi_lambda_dashboard" {
  description = <<-EOT
      This dashboard reports the performance and usage of your AWS Lambda functions, helping you optimize them and monitor your billable execution time. Further reading on monitoring AWS Lambda:

      - [How to monitor Lambda functions](https://www.datadoghq.com/blog/how-to-monitor-lambda-functions/)

      - [Datadog's Lambda Layer: Monitor custom serverless metrics](https://www.datadoghq.com/blog/datadog-lambda-layer/)

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

      Clone this template dashboard to make changes and add your own graph widgets.
        EOT
  layout_type = "free"
  title       = "Dev Delphi Lambda"
  url         = "/dashboard/9e5-a7d-juy/dev-delphi-lambda"
  template_variable {
    defaults = ["*"]
    name     = "function"
    prefix   = "functionname"
  }
  template_variable {
    defaults = ["*"]
    name     = "region"
    prefix   = "region"
  }
  widget {
    image_definition {
      has_background = false
      has_border     = false
      sizing         = "fit"
      url            = "/static/images/screenboard/integrations/amazon_lambda.png"
    }
    widget_layout {
      height          = 12
      is_column_break = false
      width           = 21
      x               = 0
      y               = 0
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "1d"
      show_legend = false
      title       = "Execution duration by function"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:aws.lambda.duration.sum{$function,environment:development,platform:delphi} by {functionname}"
        style {
          line_type  = "solid"
          line_width = "normal"
          palette    = "dog_classic"
        }
      }
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 47
      x               = 22
      y               = 23
    }
  }
  widget {
    toplist_definition {
      live_span   = "1h"
      title       = "Top invoked functions"
      title_align = "left"
      title_size  = "16"
      request {
        q = "top(sum:aws.lambda.invocations{$region,platform:delphi,environment:development} by {functionname}.as_count(), 20, 'sum', 'desc')"
        style {
          palette = "dog_classic"
        }
      }
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 47
      x               = 70
      y               = 7
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "1h"
      show_legend = false
      title       = "Execution errors by function"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "bars"
        on_right_yaxis = false
        q              = "avg:aws.lambda.errors{$function,$region,environment:development,platform:delphi} by {functionname}.as_count()"
        style {
          line_type  = "solid"
          line_width = "normal"
          palette    = "red"
        }
      }
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 47
      x               = 118
      y               = 23
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "1h"
      show_legend = false
      title       = "Throttled invocation attempts by function"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "bars"
        on_right_yaxis = false
        q              = "avg:aws.lambda.throttles{$function,$region,environment:development,platform:delphi} by {functionname}.as_count()"
        style {
          line_type  = "solid"
          line_width = "normal"
          palette    = "dog_classic"
        }
      }
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 47
      x               = 118
      y               = 39
    }
  }
  widget {
    note_definition {
      background_color = "yellow"
      content          = "Note: This integration requires the permissions \"logs:DescribeLogGroups\", \"logs:DescribeLogStreams\", and \"logs:FilterLogEvents\"  to access all features. See our [documentation](http://docs.datadoghq.com/integrations/aws/)."
      font_size        = "14"
      has_padding      = false
      show_tick        = false
      text_align       = "left"
      tick_edge        = "left"
      tick_pos         = "50%"
    }
    widget_layout {
      height          = 6
      is_column_break = false
      width           = 59
      x               = 64
      y               = 59
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "1h"
      show_legend = false
      title       = "Function invocations"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "sum:aws.lambda.invocations{$function,$region,environment:development,platform:delphi} by {functionname}"
        style {
          line_type  = "solid"
          line_width = "normal"
          palette    = "dog_classic"
        }
      }
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 47
      x               = 70
      y               = 23
    }
  }
  widget {
    toplist_definition {
      live_span   = "1h"
      title       = "Average execution time by function"
      title_align = "left"
      title_size  = "16"
      request {
        q = "top(sum:aws.lambda.duration{environment:development,platform:delphi} by {functionname}, 20, 'mean', 'desc')"
        style {
          palette = "dog_classic"
        }
      }
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 47
      x               = 22
      y               = 7
    }
  }
  widget {
    toplist_definition {
      live_span   = "1d"
      title       = "Total execution time"
      title_align = "left"
      title_size  = "16"
      request {
        q = "top(avg:aws.lambda.duration.sum{$function,$region,environment:development,platform:delphi} by {functionname}, 20, 'sum', 'desc')"
        style {
          palette = "dog_classic"
        }
      }
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 47
      x               = 22
      y               = 39
    }
  }
  widget {
    toplist_definition {
      live_span   = "1h"
      title       = "Execution errors"
      title_align = "left"
      title_size  = "16"
      request {
        q = "top(sum:aws.lambda.errors{$function,$region,platform:delphi,environment:development} by {functionname}.as_count(), 20, 'sum', 'desc')"
        style {
          palette = "dog_classic"
        }
      }
    }
    widget_layout {
      height          = 15
      is_column_break = false
      width           = 47
      x               = 118
      y               = 7
    }
  }
  widget {
    note_definition {
      background_color = "gray"
      content          = "Invocations"
      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           = 47
      x               = 70
      y               = 0
    }
  }
  widget {
    note_definition {
      background_color = "gray"
      content          = "Errors"
      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           = 47
      x               = 118
      y               = 0
    }
  }
  widget {
    note_definition {
      background_color = "gray"
      content          = "Execution time"
      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           = 47
      x               = 22
      y               = 0
    }
  }
  widget {
    query_value_definition {
      autoscale   = true
      live_span   = "1d"
      precision   = 2
      title       = "Cumulative execution time"
      title_align = "center"
      title_size  = "16"
      request {
        aggregator = "sum"
        q          = "sum:aws.lambda.duration.sum{$function,$region,environment:development,platform:delphi}"
      }
    }
    widget_layout {
      height          = 9
      is_column_break = false
      width           = 21
      x               = 0
      y               = 14
    }
  }
  widget {
    query_value_definition {
      autoscale   = true
      live_span   = "1d"
      precision   = 2
      title       = "Errors"
      title_align = "center"
      title_size  = "16"
      request {
        aggregator = "sum"
        q          = "sum:aws.lambda.errors{$region,environment:development,platform:delphi}.as_count()"
        conditional_formats {
          comparator = ">"
          hide_value = false
          palette    = "white_on_red"
          value      = 5
        }
        conditional_formats {
          comparator = "<="
          hide_value = false
          palette    = "white_on_green"
          value      = 5
        }
      }
    }
    widget_layout {
      height          = 9
      is_column_break = false
      width           = 21
      x               = 0
      y               = 24
    }
  }
}
