{{- $reportingValues :=  index .Values "openshift-reporting" -}}
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: cluster-cpu-usage-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: pod_count
    type: double
  inputs:
  - name: PodCpuUsageRawDataSourceName
    type: ReportDataSource
    default: pod-cpu-usage-raw
  query: |
    SELECT
      "timestamp",
      dt,
      sum(pod_usage_cpu_cores) as cpu_cores,
      sum(pod_usage_cpu_core_seconds) as cpu_core_seconds,
      count(*) AS pod_count
    FROM {| dataSourceTableName .Report.Inputs.PodCpuUsageRawDataSourceName |}
    GROUP BY "timestamp", dt
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: cluster-cpu-usage
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  - name: total_cluster_usage_cpu_core_seconds
    type: double
    unit: cpu_core_seconds
  - name: avg_cluster_usage_cpu_cores
    type: double
    unit: cpu_cores
  - name: avg_pod_count
    type: double
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: ClusterCpuUsageReportName
    type: Report
  - name: ClusterCpuUsageRawDataSourceName
    type: ReportDataSource
    default: cluster-cpu-usage-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.ClusterCpuUsageReportName |}
      sum(total_cluster_usage_cpu_core_seconds) as total_cluster_usage_cpu_core_seconds,
      avg(avg_cluster_usage_cpu_cores) as avg_cluster_usage_cpu_cores,
      avg(avg_pod_count) AS avg_pod_count
      FROM {| .Report.Inputs.ClusterCpuUsageReportName | 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_usage_cpu_core_seconds,
      avg(cpu_cores) as avg_cluster_usage_cpu_cores,
      avg(pod_count) AS avg_pod_count
      FROM {| dataSourceTableName .Report.Inputs.ClusterCpuUsageRawDataSourceName |}
      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-usage-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: pod_count
    type: double
  inputs:
  - name: PodMemoryUsageRawDataSourceName
    type: ReportDataSource
    default: pod-memory-usage-raw
  query: |
    SELECT
      "timestamp",
      dt,
      sum(pod_usage_memory_bytes) as memory_bytes,
      sum(pod_usage_memory_byte_seconds) as memory_byte_seconds,
      count(*) AS pod_count
    FROM {| dataSourceTableName .Report.Inputs.PodMemoryUsageRawDataSourceName |}
    GROUP BY "timestamp", dt
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: cluster-memory-usage
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  - name: total_cluster_usage_memory_byte_seconds
    type: double
    unit: memory_byte_seconds
  - name: avg_cluster_usage_memory_bytes
    type: double
    unit: memory_bytes
  - name: avg_pod_count
    type: double
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: ClusterMemoryUsageReportName
    type: Report
  - name: ClusterMemoryUsageRawDataSourceName
    type: ReportDataSource
    default: cluster-memory-usage-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.ClusterMemoryUsageReportName |}
      sum(total_cluster_usage_memory_byte_seconds) as total_cluster_usage_memory_byte_seconds,
      avg(avg_cluster_usage_memory_bytes) as avg_cluster_usage_memory_bytes,
      avg(avg_pod_count) AS avg_pod_count
      FROM {| .Report.Inputs.ClusterMemoryUsageReportName | 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_usage_memory_byte_seconds,
      avg(memory_bytes) as avg_cluster_usage_memory_bytes,
      avg(pod_count) AS avg_pod_count
      FROM {| dataSourceTableName .Report.Inputs.ClusterMemoryUsageRawDataSourceName |}
      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 |}
