---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- do:
    indices.create:
      index: "foo"
      body:
        mappings:
          properties:
            timestamp:
              type: "date"
            user:
              type: "keyword"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    bulk:
      refresh: true
      body:
      - index:
          _index: "foo"
      - timestamp: "2017-01-01T05:00:00Z"
        user: "a"
      - index:
          _index: "foo"
      - timestamp: "2017-01-01T05:00:00Z"
        user: "b"
      - index:
          _index: "foo"
      - timestamp: "2017-01-01T05:00:00Z"
        user: "c"
      - index:
          _index: "foo"
      - timestamp: "2017-01-02T05:00:00Z"
        user: "a"
      - index:
          _index: "foo"
      - timestamp: "2017-01-02T05:00:00Z"
        user: "b"
      - index:
          _index: "foo"
      - timestamp: "2017-01-03T05:00:00Z"
        user: "d"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
rescale_0_1:
- skip:
    features: "close_to"
- do:
    search:
      index: "foo"
      body:
        size: 0
        aggs:
          users_by_day:
            date_histogram:
              field: "timestamp"
              calendar_interval: "day"
            aggs:
              percent_of_total_users:
                normalize:
                  buckets_path: "_count"
                  method: "rescale_0_1"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- length:
    aggregations.users_by_day.buckets: 3
- match:
    aggregations.users_by_day.buckets.0.key_as_string: "2017-01-01T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.0.doc_count: 3
- close_to:
    aggregations.users_by_day.buckets.0.percent_of_total_users.value:
      value: 1.0
      error: 0.05
- match:
    aggregations.users_by_day.buckets.1.key_as_string: "2017-01-02T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.1.doc_count: 2
- close_to:
    aggregations.users_by_day.buckets.1.percent_of_total_users.value:
      value: 0.5
      error: 0.05
- match:
    aggregations.users_by_day.buckets.2.key_as_string: "2017-01-03T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.2.doc_count: 1
- close_to:
    aggregations.users_by_day.buckets.2.percent_of_total_users.value:
      value: 0.0
      error: 0.05
---
rescale_0_100:
- skip:
    features: "close_to"
- do:
    search:
      index: "foo"
      body:
        size: 0
        aggs:
          users_by_day:
            date_histogram:
              field: "timestamp"
              calendar_interval: "day"
            aggs:
              percent_of_total_users:
                normalize:
                  buckets_path: "_count"
                  method: "rescale_0_100"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- length:
    aggregations.users_by_day.buckets: 3
- match:
    aggregations.users_by_day.buckets.0.key_as_string: "2017-01-01T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.0.doc_count: 3
- close_to:
    aggregations.users_by_day.buckets.0.percent_of_total_users.value:
      value: 100
      error: 0.5
- match:
    aggregations.users_by_day.buckets.1.key_as_string: "2017-01-02T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.1.doc_count: 2
- close_to:
    aggregations.users_by_day.buckets.1.percent_of_total_users.value:
      value: 50
      error: 0.5
- match:
    aggregations.users_by_day.buckets.2.key_as_string: "2017-01-03T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.2.doc_count: 1
- close_to:
    aggregations.users_by_day.buckets.2.percent_of_total_users.value:
      value: 0
      error: 0.5
---
percent_of_sum:
- skip:
    features: "close_to"
- do:
    search:
      index: "foo"
      body:
        size: 0
        aggs:
          users_by_day:
            date_histogram:
              field: "timestamp"
              calendar_interval: "day"
            aggs:
              percent_of_total_users:
                normalize:
                  buckets_path: "_count"
                  method: "percent_of_sum"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- length:
    aggregations.users_by_day.buckets: 3
- match:
    aggregations.users_by_day.buckets.0.key_as_string: "2017-01-01T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.0.doc_count: 3
- close_to:
    aggregations.users_by_day.buckets.0.percent_of_total_users.value:
      value: 0.5
      error: 0.05
- match:
    aggregations.users_by_day.buckets.1.key_as_string: "2017-01-02T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.1.doc_count: 2
- close_to:
    aggregations.users_by_day.buckets.1.percent_of_total_users.value:
      value: 0.3
      error: 0.05
- match:
    aggregations.users_by_day.buckets.2.key_as_string: "2017-01-03T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.2.doc_count: 1
- close_to:
    aggregations.users_by_day.buckets.2.percent_of_total_users.value:
      value: 0.2
      error: 0.05
---
mean:
- skip:
    features: "close_to"
- do:
    search:
      index: "foo"
      body:
        size: 0
        aggs:
          users_by_day:
            date_histogram:
              field: "timestamp"
              calendar_interval: "day"
            aggs:
              percent_of_total_users:
                normalize:
                  buckets_path: "_count"
                  method: "mean"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- length:
    aggregations.users_by_day.buckets: 3
- match:
    aggregations.users_by_day.buckets.0.key_as_string: "2017-01-01T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.0.doc_count: 3
