resource "datadog_dashboard" "notifications_service_dashboard" {
  layout_type = "ordered"
  reflow_type = "fixed"
  title       = "Notification service"
  url         = "/dashboard/shy-6ki-s8m/notification-service"
  template_variable {
    available_values = [
      "dev",
      "prod",
      "qa",
      "stage",
    ]
    defaults = [
      "*",
    ]
    name   = "env"
    prefix = "env"
  }
  template_variable {
    available_values = [
      "spotify",
      "apple_music",
    ]
    defaults = [
      "*",
    ]
    name   = "dsp"
    prefix = "dsp"
  }
  template_variable {
    defaults = [
      "*",
    ]
    name   = "notification_type"
    prefix = "notification_type"
  }
  template_variable {
    defaults = [
      "*",
    ]
    name   = "event_type"
    prefix = "event_type"
  }
  widget {
    timeseries_definition {
      legend_columns = [
        "avg",
        "max",
        "min",
        "sum",
        "value",
      ]
      legend_layout = "horizontal"
      show_legend   = true
      title         = "Count of successfully sent notifications"
      title_align   = "left"
      title_size    = "16"
      request {
        display_type   = "bars"
        on_right_yaxis = false
        formula {
          formula_expression = "query1"
        }
        query {
          metric_query {
            data_source = "metrics"
            name        = "query1"
            query       = "sum:notification_service.sent_notifications{$env,$dsp,$notification_type,$event_type} by {env,notification_type,dsp,event_type}.as_count()"
          }
        }
        style {
          line_type  = "solid"
          line_width = "normal"
          palette    = "dog_classic"
        }
      }
    }
    widget_layout {
      height          = 3
      is_column_break = false
      width           = 5
      x               = 0
      y               = 0
    }
  }
  widget {
    timeseries_definition {
      show_legend = false
      title       = "Ready notifications to send"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        formula {
          formula_expression = "query1"
        }
        query {
          metric_query {
            data_source = "metrics"
            name        = "query1"
            query       = "avg:notification_service.ready_meta_notifications_to_send{$env}"
          }
        }
        style {
          line_type  = "solid"
          line_width = "normal"
          palette    = "dog_classic"
        }
      }
    }
    widget_layout {
      height          = 3
      is_column_break = false
      width           = 4
      x               = 5
      y               = 0
    }
  }
  widget {
    query_value_definition {
      autoscale   = true
      precision   = 2
      title       = "Created notification rows in DB"
      title_align = "left"
      title_size  = "16"
      request {
        conditional_formats {
          comparator = ">"
          hide_value = false
          palette    = "white_on_green"
          value      = 0
        }
        conditional_formats {
          comparator = "<="
          hide_value = false
          palette    = "white_on_red"
          value      = 0
        }
        formula {
          formula_expression = "query1"
        }
        query {
          metric_query {
            aggregator  = "sum"
            data_source = "metrics"
            name        = "query1"
            query       = "avg:notification_service.created_meta_notification{$env}.as_count()"
          }
        }
      }
      timeseries_background {
        type = "bars"
      }
    }
    widget_layout {
      height          = 3
      is_column_break = false
      width           = 3
      x               = 9
      y               = 0
    }
  }
  widget {
    sunburst_definition {
      hide_total  = false
      title       = "Sent events by DSP"
      title_align = "left"
      title_size  = "16"
      legend_table {
        type = "table"
      }
      request {
        formula {
          formula_expression = "query1"
          limit {
            count = 0
            order = "desc"
          }
        }
        query {
          metric_query {
            aggregator  = "sum"
            data_source = "metrics"
            name        = "query1"
            query       = "sum:notification_service.sent_notifications{$env,$dsp,$notification_type,$event_type} by {dsp}.as_count()"
          }
        }
      }
    }
    widget_layout {
      height          = 3
      is_column_break = false
      width           = 4
      x               = 0
      y               = 3
    }
  }
  widget {
    sunburst_definition {
      hide_total  = false
      title       = "Sent events by type"
      title_align = "left"
      title_size  = "16"
      legend_table {
        type = "table"
      }
      request {
        formula {
          formula_expression = "query1"
          limit {
            count = 0
            order = "desc"
          }
        }
        query {
          metric_query {
            aggregator  = "sum"
            data_source = "metrics"
            name        = "query1"
            query       = "sum:notification_service.sent_notifications{$env,$notification_type,$dsp,$event_type} by {notification_type}.as_count()"
          }
        }
      }
    }
    widget_layout {
      height          = 3
      is_column_break = false
      width           = 4
      x               = 4
      y               = 3
    }
  }
  widget {
    sunburst_definition {
      hide_total  = false
      title       = "Sent events by event type"
      title_align = "left"
      title_size  = "16"
      legend_table {
        type = "table"
      }
      request {
        formula {
          formula_expression = "query1"
        }
        query {
          metric_query {
            aggregator  = "sum"
            data_source = "metrics"
            name        = "query1"
            query       = "sum:notification_service.sent_notifications{$env,$event_type,$dsp,$notification_type} by {event_type}.as_count()"
          }
        }
      }
    }
    widget_layout {
      height          = 3
      is_column_break = false
      width           = 4
      x               = 8
      y               = 3
    }
  }
  widget {
    query_value_definition {
      autoscale   = true
      precision   = 2
      title       = "Count of failed notifications by stuck reason (after 3 attempts)"
      title_align = "left"
      title_size  = "16"
      request {
        conditional_formats {
          comparator = ">"
          hide_value = false
          palette    = "white_on_red"
          value      = 0
        }
        conditional_formats {
          comparator = "<="
          hide_value = false
          palette    = "white_on_green"
          value      = 0
        }
        formula {
          formula_expression = "query1"
        }
        query {
          metric_query {
            aggregator  = "sum"
            data_source = "metrics"
            name        = "query1"
            query       = "avg:notification_service.failed_stuck_meta_notifications{$env}"
          }
        }
      }
      timeseries_background {
        type = "bars"
      }
    }
    widget_layout {
      height          = 3
      is_column_break = false
      width           = 3
      x               = 0
      y               = 6
    }
  }
  widget {
    query_value_definition {
      autoscale   = true
      precision   = 2
      title       = "Count of fails in sync manager DTF"
      title_align = "left"
      title_size  = "16"
      request {
        conditional_formats {
          comparator = ">"
          hide_value = false
          palette    = "white_on_red"
          value      = 0
        }
        conditional_formats {
          comparator = "<="
          hide_value = false
          palette    = "white_on_green"
          value      = 0
        }
        formula {
          formula_expression = "query1"
        }
        query {
          metric_query {
            aggregator  = "sum"
            data_source = "metrics"
            name        = "query1"
            query       = "sum:notification_service.sync_manager_dtf_failed_count{$env}.weighted()"
          }
        }
      }
      timeseries_background {
        type = "bars"
      }
    }
    widget_layout {
      height          = 3
      is_column_break = false
      width           = 3
      x               = 3
      y               = 6
    }
  }
  widget {
    query_value_definition {
      autoscale   = true
      precision   = 2
      title       = "Count of fails in sync manager SF"
      title_align = "left"
      title_size  = "16"
      request {
        conditional_formats {
          comparator = ">"
          hide_value = false
          palette    = "white_on_red"
          value      = 0
        }
        conditional_formats {
          comparator = "<="
          hide_value = false
          palette    = "white_on_green"
          value      = 0
        }
        formula {
          formula_expression = "query1"
        }
        query {
          metric_query {
            aggregator  = "sum"
            data_source = "metrics"
            name        = "query1"
            query       = "sum:notification_service.sync_manager_sf_failed_count{$env}"
          }
        }
      }
      timeseries_background {
        type = "bars"
      }
    }
    widget_layout {
      height          = 3
      is_column_break = false
      width           = 3
      x               = 6
      y               = 6
    }
  }
  widget {
    query_value_definition {
      autoscale   = true
      precision   = 2
      title       = "Count of timed out notifications by stuck reason"
      title_align = "left"
      title_size  = "16"
      request {
        formula {
          formula_expression = "query1"
        }
        query {
          metric_query {
            aggregator  = "sum"
            data_source = "metrics"
            name        = "query1"
            query       = "avg:notification_service.timeout_stuck_meta_notifications{$env}"
          }
        }
      }
      timeseries_background {
        type = "bars"
      }
    }
    widget_layout {
      height          = 3
      is_column_break = false
      width           = 3
      x               = 9
      y               = 6
    }
  }
  widget {
    query_value_definition {
      autoscale   = true
      precision   = 2
      title       = "Count of timed out notifications by stuck (Created) reason"
      title_align = "left"
      title_size  = "16"
      request {
        formula {
          formula_expression = "query1"
        }
        query {
          metric_query {
            aggregator  = "sum"
            data_source = "metrics"
            name        = "query1"
            query       = "avg:notification_service.timeout_stuck_created_meta_notifications{$env}"
          }
        }
      }
      timeseries_background {
        type = "bars"
      }
    }
    widget_layout {
      height          = 3
      is_column_break = false
      width           = 3
      x               = 0
      y               = 9
    }
  }
}
