{{- $reportingValues :=  index .Values "openshift-reporting" -}}
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: pod-memory-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_memory_bytes
    type: double
    unit: bytes
  - name: timeprecision
    type: double
    unit: seconds
  - name: pod_request_memory_byte_seconds
    type: double
    unit: byte_seconds
  - name: timestamp
    type: timestamp
    unit: date
  - name: dt
    type: varchar
  inputs:
  - name: PodRequestMemoryBytesDataSourceName
    type: ReportDataSource
    default: pod-request-memory-bytes
  query: |
    SELECT labels['pod'] as pod,
        labels['namespace'] as namespace,
        element_at(labels, 'node') as node,
        labels,
        amount as pod_request_memory_bytes,
        timeprecision,
        amount * timeprecision as pod_request_memory_byte_seconds,
        "timestamp",
        dt
    FROM {| dataSourceTableName .Report.Inputs.PodRequestMemoryBytesDataSourceName |}
    WHERE element_at(labels, 'node') IS NOT NULL
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: pod-memory-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_memory_bytes
    type: double
    unit: bytes
  - name: timeprecision
    type: double
    unit: seconds
  - name: pod_usage_memory_byte_seconds
    type: double
    unit: byte_seconds
  - name: timestamp
    type: timestamp
    unit: date
  - name: dt
    type: varchar
  inputs:
  - name: PodUsageMemoryBytesDataSourceName
    type: ReportDataSource
    default: pod-usage-memory-bytes
  query: |
    SELECT labels['pod'] as pod,
        labels['namespace'] as namespace,
        element_at(labels, 'node') as node,
        labels,
        amount as pod_usage_memory_bytes,
        timeprecision,
        amount * timeprecision as pod_usage_memory_byte_seconds,
        "timestamp",
        dt
    FROM {| dataSourceTableName .Report.Inputs.PodUsageMemoryBytesDataSourceName |}
    WHERE element_at(labels, 'node') IS NOT NULL
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: pod-memory-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_memory_byte_seconds
    type: double
    unit: byte_seconds
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: PodMemoryRequestRawDataSourceName
    type: ReportDataSource
    default: pod-memory-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_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 |}'
    GROUP BY namespace, pod, node
    ORDER BY namespace, pod, node ASC, pod_request_memory_byte_seconds DESC
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: pod-memory-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_memory_byte_seconds
    type: double
    unit: byte_seconds
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: PodMemoryUsageRawDataSourceName
    type: ReportDataSource
    default: pod-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,
      pod,
      namespace,
      node,
      sum(pod_usage_memory_byte_seconds) as pod_usage_memory_byte_seconds
    FROM {| dataSourceTableName .Report.Inputs.PodMemoryUsageRawDataSourceName |}
    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_memory_byte_seconds DESC
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: namespace-memory-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_memory_byte_seconds
    type: double
    unit: byte_seconds
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: NamespaceMemoryRequestReportName
    type: Report
  - name: PodMemoryRequestRawDataSourceName
    type: ReportDataSource
    default: pod-memory-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.NamespaceMemoryRequestReportName |}
      namespace,
      sum(pod_request_memory_byte_seconds) as pod_request_memory_byte_seconds
    FROM {| .Report.Inputs.NamespaceMemoryRequestReportName | 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_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 |}'
    GROUP BY namespace
    {|- end |}
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: namespace-memory-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_memory_byte_seconds
    type: double
    unit: byte_seconds
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: NamespaceMemoryUsageReportName
    type: Report
  - name: PodMemoryUsageRawDataSourceName
    type: ReportDataSource
    default: pod-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.NamespaceMemoryUsageReportName |}
      namespace,
      sum(pod_usage_memory_byte_seconds) as pod_usage_memory_byte_seconds
    FROM {| .Report.Inputs.NamespaceMemoryUsageReportName | 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_memory_byte_seconds) as pod_usage_memory_byte_seconds
    FROM {| dataSourceTableName .Report.Inputs.PodMemoryUsageRawDataSourceName |}
    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-memory-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_memory_byte_seconds
    type: double
    unit: byte_seconds
  - name: pod_request_memory_byte_seconds
    type: double
    unit: byte_seconds
  - name: pod_memory_usage_percent
    type: double
  - name: pod_memory_request_percent
    type: double
  - name: total_cluster_capacity_memory_byte_seconds
    type: double
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: NamespaceMemoryRequestReportName
    type: Report
  - name: NamespaceMemoryUsageReportName
    type: Report
  - name: ClusterMemoryCapacityReportName
    type: Report
  - name: NamespaceMemoryRequestQueryName
    type: ReportQuery
    default: namespace-memory-request
  - name: NamespaceMemoryUsageQueryName
    type: ReportQuery
    default: namespace-memory-usage
  - name: ClusterMemoryCapacityQueryName
    type: ReportQuery
    default: cluster-memory-capacity
  query: |
    WITH cluster_memory_capacity AS (
      {| renderReportQuery .Report.Inputs.ClusterMemoryCapacityQueryName . |}
    ), namespace_memory_usage AS (
      {| renderReportQuery .Report.Inputs.NamespaceMemoryUsageQueryName . |}
    ), namespace_memory_request AS (
      {| renderReportQuery .Report.Inputs.NamespaceMemoryRequestQueryName . |}
    )
    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_memory_byte_seconds,
      request.pod_request_memory_byte_seconds,
      usage.pod_usage_memory_byte_seconds / capacity.total_cluster_capacity_memory_byte_seconds AS pod_memory_usage_percent,
      request.pod_request_memory_byte_seconds / capacity.total_cluster_capacity_memory_byte_seconds AS pod_memory_request_percent,
      capacity.total_cluster_capacity_memory_byte_seconds
    FROM namespace_memory_usage as usage
    JOIN cluster_memory_capacity as capacity
      ON capacity.period_start = usage.period_start
      AND capacity.period_end = usage.period_end
    LEFT JOIN namespace_memory_request as request
      ON usage.period_start = request.period_start
      AND usage.period_end = request.period_end
      AND usage.namespace = request.namespace
