resource "datadog_dashboard" "slz_contentstatus_timeboard_dashboard" {
  layout_type = "ordered"
  title       = "SLZ - ContentStatus Timeboard"
  url         = "/dashboard/6cq-8qw-8zx/slz---contentstatus-timeboard"
  template_variable {
    defaults = ["*"]
    name     = "host"
    prefix   = "host"
  }
  template_variable {
    defaults = ["production"]
    name     = "environment"
    prefix   = "environment"
  }
  widget {
    group_definition {
      layout_type = "ordered"
      show_title  = false
      title       = "All DSPs"
      widget {
        toplist_definition {
          title = "Content Status - All"
          request {
            q = "top(count:slz.content_status.content_status{$host,$environment} by {content_status_obj.content_status}.as_count(), 10, 'sum', 'desc')"
            style {
              palette = "dog_classic"
            }
          }
        }
      }
      widget {
        timeseries_definition {
          show_legend = false
          title       = "Content Status - All"
          request {
            display_type   = "line"
            on_right_yaxis = false
            q              = "top(count:slz.content_status.content_status{$host,$environment} by {content_status_obj.content_status}.as_count(), 10, 'sum', 'desc')"
            style {
              line_type  = "solid"
              line_width = "normal"
              palette    = "dog_classic"
            }
          }
        }
      }
    }
  }
  widget {
    group_definition {
      layout_type = "ordered"
      show_title  = false
      title       = "Spotify"
      widget {
        toplist_definition {
          title = "Content Status - Spotify"
          request {
            q = "top(count:slz.content_status.content_status{$host,job.dsp:spotify,$environment} by {content_status_obj.content_status}.as_count(), 10, 'sum', 'desc')"
            style {
              palette = "dog_classic"
            }
          }
        }
      }
      widget {
        timeseries_definition {
          show_legend = false
          title       = "Content Status - Spotify"
          request {
            display_type   = "line"
            on_right_yaxis = false
            q              = "top(count:slz.content_status.content_status{$host,job.dsp:spotify,$environment} by {content_status_obj.content_status}.as_count(), 10, 'sum', 'desc')"
            style {
              line_type  = "solid"
              line_width = "normal"
              palette    = "dog_classic"
            }
          }
        }
      }
    }
  }
  widget {
    group_definition {
      layout_type = "ordered"
      show_title  = false
      title       = "Apple"
      widget {
        toplist_definition {
          title = "Content Status - Apple"
          request {
            q = "top(sum:slz.content_status.content_status{$host,job.dsp:apple,$environment} by {content_status_obj.content_status}.as_count(), 10, 'sum', 'desc')"
            style {
              palette = "dog_classic"
            }
          }
        }
      }
      widget {
        timeseries_definition {
          show_legend = false
          title       = "Content Status - Apple"
          request {
            display_type   = "line"
            on_right_yaxis = false
            q              = "top(count:slz.content_status.content_status{$host,job.dsp:apple,$environment} by {content_status_obj.content_status}.as_count(), 10, 'sum', 'desc')"
            style {
              line_type  = "solid"
              line_width = "normal"
              palette    = "dog_classic"
            }
          }
        }
      }
    }
  }
}

resource "datadog_dashboard" "slz_unitofwork_timeboard_dashboard" {
  layout_type = "ordered"
  title       = "SLZ - UnitOfWork Timeboard"
  url         = "/dashboard/px4-a4i-zzc/slz---unitofwork-timeboard"
  template_variable {
    defaults = ["*"]
    name     = "host"
    prefix   = "host"
  }
  template_variable {
    defaults = ["*"]
    name     = "environment"
    prefix   = "environment"
  }
  widget {
    group_definition {
      layout_type = "ordered"
      show_title  = false
      title       = "All DSPs"
      widget {
        toplist_definition {
          title = "Completeness Status - All"
          request {
            q = "top(count:slz.uow_item.completeness_status{$host,$environment} by {uow_item.completeness_status}.as_count(), 10, 'sum', 'desc')"
            style {
              palette = "dog_classic"
            }
          }
        }
      }
      widget {
        timeseries_definition {
          show_legend = false
          title       = "Completeness Status - All"
          request {
            display_type   = "line"
            on_right_yaxis = false
            q              = "count:slz.uow_item.completeness_status{$host,$environment} by {uow_item.completeness_status}.as_count()"
            style {
              line_type  = "solid"
              line_width = "normal"
              palette    = "dog_classic"
            }
          }
        }
      }
    }
  }
  widget {
    group_definition {
      layout_type = "ordered"
      show_title  = false
      title       = "Spotify"
      widget {
        toplist_definition {
          title = "Completeness Status - Spotify"
          request {
            q = "top(count:slz.uow_item.completeness_status{dsp:spotify,$host,$environment} by {uow_item.uow_id,uow_item.completeness_status}.as_count(), 10, 'sum', 'desc')"
            style {
              palette = "dog_classic"
            }
          }
        }
      }
      widget {
        timeseries_definition {
          show_legend = false
          title       = "Completeness Status - Spotify"
          request {
            display_type   = "line"
            on_right_yaxis = false
            q              = "top(count:slz.uow_item.completeness_status{dsp:spotify,$host,$environment} by {uow_item.completeness_status}.as_count(), 10, 'sum', 'desc')"
            style {
              line_type  = "solid"
              line_width = "normal"
              palette    = "dog_classic"
            }
          }
        }
      }
    }
  }
  widget {
    group_definition {
      layout_type = "ordered"
      show_title  = false
      title       = "Apple"
      widget {
        toplist_definition {
          title = "Completeness Status - Apple"
          request {
            q = "top(count:slz.uow_item.completeness_status{dsp:apple,$host,$environment} by {uow_item.completeness_status}.as_count(), 10, 'sum', 'desc')"
            style {
              palette = "dog_classic"
            }
          }
        }
      }
      widget {
        timeseries_definition {
          show_legend = false
          title       = "Completeness Status - Apple"
          request {
            display_type   = "line"
            on_right_yaxis = false
            q              = "top(count:slz.uow_item.completeness_status{dsp:apple,$host,$environment} by {uow_item.completeness_status}.as_count(), 10, 'sum', 'desc')"
            style {
              line_type  = "solid"
              line_width = "normal"
              palette    = "dog_classic"
            }
          }
        }
      }
    }
  }
}

