{{- $reportingValues :=  index .Values "openshift-reporting" -}}
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: cluster-cpu-utilization
  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: total_cluster_usage_cpu_core_seconds
    type: double
    unit: cpu_core_seconds
  - name: cluster_cpu_utilization_percent
    type: double
  - name: avg_cluster_capacity_cpu_cores
    type: double
    unit: cpu_cores
  - name: avg_cluster_usage_cpu_cores
    type: double
    unit: cpu_cores
  - name: avg_node_count
    type: double
  - name: avg_pod_count
    type: double
  - name: avg_pod_per_node_count
    type: double
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: ClusterCpuUtilizationReportName
    type: Report
  - name: ClusterCpuCapacityReportName
    type: Report
  - name: ClusterCpuUsageReportName
    type: Report
  - name: ClusterCpuCapacityQueryName
    type: ReportQuery
    default: cluster-cpu-capacity
  - name: ClusterCpuUsageQueryName
    type: ReportQuery
    default: cluster-cpu-usage
  query: |
    {|/* Handle aggregating a sub-report */|}
    {|- if .Report.Inputs.ClusterCpuUtilizationReportName -|}
    SELECT
      timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart| prestoTimestamp |}' AS period_start,
      timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}' AS period_end,
      sum(total_cluster_capacity_cpu_core_seconds) AS total_cluster_capacity_cpu_core_seconds,
      sum(total_cluster_usage_cpu_core_seconds) AS total_cluster_usage_cpu_core_seconds,
      avg(cluster_cpu_utilization_percent) AS cluster_cpu_utilization_percent,
      avg(avg_cluster_capacity_cpu_cores) AS avg_cluster_capacity_cpu_cores,
      avg(avg_cluster_usage_cpu_cores) AS avg_cluster_usage_cpu_cores,
      avg(avg_node_count) AS avg_node_count,
      avg(avg_pod_count) AS avg_pod_count,
      avg(avg_pod_per_node_count) AS avg_pod_per_node_count
    FROM {| .Report.Inputs.ClusterCpuUtilizationReportName | reportTableName |}
    WHERE period_start  >= timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prestoTimestamp |}'
    AND period_end <= timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}'
    {|- else -|}
    {|- if or (and (not .Report.Inputs.ClusterCpuCapacityReportName) .Report.Inputs.ClusterCpuUsageReportName) (and (not .Report.Inputs.ClusterCpuUsageReportName) .Report.Inputs.ClusterCpuCapacityReportName) -|}
    {|- fail "input ClusterCpuCapacityReportName and ClusterCpuUsageReportName must both be set" -|}
    {|- end -|}
    WITH cluster_cpu_capacity AS (
      {| renderReportQuery .Report.Inputs.ClusterCpuCapacityQueryName . |}
    ), cluster_cpu_usage AS (
      {| renderReportQuery .Report.Inputs.ClusterCpuUsageQueryName . |}
    )
    SELECT
      timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart| prestoTimestamp |}' AS period_start,
      timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}' AS period_end,
      capacity.total_cluster_capacity_cpu_core_seconds,
      usage.total_cluster_usage_cpu_core_seconds,
      usage.total_cluster_usage_cpu_core_seconds / capacity.total_cluster_capacity_cpu_core_seconds AS cluster_cpu_utilization_percent,
      capacity.avg_cluster_capacity_cpu_cores,
      usage.avg_cluster_usage_cpu_cores,
      capacity.avg_node_count,
      usage.avg_pod_count,
      usage.avg_pod_count / capacity.avg_node_count AS avg_pod_per_node_count
    FROM cluster_cpu_usage AS usage
    JOIN cluster_cpu_capacity AS capacity
    ON capacity.period_start = usage.period_start
    AND capacity.period_end = usage.period_end
    {|- end -|}
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: cluster-memory-utilization
  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: total_cluster_usage_memory_byte_seconds
    type: double
    unit: memory_byte_seconds
  - name: cluster_memory_utilization_percent
    type: double
  - name: avg_cluster_capacity_memory_bytes
    type: double
    unit: memory_bytes
  - name: avg_cluster_usage_memory_bytes
    type: double
    unit: memory_bytes
  - name: avg_node_count
    type: double
  - name: avg_pod_count
    type: double
  - name: avg_pod_per_node_count
    type: double
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: ClusterMemoryUtilizationReportName
    type: Report
  - name: ClusterMemoryCapacityReportName
    type: Report
  - name: ClusterMemoryUsageReportName
    type: Report
  - name: ClusterMemoryCapacityQueryName
    type: ReportQuery
    default: cluster-memory-capacity
  - name: ClusterMemoryUsageQueryName
    type: ReportQuery
    default: cluster-memory-usage
  query: |
    {|/* Handle aggregating a sub-report */|}
    {|- if .Report.Inputs.ClusterMemoryUtilizationReportName -|}
    SELECT
      timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart| prestoTimestamp |}' AS period_start,
      timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}' AS period_end,
      sum(total_cluster_capacity_memory_byte_seconds) AS total_cluster_capacity_memory_byte_seconds,
      sum(total_cluster_usage_memory_byte_seconds) AS total_cluster_usage_memory_byte_seconds,
      avg(cluster_memory_utilization_percent) AS cluster_memory_utilization_percent,
      avg(avg_cluster_capacity_memory_bytes) AS avg_cluster_capacity_memory_bytes,
      avg(avg_cluster_usage_memory_bytes) AS avg_cluster_usage_memory_bytes,
      avg(avg_node_count) AS avg_node_count,
      avg(avg_pod_count) AS avg_pod_count,
      avg(avg_pod_per_node_count) AS avg_pod_per_node_count
    FROM {| .Report.Inputs.ClusterMemoryUtilizationReportName | reportTableName |}
    WHERE period_start  >= timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prestoTimestamp |}'
    AND period_end <= timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}'
    {|- else -|}
    {|/* Validate both inputs are specified if only one is specified*/|}
    {|- if or (and (not .Report.Inputs.ClusterMemoryCapacityReportName) .Report.Inputs.ClusterMemoryUsageReportName) (and (not .Report.Inputs.ClusterMemoryUsageReportName) .Report.Inputs.ClusterMemoryCapacityReportName) -|}
    {|- fail "input ClusterMemoryCapacityReportName and ClusterMemoryUsageReportName must both be set" -|}
    {|- end -|}
    WITH cluster_memory_capacity AS (
      {| renderReportQuery .Report.Inputs.ClusterMemoryCapacityQueryName . |}
    ), cluster_memory_usage AS (
      {| renderReportQuery .Report.Inputs.ClusterMemoryUsageQueryName . |}
    )
    SELECT
      timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart| prestoTimestamp |}' AS period_start,
      timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}' AS period_end,
      capacity.total_cluster_capacity_memory_byte_seconds,
      usage.total_cluster_usage_memory_byte_seconds,
      usage.total_cluster_usage_memory_byte_seconds / capacity.total_cluster_capacity_memory_byte_seconds AS cluster_memory_utilization_percent,
      capacity.avg_cluster_capacity_memory_bytes,
      usage.avg_cluster_usage_memory_bytes,
      capacity.avg_node_count,
      usage.avg_pod_count,
      usage.avg_pod_count / capacity.avg_node_count AS avg_pod_per_node_count
    FROM cluster_memory_usage AS usage
    JOIN cluster_memory_capacity AS capacity
    ON capacity.period_start = usage.period_start
    AND capacity.period_end = usage.period_end
    {|- end -|}
