resource "datadog_dashboard" "dot_net_jobs_monitoring_outdated_dashboard" {
  description = <<-EOT
      ## Title

      Describe this dashboard. Add links to other dashboards, monitors, wikis,  and docs to help your teammates. Markdown is supported.

      - [This might link to a dashboard](#)
      - [This might link to a wiki](#)
        EOT
  layout_type = "ordered"
  reflow_type = "auto"
  title       = ".NET jobs monitoring (outdated)"
  url         = "/dashboard/vbb-qfz-gvc/net-jobs-monitoring-outdated"
  widget {
    timeseries_definition {
      legend_layout = "auto"
      legend_size   = "0"
      show_legend   = true
      title         = "host:PROD-STREAMS"
      marker {
        display_type = "error dashed"
        value        = "y = 0"
      }
      request {
        display_type   = "line"
        on_right_yaxis = false
        log_query {
          index        = "*"
          search_query = "service:TaskScheduler @Event.EventData.Name:(ActionStart OR ActionSuccess) @Event.EventData.Data.TaskName:*Filtr* host:PROD-STREAMS"
          compute_query {
            aggregation = "count"
            interval    = 0
          }
          group_by {
            facet = "@Event.EventData.Data.TaskName"
            limit = 100
            sort_query {
              aggregation = "count"
              order       = "desc"
            }
          }
        }
        style {
          line_type  = "solid"
          line_width = "thin"
          palette    = "dog_classic"
        }
      }
      yaxis {
        include_zero = false
        min          = ".5"
        scale        = "log"
      }
    }
  }
  widget {
    timeseries_definition {
      legend_layout = "auto"
      legend_size   = "0"
      show_legend   = true
      title         = "host:PROD-GENERAL"
      request {
        display_type   = "line"
        on_right_yaxis = false
        log_query {
          index        = "*"
          search_query = "service:TaskScheduler @Event.EventData.Name:(ActionStart OR ActionSuccess) @Event.EventData.Data.TaskName:*Filtr* host:PROD-GENERAL"
          compute_query {
            aggregation = "count"
            interval    = 0
          }
          group_by {
            facet = "@Event.EventData.Data.TaskName"
            limit = 100
            sort_query {
              aggregation = "count"
              order       = "desc"
            }
          }
        }
        style {
          line_type  = "solid"
          line_width = "thin"
          palette    = "dog_classic"
        }
      }
      yaxis {
        include_zero = false
        min          = ".5"
        scale        = "log"
      }
    }
  }
  widget {
    timeseries_definition {
      legend_layout = "auto"
      legend_size   = "0"
      show_legend   = true
      title         = "host:PROD-PLAYLISTINFO"
      request {
        display_type   = "line"
        on_right_yaxis = false
        log_query {
          index        = "*"
          search_query = "service:TaskScheduler @Event.EventData.Name:(ActionStart OR ActionSuccess) @Event.EventData.Data.TaskName:*Filtr* host:PROD-PLAYLISTINFO"
          compute_query {
            aggregation = "count"
            interval    = 0
          }
          group_by {
            facet = "@Event.EventData.Data.TaskName"
            limit = 100
            sort_query {
              aggregation = "count"
              order       = "desc"
            }
          }
        }
        style {
          line_type  = "solid"
          line_width = "thin"
          palette    = "dog_classic"
        }
      }
      yaxis {
        include_zero = false
        min          = ".5"
        scale        = "log"
      }
    }
  }
}

resource "datadog_dashboard" "apollo_scheduled_jobs_dashboard" {
  description = <<-EOT
            ## Apollo Scheduled Jobs

            execution time and status
        EOT
  layout_type = "ordered"
  reflow_type = "fixed"
  title       = "Apollo Scheduled Jobs monitoring"
  url         = "/dashboard/bfh-x5d-ysg/apollo-scheduled-jobs-monitoring"
  template_variable {
    available_values = [
      "dev-apollo-clu",
      "prod-apollo-clu",
    ]
    defaults = [
      "dev-apollo-clu",
    ]
    name   = "ecs_cluster"
    prefix = "ecs_cluster"
  }
  template_variable_preset {
    name = "dev-last-week"
  }
  template_variable_preset {
    name = "prod-last-week"
    template_variable {
      name = "ecs_cluster"
      values = [
        "prod-apollo-clu",
      ]
    }
  }
  widget {
    free_text_definition {
      color      = "#4d4d4d"
      font_size  = "24"
      text       = "Data Cleaner"
      text_align = "left"
    }
    widget_layout {
      height          = 1
      is_column_break = false
      width           = 12
      x               = 0
      y               = 0
    }
  }
  widget {
    query_table_definition {
      title       = "Latest execution time"
      title_align = "left"
      title_size  = "16"
      request {
        limit = 0
        formula {
          alias              = "seconds"
          formula_expression = "query1"
          limit {
            count = 500
            order = "desc"
          }
        }
        formula {
          alias              = "minutes"
          formula_expression = "query1 / 60"
        }
        query {
          metric_query {
            aggregator  = "last"
            data_source = "metrics"
            name        = "query1"
            query       = "avg:dogwrap.duration{event_name:apollo-data-cleaner,cluster:$ecs_cluster.value} by {model}"
          }
        }
      }
    }
    widget_layout {
      height          = 4
      is_column_break = false
      width           = 6
      x               = 0
      y               = 1
    }
  }
  widget {
    event_stream_definition {
      event_size     = "s"
      query          = "tags:(\"service:data_cleaner\" AND cluster:$ecs_cluster.value) source:my_apps"
      tags_execution = "and"
      title_align    = "left"
      title_size     = "16"
    }
    widget_layout {
      height          = 4
      is_column_break = false
      width           = 6
      x               = 6
      y               = 1
    }
  }
  widget {
    timeseries_definition {
      legend_columns = [
        "avg",
        "max",
        "min",
        "sum",
        "value",
      ]
      legend_layout = "auto"
      show_legend   = true
      title         = "SpotifyPlaylistStatistics"
      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       = "max:dogwrap.duration{model:apollo_main_db.spotify.models.spotifyplayliststatistics,cluster:$ecs_cluster.value}"
          }
        }
        style {
          line_type  = "solid"
          line_width = "normal"
          palette    = "dog_classic"
        }
      }
      yaxis {
        include_zero = true
        max          = "auto"
        min          = "auto"
        scale        = "linear"
      }
    }
    widget_layout {
      height          = 2
      is_column_break = false
      width           = 4
      x               = 0
      y               = 5
    }
  }
  widget {
    timeseries_definition {
      legend_columns = [
        "avg",
        "max",
        "min",
        "sum",
        "value",
      ]
      legend_layout = "auto"
      show_legend   = true
      title         = "AppleMusicPlaylistStatistics"
      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       = "max:dogwrap.duration{model:apollo_main_db.apple.models.applemusicplayliststatistics,cluster:$ecs_cluster.value}"
          }
        }
        style {
          line_type  = "solid"
          line_width = "normal"
          palette    = "dog_classic"
        }
      }
      yaxis {
        include_zero = true
        max          = "auto"
        min          = "auto"
        scale        = "linear"
      }
    }
    widget_layout {
      height          = 2
      is_column_break = false
      width           = 4
      x               = 4
      y               = 5
    }
  }
  widget {
    timeseries_definition {
      legend_columns = [
        "avg",
        "max",
        "min",
        "sum",
        "value",
      ]
      legend_layout = "auto"
      show_legend   = true
      title         = "PushMessageReduced"
      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       = "max:dogwrap.duration{model:apollo_main_db.push_notifications.models.pushmessagereduced,cluster:$ecs_cluster.value}"
          }
        }
        style {
          line_type  = "solid"
          line_width = "normal"
          palette    = "dog_classic"
        }
      }
      yaxis {
        include_zero = true
        max          = "auto"
        min          = "auto"
        scale        = "linear"
      }
    }
    widget_layout {
      height          = 2
      is_column_break = false
      width           = 4
      x               = 8
      y               = 5
    }
  }
  widget {
    timeseries_definition {
      legend_columns = [
        "avg",
        "max",
        "min",
        "sum",
        "value",
      ]
      legend_layout = "auto"
      show_legend   = true
      title         = "ApplePlaylistTracklistHistoryReduced2"
      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       = "max:dogwrap.duration{model:apollo_main_db.apple.models.appleplaylisttracklisthistoryreduced2,cluster:$ecs_cluster.value}"
          }
        }
        style {
          line_type  = "solid"
          line_width = "normal"
          palette    = "dog_classic"
        }
      }
      yaxis {
        include_zero = true
        max          = "auto"
        min          = "auto"
        scale        = "linear"
      }
    }
    widget_layout {
      height          = 2
      is_column_break = false
      width           = 4
      x               = 0
      y               = 7
    }
  }
  widget {
    timeseries_definition {
      legend_columns = [
        "avg",
        "max",
        "min",
        "sum",
        "value",
      ]
      legend_layout = "auto"
      show_legend   = true
      title         = "SpotifyPlaylistTrackListHistory2Reduced2"
      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       = "max:dogwrap.duration{model:apollo_main_db.spotify.models.spotifyplaylisttracklisthistory2reduced2,cluster:$ecs_cluster.value}"
          }
        }
        style {
          line_type  = "solid"
          line_width = "normal"
          palette    = "dog_classic"
        }
      }
      yaxis {
        include_zero = true
        max          = "auto"
        min          = "auto"
        scale        = "linear"
      }
    }
    widget_layout {
      height          = 2
      is_column_break = false
      width           = 4
      x               = 4
      y               = 7
    }
  }
  widget {
    free_text_definition {
      color      = "#4d4d4d"
      font_size  = "16"
      text       = "Scheduled jobs"
      text_align = "left"
    }
    widget_layout {
      height          = 1
      is_column_break = false
      width           = 12
      x               = 0
      y               = 9
    }
  }
  widget {
    query_table_definition {
      has_search_bar = "auto"
      title_align    = "left"
      title_size     = "16"
      request {
        limit = 0
        formula {
          alias              = "seconds"
          cell_display_mode  = "bar"
          formula_expression = "query1"
          limit {
            count = 500
            order = "desc"
          }
        }
        formula {
          alias              = "minutes"
          formula_expression = "query1 / 60"
        }
        query {
          metric_query {
            aggregator  = "avg"
            data_source = "metrics"
            name        = "query1"
            query       = "avg:dogwrap.duration{cluster:$ecs_cluster.value} by {service}"
          }
        }
      }
    }
    widget_layout {
      height          = 4
      is_column_break = false
      width           = 6
      x               = 0
      y               = 10
    }
  }
  widget {
    event_stream_definition {
      event_size     = "s"
      query          = "tags:(cluster:$ecs_cluster.value) source:my_apps"
      tags_execution = "and"
      title_align    = "left"
      title_size     = "16"
    }
    widget_layout {
      height          = 4
      is_column_break = false
      width           = 6
      x               = 6
      y               = 10
    }
  }
}