resource "datadog_dashboard" "slz_uow_active_for_n_days_dashboard" {
  description = <<-EOT
      ## SLZ-UoW data monitoring.

      SLZ-UoW that are not received data for 1 calender day in last 7 calender days. JIRA DLP-1701.


      - [This might link to a dashboard](#)
      - [This might link to a wiki](#)
        EOT
  layout_type = "ordered"
  title       = "SLZ-UoW active for 'N' days."
  url         = "/dashboard/iqb-emt-fgp/slz-uow-active-for-n-days"
  template_variable {
    defaults = ["*"]
    name     = "var"
    prefix   = "dsp"
  }
  widget {
    alert_graph_definition {
      alert_id = "15761573"
      title    = "Alert: SLZ-UoW Staying active 'N' days"
      viz_type = "timeseries"
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      show_legend = false
      title       = "Env:PROD slz.uow_item.completeness_status.min_complete with dsp:amazonadsuppo..."
      request {
        display_type   = "bars"
        on_right_yaxis = false
        q              = "avg:slz.uow_item.completeness_status.min_complete{dsp:amazonadsupported}.as_count()"
      }
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      show_legend = false
      title       = "Env:PROD slz.uow_item.completeness_status.min_complete with dsp:amazonmusicun..."
      request {
        display_type   = "bars"
        on_right_yaxis = false
        q              = "avg:slz.uow_item.completeness_status.min_complete{dsp:amazonmusicunlimited}.as_count()"
      }
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      show_legend = false
      title       = "Env:PROD slz.uow_item.completeness_status.min_complete with dsp:amazonprime"
      request {
        display_type   = "bars"
        on_right_yaxis = false
        q              = "avg:slz.uow_item.completeness_status.min_complete{dsp:amazonprime}.as_count()"
      }
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      show_legend = false
      title       = "Env:PROD slz.uow_item.completeness_status.min_complete with dsp:apple"
      request {
        display_type   = "bars"
        on_right_yaxis = false
        q              = "avg:slz.uow_item.completeness_status.min_complete{dsp:apple}.as_count()"
      }
    }
  }
}

resource "datadog_dashboard" "slz_uow_content_status_dashboard" {
  layout_type = "free"
  title       = "SLZ - UoW & Content Status Dashboard"
  url         = "/dashboard/k7d-6up-prp/slz---uow--content-status-dashboard"
  template_variable {
    defaults = ["*"]
    name     = "source"
    prefix   = "source"
  }
  template_variable {
    defaults = ["*"]
    name     = "host"
    prefix   = "host"
  }
  template_variable {
    defaults = ["*"]
    name     = "report_type"
    prefix   = "@report_type"
  }
  template_variable {
    defaults = ["*"]
    name     = "environment"
    prefix   = "environment"
  }
  widget {
    manage_status_definition {
      color_preference    = "text"
      display_format      = "countsAndList"
      hide_zero_counts    = true
      query               = "tag:delphi"
      show_last_triggered = false
      show_priority       = false
      sort                = "status,asc"
      title               = "Monitors Summary"
      title_align         = "left"
      title_size          = "13"
    }
    widget_layout {
      height          = 19
      is_column_break = false
      width           = 24
      x               = 87
      y               = 10
    }
  }
  widget {
    free_text_definition {
      color      = "#4d4d4d"
      font_size  = "auto"
      text       = "Logging Metrics"
      text_align = "left"
    }
    widget_layout {
      height          = 6
      is_column_break = false
      width           = 24
      x               = 7
      y               = 1
    }
  }
  widget {
    query_value_definition {
      autoscale   = true
      precision   = 0
      title       = "Log Count"
      title_align = "left"
      title_size  = "16"
      request {
        aggregator = "avg"
        log_query {
          index        = "main"
          search_query = "$source $host $report_type $environment"
          compute_query {
            aggregation = "count"
            interval    = 0
          }
        }
      }
    }
    widget_layout {
      height          = 7
      is_column_break = false
      width           = 11
      x               = 35
      y               = 1
    }
  }
  widget {
    log_stream_definition {
      columns = [
        "tag_environment",
        "log_job.dsp",
        "log_content_status_obj.uow_id",
        "log_content_status_obj.failure_count",
        "log_content_status_obj.failure_description",
      ]
      query               = "@content_status_obj.failure_description:* "
      show_date_column    = false
      show_message_column = false
      title               = "Failure Logs"
      title_align         = "left"
      title_size          = "16"
    }
    widget_layout {
      height          = 33
      is_column_break = false
      width           = 103
      x               = 7
      y               = 67
    }
  }
  widget {
    toplist_definition {
      title       = "Content Status - All"
      title_align = "left"
      title_size  = "16"
      request {
        q = "top(count:slz.content_status.content_status{$host,$environment,$report_type,$source} by {content_status_obj.content_status}.as_count(), 10, 'sum', 'desc')"
        style {
          palette = "dog_classic"
        }
      }
    }
    widget_layout {
      height          = 17
      is_column_break = false
      width           = 39
      x               = 47
      y               = 10
    }
  }
  widget {
    toplist_definition {
      title       = "Completeness Status - Spotify"
      title_align = "left"
      title_size  = "16"
      request {
        q = "top(count:slz.uow_item.completeness_status{dsp:spotify,$host,$environment,$report_type,$source} by {uow_item.completeness_status}.as_count(), 10, 'sum', 'desc')"
        style {
          palette = "dog_classic"
        }
      }
    }
    widget_layout {
      height          = 17
      is_column_break = false
      width           = 39
      x               = 7
      y               = 29
    }
  }
  widget {
    toplist_definition {
      title       = "Completeness Status - Apple"
      title_align = "left"
      title_size  = "16"
      request {
        q = "top(count:slz.uow_item.completeness_status{dsp:apple,$host,$environment,$report_type,$source} by {uow_item.completeness_status}.as_count(), 10, 'sum', 'desc')"
        style {
          palette = "dog_classic"
        }
      }
    }
    widget_layout {
      height          = 17
      is_column_break = false
      width           = 39
      x               = 7
      y               = 48
    }
  }
  widget {
    image_definition {
      has_background = false
      has_border     = false
      sizing         = "zoom"
      url            = "https://www.apple.com/v/apple-music/k/images/shared/og_image.png?201907151226"
    }
    widget_layout {
      height          = 6
      is_column_break = false
      width           = 6
      x               = 0
      y               = 48
    }
  }
  widget {
    image_definition {
      has_background = false
      has_border     = false
      sizing         = "zoom"
      url            = "https://developer.spotify.com/assets/branding-guidelines/icon1@2x.png"
    }
    widget_layout {
      height          = 6
      is_column_break = false
      width           = 6
      x               = 0
      y               = 29
    }
  }
  widget {
    toplist_definition {
      title       = "Content Status - Spotify"
      title_align = "left"
      title_size  = "16"
      request {
        q = "top(count:slz.content_status.content_status{$host,job.dsp:spotify,$environment,$source,$report_type} by {content_status_obj.content_status}.as_count(), 10, 'sum', 'desc')"
        style {
          palette = "dog_classic"
        }
      }
    }
    widget_layout {
      height          = 17
      is_column_break = false
      width           = 39
      x               = 47
      y               = 29
    }
  }
  widget {
    toplist_definition {
      title       = "Content Status - Apple"
      title_align = "left"
      title_size  = "16"
      request {
        q = "top(count:slz.content_status.content_status{$host,$environment,job.dsp:apple,$source,$report_type} by {content_status_obj.content_status}.as_count(), 10, 'sum', 'desc')"
        style {
          palette = "dog_classic"
        }
      }
    }
    widget_layout {
      height          = 17
      is_column_break = false
      width           = 39
      x               = 47
      y               = 48
    }
  }
  widget {
    toplist_definition {
      title       = "Completeness Status - All"
      title_align = "left"
      title_size  = "16"
      request {
        q = "top(count:slz.uow_item.completeness_status{$host,$environment,$source,$report_type} by {uow_item.completeness_status}.as_count(), 10, 'sum', 'desc')"
        style {
          palette = "dog_classic"
        }
      }
    }
    widget_layout {
      height          = 17
      is_column_break = false
      width           = 39
      x               = 7
      y               = 10
    }
  }
}
