setup:
  - do:
      indices.create:
        index: test
        body:
          settings:
            number_of_shards: 1
            index:
              mode: time_series
              routing_path: [ metricset, k8s.pod.uid ]
              time_series:
                start_time: 2021-04-28T00:00:00Z
                end_time: 2021-04-29T00:00:00Z
          mappings:
            properties:
              "@timestamp":
                type: date
              metricset:
                type: keyword
                time_series_dimension: true
              k8s:
                properties:
                  pod:
                    properties:
                      uid:
                        type: keyword
                        time_series_dimension: true
                      name:
                        type: keyword
                      created_at:
                        type: date_nanos
                      running:
                        type: boolean
                      number_of_containers:
                        type: integer
                      ip:
                        type: ip
                      tags:
                        type: keyword
                      values:
                        type: integer
                      network:
                        properties:
                          tx:
                            type: long
                            time_series_metric: gauge
                          rx:
                            type: long
                            time_series_metric: gauge
  - do:
      bulk:
        refresh: true
        index: test
        body:
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:50:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "ip": "10.10.55.1", "network": {"tx": 2001810, "rx": 802133}, "created_at": "2021-04-28T19:34:00.000Z", "running": false, "number_of_containers": 2, "tags": ["backend", "prod"], "values": [2, 3, 6]}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:50:24.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "ip": "10.10.55.26", "network": {"tx": 2005177, "rx": 801479}, "created_at": "2021-04-28T19:35:00.000Z", "running": true, "number_of_containers": 2, "tags": ["backend", "prod", "us-west1"], "values": [1, 1, 3]}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T20:50:44.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "ip": "10.10.55.41", "network": {"tx": 2006223, "rx": 802337}, "created_at": "2021-04-28T19:36:00.000Z", "running": true, "number_of_containers": 2, "tags": ["backend", "prod", "us-west2"], "values": [4, 1, 2]}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T20:51:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "ip": "10.10.55.22", "network": {"tx": 2012916, "rx": 803685}, "created_at": "2021-04-28T19:37:00.000Z", "running": true, "number_of_containers": 2, "tags": ["backend", "prod"], "values": [2, 3, 1]}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:50:03.142Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "ip": "10.10.55.33", "network": {"tx": 1434521, "rx": 530575}, "created_at": "2021-04-28T19:42:00.000Z", "running": false, "number_of_containers": 1, "tags": ["backend", "test"], "values": [2, 3, 4]}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:50:23.142Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "ip": "10.10.55.56", "network": {"tx": 1434577, "rx": 530600}, "created_at": "2021-04-28T19:43:00.000Z", "running": false, "number_of_containers": 1, "tags": ["backend", "test", "us-west2"], "values": [2, 1, 1]}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T19:50:53.142Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "ip": "10.10.55.37", "network": {"tx": 1434587, "rx": 530604}, "created_at": "2021-04-28T19:44:00.000Z", "running": true, "number_of_containers": 1, "tags": ["backend", "test", "us-west1"], "values": [4, 5, 2]}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T19:51:03.142Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "ip": "10.10.55.120", "network": {"tx": 1434595, "rx": 530605}, "created_at": "2021-04-28T19:45:00.000Z", "running": true, "number_of_containers": 1, "tags": ["backend", "test", "us-west1"], "values": [3, 2, 1]}}}'

  - do:
      indices.put_settings:
        index: test
        body:
          index.blocks.write: true

---
"Query stats on downsampled index":
  - requires:
      test_runner_features: [capabilities]
      capabilities:
        - method: POST
          path: /_query
          parameters: []
          capabilities: [aggregate_metric_double]
      reason: "Support for aggregate_metric_double"
  - do:
      indices.downsample:
        index: test
        target_index: test-downsample
        body: >
          {
            "fixed_interval": "1h"
          }
  - is_true: acknowledged

  - do:
      esql.query:
        body:
          query: "FROM test-downsample |
          STATS max(k8s.pod.network.rx), min(k8s.pod.network.rx), sum(k8s.pod.network.rx), count(k8s.pod.network.rx)
          | LIMIT 100"

  - length: {values: 1}
  - length: {values.0: 4}
  - match: {columns.0.name: "max(k8s.pod.network.rx)"}
  - match: {columns.0.type: "double"}
  - match: {columns.1.name: "min(k8s.pod.network.rx)"}
  - match: {columns.1.type: "double"}
  - match: {columns.2.name: "sum(k8s.pod.network.rx)"}
  - match: {columns.2.type: "double"}
  - match: {columns.3.name: "count(k8s.pod.network.rx)"}
  - match: {columns.3.type: "long"}
  - match: {values.0.0: 803685.0}
  - match: {values.0.1: 530575.0}
  - match: {values.0.2: 5332018.0}
  - match: {values.0.3: 8}

