{{- $reportingValues :=  index .Values "openshift-reporting" -}}
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: node-memory-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_memory_bytes
    type: double
    unit: byte_seconds
  - name: resource_id
    tableHidden: true
    type: varchar
  - name: timeprecision
    type: double
    unit: seconds
  - name: node_capacity_memory_byte_seconds
    type: double
    unit: byte_seconds
  - name: timestamp
    type: timestamp
    unit: date
  - name: dt
    type: varchar
  inputs:
  - name: NodeCapacityMemoryBytesDataSourceName
    type: ReportDataSource
    default: node-capacity-memory-bytes
  query: |
    SELECT labels['node'] as node,
        labels,
        amount as node_capacity_memory_bytes,
        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_memory_byte_seconds,
        "timestamp",
        dt
    FROM {| dataSourceTableName .Report.Inputs.NodeCapacityMemoryBytesDataSourceName |}
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: node-memory-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_memory_byte_seconds
    type: double
    unit: memory_byte_seconds
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: NodeMemoryCapacityRawDataSourceName
    type: ReportDataSource
    default: node-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,
      node,
      resource_id,
      sum(node_capacity_memory_byte_seconds) as node_capacity_memory_byte_seconds
    FROM {| dataSourceTableName .Report.Inputs.NodeMemoryCapacityRawDataSourceName |}
    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-memory-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_memory_bytes
    type: double
    unit: bytes
  - name: resource_id
    tableHidden: true
    type: varchar
  - name: timeprecision
    type: double
    unit: seconds
  - name: node_allocatable_memory_byte_seconds
    type: double
    unit: byte_seconds
  - name: timestamp
    type: timestamp
    unit: date
  - name: dt
    type: varchar
  inputs:
  - name: NodeAllocatableMemoryBytesDataSourceName
    type: ReportDataSource
    default: node-allocatable-memory-bytes
  query: |
    SELECT labels['node'] as node,
        labels,
        amount as node_allocatable_memory_bytes,
        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_memory_byte_seconds,
        "timestamp",
        dt
    FROM {| dataSourceTableName .Report.Inputs.NodeAllocatableMemoryBytesDataSourceName |}
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: node-memory-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_memory_byte_seconds
    type: double
    unit: memory_byte_seconds
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: NodeMemoryAllocatableRawDataSourceName
    type: ReportDataSource
    default: node-memory-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_memory_byte_seconds) as node_allocatable_memory_byte_seconds
    FROM {| dataSourceTableName .Report.Inputs.NodeMemoryAllocatableRawDataSourceName |}
    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-memory-utilization
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  - name: node_allocatable_memory_byte_seconds
    type: double
    unit: byte_seconds
  - name: pod_request_memory_byte_seconds
    type: double
    unit: byte_seconds
  - name: memory_used_percent
    type: double
  - name: memory_unused_percent
    type: double
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: NodeMemoryAllocatableRawDataSourceName
    type: ReportDataSource
    default: node-memory-allocatable-raw
  - name: PodMemoryRequestRawDataSourceName
    type: ReportDataSource
    default: pod-memory-request-raw
  query: |
    WITH node_memory_allocatable AS (
      SELECT sum(node_allocatable_memory_byte_seconds) as node_allocatable_memory_byte_seconds
      FROM {| dataSourceTableName .Report.Inputs.NodeMemoryAllocatableRawDataSourceName |}
        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_memory_consumption AS (
      SELECT sum(pod_request_memory_byte_seconds) as pod_request_memory_byte_seconds
      FROM {| dataSourceTableName .Report.Inputs.PodMemoryRequestRawDataSourceName |}
      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_memory_allocatable.*,
      pod_memory_consumption.*,
      pod_memory_consumption.pod_request_memory_byte_seconds / node_memory_allocatable.node_allocatable_memory_byte_seconds,
      1 - (pod_memory_consumption.pod_request_memory_byte_seconds / node_memory_allocatable.node_allocatable_memory_byte_seconds)
    FROM node_memory_allocatable
    CROSS JOIN pod_memory_consumption
