apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: "bucket-cpu-rate"
  namespace: "openshift-metering"
spec:
  columns:
  - name: period_start
    type: timestamp
  - name: period_end
    type: timestamp
  - name: avg_cpu 
    type: double
  - name: bucket
    type: varchar
  - name: cluster 
    type: varchar
  inputs:
  - name: BucketComputeRateDataSourceName
    type: ReportDataSource
    default: bucket-cpu-rate
  - name: ReportingStart
    type: time
  - name: ReportingEnd
  query: |
    SELECT
      timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart| prestoTimestamp |}' AS period_start,
      timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}' AS period_end,
    	avg(amount)  as cpu_rate,
    	labels['bucket'] as bucket,
    	labels['service'] as service
    FROM {| dataSourceTableName .Report.Inputs.BucketComputeRateDataSourceName |}
    WHERE "timestamp" >= timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prestoTimestamp |}'
    AND "timestamp" < timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}'
    GROUP BY labels['bucket'], labels['service']
    ORDER BY cpu_rate DESC
