{{- $reportingValues :=  index .Values "openshift-reporting" -}}
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: node-cpu-capacity-raw
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: node
    type: varchar
    unit: kubernetes_node
  - name: labels
    tableHidden: true
    type: map<varchar, varchar>
  - name: node_capacity_cpu_cores
    type: double
    unit: cpu_cores
  - name: resource_id
    type: varchar
  - name: timeprecision
    type: double
    unit: seconds
  - name: node_capacity_cpu_core_seconds
    type: double
    unit: cpu_core_seconds
  - name: timestamp
    type: timestamp
    unit: date
  - name: dt
    type: varchar
  inputs:
  - name: NodeCapacityCpuCoresDataSourceName
    type: ReportDataSource
    default: node-capacity-cpu-cores
  query: |
    SELECT labels['node'] as node,
        labels,
        amount as node_capacity_cpu_cores,
        CASE
          WHEN split_part(element_at(labels, 'provider_id'), ':///', 1) = 'aws'
            THEN split_part(split_part(element_at(labels, 'provider_id'), ':///', 2), '/', 2)
          WHEN split_part(element_at(labels, 'provider_id'), ':///', 1) = 'azure'
            THEN split_part(split_part(element_at(labels, 'provider_id'), ':///', 2), '/', 8)
          ELSE split_part(split_part(element_at(labels, 'provider_id'), ':///', 2), '/', 2)
        END as resource_id,
        timeprecision,
        amount * timeprecision as node_capacity_cpu_core_seconds,
        "timestamp",
        dt
    FROM {| dataSourceTableName .Report.Inputs.NodeCapacityCpuCoresDataSourceName |}
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: node-cpu-capacity
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  - name: node
    type: varchar
    unit: kubernetes_node
  - name: resource_id
    type: varchar
  - name: node_capacity_cpu_core_seconds
    type: double
    unit: cpu_core_seconds
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: NodeCpuCapacityRawDataSourceName
    type: ReportDataSource
    default: node-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,
      node,
      resource_id,
      sum(node_capacity_cpu_core_seconds) as node_capacity_cpu_core_seconds
    FROM {| dataSourceTableName .Report.Inputs.NodeCpuCapacityRawDataSourceName |}
    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 node, resource_id
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: node-cpu-allocatable-raw
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: node
    type: varchar
    unit: kubernetes_node
  - name: labels
    tableHidden: true
    type: map<varchar, varchar>
  - name: node_allocatable_cpu_cores
    type: double
    unit: cpu_cores
  - name: resource_id
    type: varchar
  - name: timeprecision
    type: double
    unit: seconds
  - name: node_allocatable_cpu_core_seconds
    type: double
    unit: cpu_core_seconds
  - name: timestamp
    type: timestamp
    unit: date
  - name: dt
    type: varchar
  inputs:
  - name: NodeAllocatableCpuCoresDataSourceName
    type: ReportDataSource
    default: node-allocatable-cpu-cores
  query: |
    SELECT labels['node'] as node,
        labels,
        amount as node_allocatable_cpu_cores,
        CASE
          WHEN split_part(element_at(labels, 'provider_id'), ':///', 1) = 'aws'
            THEN split_part(split_part(element_at(labels, 'provider_id'), ':///', 2), '/', 2)
          WHEN split_part(element_at(labels, 'provider_id'), ':///', 1) = 'azure'
            THEN split_part(split_part(element_at(labels, 'provider_id'), ':///', 2), '/', 8)
          ELSE split_part(split_part(element_at(labels, 'provider_id'), ':///', 2), '/', 2)
        END as resource_id,
        timeprecision,
        amount * timeprecision as node_allocatable_cpu_core_seconds,
        "timestamp",
        dt
    FROM {| dataSourceTableName .Report.Inputs.NodeAllocatableCpuCoresDataSourceName |}
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: node-cpu-allocatable
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  - name: node
    type: varchar
    unit: kubernetes_node
  - name: resource_id
    type: varchar
  - name: node_allocatable_cpu_core_seconds
    type: double
    unit: cpu_core_seconds
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: NodeCpuAllocatableRawDataSourceName
    type: ReportDataSource
    default: node-cpu-allocatable-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,
      node,
      resource_id,
      sum(node_allocatable_cpu_core_seconds) as node_allocatable_cpu_core_seconds
    FROM {| dataSourceTableName .Report.Inputs.NodeCpuAllocatableRawDataSourceName |}
    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 node, resource_id
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: node-cpu-utilization
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  - name: node_allocatable_cpu_core_seconds
    type: double
    unit: cpu_core_seconds
  - name: pod_request_cpu_core_seconds
    type: double
    unit: cpu_core_seconds
  - name: cpu_used_percent
    type: double
  - name: cpu_unused_percent
    type: double
  inputs:
  - name: NodeCpuAllocatableRawDataSourceName
    type: ReportDataSource
    default: node-cpu-allocatable-raw
  - name: PodCpuRequestRawDataSourceName
    type: ReportDataSource
    default: pod-cpu-request-raw
  query: |
    WITH node_cpu_allocatable AS (
      SELECT sum(node_allocatable_cpu_core_seconds) as node_allocatable_cpu_core_seconds
      FROM {| dataSourceTableName .Report.Inputs.NodeCpuAllocatableRawDataSourceName |}
        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 |}'
    ), pod_cpu_consumption AS (
      SELECT 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 |}'
    )
    SELECT
      timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart| prestoTimestamp |}' AS period_start,
      timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}' AS period_end,
      node_cpu_allocatable.*,
      pod_cpu_consumption.*,
      pod_cpu_consumption.pod_request_cpu_core_seconds / node_cpu_allocatable.node_allocatable_cpu_core_seconds,
      1 - (pod_cpu_consumption.pod_request_cpu_core_seconds / node_cpu_allocatable.node_allocatable_cpu_core_seconds)
    FROM node_cpu_allocatable
    CROSS JOIN pod_cpu_consumption
