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: get-influencers-test
        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-get-influencers-test
        id:     get-influencers-test_1464739200000_1_1
        body:
          {
            "job_id": "get-influencers-test",
            "timestamp": "2016-06-01T00:00:00Z",
            "influencer_field_name": "foo",
            "influencer_field_value": "bar",
            "influencer_score": 50.0,
            "result_type" : "influencer",
            "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-get-influencers-test
        id:     get-influencers-test_1464825600000_1_2
        body:
          {
            "job_id": "get-influencers-test",
            "timestamp": "2016-06-02T00:00:00Z",
            "influencer_field_name": "foo",
            "influencer_field_value": "zoo",
            "influencer_score": 80.0,
            "result_type" : "influencer",
            "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-get-influencers-test
        id:     get-influencers-test_1464912000000_1_3
        body:
          {
            "job_id": "get-influencers-test",
            "timestamp": "2016-06-03T00:00:00Z",
            "influencer_field_name": "foo",
            "influencer_field_value": "zoo",
            "influencer_score": 60.0,
            "result_type" : "influencer",
            "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-get-influencers-test

---
"Test result influencers api":
  - do:
      ml.get_influencers:
        job_id: "get-influencers-test"

  - match: { count: 3 }
  - match: { influencers.0.influencer_score: 80 }
  - match: { influencers.0.timestamp: 1464825600000 }
  - match: { influencers.1.influencer_score: 60 }
  - match: { influencers.1.timestamp: 1464912000000 }
  - match: { influencers.2.influencer_score: 50 }
  - match: { influencers.2.timestamp: 1464739200000 }

---
"Test get influencers with paging":
  - do:
      ml.get_influencers:
        job_id: "get-influencers-test"
        from: 1
        size: 2

  - match: { count: 3 }
  - length: { influencers: 2 }
  - match: { influencers.0.influencer_score: 60 }
  - match: { influencers.0.timestamp: 1464912000000 }
  - match: { influencers.1.influencer_score: 50 }
  - match: { influencers.1.timestamp: 1464739200000 }

---
"Test get influencers with paging in body":
  - do:
      ml.get_influencers:
        job_id: "get-influencers-test"
        body:  >
          {
            "page": {
              "from": 1,
              "size": 2
            }
          }

  - match: { count: 3 }
  - length: { influencers: 2 }
  - match: { influencers.0.influencer_score: 60 }
  - match: { influencers.0.timestamp: 1464912000000 }
  - match: { influencers.1.influencer_score: 50 }
  - match: { influencers.1.timestamp: 1464739200000 }

---
"Test get influencers given exclude_interim false":
  - do:
      ml.get_influencers:
        job_id: "get-influencers-test"
        exclude_interim: false

  - match: { count: 3 }

---
"Test get influencers given exclude_interim true":
  - do:
      ml.get_influencers:
        job_id: "get-influencers-test"
        exclude_interim: true

  - match: { count: 2 }
  - match: { influencers.0.timestamp: 1464912000000 }
  - match: { influencers.0.is_interim: false }
  - match: { influencers.1.timestamp: 1464739200000 }
  - match: { influencers.1.is_interim: false }

---
"Test result influencers api with time range":
  - do:
      ml.get_influencers:
        job_id: "get-influencers-test"
        start: "2016-06-01T00:00:00Z"
        end: "2016-06-01T01:00:00Z"

  - match: { count: 1 }
  - match: { influencers.0.timestamp: 1464739200000 }

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

---
"Test get influencers api with influencer score filter":
  - do:
      ml.get_influencers:
        job_id: "get-influencers-test"
        influencer_score: 70

  - match: { count: 1 }
  - match: { influencers.0.timestamp: 1464825600000 }

---
"Test get influencers api sort":
  - do:
      ml.get_influencers:
        job_id: "get-influencers-test"
        desc: false

  - match: { count: 3 }
  - match: { influencers.0.influencer_score: 50 }
  - match: { influencers.1.influencer_score: 60 }
  - match: { influencers.2.influencer_score: 80}

  - do:
      ml.get_influencers:
        job_id: "get-influencers-test"
        sort: timestamp

  - match: { count: 3 }
  - match: { influencers.0.timestamp: 1464912000000 }
  - match: { influencers.1.timestamp: 1464825600000 }
  - match: { influencers.2.timestamp: 1464739200000 }
