{{- $reportingValues :=  index .Values "openshift-reporting" -}}
{{- if $reportingValues.spec.awsBillingReportDataSource.enabled -}}
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: aws-ec2-billing-data-raw
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: resource_id
    type: varchar
  - name: usage_start_date
    type: timestamp
  - name: usage_end_date
    type: timestamp
  - name: period_cost
    type: double
  - name: partition_start
    type: varchar
  - name: partition_stop
    type: varchar
  inputs:
  - name: NodeMemoryAllocatableRawDataSourceName
    type: ReportDataSource
    default: node-memory-allocatable-raw
  - name: AWSBillingDataSourceName
    type: ReportDataSource
    default: aws-billing
  query: |
    WITH resource_id_list AS (
      SELECT resource_id
      FROM {| dataSourceTableName .Report.Inputs.NodeMemoryAllocatableRawDataSourceName |}
      GROUP BY resource_id
    )
    SELECT lineItem_resourceId as resource_id,
           lineItem_UsageStartDate as usage_start_date,
           lineItem_UsageEndDate as usage_end_date,
           lineItem_BlendedCost as period_cost,
           billing_period_start as partition_start,
           billing_period_end as partition_stop
    FROM {| dataSourceTableName .Report.Inputs.AWSBillingDataSourceName |} as aws_billing
    INNER JOIN resource_id_list
    ON aws_billing.lineItem_resourceId = resource_id_list.resource_id
    WHERE position('.csv' IN aws_billing."$path") != 0 -- This prevents JSON manifest files from being loaded.
    AND lineitem_productcode = 'AmazonEC2'
    AND lineItem_operation LIKE 'RunInstances%'
    AND lineItem_UsageStartDate IS NOT NULL
    AND lineItem_UsageEndDate IS NOT NULL

---

apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: aws-ec2-billing-data
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: resource_id
    type: varchar
  - name: usage_start_date
    type: timestamp
  - name: usage_end_date
    type: timestamp
  - name: period_cost
    type: double
  - name: partition_start
    type: varchar
  - name: partition_stop
    type: varchar
  - name: period_percent
    type: double
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: AwsEc2BillingDataRawDataSourceName
    type: ReportDataSource
    default: aws-ec2-billing-data-raw
  query: |
    SELECT aws_billing.*,
           CASE
               -- AWS data covers entire reporting period
               WHEN (aws_billing.usage_start_date <= timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prestoTimestamp |}') AND ( timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}' <= aws_billing.usage_end_date)
                   THEN cast(date_diff('millisecond', timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prestoTimestamp |}', timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}') as double) / cast(date_diff('millisecond', aws_billing.usage_start_date, aws_billing.usage_end_date) as double)

               -- AWS data covers start to middle
               WHEN (aws_billing.usage_start_date <= timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prestoTimestamp |}')
                   THEN cast(date_diff('millisecond', timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prestoTimestamp |}', aws_billing.usage_end_date) as double) / cast(date_diff('millisecond', aws_billing.usage_start_date, aws_billing.usage_end_date) as double)

               -- AWS data covers middle to end
               WHEN ( timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}' <= aws_billing.usage_end_date)
                   THEN cast(date_diff('millisecond', aws_billing.usage_start_date, timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}') as double) / cast(date_diff('millisecond', aws_billing.usage_start_date, aws_billing.usage_end_date) as double)
               ELSE 1
           END as period_percent,
           timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart| prestoTimestamp |}' AS period_start,
           timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}' AS period_end
    FROM {| dataSourceTableName .Report.Inputs.AwsEc2BillingDataRawDataSourceName |} as aws_billing

    -- make sure the partition overlaps with our range
    WHERE (partition_stop >= '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | billingPeriodTimestamp |}' AND partition_start <= '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | billingPeriodTimestamp |}')

    -- make sure lineItem entries overlap with our range
    AND (usage_end_date >= timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prestoTimestamp |}' AND usage_start_date <= timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}')
---
apiVersion: metering.openshift.io/v1
kind: ReportQuery
metadata:
  name: aws-ec2-cluster-cost
  labels:
    operator-metering: "true"
spec:
  columns:
  - name: period_start
    type: timestamp
    unit: date
  - name: period_end
    type: timestamp
    unit: date
  - name: cluster_cost
    type: double
  inputs:
  - name: ReportingStart
    type: time
  - name: ReportingEnd
    type: time
  - name: AwsEc2BillingDataQueryName
    type: ReportQuery
    default: aws-ec2-billing-data
  query: |
    WITH aws_billing_filtered AS (
      {| renderReportQuery .Report.Inputs.AwsEc2BillingDataQueryName . |}
    )
    SELECT
        timestamp '{| default .Report.ReportingStart .Report.Inputs.ReportingStart | prestoTimestamp |}' AS period_start,
        timestamp '{| default .Report.ReportingEnd .Report.Inputs.ReportingEnd | prestoTimestamp |}' AS period_end,
        sum(period_cost * period_percent) as cluster_cost
    FROM aws_billing_filtered
{{- end }}
