setup:
  - skip:
      features: headers
  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      indices.create:
        index: index-1
        body:
          settings:
            index:
              number_of_replicas: 1
          mappings:
            properties:
              time:
                type: date

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      indices.create:
        index: index-2
        body:
          settings:
            index:
              number_of_replicas: 1
          mappings:
            properties:
              time:
                type: date

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      ml.put_job:
        job_id: get-datafeed-stats-1
        body:  >
          {
            "job_id":"get-datafeed-stats-1",
            "analysis_config" : {
                "bucket_span": "1h",
                "detectors" :[{"function":"count"}]
            },
            "analysis_limits" : {
                "model_memory_limit": "10mb"
            },
            "data_description" : {
                "format":"xcontent",
                "time_field":"time",
                "time_format":"epoch"
            }
          }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      ml.put_job:
        job_id: get-datafeed-stats-2
        body:  >
          {
            "job_id":"get-datafeed-stats-2",
            "analysis_config" : {
                "bucket_span": "1h",
                "detectors" :[{"function":"count"}]
            },
            "analysis_limits" : {
                "model_memory_limit": "10mb"
            },
            "data_description" : {
                "time_field":"time"
            }
          }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      ml.put_datafeed:
        datafeed_id: datafeed-1
        body: >
          {
            "job_id":"get-datafeed-stats-1",
            "indexes":["index-1"]
          }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      ml.put_datafeed:
        datafeed_id: datafeed-2
        body: >
          {
            "job_id":"get-datafeed-stats-2",
            "indexes":["index-2"]
          }

---
"Test get datafeed stats given missing datafeed_id":

  - do:
      catch: missing
      ml.get_datafeed_stats:
        datafeed_id: missing-datafeed

---
"Test get datafeed stats with expression that does not match and allow_no_match":
  - skip:
      features:
        - "warnings"

  - do:
      ml.get_datafeed_stats:
        datafeed_id: "missing-*"
        allow_no_match: true
  - match: { count: 0 }
  - match: { datafeeds: [] }

---
"Test get datafeed stats with expression that does not match and not allow_no_match":
  - skip:
      features:
        - "warnings"

  - do:
      catch: missing
      ml.get_datafeed_stats:
        datafeed_id: "missing-*"
        allow_no_match: false

---
"Test get single datafeed stats":

  - do:
      ml.get_datafeed_stats:
        datafeed_id: datafeed-1
  - match:  { datafeeds.0.datafeed_id: "datafeed-1"}
  - match:  { datafeeds.0.state: "stopped"}
  - is_false: datafeeds.0.node
  - is_false: datafeeds.0.running_state
  - match:  { datafeeds.0.timing_stats.job_id: "get-datafeed-stats-1" }
  - match:  { datafeeds.0.timing_stats.search_count: 0 }
  - match:  { datafeeds.0.timing_stats.total_search_time_ms: 0.0}

  - do:
      ml.get_datafeed_stats:
        datafeed_id: datafeed-2
  - match:  { datafeeds.0.datafeed_id: "datafeed-2"}
  - match:  { datafeeds.0.state: "stopped"}
  - is_false: datafeeds.0.node
  - is_false: datafeeds.0.running_state
  - match:  { datafeeds.0.timing_stats.job_id: "get-datafeed-stats-2" }
  - match:  { datafeeds.0.timing_stats.search_count: 0 }
  - match:  { datafeeds.0.timing_stats.total_search_time_ms: 0.0}

---
"Test get stats for started datafeed":

  - do:
      ml.open_job:
        job_id: get-datafeed-stats-1

  - do:
      ml.start_datafeed:
        datafeed_id: "datafeed-1"
        start: 0

  - do:
      ml.get_datafeed_stats:
        datafeed_id: datafeed-1
  - match: { datafeeds.0.datafeed_id: "datafeed-1"}
  - match: { datafeeds.0.state: "started"}
  - is_true: datafeeds.0.node.name
  - is_true: datafeeds.0.node.transport_address
  - is_true: datafeeds.0.running_state
  - is_true: datafeeds.0.running_state.real_time_configured

