locals {
  service_prefix = "lambda-abacus"
  lambda_names = [
    "ledger-accounting-run-balance",
    "commit-royalties",
    "commit-mechanicals",
    "reserves-take",
    "reserves-schedule",
  ]
}

resource "datadog_dashboard" "accounting_run_pipeline" {
  title       = "Accounting Run Pipeline"
  description = "Dashboard for the Accounting Run pipeline lambdas"
  layout_type = "ordered"
  reflow_type = "auto"
  tags        = ["team:abacus"]

  # -------------------------------------------------------------------
  # Group 1 - Pipeline Status
  # -------------------------------------------------------------------
  widget {
    group_definition {
      layout_type = "ordered"
      title       = "Pipeline Status"

      widget {
        timeseries_definition {
          title       = "Lambda Invocations"
          title_size  = "16"
          title_align = "left"
          show_legend = true
          legend_size = "0"

          dynamic "request" {
            for_each = local.lambda_names
            content {
              display_type = "line"
              q            = "sum:aws.lambda.invocations{functionname:${local.service_prefix}-${request.value}}.as_count()"

              style {
                line_type  = "solid"
                line_width = "normal"
                palette    = "dog_classic"
              }
            }
          }
        }
      }
    }
  }

  # -------------------------------------------------------------------
  # Group 2 - Lambda Performance
  # -------------------------------------------------------------------
  widget {
    group_definition {
      layout_type = "ordered"
      title       = "Lambda Performance"

      dynamic "widget" {
        for_each = local.lambda_names
        content {
          query_value_definition {
            title     = "${widget.value} - Errors"
            autoscale = true
            precision = 0

            request {
              q          = "sum:aws.lambda.errors{functionname:${local.service_prefix}-${widget.value}}.as_count()"
              aggregator = "sum"

              conditional_formats {
                comparator = ">"
                value      = 0
                palette    = "white_on_red"
                hide_value = false
              }

              conditional_formats {
                comparator = "<="
                value      = 0
                palette    = "white_on_green"
                hide_value = false
              }
            }
          }
        }
      }

      dynamic "widget" {
        for_each = local.lambda_names
        content {
          query_value_definition {
            title     = "${widget.value} - p95 Duration (ms)"
            autoscale = true
            precision = 2

            request {
              q          = "avg:aws.lambda.duration.p95{functionname:${local.service_prefix}-${widget.value}}"
              aggregator = "avg"

              conditional_formats {
                comparator = ">"
                value      = 10000
                palette    = "white_on_red"
                hide_value = false
              }

              conditional_formats {
                comparator = "<="
                value      = 10000
                palette    = "white_on_green"
                hide_value = false
              }
            }
          }
        }
      }
    }
  }

  # -------------------------------------------------------------------
  # Group 3 - Lambda Errors
  # -------------------------------------------------------------------
  widget {
    group_definition {
      layout_type = "ordered"
      title       = "Lambda Errors"

      widget {
        timeseries_definition {
          title       = "Lambda Errors"
          title_size  = "16"
          title_align = "left"
          show_legend = true
          legend_size = "0"

          dynamic "request" {
            for_each = local.lambda_names
            content {
              display_type = "bars"
              q            = "sum:aws.lambda.errors{functionname:${local.service_prefix}-${request.value}}.as_count()"

              style {
                line_type  = "solid"
                line_width = "normal"
                palette    = "warm"
              }
            }
          }
        }
      }
    }
  }

  # -------------------------------------------------------------------
  # Group 4 - Lambda Duration
  # -------------------------------------------------------------------
  widget {
    group_definition {
      layout_type = "ordered"
      title       = "Lambda Duration"

      widget {
        timeseries_definition {
          title       = "Lambda Duration - p50"
          title_size  = "16"
          title_align = "left"
          show_legend = true
          legend_size = "0"

          dynamic "request" {
            for_each = local.lambda_names
            content {
              display_type = "line"
              q            = "avg:aws.lambda.duration.p50{functionname:${local.service_prefix}-${request.value}}"

              style {
                line_type  = "solid"
                line_width = "normal"
                palette    = "dog_classic"
              }
            }
          }
        }
      }

      widget {
        timeseries_definition {
          title       = "Lambda Duration - p95"
          title_size  = "16"
          title_align = "left"
          show_legend = true
          legend_size = "0"

          dynamic "request" {
            for_each = local.lambda_names
            content {
              display_type = "line"
              q            = "avg:aws.lambda.duration.p95{functionname:${local.service_prefix}-${request.value}}"

              style {
                line_type  = "solid"
                line_width = "normal"
                palette    = "cool"
              }
            }
          }
        }
      }
    }
  }
}
