setup:
  - requires:
      cluster_features: ["gte_v8.5.0"]
      reason: "rollup renamed to downsample in 8.5.0"

  - do:
      indices.create:
        index: test
        body:
          settings:
            number_of_shards: 1
            number_of_replicas: 0
            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
                      multi-counter:
                        type: long
                        time_series_metric: counter
                      multi-gauge:
                        type: integer
                        time_series_metric: gauge
                      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", "multi-counter" : [10, 11, 12], "multi-gauge": [100, 200, 150], "network": {"tx": 2001818691, "rx": 802133794}, "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", "multi-counter" : [21, 22, 23], "multi-gauge": [90, 91, 95], "network": {"tx": 2005177954, "rx": 801479970}, "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", "multi-counter" : [1, 5, 10], "multi-gauge": [103, 110, 109], "network": {"tx": 2006223737, "rx": 802337279}, "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", "multi-counter" : [101, 102, 105], "multi-gauge": [100, 100, 100], "network": {"tx": 2012916202, "rx": 803685721}, "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", "multi-counter" : [7, 11, 44], "multi-gauge": [100, 100, 102], "network": {"tx": 1434521831, "rx": 530575198}, "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", "multi-counter" : [0, 0, 1], "multi-gauge": [101, 102, 102], "network": {"tx": 1434577921, "rx": 530600088}, "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", "multi-counter" : [1000, 1001, 1002], "multi-gauge": [99, 100, 110], "network": {"tx": 1434587694, "rx": 530604797}, "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", "multi-counter" : [76, 77, 78], "multi-gauge": [95, 98, 100], "network": {"tx": 1434595272, "rx": 530605511}, "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

  - do:
      indices.create:
        index: test-histogram
        body:
          settings:
            number_of_shards: 1
            number_of_replicas: 0
            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
                      latency:
                        type: histogram
                      empty-histogram:
                        type: histogram
                      network:
                        properties:
                          tx:
                            type: long
                            time_series_metric: gauge
                          rx:
                            type: long
                            time_series_metric: gauge
  - do:
      bulk:
        refresh: true
        index: test-histogram
        body:
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:50:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "network": {"tx": 2001818691, "rx": 802133794}, "latency": {"counts": [10, 12, 20, 5], "values": [1.0, 10.0, 100.0, 1000.0]}}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:55:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "network": {"tx": 2001819988, "rx": 802133911}, "latency": {"counts": [8, 7, 10, 12], "values": [1.0, 2.0, 5.0, 10.0]}}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T19:00:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "network": {"tx": 1001818691, "rx": 502133794}, "latency": {"counts": [1, 5, 5, 22], "values": [1.0, 10.0, 100.0, 1000.0]}}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T19:05:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "network": {"tx": 1001822087, "rx": 502134222}, "latency": {"counts": [7, 15, 10, 10], "values": [1.0, 2.0, 5.0, 10.0]}}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:51:20.467Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "network": {"tx": 1781818691, "rx": 533135238}, "latency": {"counts": [2, 4, 16, 4], "values": [1.0, 2.0, 5.0, 10.0]}}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:56:20.467Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "network": {"tx": 1781818691, "rx": 533135567}, "latency": {"counts": [2, 2, 8, 8], "values": [1.0, 10.0, 100.0, 1000.0]}}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T19:01:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "network": {"tx": 1651818691, "rx": 487133866}, "latency": {"counts": [4, 5, 4, 13], "values": [1.0, 10.0, 100.0, 1000.0]}}}}'

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

  - do:
      indices.create:
        index: test-object
        body:
          settings:
            number_of_shards: 1
            number_of_replicas: 0
            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
                      agent:
                        type: object
                        properties:
                          id:
                            type: keyword
                      value:
                        type: long
                        time_series_metric: gauge

  - do:
      bulk:
        refresh: true
        index: test-object
        body:
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:50:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "agent": { "id": "first", "version": "2.0.4" }, "value": 10 }}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:50:24.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "agent": { "id": "first", "version": "2.0.4" }, "value": 20 }}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T20:50:44.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "agent": { "id": "first", "version": "2.0.4" }, "value": 12 }}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T20:51:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "agent": { "id": "first", "version": "2.0.4" }, "value": 15 }}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:50:03.142Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "agent": { "id": "second", "version": "2.1.7" }, "value": 9 }}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:50:23.142Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "agent": { "id": "second", "version": "2.1.7" }, "value": 16 }}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T19:50:53.142Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "agent": { "id": "second", "version": "2.1.7" }, "value": 25 }}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T19:51:03.142Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "agent": { "id": "second", "version": "2.1.7" }, "value": 17 }}}'

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

  - do:
      indices.create:
        index: test-empty-missing
        body:
          settings:
            number_of_shards: 1
            number_of_replicas: 0
            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
                      value:
                        type: integer
                        time_series_metric: gauge
                      label:
                        type: keyword

  - do:
      bulk:
        refresh: true
        index: test-empty-missing
        body:
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:55:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "value": 10 }}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:50:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "value": 20, "label": null, "unmapped": null }}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:45:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "value": 30, "label": "abc", "unmapped": "abc"  }}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:40:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "value": 40, "label": "xyz", "unmapped": "xyz" }}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:55:20.467Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "value": 10 }}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:50:20.467Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "value": 20, "label": null, "unmapped": null }}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:45:20.467Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "value": 30, "label": "xyz", "unmapped": "xyz" }}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:40:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "value": 40, "label": "abc", "unmapped": "abc" }}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:55:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e9597ab", "value": 10 }}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:50:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e9597ab", "value": 20, "label": null, "unmapped": null }}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:45:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e9597ab", "value": 30, "label": null  }}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:40:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e9597ab", "value": 40, "unmapped": null }}}'

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

