# Test the mode:time_series properly groups by _tsid. If we could put this in
# rest-api-spec we would, but it requires painless.

setup:
  - requires:
      cluster_features: ["gte_v8.1.0"]
      reason: introduced in 8.1.0

  - do:
      indices.create:
          index: test
          body:
            settings:
              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
                number_of_shards: 10
                number_of_replicas: 1
            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
                        ip:
                          type: ip
                        network:
                          properties:
                            tx:
                              type: long
                            rx:
                              type: long
  - do:
      bulk:
        refresh: true
        index: test
        body:
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:35:24.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "ip": "10.10.55.1", "network": {"tx": 2001818691, "rx": 802133794}}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T19:50:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "ip": "10.10.55.1", "network": {"tx": 2005177954, "rx": 801479970}}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T17:53:34.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "ip": "10.10.55.1", "network": {"tx": 2006223737, "rx": 802337279}}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:03:24.467Z", "metricset": "pod", "k8s": {"pod": {"name": "cat", "uid":"947e4ced-1786-4e53-9e0c-5c447e959507", "ip": "10.10.55.2", "network": {"tx": 2012916202, "rx": 803685721}}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:35:24.467Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "ip": "10.10.55.3", "network": {"tx": 1434521831, "rx": 530575198}}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T19:50:04.467Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "ip": "10.10.55.3", "network": {"tx": 1434577921, "rx": 530600088}}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T17:53:34.467Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "ip": "10.10.55.3", "network": {"tx": 1434587694, "rx": 530604797}}}}'
          - '{"index": {}}'
          - '{"@timestamp": "2021-04-28T18:03:24.467Z", "metricset": "pod", "k8s": {"pod": {"name": "dog", "uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9", "ip": "10.10.55.3", "network": {"tx": 1434595272, "rx": 530605511}}}}'

---
"index with replicas and shards is green":
  - requires:
      cluster_features: ["gte_v8.1.0"]
      reason: introduced in 8.1.0

  - do:
      cluster.health:
        wait_for_status: green
  - match:     { status:     green }

---
"each shard has unique _tsids":
  - requires:
      cluster_features: ["gte_v8.1.0"]
      reason: introduced in 8.1.0

  - do:
      search:
        index: test
        body:
          size: 0
          aggs:
            check:
              scripted_metric:
                init_script: "state.timeSeries = new HashSet()"
                map_script: "state.timeSeries.add(doc['metricset'].value + ':' + doc['k8s.pod.uid'].value)"
                combine_script: "return state.timeSeries"
                reduce_script: |
                  Set timeSeries = new TreeSet();
                  for (s in states) {
                    for (ts in s) {
                      boolean newTs = timeSeries.add(ts);
                      if (false == newTs) {
                        throw new IllegalArgumentException(ts + " appeared in two shards");
                      }
                    }
                  }
                  return timeSeries;
  - match: {hits.total.value: 8}
  - match: {aggregations.check.value: ['pod:947e4ced-1786-4e53-9e0c-5c447e959507', 'pod:df3145b3-0563-4d3b-a0f7-897eb2876ea9']}

# todo clone and shrink
