resource "datadog_dashboard" "gcp_bigtable_metrics" {
  title       = "GCP BigTable"
  description = "GCP BigTable metrics. Managed by Terraform"
  layout_type = "ordered"

  template_variable {
    name     = "cluster"
    defaults = ["delphi-ca-ssd-us-east4-a"]
    prefix   = "cluster"
  }

  template_variable_preset {
    name = "delphi-prod"
    template_variable {
      name   = "cluster"
      values = ["delphi-ca-ssd-us-east4-a"]
    }
  }

  template_variable_preset {
    name = "delphi-stage"
    template_variable {
      name   = "cluster"
      values = ["stg-delphi-ca-ssd-us-east4-b"]
    }
  }

  widget {
    group_definition {
      layout_type = "ordered"
      title       = "CPU Utilization"
      show_title  = false

      widget {
        timeseries_definition {
          title         = "CPU load of a cluster."
          legend_layout = "auto"
          legend_columns = [
            "avg",
            "max",
            "min",
            "sum",
            "value",
          ]
          request {
            q            = "avg:gcp.bigtable.cluster.cpu_load{$cluster}"
            display_type = "line"
            style {
              palette    = "dog_classic"
              line_type  = "solid"
              line_width = "normal"
            }
          }
          marker {
            value        = "y = 0.7"
            label        = "recommended max"
            display_type = "warning dashed"
          }
          yaxis {
            include_zero = true
            max          = "auto"
            min          = "auto"
            scale        = "linear"
          }
        }
      }

      widget {
        timeseries_definition {
          title         = "CPU load of the busiest node in a cluster."
          legend_layout = "auto"
          legend_columns = [
            "avg",
            "max",
            "min",
            "sum",
            "value",
          ]
          request {
            q            = "avg:gcp.bigtable.cluster.cpu_load_hottest_node{$cluster}"
            display_type = "line"
            style {
              palette    = "dog_classic"
              line_type  = "solid"
              line_width = "normal"
            }
          }
          marker {
            value        = "y = 0.9"
            label        = "recommended max"
            display_type = "warning dashed"
          }
          yaxis {
            include_zero = true
            max          = "auto"
            min          = "auto"
            scale        = "linear"
          }
        }
      }
    }
  }

  widget {
    group_definition {
      layout_type = "ordered"
      title       = "Storage usage"
      show_title  = false

      widget {
        timeseries_definition {
          title         = "Storage used as a fraction of total storage capacity."
          legend_layout = "auto"
          legend_columns = [
            "avg",
            "max",
            "min",
            "sum",
            "value",
          ]
          request {
            q            = "avg:gcp.bigtable.cluster.storage_utilization{$cluster}"
            display_type = "line"
            style {
              palette    = "dog_classic"
              line_type  = "solid"
              line_width = "normal"
            }
          }
          marker {
            value        = "y = 0.8"
            label        = "80% of cluster storage"
            display_type = "warning dashed"
          }
          yaxis {
            include_zero = true
            max          = "auto"
            min          = "auto"
            scale        = "linear"
          }
        }
      }

      widget {
        timeseries_definition {
          title         = "Capacity of a cluster vs amount of stored data."
          legend_layout = "auto"
          legend_columns = [
            "avg",
            "max",
            "min",
            "sum",
            "value",
          ]
          request {
            q            = "avg:gcp.bigtable.disk.bytes_used{$cluster}"
            display_type = "line"
            style {
              palette    = "dog_classic"
              line_type  = "solid"
              line_width = "normal"
            }
          }
          request {
            q            = "avg:gcp.bigtable.disk.storage_capacity{$cluster}"
            display_type = "line"
            style {
              palette    = "dog_classic"
              line_type  = "solid"
              line_width = "normal"
            }
          }
          yaxis {
            include_zero = true
            max          = "auto"
            min          = "auto"
            scale        = "linear"
          }
        }
      }

      widget {
        timeseries_definition {
          title         = "Amount of compressed data stored in a table."
          legend_layout = "auto"
          legend_columns = [
            "avg",
            "max",
            "min",
            "sum",
            "value",
          ]
          request {
            q            = "avg:gcp.bigtable.table.bytes_used{$cluster} by {table}"
            display_type = "area"
            style {
              palette    = "dog_classic"
              line_type  = "solid"
              line_width = "normal"
            }
          }
          yaxis {
            include_zero = true
            max          = "auto"
            min          = "auto"
            scale        = "linear"
          }
        }
      }

      widget {
        timeseries_definition {
          title         = "Amount of data used for backup."
          legend_layout = "auto"
          legend_columns = [
            "avg",
            "max",
            "min",
            "sum",
            "value",
          ]
          request {
            q            = "avg:gcp.bigtable.backup.bytes_used{$cluster} by {backup}"
            display_type = "area"
            style {
              palette    = "dog_classic"
              line_type  = "solid"
              line_width = "normal"
            }
          }
          yaxis {
            include_zero = true
            max          = "auto"
            min          = "auto"
            scale        = "linear"
          }
        }
      }
    }
  }

  widget {
    group_definition {
      layout_type = "ordered"
      show_title  = false

      widget {
        timeseries_definition {
          title         = "Number of uncompressed bytes of response data sent by servers for a table."
          legend_layout = "auto"
          legend_columns = [
            "avg",
            "max",
            "min",
            "sum",
            "value",
          ]
          request {
            q            = "avg:gcp.bigtable.server.sent_bytes_count{$cluster} by {table}"
            display_type = "line"
            style {
              palette    = "dog_classic"
              line_type  = "solid"
              line_width = "normal"
            }
          }
          yaxis {
            include_zero = true
            max          = "auto"
            min          = "auto"
            scale        = "linear"
          }
        }
      }

      widget {
        timeseries_definition {
          title         = "Number of uncompressed bytes of request data received by servers for a table."
          legend_layout = "auto"
          legend_columns = [
            "avg",
            "max",
            "min",
            "sum",
            "value",
          ]
          request {
            q            = "avg:gcp.bigtable.server.received_bytes_count{$cluster} by {table}"
            display_type = "line"
            style {
              palette    = "dog_classic"
              line_type  = "solid"
              line_width = "normal"
            }
          }
          yaxis {
            include_zero = true
            max          = "auto"
            min          = "auto"
            scale        = "linear"
          }
        }
      }
    }
  }

  widget {
    group_definition {
      layout_type = "ordered"
      show_title  = false

      widget {
        timeseries_definition {
          title         = "Number of rows returned by server requests for a table."
          legend_layout = "auto"
          legend_columns = [
            "avg",
            "max",
            "min",
            "sum",
            "value",
          ]
          request {
            q            = "avg:gcp.bigtable.server.returned_rows_count{$cluster} by {table}"
            display_type = "line"
            style {
              palette    = "dog_classic"
              line_type  = "solid"
              line_width = "normal"
            }
          }
          yaxis {
            include_zero = true
            max          = "auto"
            min          = "auto"
            scale        = "linear"
          }
        }
      }

      widget {
        timeseries_definition {
          title         = "Number of rows modified by server requests for a table."
          legend_layout = "auto"
          legend_columns = [
            "avg",
            "max",
            "min",
            "sum",
            "value",
          ]
          request {
            q            = "avg:gcp.bigtable.server.modified_rows_count{$cluster} by {table}"
            display_type = "line"
            style {
              palette    = "dog_classic"
              line_type  = "solid"
              line_width = "normal"
            }
          }
          yaxis {
            include_zero = true
            max          = "auto"
            min          = "auto"
            scale        = "linear"
          }
        }
      }

      widget {
        timeseries_definition {
          title         = "Distribution of server request latencies for a table."
          legend_layout = "auto"
          legend_columns = [
            "avg",
            "max",
            "min",
            "sum",
            "value",
          ]
          request {
            q            = "avg:gcp.bigtable.server.latencies.avg{$cluster} by {table}"
            display_type = "line"
            style {
              palette    = "dog_classic"
              line_type  = "solid"
              line_width = "normal"
            }
          }
          yaxis {
            include_zero = true
            max          = "auto"
            min          = "auto"
            scale        = "linear"
          }
        }
      }

      widget {
        timeseries_definition {
          title         = "Number of server requests for a table."
          legend_layout = "auto"
          legend_columns = [
            "avg",
            "max",
            "min",
            "sum",
            "value",
          ]
          request {
            q            = "avg:gcp.bigtable.server.request_count{$cluster} by {table}"
            display_type = "line"
            style {
              palette    = "dog_classic"
              line_type  = "solid"
              line_width = "normal"
            }
          }
          yaxis {
            include_zero = true
            max          = "auto"
            min          = "auto"
            scale        = "linear"
          }
        }
      }
    }
  }

  widget {
    timeseries_definition {
      title         = "Number of nodes in a cluster."
      legend_layout = "auto"
      legend_columns = [
        "avg",
        "max",
        "min",
        "sum",
        "value",
      ]
      request {
        q            = "avg:gcp.bigtable.cluster.node_count{$cluster}"
        display_type = "line"
        style {
          palette    = "dog_classic"
          line_type  = "solid"
          line_width = "normal"
        }
      }
      yaxis {
        include_zero = true
        max          = "auto"
        min          = "auto"
        scale        = "linear"
      }
    }
  }
}

resource "datadog_dashboard" "bigtable_errors_by_table_dashboard" {
  layout_type = "ordered"
  title       = "BigTable Errors"
  url         = "/dashboard/2ms-r8e-x2s/bigtable"
  widget {
    timeseries_definition {
      show_legend = false
      title       = "gcp.bigtable.server.error_count with table:amazon_music"
      request {
        display_type   = "bars"
        on_right_yaxis = false
        q              = "sum:gcp.bigtable.server.error_count{project_id:delphi-stage,table:amazon_music}.as_count()"
      }
    }
  }
  widget {
    timeseries_definition {
      show_legend = false
      title       = "gcp.bigtable.server.error_count with table:apple_music"
      request {
        display_type   = "bars"
        on_right_yaxis = false
        q              = "sum:gcp.bigtable.server.error_count{project_id:delphi-stage,table:apple_music}.as_count()"
      }
    }
  }
  widget {
    timeseries_definition {
      show_legend = false
      title       = "gcp.bigtable.server.error_count with table:spotify"
      request {
        display_type   = "bars"
        on_right_yaxis = false
        q              = "sum:gcp.bigtable.server.error_count{project_id:delphi-stage,table:spotify}.as_count()"
      }
    }
  }
}