---
"Downsample index":
  - requires:
      cluster_features: ["gte_v8.13.0"]
      reason: _tsid hashing introduced in 8.13

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

  - do:
      search:
        index: test-downsample
        body:
          sort: [ "_tsid", "@timestamp" ]

  - length: { hits.hits: 4 }
  - match: { hits.hits.0._source._doc_count: 2 }
  - match: { hits.hits.0._source.k8s.pod.uid: df3145b3-0563-4d3b-a0f7-897eb2876ea9 }
  - match: { hits.hits.0._source.metricset: pod }
  - match: { hits.hits.0._source.@timestamp: "2021-04-28T18:00:00.000Z" }
  - match: { hits.hits.0._source.k8s.pod.multi-counter: 0 }
  - match: { hits.hits.0._source.k8s.pod.multi-gauge.min: 100.0 }
  - match: { hits.hits.0._source.k8s.pod.multi-gauge.max: 102.0 }
  - match: { hits.hits.0._source.k8s.pod.multi-gauge.sum: 607.0 }
  - match: { hits.hits.0._source.k8s.pod.multi-gauge.value_count: 6 }
  - match: { hits.hits.0._source.k8s.pod.network.tx.min: 1434521831 }
  - match: { hits.hits.0._source.k8s.pod.network.tx.max: 1434577921 }
  - match: { hits.hits.0._source.k8s.pod.network.tx.value_count: 2 }
  - match: { hits.hits.0._source.k8s.pod.ip: "10.10.55.56" }
  - match: { hits.hits.0._source.k8s.pod.created_at: "2021-04-28T19:43:00.000Z" }
  - match: { hits.hits.0._source.k8s.pod.number_of_containers: 1 }
  - match: { hits.hits.0._source.k8s.pod.tags: [ "backend", "test", "us-west2" ] }
  - match: { hits.hits.0._source.k8s.pod.values: [ 1, 1, 2 ] }
  - is_false: hits.hits.0._source.k8s.pod.running

  # Assert downsample index settings
  - do:
      indices.get_settings:
        index: test-downsample

  - match: { test-downsample.settings.index.mode: time_series }
  - match: { test-downsample.settings.index.time_series.end_time:   2021-04-29T00:00:00Z }
  - match: { test-downsample.settings.index.time_series.start_time: 2021-04-28T00:00:00Z }
  - match: { test-downsample.settings.index.routing_path: [ "metricset", "k8s.pod.uid"] }
  - match: { test-downsample.settings.index.downsample.source.name: test }
  - match: { test-downsample.settings.index.number_of_shards: "1" }
  - match: { test-downsample.settings.index.number_of_replicas: "0" }

  # Assert downsample index mapping
  - do:
      indices.get_mapping:
        index: test-downsample

  - match: { test-downsample.mappings.properties.@timestamp.type: date }
  - match: { test-downsample.mappings.properties.@timestamp.meta.fixed_interval: 1h }
  - match: { test-downsample.mappings.properties.@timestamp.meta.time_zone: UTC }
  - match: { test-downsample.mappings.properties.k8s.properties.pod.properties.multi-gauge.type: aggregate_metric_double }
  - match: { test-downsample.mappings.properties.k8s.properties.pod.properties.multi-gauge.metrics: [ "min", "max", "sum", "value_count" ] }
  - match: { test-downsample.mappings.properties.k8s.properties.pod.properties.multi-gauge.default_metric: max }
  - match: { test-downsample.mappings.properties.k8s.properties.pod.properties.multi-gauge.time_series_metric: gauge }
  - match: { test-downsample.mappings.properties.k8s.properties.pod.properties.multi-counter.type: long }
  - match: { test-downsample.mappings.properties.k8s.properties.pod.properties.multi-counter.time_series_metric: counter }
  - match: { test-downsample.mappings.properties.k8s.properties.pod.properties.uid.type: keyword }
  - match: { test-downsample.mappings.properties.k8s.properties.pod.properties.uid.time_series_dimension: true }


  # Assert source index has not been deleted
  - do:
      indices.get:
        index: test

  # Assert downsample index has been force merged
  - do:
      indices.segments:
        index: test-downsample

  - match:   { _shards.total: 1}
  - match:   { indices.test-downsample.shards.0.0.num_committed_segments: 1}
  - match:   { indices.test-downsample.shards.0.0.num_search_segments: 1}
