{{- $reportingValues :=  index .Values "openshift-reporting" -}}
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: cluster-cpu-capacity-raw
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: timestamp
    type: timestamp
    unit: date
  - name: dt
    type: varchar
  - name: cpu_cores
    type: double
  - name: cpu_core_seconds
    type: double
  - name: node_count
    type: double
  inputs:
  - name: NodeCpuCapacityRawDataSourceName
    type: ReportDataSource
    default: node-cpu-capacity-raw
  query: |
    SELECT
      "timestamp",
      dt,
      sum(node_capacity_cpu_cores) as cpu_cores,
      sum(node_capacity_cpu_core_seconds) as cpu_core_seconds,
      count(*) AS node_count
    FROM {| dataSourceTableName .Report.Inputs.NodeCpuCapacityRawDataSourceName |}
    GROUP BY "timestamp", dt
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: cluster-cpu-capacity
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  - name: total_cluster_capacity_cpu_core_seconds
    type: double
    unit: cpu_core_seconds
  - name: avg_cluster_capacity_cpu_cores
    type: double
    unit: cpu_cores
  - name: avg_node_count
    type: double
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: ClusterCpuCapacityReportName
    type: Report
  - name: ClusterCpuCapacityRawDataSourceName
    type: ReportDataSource
    default: cluster-cpu-capacity-raw
  query: |
    SELECT
      timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prestoTimestamp |}' AS period_start,
      timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}' AS period_end,
    {|- if .Report.Inputs.ClusterCpuCapacityReportName |}
      sum(total_cluster_capacity_cpu_core_seconds) as total_cluster_capacity_cpu_core_seconds,
      avg(avg_cluster_capacity_cpu_cores) as avg_cluster_capacity_cpu_cores,
      avg(avg_node_count) AS avg_node_count
      FROM {| .Report.Inputs.ClusterCpuCapacityReportName | reportTableName |}
      WHERE period_start  >= timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prestoTimestamp |}'
      AND period_end <= timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}'
    {|- else |}
      sum(cpu_core_seconds)  as total_cluster_capacity_cpu_core_seconds,
      avg(cpu_cores) as avg_cluster_capacity_cpu_cores,
      avg(node_count) AS avg_node_count
      FROM {| dataSourceTableName .Report.Inputs.ClusterCpuCapacityRawDataSourceName |}
      WHERE "timestamp"  >= timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prestoTimestamp |}'
      AND "timestamp" < timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}'
      AND dt >= '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prometheusMetricPartitionFormat |}'
      AND dt <= '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prometheusMetricPartitionFormat |}'
    {|- end |}
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: cluster-memory-capacity-raw
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: timestamp
    type: timestamp
    unit: date
  - name: dt
    type: varchar
  - name: memory_bytes
    type: double
  - name: memory_byte_seconds
    type: double
  - name: node_count
    type: double
  inputs:
  - name: NodeMemoryCapacityRawDataSourceName
    type: ReportDataSource
    default: node-memory-capacity-raw
  query: |
    SELECT
      "timestamp",
      dt,
      sum(node_capacity_memory_bytes) as memory_bytes,
      sum(node_capacity_memory_byte_seconds) as memory_byte_seconds,
      count(*) AS node_count
    FROM {| dataSourceTableName .Report.Inputs.NodeMemoryCapacityRawDataSourceName |}
    GROUP BY "timestamp", dt
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: cluster-memory-capacity
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  - name: total_cluster_capacity_memory_byte_seconds
    type: double
    unit: memory_byte_seconds
  - name: avg_cluster_capacity_memory_bytes
    type: double
    unit: memory_bytes
  - name: avg_node_count
    type: double
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: ClusterMemoryCapacityReportName
    type: Report
  - name: ClusterMemoryCapacityRawDataSourceName
    type: ReportDataSource
    default: cluster-memory-capacity-raw
  query: |
    SELECT
      timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prestoTimestamp |}' AS period_start,
      timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}' AS period_end,
    {|- if .Report.Inputs.ClusterMemoryCapacityReportName |}
      sum(total_cluster_capacity_memory_byte_seconds) as total_cluster_capacity_memory_byte_seconds,
      avg(avg_cluster_capacity_memory_bytes) as avg_cluster_capacity_memory_bytes,
      avg(avg_node_count) AS avg_node_count
      FROM {| .Report.Inputs.ClusterMemoryCapacityReportName | reportTableName |}
      WHERE period_start  >= timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prestoTimestamp |}'
      AND period_end <= timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}'
    {|- else |}
      sum(memory_byte_seconds)  as total_cluster_capacity_memory_byte_seconds,
      avg(memory_bytes) as avg_cluster_capacity_memory_bytes,
      avg(node_count) AS avg_node_count
      FROM {| dataSourceTableName .Report.Inputs.ClusterMemoryCapacityRawDataSourceName |}
      WHERE "timestamp"  >= timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prestoTimestamp |}'
      AND "timestamp" < timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}'
      AND dt >= '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prometheusMetricPartitionFormat |}'
      AND dt <= '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prometheusMetricPartitionFormat |}'
    {|- end |}
