{{- $reportingValues :=  index .Values "openshift-reporting" -}}
{{- if $reportingValues.spec.awsBillingReportDataSource.enabled -}}
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: pod-cpu-request-aws
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  - name: pod
    type: varchar
  - name: namespace
    type: varchar
  - name: node
    type: varchar
  - name: pod_request_cpu_core_seconds
    type: double
  - name: pod_cpu_usage_percent
    type: double
  - name: pod_cost
    type: double
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: PodCpuRequestRawDataSourceName
    type: ReportDataSource
    default: pod-cpu-request-raw
  - name: NodeCpuAllocatableRawDataSourceName
    type: ReportDataSource
    default: node-cpu-allocatable-raw
  - name: AwsEc2BillingDataQueryName
    type: ReportQuery
    default: aws-ec2-billing-data
  query: |
    WITH aws_billing_filtered AS (
      {| renderReportQuery .Report.Inputs.AwsEc2BillingDataQueryName . |}
    ),
    aws_billing_sum AS (
        SELECT sum(aws_billing_filtered.period_cost * aws_billing_filtered.period_percent) as cluster_cost
        FROM aws_billing_filtered
    ),
    node_cpu_allocatable AS (
      SELECT sum(node_allocatable_cpu_core_seconds) as node_allocatable_cpu_core_seconds
      FROM {| dataSourceTableName .Report.Inputs.NodeCpuAllocatableRawDataSourceName |}
        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_cpu_consumption AS (
      SELECT pod,
             namespace,
             node,
             sum(pod_request_cpu_core_seconds) as pod_request_cpu_core_seconds
      FROM {| dataSourceTableName .Report.Inputs.PodCpuRequestRawDataSourceName |}
      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 pod, namespace, node
    ),
    cluster_usage AS (
        SELECT pod_cpu_consumption.*,
               pod_cpu_consumption.pod_request_cpu_core_seconds / node_cpu_allocatable.node_allocatable_cpu_core_seconds as pod_cpu_usage_percent
        FROM pod_cpu_consumption
        CROSS JOIN node_cpu_allocatable
        ORDER BY pod_cpu_consumption.pod_request_cpu_core_seconds DESC
    )
    SELECT
      timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart| prestoTimestamp |}' AS period_start,
      timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}' AS period_end,
      cluster_usage.*,
      aws_billing_sum.cluster_cost * cluster_usage.pod_cpu_usage_percent as pod_cost
    FROM cluster_usage
    CROSS JOIN aws_billing_sum
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: pod-cpu-usage-aws
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  - name: pod
    type: varchar
  - name: namespace
    type: varchar
  - name: node
    type: varchar
  - name: pod_usage_cpu_core_seconds
    type: double
  - name: pod_cpu_usage_percent
    type: double
  - name: pod_cost
    type: double
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: PodCpuUsageRawDataSourceName
    type: ReportDataSource
    default: pod-cpu-usage-raw
  - name: NodeCpuAllocatableRawDataSourceName
    type: ReportDataSource
    default: node-cpu-allocatable-raw
  - name: AwsEc2BillingDataQueryName
    type: ReportQuery
    default: aws-ec2-billing-data
  query: |
    WITH aws_billing_filtered AS (
      {| renderReportQuery .Report.Inputs.AwsEc2BillingDataQueryName . |}
    ),
    aws_billing_sum AS (
        SELECT sum(aws_billing_filtered.period_cost * aws_billing_filtered.period_percent) as cluster_cost
        FROM aws_billing_filtered
    ),
    node_cpu_allocatable AS (
      SELECT sum(node_allocatable_cpu_core_seconds) as node_allocatable_cpu_core_seconds
      FROM {| dataSourceTableName .Report.Inputs.NodeCpuAllocatableRawDataSourceName |}
        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_cpu_consumption AS (
      SELECT pod,
             namespace,
             node,
             sum(pod_usage_cpu_core_seconds) as pod_usage_cpu_core_seconds
      FROM {| dataSourceTableName .Report.Inputs.PodCpuUsageRawDataSourceName |}
      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 pod, namespace, node
    ),
    cluster_usage AS (
        SELECT pod_cpu_consumption.*,
               pod_cpu_consumption.pod_usage_cpu_core_seconds / node_cpu_allocatable.node_allocatable_cpu_core_seconds as pod_cpu_usage_percent
        FROM pod_cpu_consumption
        CROSS JOIN node_cpu_allocatable
        ORDER BY pod_cpu_consumption.pod_usage_cpu_core_seconds DESC
    )
    SELECT
      timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart| prestoTimestamp |}' AS period_start,
      timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}' AS period_end,
      cluster_usage.*,
      aws_billing_sum.cluster_cost * cluster_usage.pod_cpu_usage_percent as pod_cost
    FROM cluster_usage
    CROSS JOIN aws_billing_sum
{{- end }}