- close_to:
    aggregations.users_by_day.buckets.0.percent_of_total_users.value:
      value: 0.5
      error: 0.05
- match:
    aggregations.users_by_day.buckets.1.key_as_string: "2017-01-02T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.1.doc_count: 2
- close_to:
    aggregations.users_by_day.buckets.1.percent_of_total_users.value:
      value: 0.0
      error: 0.05
- match:
    aggregations.users_by_day.buckets.2.key_as_string: "2017-01-03T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.2.doc_count: 1
- close_to:
    aggregations.users_by_day.buckets.2.percent_of_total_users.value:
      value: -0.5
      error: 0.05
---
zscore:
- skip:
    features: "close_to"
- do:
    search:
      index: "foo"
      body:
        size: 0
        aggs:
          users_by_day:
            date_histogram:
              field: "timestamp"
              calendar_interval: "day"
            aggs:
              percent_of_total_users:
                normalize:
                  buckets_path: "_count"
                  method: "z-score"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- length:
    aggregations.users_by_day.buckets: 3
- match:
    aggregations.users_by_day.buckets.0.key_as_string: "2017-01-01T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.0.doc_count: 3
- close_to:
    aggregations.users_by_day.buckets.0.percent_of_total_users.value:
      value: 1.2
      error: 0.05
- match:
    aggregations.users_by_day.buckets.1.key_as_string: "2017-01-02T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.1.doc_count: 2
- close_to:
    aggregations.users_by_day.buckets.1.percent_of_total_users.value:
      value: 0.0
      error: 0.05
- match:
    aggregations.users_by_day.buckets.2.key_as_string: "2017-01-03T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.2.doc_count: 1
- close_to:
    aggregations.users_by_day.buckets.2.percent_of_total_users.value:
      value: -1.22
      error: 0.05
---
softmax:
- skip:
    features: "close_to"
- do:
    search:
      index: "foo"
      body:
        size: 0
        aggs:
          users_by_day:
            date_histogram:
              field: "timestamp"
              calendar_interval: "day"
            aggs:
              percent_of_total_users:
                normalize:
                  buckets_path: "_count"
                  method: "softmax"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- length:
    aggregations.users_by_day.buckets: 3
- match:
    aggregations.users_by_day.buckets.0.key_as_string: "2017-01-01T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.0.doc_count: 3
- close_to:
    aggregations.users_by_day.buckets.0.percent_of_total_users.value:
      value: 0.67
      error: 0.05
- match:
    aggregations.users_by_day.buckets.1.key_as_string: "2017-01-02T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.1.doc_count: 2
- close_to:
    aggregations.users_by_day.buckets.1.percent_of_total_users.value:
      value: 0.24
      error: 0.05
- match:
    aggregations.users_by_day.buckets.2.key_as_string: "2017-01-03T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.2.doc_count: 1
- close_to:
    aggregations.users_by_day.buckets.2.percent_of_total_users.value:
      value: 0.09
      error: 0.05
---
format:
- skip:
    features: "close_to"
- do:
    search:
      index: "foo"
      body:
        size: 0
        aggs:
          users_by_day:
            date_histogram:
              field: "timestamp"
              calendar_interval: "day"
            aggs:
              percent_of_total_users:
                normalize:
                  buckets_path: "_count"
                  method: "percent_of_sum"
                  format: "00.00%"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- length:
    aggregations.users_by_day.buckets: 3
- match:
    aggregations.users_by_day.buckets.0.key_as_string: "2017-01-01T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.0.doc_count: 3
- close_to:
    aggregations.users_by_day.buckets.0.percent_of_total_users.value:
      value: 0.5
      error: 0.05
- match:
    aggregations.users_by_day.buckets.0.percent_of_total_users.value_as_string: "50.00%"
- match:
    aggregations.users_by_day.buckets.1.key_as_string: "2017-01-02T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.1.doc_count: 2
- close_to:
    aggregations.users_by_day.buckets.1.percent_of_total_users.value:
      value: 0.3
      error: 0.05
- match:
    aggregations.users_by_day.buckets.1.percent_of_total_users.value_as_string: "33.33%"
- match:
    aggregations.users_by_day.buckets.2.key_as_string: "2017-01-03T00:00:00.000Z"
- match:
    aggregations.users_by_day.buckets.2.doc_count: 1
- close_to:
    aggregations.users_by_day.buckets.2.percent_of_total_users.value:
      value: 0.2
      error: 0.05
- match:
    aggregations.users_by_day.buckets.2.percent_of_total_users.value_as_string: "16.67%"
---
bad path:
- skip:
    features: "close_to"
- do:
    catch: "/No aggregation found for path \\[badpath\\]/"
    search:
      index: "foo"
      body:
        size: 0
        aggs:
          users_by_day:
            date_histogram:
              field: "timestamp"
              calendar_interval: "day"
            aggs:
              percent_of_total_users:
                normalize:
                  buckets_path: "badpath"
                  method: "rescale_0_1"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
