{{- $reportingValues :=  index .Values "openshift-reporting" -}}
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: pod-cpu-request-raw
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: pod
    type: varchar
    unit: kubernetes_pod
  - name: namespace
    type: varchar
    unit: kubernetes_namespace
  - name: node
    type: varchar
    unit: kubernetes_node
  - name: labels
    tableHidden: true
    type: map<varchar, varchar>
  - name: pod_request_cpu_cores
    type: double
    unit: cpu_cores
  - name: timeprecision
    type: double
    unit: seconds
  - name: pod_request_cpu_core_seconds
    type: double
    unit: cpu_core_seconds
  - name: timestamp
    type: timestamp
    unit: date
  - name: dt
    type: varchar
  inputs:
  - name: PodRequestCpuCoresDataSourceName
    type: ReportDataSource
    default: pod-request-cpu-cores
  query: |
    SELECT labels['pod'] as pod,
        labels['namespace'] as namespace,
        element_at(labels, 'node') as node,
        labels,
        amount as pod_request_cpu_cores,
        timeprecision,
        amount * timeprecision as pod_request_cpu_core_seconds,
        "timestamp",
        dt
    FROM {| dataSourceTableName .Report.Inputs.PodRequestCpuCoresDataSourceName |}
    WHERE element_at(labels, 'node') IS NOT NULL
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: pod-cpu-usage-raw
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: pod
    type: varchar
    unit: kubernetes_pod
  - name: namespace
    type: varchar
    unit: kubernetes_namespace
  - name: node
    type: varchar
    unit: kubernetes_node
  - name: labels
    tableHidden: true
    type: map<varchar, varchar>
  - name: pod_usage_cpu_cores
    type: double
    unit: cpu_cores
  - name: timeprecision
    type: double
    unit: seconds
  - name: pod_usage_cpu_core_seconds
    type: double
    unit: cpu_core_seconds
  - name: timestamp
    type: timestamp
    unit: date
  - name: dt
    type: varchar
  inputs:
  - name: PodUsageCpuCoresDataSourceName
    type: ReportDataSource
    default: pod-usage-cpu-cores
  query: |
    SELECT labels['pod'] as pod,
        labels['namespace'] as namespace,
        element_at(labels, 'node') as node,
        labels,
        amount as pod_usage_cpu_cores,
        timeprecision,
        amount * timeprecision as pod_usage_cpu_core_seconds,
        "timestamp",
        dt
    FROM {| dataSourceTableName .Report.Inputs.PodUsageCpuCoresDataSourceName |}
    WHERE element_at(labels, 'node') IS NOT NULL
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: pod-cpu-request
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  - name: pod
    type: varchar
    unit: kubernetes_pod
  - name: namespace
    type: varchar
    unit: kubernetes_namespace
  - name: node
    type: varchar
    unit: kubernetes_node
  - name: pod_request_cpu_core_seconds
    type: double
    unit: cpu_core_seconds
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: PodCpuRequestRawDataSourceName
    type: ReportDataSource
    default: pod-cpu-request-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,
      pod,
      namespace,
      node,
      sum(pod_request_cpu_core_seconds) as pod_request_cpu_core_seconds
    FROM {| dataSourceTableName .Report.Inputs.PodCpuRequestRawDataSourceName |}
    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 |}'
    GROUP BY namespace, pod, node
    ORDER BY namespace, pod, node ASC, pod_request_cpu_core_seconds DESC
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: pod-cpu-usage
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  - name: pod
    type: varchar
    unit: kubernetes_pod
  - name: namespace
    type: varchar
    unit: kubernetes_namespace
  - name: node
    type: varchar
    unit: kubernetes_node
  - name: pod_usage_cpu_core_seconds
    type: double
    unit: cpu_core_seconds
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: PodCpuUsageRawDataSourceName
    type: ReportDataSource
    default: pod-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,
      pod,
      namespace,
      node,
      sum(pod_usage_cpu_core_seconds) as pod_usage_cpu_core_seconds
    FROM {| dataSourceTableName .Report.Inputs.PodCpuUsageRawDataSourceName |}
    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 |}'
    GROUP BY namespace, pod, node
    ORDER BY namespace, pod, node ASC, pod_usage_cpu_core_seconds DESC
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: namespace-cpu-request
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  - name: namespace
    type: varchar
    unit: kubernetes_namespace
  - name: pod_request_cpu_core_seconds
    type: double
    unit: cpu_core_seconds
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: NamespaceCPURequestReportName
    type: Report
  - name: PodCpuRequestRawDataSourceName
    type: ReportDataSource
    default: pod-cpu-request-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.NamespaceCPURequestReportName |}
      namespace,
      sum(pod_request_cpu_core_seconds) as pod_request_cpu_core_seconds
    FROM {| .Report.Inputs.NamespaceCPURequestReportName | reportTableName |}
    WHERE period_start  >= timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prestoTimestamp |}'
    AND period_end <= timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}'
    GROUP BY namespace
    {|- else |}
      namespace,
      sum(pod_request_cpu_core_seconds) as pod_request_cpu_core_seconds
    FROM {| dataSourceTableName .Report.Inputs.PodCpuRequestRawDataSourceName |}
    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 |}'
    GROUP BY namespace
    {|- end |}
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: namespace-cpu-usage
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  - name: namespace
    type: varchar
    unit: kubernetes_namespace
  - name: pod_usage_cpu_core_seconds
    type: double
    unit: cpu_core_seconds
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: NamespaceCPUUsageReportName
    type: Report
  - name: PodCpuUsageRawDataSourceName
    type: ReportDataSource
    default: pod-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.NamespaceCPUUsageReportName |}
      namespace,
      sum(pod_usage_cpu_core_seconds) as pod_usage_cpu_core_seconds
    FROM {| .Report.Inputs.NamespaceCPUUsageReportName | reportTableName |}
    WHERE period_start  >= timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prestoTimestamp |}'
    AND period_end <= timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}'
    GROUP BY namespace
    {|- else |}
      namespace,
      sum(pod_usage_cpu_core_seconds) as pod_usage_cpu_core_seconds
    FROM {| dataSourceTableName .Report.Inputs.PodCpuUsageRawDataSourceName |}
    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 |}'
    GROUP BY namespace
    {|- end |}
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: namespace-cpu-utilization
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  - name: namespace
    type: varchar
    unit: kubernetes_namespace
  - name: pod_usage_cpu_core_seconds
    type: double
    unit: cpu_core_seconds
  - name: pod_request_cpu_core_seconds
    type: double
    unit: cpu_core_seconds
  - name: pod_cpu_usage_percent
    type: double
  - name: pod_cpu_request_percent
    type: double
  - name: total_cluster_capacity_cpu_core_seconds
    type: double
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: NamespaceCPURequestReportName
    type: Report
  - name: NamespaceCPUUsageReportName
    type: Report
  - name: ClusterCpuCapacityReportName
    type: Report
  - name: NamespaceCpuRequestQueryName
    type: ReportQuery
    default: namespace-cpu-request
  - name: NamespaceCpuUsageQueryName
    type: ReportQuery
    default: namespace-cpu-usage
  - name: ClusterCpuCapacityQueryName
    type: ReportQuery
    default: cluster-cpu-capacity
  query: |
    WITH cluster_cpu_capacity AS (
      {| renderReportQuery .Report.Inputs.ClusterCpuCapacityQueryName . |}
    ), namespace_cpu_usage AS (
      {| renderReportQuery .Report.Inputs.NamespaceCpuUsageQueryName . |}
    ), namespace_cpu_request AS (
      {| renderReportQuery .Report.Inputs.NamespaceCpuRequestQueryName . |}
    )
    SELECT
      timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart| prestoTimestamp |}' AS period_start,
      timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}' AS period_end,
      usage.namespace,
      usage.pod_usage_cpu_core_seconds,
      request.pod_request_cpu_core_seconds,
      usage.pod_usage_cpu_core_seconds / capacity.total_cluster_capacity_cpu_core_seconds AS pod_cpu_usage_percent,
      request.pod_request_cpu_core_seconds / capacity.total_cluster_capacity_cpu_core_seconds AS pod_cpu_request_percent,
      capacity.total_cluster_capacity_cpu_core_seconds
    FROM namespace_cpu_usage as usage
    JOIN cluster_cpu_capacity as capacity
      ON capacity.period_start = usage.period_start
      AND capacity.period_end = usage.period_end
    LEFT JOIN namespace_cpu_request as request
      ON usage.period_start = request.period_start
      AND usage.period_end = request.period_end
      AND usage.namespace = request.namespace