---
"Render stats from downsampled index":
  - requires:
      test_runner_features: [capabilities]
      capabilities:
        - method: POST
          path: /_query
          parameters: []
          capabilities: [aggregate_metric_double_rendering]
      reason: "Support for rendering aggregate_metric_doubles"
  - do:
      indices.downsample:
        index: test
        target_index: test-downsample
        body: >
          {
            "fixed_interval": "1h"
          }
  - is_true: acknowledged

  - do:
      esql.query:
        body:
          query: "FROM test-downsample | WHERE @timestamp == \"2021-04-28T19:00:00.000Z\" | KEEP k8s.pod.network.rx | LIMIT 100"
  - length: {values: 1}
  - length: {values.0: 1}
  - match: {columns.0.name: "k8s.pod.network.rx"}
  - match: {columns.0.type: "aggregate_metric_double"}
  - match: {values.0.0: '{"min":530604.0,"max":530605.0,"sum":1061209.0,"value_count":2}'}

---
"Stats from downsampled and non-downsampled index simultaneously":
  - requires:
      test_runner_features: [capabilities]
      capabilities:
        - method: POST
          path: /_query
          parameters: []
          capabilities: [aggregate_metric_double_convert_to]
      reason: "Support for to_aggregate_metric_double function"

  - do:
      indices.downsample:
        index: test
        target_index: test-downsample
        body: >
          {
            "fixed_interval": "1h"
          }
  - is_true: acknowledged

  - do:
      indices.create:
        index: test-2
        body:
          settings:
            number_of_shards: 1
            index:
              mode: time_series
              routing_path: [ metricset, k8s.pod.uid ]
              time_series:
                start_time: 2021-04-29T00:00:00Z
                end_time: 2021-04-30T00:00:00Z
          mappings:
            properties:
              "@timestamp":
                type: date
              metricset:
                type: keyword
                time_series_dimension: true
              k8s:
                properties:
                  pod:
                    properties:
                      uid:
                        type: keyword
                        time_series_dimension: true
                      name:
                        type: keyword
                      created_at:
                        type: date_nanos
                      running:
                        type: boolean
                      number_of_containers:
                        type: integer
                      ip:
                        type: ip
                      tags:
                        type: keyword
                      values:
                        type: integer
                      network:
                        properties:
                          tx:
                            type: long
                            time_series_metric: gauge
                          rx:
                            type: long
                            time_series_metric: gauge

  - do:
      bulk:
        refresh: true
        index: test-2
        body:
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-29T21:50:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "ip": "10.10.55.1", "network": {"tx": 2001810, "rx": 802339}, "created_at": "2021-04-28T19:34:00.000Z", "running": false, "number_of_containers": 2, "tags": ["backend", "prod"], "values": [2, 3, 6]}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-29T21:50:24.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "ip": "10.10.55.26", "network": {"tx": 2000177, "rx": 800479}, "created_at": "2021-04-28T19:35:00.000Z", "running": true, "number_of_containers": 2, "tags": ["backend", "prod", "us-west1"], "values": [1, 1, 3]}}}'
          - '{"index": {}}'

  - do:
      esql.query:
        body:
          query: "FROM test-* |
          WHERE k8s.pod.uid == \"947e4ced-1786-4e53-9e0c-5c447e959507\" |
          EVAL rx = to_aggregate_metric_double(k8s.pod.network.rx) |
          STATS max(rx), min(rx), sum(rx), count(rx) |
          LIMIT 100"

  - length: {values: 1}
  - length: {values.0: 4}
  - match: {columns.0.name: "max(rx)"}
  - match: {columns.0.type: "double"}
  - match: {columns.1.name: "min(rx)"}
  - match: {columns.1.type: "double"}
  - match: {columns.2.name: "sum(rx)"}
  - match: {columns.2.type: "double"}
  - match: {columns.3.name: "count(rx)"}
  - match: {columns.3.type: "long"}
  - match: {values.0.0: 803685.0}
  - match: {values.0.1: 800479.0}
  - match: {values.0.2: 4812452.0}
  - match: {values.0.3: 6}
