setup:
  - skip:
      features: headers
  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      ml.put_job:
        job_id: jobs-get-result-buckets
        body:  >
          {
            "analysis_config" : {
                "detectors" :[{"function":"metric","field_name":"responsetime","by_field_name":"airline"}]
            },
            "data_description" : {
                "format":"xcontent",
                "time_field":"time"
            }
          }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
        Content-Type: application/json
      index:
        index:  .ml-anomalies-jobs-get-result-buckets
        id:     "jobs-get-result-buckets_1464739200000_1"
        body:
          {
            "job_id": "jobs-get-result-buckets",
            "result_type": "bucket",
            "timestamp": "2016-06-01T00:00:00Z",
            "anomaly_score": 90.0,
            "bucket_span":1
          }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
        Content-Type: application/json
      index:
        index:  .ml-anomalies-jobs-get-result-buckets
        id:     "jobs-get-result-buckets_1470009600000_2"
        body:
          {
            "job_id": "jobs-get-result-buckets",
            "result_type": "bucket",
            "timestamp": "2016-08-01T00:00:00Z",
            "anomaly_score": 60.0,
            "bucket_span":1,
            "is_interim": true
          }
  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
        Content-Type: application/json
      index:
        index:  .ml-anomalies-jobs-get-result-buckets
        id:     "jobs-get-result-buckets_1470096000000_3"
        body:
          {
            "job_id": "jobs-get-result-buckets",
            "result_type": "bucket",
            "timestamp": "2016-08-02T00:00:00Z",
            "anomaly_score": 60.0,
            "bucket_span":1,
          }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      indices.refresh:
        index: .ml-anomalies-jobs-get-result-buckets

---
"Test result buckets api with time range":
  - do:
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        start: "2016-05-01T00:00:00Z"
        end: "2016-07-01T00:00:00Z"

  - match: { count: 1 }
  - match: { buckets.0.timestamp: 1464739200000 }
  - match: { buckets.0.job_id: jobs-get-result-buckets}
  - match: { buckets.0.result_type: bucket}

---
"Test result buckets api":
  - do:
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"

  - match: { count: 3 }
  - match: { buckets.0.timestamp: 1464739200000 }
  - match: { buckets.0.job_id: jobs-get-result-buckets}
  - match: { buckets.0.result_type: bucket}
  - match: { buckets.1.timestamp: 1470009600000 }
  - match: { buckets.1.job_id: jobs-get-result-buckets}
  - match: { buckets.1.result_type: bucket}
  - match: { buckets.2.timestamp: 1470096000000 }
  - match: { buckets.2.job_id: jobs-get-result-buckets}
  - match: { buckets.2.result_type: bucket}
  - is_false: buckets.0.partition_scores
  - is_false: buckets.1.partition_scores
  - is_false: buckets.2.partition_scores

---
"Test get buckets with paging":
  - do:
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        from: 1
        size: 2

  - match: { count: 3 }
  - length: { buckets: 2 }
  - match: { buckets.0.timestamp: 1470009600000 }
  - match: { buckets.0.result_type: bucket}
  - match: { buckets.1.timestamp: 1470096000000 }
  - match: { buckets.1.result_type: bucket}

---
"Test get buckets with paging in body":
  - do:
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        body:  >
          {
            "page": {
              "from": 1,
              "size": 2
            }
          }

  - match: { count: 3 }
  - length: { buckets: 2 }
  - match: { buckets.0.timestamp: 1470009600000 }
  - match: { buckets.0.result_type: bucket}
  - match: { buckets.1.timestamp: 1470096000000 }
  - match: { buckets.1.result_type: bucket}

---
"Test get buckets given exclude_interim is false":
  - do:
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        exclude_interim: false

  - match: { count: 3 }

---
"Test get buckets given exclude_interim is true":
  - do:
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        exclude_interim: true

  - match: { count: 2 }
  - match: { buckets.0.timestamp: 1464739200000 }
  - match: { buckets.0.is_interim: false }
  - match: { buckets.1.timestamp: 1470096000000 }
  - match: { buckets.1.is_interim: false }

---
"Test result single bucket api":
  - do:
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        timestamp: "2016-06-01T00:00:00Z"

  - match: { buckets.0.timestamp: 1464739200000}
  - match: { buckets.0.job_id: jobs-get-result-buckets }
  - match: { buckets.0.result_type: bucket}

---
"Test result single bucket api with empty body":
  - do:
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        timestamp: "2016-06-01T00:00:00Z"
        body: {}

  - match: { buckets.0.timestamp: 1464739200000}
  - match: { buckets.0.job_id: jobs-get-result-buckets }
  - match: { buckets.0.result_type: bucket}

---
"Test mutually-exclusive params":
  - do:
      catch: bad_request
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        timestamp: "2016-06-01T00:00:00Z"
        start: "2016-05-01T00:00:00Z"

  - do:
      catch: bad_request
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        timestamp: "2016-06-01T00:00:00Z"
        end: "2016-05-01T00:00:00Z"

  - do:
      catch: bad_request
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        timestamp: "2016-06-01T00:00:00Z"
        from: "2016-05-01T00:00:00Z"

  - do:
      catch: bad_request
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        timestamp: "2016-06-01T00:00:00Z"
        end: "2016-05-01T00:00:00Z"

  - do:
      catch: bad_request
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        timestamp: "2016-06-01T00:00:00Z"
        anomaly_score: "80.0"

---
"Test mutually-exclusive params via body":
  - do:
      catch: bad_request
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        body:
          timestamp: "2016-06-01T00:00:00Z"
          start: "2016-05-01T00:00:00Z"

  - do:
      catch: bad_request
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        body:
          timestamp: "2016-06-01T00:00:00Z"
          end: "2016-05-01T00:00:00Z"

  - do:
      catch: bad_request
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        body:
          timestamp: "2016-06-01T00:00:00Z"
          from: "2016-05-01T00:00:00Z"

  - do:
      catch: bad_request
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        body:
          timestamp: "2016-06-01T00:00:00Z"
          end: "2016-05-01T00:00:00Z"

  - do:
      catch: bad_request
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        body:
          timestamp: "2016-06-01T00:00:00Z"
          anomaly_score: "80.0"

---
"Test with unknown job id":
  - do:
      catch: missing
      ml.get_buckets:
        job_id: "non-existent-job"

---
"Test get buckets with sort field and secondary sort by time":
  - do:
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        sort: anomaly_score

  - match: { count: 3 }
  - match: { buckets.0.anomaly_score: 60.0 }
  - match: { buckets.0.timestamp: 1470009600000 }
  - match: { buckets.1.anomaly_score: 60.0 }
  - match: { buckets.1.timestamp: 1470096000000 }
  - match: { buckets.2.anomaly_score: 90.0}
  - match: { buckets.2.timestamp: 1464739200000 }

  - do:
      ml.get_buckets:
        job_id: "jobs-get-result-buckets"
        sort: anomaly_score
        desc: true

  - match: { count: 3 }
  - match: { buckets.0.anomaly_score: 90.0 }
  - match: { buckets.0.timestamp: 1464739200000 }
  - match: { buckets.1.anomaly_score: 60.0}
  - match: { buckets.1.timestamp: 1470096000000 }
  - match: { buckets.2.anomaly_score: 60.0}
  - match: { buckets.2.timestamp: 1470009600000 }