---
"Test get stats for started datafeed contains timing stats":

  - do:
      ml.open_job:
        job_id: get-datafeed-stats-1

  - do:
      ml.start_datafeed:
        datafeed_id: "datafeed-1"
        start: 0
  - match: { started: true}

  - do:
      ml.get_datafeed_stats:
        datafeed_id: datafeed-1
  - match:  { datafeeds.0.datafeed_id: "datafeed-1"}
  - match:  { datafeeds.0.state: "started"}
  - match:  { datafeeds.0.timing_stats.job_id: "get-datafeed-stats-1"}
  # We don't really know at this point if datafeed managed to perform at least one search, hence the very relaxed assertion
  - gte:  { datafeeds.0.timing_stats.search_count: 0}
  - gte:  { datafeeds.0.timing_stats.bucket_count: 0}
  - gte:  { datafeeds.0.timing_stats.total_search_time_ms: 0.0}

  - do:
      ml.stop_datafeed:
        datafeed_id: "datafeed-1"
  - match: { stopped: true}

  - do:
      ml.get_datafeed_stats:
        datafeed_id: datafeed-1
  - match: { datafeeds.0.datafeed_id: "datafeed-1"}
  - match: { datafeeds.0.state: "stopped"}
  - match: { datafeeds.0.timing_stats.job_id: "get-datafeed-stats-1"}
  # We don't really know at this point if datafeed managed to perform at least one search, hence the very relaxed assertion
  - gte:   { datafeeds.0.timing_stats.search_count: 0}
  - gte:   { datafeeds.0.timing_stats.bucket_count: 0}
  - gte:   { datafeeds.0.timing_stats.total_search_time_ms: 0.0}

---
"Test implicit get all datafeed stats given started datafeeds":

  - do:
      ml.open_job:
        job_id: get-datafeed-stats-1

  - do:
      ml.start_datafeed:
        datafeed_id: "datafeed-1"
        start: 0

  - do:
      ml.open_job:
        job_id: get-datafeed-stats-2

  - do:
      ml.start_datafeed:
        datafeed_id: "datafeed-2"
        start: 0

  - do:
      ml.get_datafeed_stats: {}
  - match: { count: 2 }
  - match: { datafeeds.0.datafeed_id: "datafeed-1"}
  - match: { datafeeds.0.state: "started"}
  - match: { datafeeds.1.datafeed_id: "datafeed-2"}
  - match: { datafeeds.1.state: "started"}

---
"Test explicit get all datafeed stats given stopped datafeeds":

  - do:
      ml.get_datafeed_stats:
        datafeed_id: _all
  - match: { count: 2 }
  - match: { datafeeds.0.datafeed_id: "datafeed-1"}
  - match: { datafeeds.0.state: "stopped"}
  - match: { datafeeds.1.datafeed_id: "datafeed-2"}
  - match: { datafeeds.1.state: "stopped"}

---
"Test implicit get all datafeed stats given stopped datafeeds":

  - do:
      ml.get_datafeed_stats: {}
  - match: { count: 2 }
  - match: { datafeeds.0.datafeed_id: "datafeed-1"}
  - match: { datafeeds.0.state: "stopped"}
  - match: { datafeeds.1.datafeed_id: "datafeed-2"}
  - match: { datafeeds.1.state: "stopped"}

---
"Test get datafeed stats when total_search_time_ms mapping is missing":

  - skip:
      features: headers

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      indices.delete:
        index: ".ml-anomalies-shared"

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      indices.create:
        index: "ml-special-index-to-avoid-picking-up-template"
        body:
          mappings:
            properties:
              timestamp:
                type: date

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      indices.put_alias:
        index: "ml-special-index-to-avoid-picking-up-template"
        name: ".ml-anomalies-get-datafeed-stats-1"

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      indices.put_alias:
        index: "ml-special-index-to-avoid-picking-up-template"
        name: ".ml-anomalies-.write-get-datafeed-stats-1"

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      indices.put_alias:
        index: "ml-special-index-to-avoid-picking-up-template"
        name: ".ml-anomalies-get-datafeed-stats-2"

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      indices.put_alias:
        index: "ml-special-index-to-avoid-picking-up-template"
        name: ".ml-anomalies-.write-get-datafeed-stats-2"

  - do:
      ml.get_datafeed_stats: {}
  - match: { count: 2 }
  - match: { datafeeds.0.datafeed_id: "datafeed-1"}
  - match: { datafeeds.0.state: "stopped"}
  - match: { datafeeds.1.datafeed_id: "datafeed-2"}
  - match: { datafeeds.1.state: "stopped"}
