{{- $reportingValues :=  index .Values "openshift-reporting" -}}
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: persistentvolumeclaim-request-raw
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: labels
    tableHidden: true
    type: map<varchar, varchar>
  - name: persistentvolumeclaim
    type: varchar
    unit: kubernetes_persistentvolumeclaim
  - name: persistentvolume
    type: varchar
    unit: kubernetes_persistentvolume
  - name: namespace
    type: varchar
    unit: kubernetes_namespace
  - name: storageclass
    type: varchar
    unit: kubernetes_storageclass
  - name: volume_request_storage_bytes
    type: double
    unit: bytes
  - name: timeprecision
    type: double
    unit: seconds
  - name: volume_request_storage_byte_seconds
    type: double
    unit: byte_seconds
  - name: timestamp
    type: timestamp
    unit: date
  - name: dt
    type: varchar
  inputs:
  - name: PersistentvolumeclaimRequestBytesDataSourceName
    type: ReportDataSource
    default: persistentvolumeclaim-request-bytes
  query: |
    SELECT labels,
    labels['persistentvolumeclaim'] as persistentvolumeclaim,
    element_at(labels, 'volumename') as persistentvolume,
        labels['namespace'] as namespace,
        element_at(labels, 'storageclass') as storageclass,
        amount as volume_request_storage_bytes,
        timeprecision,
        amount * timeprecision as volume_request_storage_byte_seconds,
        "timestamp",
        dt
    FROM {| dataSourceTableName .Report.Inputs.PersistentvolumeclaimRequestBytesDataSourceName |}
    WHERE element_at(labels, 'volumename') IS NOT NULL
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: persistentvolumeclaim-request
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  - name: persistentvolumeclaim
    type: varchar
    unit: kubernetes_persistentvolumeclaim
  - name: persistentvolume
    type: varchar
    unit: kubernetes_persistentvolume
  - name: namespace
    type: varchar
    unit: kubernetes_namespace
  - name: storageclass
    type: varchar
    unit: kubernetes_storageclass
  - name: volume_request_storage_byte_seconds
    type: double
    unit: byte_seconds
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: PersistentvolumeclaimRequestRawDataSourceName
    type: ReportDataSource
    default: persistentvolumeclaim-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,
      persistentvolumeclaim,
      persistentvolume,
      namespace,
      storageclass,
      sum(volume_request_storage_byte_seconds) as volume_request_storage_byte_seconds
    FROM {| dataSourceTableName .Report.Inputs.PersistentvolumeclaimRequestRawDataSourceName |}
    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 persistentvolumeclaim, namespace, persistentvolume, storageclass
    ORDER BY persistentvolumeclaim, namespace, persistentvolume, storageclass ASC, volume_request_storage_byte_seconds DESC
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: namespace-persistentvolumeclaim-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: volume_request_storage_byte_seconds
    type: double
    unit: byte_seconds
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: PersistentvolumeclaimRequestRawDataSourceName
    type: ReportDataSource
    default: persistentvolumeclaim-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,
      namespace,
      sum(volume_request_storage_byte_seconds) as volume_request_storage_byte_seconds
    FROM {| dataSourceTableName .Report.Inputs.PersistentvolumeclaimRequestRawDataSourceName |}
    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
    ORDER BY volume_request_storage_byte_seconds DESC
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: cluster-persistentvolumeclaim-request
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  - name: volume_request_storage_byte_seconds
    type: double
    unit: byte_seconds
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: PersistentvolumeclaimRequestRawDataSourceName
    type: ReportDataSource
    default: persistentvolumeclaim-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,
      sum(volume_request_storage_byte_seconds) as volume_request_storage_byte_seconds
    FROM {| dataSourceTableName .Report.Inputs.PersistentvolumeclaimRequestRawDataSourceName |}
    WHERE "timestamp" >= timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prestoTimestamp |}'
    AND dt >= '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prometheusMetricPartitionFormat |}'
    AND dt <= '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prometheusMetricPartitionFormat |}'
    AND "timestamp" < timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}'
