{{- $reportingValues :=  index .Values "openshift-reporting" -}}
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: persistentvolumeclaim-phase-raw
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: namespace
    type: varchar
  - name: persistentvolumeclaim
    type: varchar
  - name: labels
    tableHidden: true
    type: map<varchar, varchar>
  - name: value
    type: double
  - name: phase
    type: varchar
  - name: timeprecision
    type: double
    unit: seconds
  - name: timestamp
    type: timestamp
    unit: date
  - name: dt
    type: varchar
  inputs:
  - name: PersistentvolumeclaimPhaseDataSourceName
    type: ReportDataSource
    default: persistentvolumeclaim-phase
  query: |
    SELECT
        labels['namespace'] as namespace,
        labels['persistentvolumeclaim'] as persistentvolumeclaim,
        labels,
        amount as value,
        labels['phase'] as phase,
        timeprecision,
        "timestamp",
        dt
    FROM {| dataSourceTableName .Report.Inputs.PersistentvolumeclaimPhaseDataSourceName |}
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: persistentvolumeclaim-usage-raw
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: namespace
    type: varchar
  - name: persistentvolumeclaim
    type: varchar
  - name: labels
    tableHidden: true
    type: map<varchar, varchar>
  - name: persistentvolumeclaim_usage_bytes
    type: double
    unit: bytes
  - name: timeprecision
    type: double
    unit: seconds
  - name: persistentvolumeclaim_usage_byte_seconds
    type: double
    unit: byte_seconds
  - name: timestamp
    type: timestamp
    unit: date
  - name: dt
    type: varchar
  inputs:
  - name: PersistentvolumeclaimUsageBytesDataSourceName
    type: ReportDataSource
    default: persistentvolumeclaim-usage-bytes
  query: |
    SELECT
        coalesce(element_at(labels, 'exported_namespace'), element_at(labels, 'namespace')) as namespace,
        labels['persistentvolumeclaim'] as persistentvolumeclaim,
        labels,
        amount as persistentvolumeclaim_usage_bytes,
        timeprecision,
        amount * timeprecision as persistentvolumeclaim_usage_byte_seconds,
        "timestamp",
        dt
    FROM {| dataSourceTableName .Report.Inputs.PersistentvolumeclaimUsageBytesDataSourceName |}
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: persistentvolumeclaim-usage-with-phase-raw
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: namespace
    type: varchar
    unit: kubernetes_namespace
  - name: timestamp
    type: timestamp
    unit: date
  - name: persistentvolumeclaim
    type: varchar
  - name: persistentvolumeclaim_usage_bytes
    type: double
    unit: bytes
  - name: dt
    type: varchar
  inputs:
  - name: PersistentvolumeclaimUsageRawDataSourceName
    type: ReportDataSource
    default: persistentvolumeclaim-usage-raw
  - name: PersistentvolumeclaimPhaseRawDataSourceName
    type: ReportDataSource
    default: persistentvolumeclaim-phase-raw
  query: |
    SELECT
      phase.namespace AS namespace,
      phase.timestamp AS timestamp,
      phase.persistentvolumeclaim AS persistentvolumeclaim,
      min(persistentvolumeclaim_usage_bytes) AS persistentvolumeclaim_usage_bytes,
      phase.dt AS dt
    FROM {| dataSourceTableName .Report.Inputs.PersistentvolumeclaimPhaseRawDataSourceName |} AS phase
    FULL OUTER JOIN {| dataSourceTableName .Report.Inputs.PersistentvolumeclaimUsageRawDataSourceName |} AS usage
    ON phase.namespace=usage.namespace
    AND phase.persistentvolumeclaim=usage.persistentvolumeclaim
    AND phase.timestamp=usage.timestamp
    GROUP BY phase.timestamp, phase.dt, phase.namespace, phase.persistentvolumeclaim
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: persistentvolumeclaim-usage
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
  - name: namespace
    type: varchar
    unit: kubernetes_namespace
  - name: persistentvolumeclaim
    type: varchar
    unit: kubernetes_persistentvolumeclaim
  - name: persistentvolumeclaim_usage_bytes
    type: double
    unit: bytes
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: PersistentvolumeclaimUsageWithPhaseRawDataSourceName
    type: ReportDataSource
    default: persistentvolumeclaim-usage-with-phase-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,
      persistentvolumeclaim,
      sum(persistentvolumeclaim_usage_bytes)
    FROM {| dataSourceTableName .Report.Inputs.PersistentvolumeclaimUsageWithPhaseRawDataSourceName |}
    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, persistentvolumeclaim

---

apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: "namespace-persistentvolumeclaim-usage"
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
  - name: namespace
    type: varchar
    unit: kubernetes_namespace
  - name: persistentvolumeclaim_usage_bytes
    type: double
    unit: bytes
  inputs:
  - name: ReportingStart
  - name: ReportingEnd
  - name: PersistentvolumeclaimUsageWithPhaseRawDataSourceName
    type: ReportDataSource
    default: persistentvolumeclaim-usage-with-phase-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(persistentvolumeclaim_usage_bytes)
    FROM {| dataSourceTableName .Report.Inputs.PersistentvolumeclaimUsageWithPhaseRawDataSourceName |}
    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
