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: job-stats-test
        body:  >
          {
            "job_id":"job-stats-test",
            "description":"Analysis of response time by airline",
            "analysis_config" : {
                "bucket_span": "1h",
                "detectors" :[{"function":"metric","field_name":"responsetime","by_field_name":"airline"}]
            },
            "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.open_job:
        job_id: job-stats-test

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      ml.put_job:
        job_id: jobs-get-stats-datafeed-job
        body:  >
          {
            "job_id":"jobs-get-stats-datafeed-job",
            "description":"A job with a datafeed",
            "analysis_config" : {
                "bucket_span": "1h",
                "detectors" :[{"function":"metric","field_name":"responsetime","by_field_name":"airline"}]
            },
            "analysis_limits" : {
                "model_memory_limit": "10mb"
            },
            "data_description" : {
                "format" : "xcontent",
                "time_field":"time",
                "time_format":"yyyy-MM-dd'T'HH:mm:ssX"
            }
          }
  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      ml.open_job:
        job_id: jobs-get-stats-datafeed-job

  - 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":"jobs-get-stats-datafeed-job",
            "indexes":["farequote"]
          }

---
"Test cat anomaly detector jobs":

  - skip:
      features:
        - "warnings"

  - do:
      warnings:
        - 'Posting data directly to anomaly detection jobs is deprecated, in a future major version it will be compulsory to use a datafeed'
      ml.post_data:
        job_id: job-stats-test
        body: >
          {"airline":"AAL","responsetime":"132.2046","time":"1403481600"}
          {"airline":"JZA","responsetime":"990.4628","time":"1403481600"}
          {"airline":"JZA","responsetime":"244.1276","time":"1403485200"}

  - do:
      warnings:
        - 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
      ml.flush_job:
        job_id: job-stats-test
  - match: { flushed: true }

  - do:
      cat.ml_jobs:
        job_id: job-stats-test
  - match:
      $body: |
        / #id                    state    data.processed_records     model.bytes    model.memory_status     forecasts.total     buckets.count
        ^ (job\-stats\-test \s+  \w+  \s+ \d+                   \s+  .*?        \s+ \w+                 \s+ \d+           \s+  \d+         \n)+  $/

  - do:
      cat.ml_jobs:
        v: true
        job_id: job-stats-test
  - match:
      $body: |
        /^  id                \s+  state \s+ data\.processed_records \s+ model\.bytes \s+ model\.memory_status \s+ forecasts\.total \s+ buckets\.count  \n
           (job\-stats\-test  \s+  \w+   \s+ \d+                     \s+ .*?         \s+ \w+                  \s+ \d+             \s+ \d+            \n)+  $/

  - do:
      cat.ml_jobs:
        h: id,data.processed_records,data.processed_fields,data.input_bytes
        v: true
  - match:
      $body: |
        /^  id                \s+  data\.processed_records \s+ data\.processed_fields \s+ data\.input_bytes \n
           (job\-stats\-test  \s+  \d+                     \s+ \d+                    \s+ \w+               \n)+
           (jobs\-get\-stats\-datafeed\-job  \s+  \d+                     \s+ \d+                    \s+ \w+               \n)+  $/
