{{- $reportingValues :=  index .Values "openshift-reporting" -}}
{{- if $reportingValues.spec.awsBillingReportDataSource.enabled -}}
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: pod-memory-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_memory_byte_seconds
    type: double
  - name: pod_memory_usage_percent
    type: double
  - name: pod_cost
    type: double
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: PodMemoryRequestRawDataSourceName
    type: ReportDataSource
    default: pod-memory-request-raw
  - name: NodeMemoryAllocatableRawDataSourceName
    type: ReportDataSource
    default: node-memory-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_memory_allocatable AS (
      SELECT sum(node_allocatable_memory_byte_seconds) as node_allocatable_memory_byte_seconds
      FROM {| dataSourceTableName .Report.Inputs.NodeMemoryAllocatableRawDataSourceName |}
        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_memory_consumption AS (
      SELECT 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 pod, namespace, node
    ),
    cluster_usage AS (
        SELECT pod_memory_consumption.*,
               pod_memory_consumption.pod_request_memory_byte_seconds / node_memory_allocatable.node_allocatable_memory_byte_seconds as pod_memory_usage_percent
        FROM pod_memory_consumption
        CROSS JOIN node_memory_allocatable
        ORDER BY pod_memory_consumption.pod_request_memory_byte_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_memory_usage_percent as pod_cost
    FROM cluster_usage
    CROSS JOIN aws_billing_sum
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: pod-memory-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_memory_byte_seconds
    type: double
  - name: pod_memory_usage_percent
    type: double
  - name: pod_cost
    type: double
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: PodMemoryUsageRawDataSourceName
    type: ReportDataSource
    default: pod-memory-usage-raw
  - name: NodeMemoryAllocatableRawDataSourceName
    type: ReportDataSource
    default: node-memory-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_memory_allocatable AS (
      SELECT sum(node_allocatable_memory_byte_seconds) as node_allocatable_memory_byte_seconds
      FROM {| dataSourceTableName .Report.Inputs.NodeMemoryAllocatableRawDataSourceName |}
        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_memory_consumption AS (
      SELECT 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 pod, namespace, node
    ),
    cluster_usage AS (
        SELECT pod_memory_consumption.*,
               pod_memory_consumption.pod_usage_memory_byte_seconds / node_memory_allocatable.node_allocatable_memory_byte_seconds as pod_memory_usage_percent
        FROM pod_memory_consumption
        CROSS JOIN node_memory_allocatable
        ORDER BY pod_memory_consumption.pod_usage_memory_byte_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_memory_usage_percent as pod_cost
    FROM cluster_usage
    CROSS JOIN aws_billing_sum
{{- end }}
