setup:
  - do:
      indices.create:
        index: airline-data
        body:
          mappings:
            properties:
              time:
                type: date
              airline:
                type: keyword
              responsetime:
                type: float
              event_rate:
                type: integer

  - do:
      index:
        index: airline-data
        id: "1"
        body: >
          {
            "time": "2017-02-18T00:00:00Z",
            "airline": "foo",
            "responsetime": 1.0,
            "event_rate": 5
          }

  - do:
      index:
        index: airline-data
        id: "2"
        body: >
          {
            "time": "2017-02-18T00:30:00Z",
            "airline": "foo",
            "responsetime": 1.0,
            "event_rate": 6
          }

  - do:
      index:
        index: airline-data
        id: "3"
        body: >
          {
            "time": "2017-02-18T01:00:00Z",
            "airline": "bar",
            "responsetime": 42.0,
            "event_rate": 8
          }

  - do:
      index:
        index: airline-data
        id: "4"
        body: >
          {
            "time": "2017-02-18T01:01:00Z",
            "airline": "foo",
            "responsetime": 42.0,
            "event_rate": 7
          }

  - do:
      indices.refresh:
        index: airline-data

---
"Test preview scroll datafeed":

  - do:
      ml.put_job:
        job_id: preview-datafeed-job
        body:  >
          {
            "analysis_config" : {
                "bucket_span": "1h",
                "detectors" :[{"function":"sum","field_name":"responsetime","by_field_name":"airline"}]
            },
            "data_description" : {
                "time_field":"time"
            }
          }

  - do:
      ml.put_datafeed:
        datafeed_id: preview-datafeed-feed
        body:  >
          {
            "job_id":"preview-datafeed-job",
            "indexes":"airline-data"
          }

  - do:
      ml.preview_datafeed:
        datafeed_id: preview-datafeed-feed
  - length: { $body: 4 }
  - match: { 0.time: 1487376000000 }
  - match: { 0.airline: foo }
  - match: { 0.responsetime: 1.0 }
  - match: { 1.time: 1487377800000 }
  - match: { 1.airline: foo }
  - match: { 1.responsetime: 1.0 }
  - match: { 2.time: 1487379600000 }
  - match: { 2.airline: bar }
  - match: { 2.responsetime: 42.0 }
  - match: { 3.time: 1487379660000 }
  - match: { 3.airline: foo }
  - match: { 3.responsetime: 42.0 }

  - do:
      ml.preview_datafeed:
        datafeed_id: preview-datafeed-feed
        body: >
          {}
  - length: { $body: 4 }
  - match: { 0.time: 1487376000000 }
  - match: { 0.airline: foo }
  - match: { 0.responsetime: 1.0 }
  - match: { 1.time: 1487377800000 }
  - match: { 1.airline: foo }
  - match: { 1.responsetime: 1.0 }
  - match: { 2.time: 1487379600000 }
  - match: { 2.airline: bar }
  - match: { 2.responsetime: 42.0 }
  - match: { 3.time: 1487379660000 }
  - match: { 3.airline: foo }
  - match: { 3.responsetime: 42.0 }

  - do:
      ml.preview_datafeed:
        body: >
          {
            "datafeed_config": {
              "job_id":"preview-datafeed-job",
              "indexes":"airline-data"
            }
          }
  - length: { $body: 4 }
  - match: { 0.time: 1487376000000 }
  - match: { 0.airline: foo }
  - match: { 0.responsetime: 1.0 }
  - match: { 1.time: 1487377800000 }
  - match: { 1.airline: foo }
  - match: { 1.responsetime: 1.0 }
  - match: { 2.time: 1487379600000 }
  - match: { 2.airline: bar }
  - match: { 2.responsetime: 42.0 }
  - match: { 3.time: 1487379660000 }
  - match: { 3.airline: foo }
  - match: { 3.responsetime: 42.0 }

  - do:
      ml.preview_datafeed:
        body: >
          {
            "datafeed_config": {
              "job_id":"preview-datafeed-job",
              "indexes":"airline-data"
            },
            "job_config": {
              "analysis_config": {
                "bucket_span": "1h",
                "detectors": [{"function":"sum","field_name":"responsetime","by_field_name":"airline"}]
              },
              "data_description": {
                "time_field":"time"
              }
            }
          }
  - length: { $body: 4 }
  - match: { 0.time: 1487376000000 }
  - match: { 0.airline: foo }
  - match: { 0.responsetime: 1.0 }
  - match: { 1.time: 1487377800000 }
  - match: { 1.airline: foo }
  - match: { 1.responsetime: 1.0 }
  - match: { 2.time: 1487379600000 }
  - match: { 2.airline: bar }
  - match: { 2.responsetime: 42.0 }
  - match: { 3.time: 1487379660000 }
  - match: { 3.airline: foo }
  - match: { 3.responsetime: 42.0 }

  - do:
      ml.preview_datafeed:
        body: >
          {
            "job_config": {
              "analysis_config": {
                "bucket_span": "1h",
                "detectors": [{"function":"sum","field_name":"responsetime","by_field_name":"airline"}]
              },
              "data_description": {
                "time_field":"time"
              },
              "datafeed_config": {
                "indexes":"airline-data"
              }
            }
          }
  - length: { $body: 4 }
  - match: { 0.time: 1487376000000 }
  - match: { 0.airline: foo }
  - match: { 0.responsetime: 1.0 }
  - match: { 1.time: 1487377800000 }
  - match: { 1.airline: foo }
  - match: { 1.responsetime: 1.0 }
  - match: { 2.time: 1487379600000 }
  - match: { 2.airline: bar }
  - match: { 2.responsetime: 42.0 }
  - match: { 3.time: 1487379660000 }
  - match: { 3.airline: foo }
  - match: { 3.responsetime: 42.0 }
---
"Test preview aggregation datafeed with doc_count":

  - do:
      ml.put_job:
        job_id: aggregation-doc-count-job
        body:  >
          {
            "analysis_config" : {
                "bucket_span": "1h",
                "summary_count_field_name": "doc_count",
                "detectors" :[{"function":"sum","field_name":"responsetime","by_field_name":"airline"}]
            },
            "data_description" : {
                "time_field":"time"
            }
          }

  - do:
      ml.put_datafeed:
        datafeed_id: aggregation-doc-count-feed
        body:  >
          {
            "job_id":"aggregation-doc-count-job",
            "indexes":"airline-data",
            "aggregations": {
              "buckets": {
                "histogram": {
                  "field": "time",
                  "interval": 3600000
                },
                "aggregations": {
                  "time": {
                    "max": {
                      "field": "time"
                    }
                  },
                  "airline": {
                    "terms": {
                      "field": "airline",
                      "size": 100
                    },
                    "aggregations": {
                      "responsetime": {
                        "sum": {
                           "field": "responsetime"
                        }
                      }
                    }
                  }
                }
              }
            }
          }

  - do:
      ml.preview_datafeed:
        datafeed_id: aggregation-doc-count-feed
  - length: { $body: 3 }
  - match: { 0.time: 1487377800000 }
  - match: { 0.airline: foo }
  - match: { 0.responsetime: 2.0 }
  - match: { 0.doc_count: 2 }
  - match: { 1.time: 1487379660000 }
  - match: { 1.airline: bar }
  - match: { 1.responsetime: 42.0 }
  - match: { 1.doc_count: 1 }
  - match: { 1.time: 1487379660000 }
  - match: { 2.airline: foo }
  - match: { 2.responsetime: 42.0 }
  - match: { 2.doc_count: 1 }
  - do:
      ml.preview_datafeed:
        body: >
          {
            "datafeed_config": {
              "indexes":"airline-data",
              "aggregations": {
                "buckets": {
                  "histogram": {
                    "field": "time",
                    "interval": 3600000
                  },
                  "aggregations": {
                    "time": {
                      "max": {
                        "field": "time"
                      }
                    },
                    "airline": {
                      "terms": {
                        "field": "airline",
                        "size": 100
                      },
                      "aggregations": {
                        "responsetime": {
                          "sum": {
                             "field": "responsetime"
                          }
                        }
                      }
                    }
                  }
                }
              }
            },
            "job_config": {
              "analysis_config" : {
                  "bucket_span": "1h",
                  "summary_count_field_name": "doc_count",
                  "detectors" :[{"function":"sum","field_name":"responsetime","by_field_name":"airline"}]
              },
              "data_description" : {
                  "time_field":"time"
              }
            }
          }
  - length: { $body: 3 }
  - match: { 0.time: 1487377800000 }
  - match: { 0.airline: foo }
  - match: { 0.responsetime: 2.0 }
  - match: { 0.doc_count: 2 }
  - match: { 1.time: 1487379660000 }
  - match: { 1.airline: bar }
  - match: { 1.responsetime: 42.0 }
  - match: { 1.doc_count: 1 }
  - match: { 1.time: 1487379660000 }
  - match: { 2.airline: foo }
  - match: { 2.responsetime: 42.0 }
  - match: { 2.doc_count: 1 }

  - do:
      ml.preview_datafeed:
        body: >
          {
            "job_config": {
              "analysis_config" : {
                  "bucket_span": "1h",
                  "summary_count_field_name": "doc_count",
                  "detectors" :[{"function":"sum","field_name":"responsetime","by_field_name":"airline"}]
              },
              "data_description" : {
                  "time_field":"time"
              },
              "datafeed_config": {
                "indexes":"airline-data",
                "aggregations": {
                  "buckets": {
                    "histogram": {
                      "field": "time",
                      "interval": 3600000
                    },
                    "aggregations": {
                      "time": {
                        "max": {
                          "field": "time"
                        }
                      },
                      "airline": {
                        "terms": {
                          "field": "airline",
                          "size": 100
                        },
                        "aggregations": {
                          "responsetime": {
                            "sum": {
                               "field": "responsetime"
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
  - length: { $body: 3 }
  - match: { 0.time: 1487377800000 }
  - match: { 0.airline: foo }
  - match: { 0.responsetime: 2.0 }
  - match: { 0.doc_count: 2 }
  - match: { 1.time: 1487379660000 }
  - match: { 1.airline: bar }
  - match: { 1.responsetime: 42.0 }
  - match: { 1.doc_count: 1 }
  - match: { 1.time: 1487379660000 }
  - match: { 2.airline: foo }
  - match: { 2.responsetime: 42.0 }
  - match: { 2.doc_count: 1 }
---
"Test preview single metric aggregation datafeed with different summary count field":

  - do:
      ml.put_job:
        job_id: aggregation-custom-single-metric-summary-job
        body:  >
          {
            "analysis_config" : {
                "bucket_span": "3600s",
                "summary_count_field_name": "dc_airline",
                "detectors" :[{"function":"count"}]
            },
            "data_description" : {
                "time_field":"time"
            }
          }

  - do:
      ml.put_datafeed:
        datafeed_id: aggregation-custom-single-metric-summary-feed
        body:  >
          {
            "job_id":"aggregation-custom-single-metric-summary-job",
            "indexes":"airline-data",
            "aggregations": {
              "buckets": {
                "histogram": {
                  "field": "time",
                  "interval": 3600000
                },
                "aggregations": {
                  "time": {
                    "max": {
                      "field": "time"
                    }
                  },
                  "dc_airline": {
                    "cardinality": {
                      "field": "airline"
                    }
                  }
                }
              }
            }
          }

  - do:
      ml.preview_datafeed:
        datafeed_id: aggregation-custom-single-metric-summary-feed
  - length: { $body: 2 }
  - match: { 0.time: 1487377800000 }
  - match: { 0.dc_airline: 1 }
  - is_false: 0.doc_count
  - match: { 1.time: 1487379660000 }
  - match: { 1.dc_airline: 2 }
  - is_false: 1.doc_count

---
"Test preview multi metric aggregation datafeed with different summary count field":

  - do:
      ml.put_job:
        job_id: aggregation-custom-multi-metric-summary-job
        body:  >
          {
            "analysis_config" : {
                "bucket_span": "3600s",
                "summary_count_field_name": "event_rate",
                "detectors" :[{"function":"mean","field_name":"responsetime","by_field_name":"airline"}]
            },
            "data_description" : {
                "time_field":"time"
            }
          }

  - do:
      ml.put_datafeed:
        datafeed_id: aggregation-custom-multi-metric-summary-feed
        body:  >
          {
            "job_id":"aggregation-custom-multi-metric-summary-job",
            "indexes":"airline-data",
            "aggregations": {
              "buckets": {
                "histogram": {
                  "field": "time",
                  "interval": 3600000
                },
                "aggregations": {
                  "time": {
                    "max": {
                      "field": "time"
                    }
                  },
                  "airline": {
                    "terms": {
                        "field": "airline"
                    },
                    "aggs": {
                      "responsetime": {
                        "sum": {
                          "field": "responsetime"
                        }
                      },
                      "event_rate": {
                        "sum": {
                          "field": "event_rate"
                        }
                      }
                    }
                  }
                }
              }
            }
          }

  - do:
      ml.preview_datafeed:
        datafeed_id: aggregation-custom-multi-metric-summary-feed
  - length: { $body: 3 }
  - match: { 0.time: 1487377800000 }
  - match: { 0.airline: foo }
  - match: { 0.responsetime: 2.0 }
  - match: { 0.event_rate: 11 }
  - is_false: 0.doc_count
  - match: { 1.time: 1487379660000 }
  - match: { 1.airline: bar }
  - match: { 1.responsetime: 42.0 }
  - match: { 1.event_rate: 8 }
  - is_false: 1.doc_count
  - match: { 1.time: 1487379660000 }
  - match: { 2.airline: foo }
  - match: { 2.responsetime: 42.0 }
  - match: { 2.event_rate: 7 }
  - is_false: 2.doc_count

---
"Test preview missing datafeed":

  - do:
      catch: missing
      ml.preview_datafeed:
        datafeed_id: missing-feed

---
"Test preview with datafeed_id and job config":

  - do:
      catch: bad_request
      ml.preview_datafeed:
        datafeed_id: some_datafeed_id
        body: >
          {
             "job_config": {
               "analysis_config" : {
                 "bucket_span": "1h",
                 "detectors" :[{"function":"sum","field_name":"responsetime","by_field_name":"airline"}]
               },
               "data_description" : {
                 "time_field":"time"
               }
             }
          }
---
"Test preview with datafeed id and config":

  - do:
      catch: bad_request
      ml.preview_datafeed:
        body: >
          {
             "job_config": {
               "analysis_config" : {
                 "bucket_span": "1h",
                 "detectors" :[{"function":"sum","field_name":"responsetime","by_field_name":"airline"}]
               },
               "data_description" : {
                 "time_field":"time"
               }
             }
          }
---
"Test preview with datafeed config and job config with datafeed config":

  - do:
      catch: bad_request
      ml.preview_datafeed:
        body: >
          {
            "datafeed_config": {
              "indexes":"airline-data"
            },
            "job_config": {
              "analysis_config" : {
                "bucket_span": "1h",
                "detectors" :[{"function":"sum","field_name":"responsetime","by_field_name":"airline"}]
              },
              "data_description" : {
                "time_field":"time"
              },
              "datafeed_config": {
                "indexes":"airline-data"
              }
            }
          }
---
"Test preview datafeed with unavailable index":

  - do:
      ml.put_job:
        job_id: unavailable-job
        body:  >
          {
            "analysis_config" : {
                "bucket_span": "1h",
                "detectors" :[{"function":"sum","field_name":"responsetime","by_field_name":"airline"}]
            },
            "data_description" : {
                "time_field":"time"
            }
          }

  - do:
      ml.put_datafeed:
        datafeed_id: unavailable-feed
        body:  >
          {
            "job_id":"unavailable-job",
            "indexes":"unavailable-data"
          }

  - do:
      catch: missing
      ml.preview_datafeed:
        datafeed_id: unavailable-feed

---
"Test preview datafeed with query that matches nothing":

  - do:
      ml.put_job:
        job_id: empty-job
        body:  >
          {
            "analysis_config" : {
                "bucket_span": "1h",
                "detectors" :[{"function":"sum","field_name":"responsetime","by_field_name":"airline"}]
            },
            "data_description" : {
                "time_field":"time"
            }
          }

  - do:
      ml.put_datafeed:
        datafeed_id: empty-feed
        body:  >
          {
            "job_id":"empty-job",
            "indexes":"airline-data",
            "query": {
              "term": {"airline":"missing"}
            }
          }

  - do:
      ml.preview_datafeed:
        datafeed_id: empty-feed
  - length: { $body: 0 }


---
"Test preview aggregation with runtime fields":

  - do:
      ml.put_job:
        job_id: runtime-field-aggregation-job
        body:  >
          {
            "analysis_config" : {
                "bucket_span": "3600s",
                "summary_count_field_name": "doc_count",
                "detectors" :[{"function":"mean","field_name":"responsetime_x_2","by_field_name":"airline"}]
            },
            "data_description" : {
                "time_field":"time"
            }
          }

  - do:
      ml.put_datafeed:
        datafeed_id: aggregation-doc-count-feed
        body:  >
          {
            "job_id":"runtime-field-aggregation-job",
            "indexes":"airline-data",
            "runtime_mappings" : {
              "responsetime_x_2": {
                "type": "double",
                "script": "emit(doc['responsetime'].value * 2.0)"
              }
            },
            "aggregations": {
              "buckets": {
                "histogram": {
                  "field": "time",
                  "interval": 3600000
                },
                "aggregations": {
                  "time": {
                    "max": {
                      "field": "time"
                    }
                  },
                  "airline": {
                    "terms": {
                      "field": "airline",
                      "size": 100
                    },
                    "aggregations": {
                      "responsetime_x_2": {
                        "sum": {
                           "field": "responsetime_x_2"
                        }
                      }
                    }
                  }
                }
              }
            }
          }

  - do:
      ml.preview_datafeed:
        datafeed_id: aggregation-doc-count-feed
  - length: { $body: 3 }
  - match: { 0.time: 1487377800000 }
  - match: { 0.airline: foo }
  - match: { 0.responsetime_x_2: 4.0 }
  - match: { 0.doc_count: 2 }
  - match: { 1.time: 1487379660000 }
  - match: { 1.airline: bar }
  - match: { 1.responsetime_x_2: 84.0 }
  - match: { 1.doc_count: 1 }
  - match: { 1.time: 1487379660000 }
  - match: { 2.airline: foo }
  - match: { 2.responsetime_x_2: 84.0 }
  - match: { 2.doc_count: 1 }

---
"Test preview datafeed where time field is data_nanos":

  - do:
      indices.create:
        index: index-airline-data-date-nanos
        body:
          mappings:
            properties:
              time:
                type: date_nanos
              airline:
                type: keyword
              responsetime:
                type: float
              event_rate:
                type: integer

  - do:
      index:
        index: index-airline-data-date-nanos
        id: "1"
        body: >
          {
            "time": "2017-02-18T00:00:00Z",
            "airline": "foo",
            "responsetime": 1.0,
            "event_rate": 5
          }

  - do:
      index:
        index: index-airline-data-date-nanos
        id: "2"
        body: >
          {
            "time": "2017-02-18T00:30:00Z",
            "airline": "foo",
            "responsetime": 1.0,
            "event_rate": 6
          }

  - do:
      index:
        index: index-airline-data-date-nanos
        id: "3"
        body: >
          {
            "time": "2017-02-18T01:00:00Z",
            "airline": "bar",
            "responsetime": 42.0,
            "event_rate": 8
          }

  - do:
      index:
        index: index-airline-data-date-nanos
        id: "4"
        body: >
          {
            "time": "2017-02-18T01:01:00Z",
            "airline": "foo",
            "responsetime": 42.0,
            "event_rate": 7
          }

  - do:
      indices.refresh:
        index: index-airline-data-date-nanos

  - do:
      ml.preview_datafeed:
        body: >
          {
            "datafeed_config": {
              "indexes":"index-airline-data-date-nanos"
            },
            "job_config": {
              "analysis_config": {
                "bucket_span": "1h",
                "detectors": [{"function":"sum","field_name":"responsetime","by_field_name":"airline"}]
              },
              "data_description": {
                "time_field":"time"
              }
            }
          }
  - length: { $body: 4 }
  - match: { 0.time: 1487376000000 }
  - match: { 0.airline: foo }
  - match: { 0.responsetime: 1.0 }
  - match: { 1.time: 1487377800000 }
  - match: { 1.airline: foo }
  - match: { 1.responsetime: 1.0 }
  - match: { 2.time: 1487379600000 }
  - match: { 2.airline: bar }
  - match: { 2.responsetime: 42.0 }
  - match: { 3.time: 1487379660000 }
  - match: { 3.airline: foo }
  - match: { 3.responsetime: 42.0 }
---
"Test preview datafeed with start and end":
  - do:
      ml.preview_datafeed:
        start: "2017-02-18T00:30:00Z"
        end: "2017-02-18T01:00:00Z"
        body: >
          {
            "datafeed_config": {
              "job_id":"preview-datafeed-job",
              "indexes":"airline-data"
            },
            "job_config": {
              "analysis_config": {
                "bucket_span": "1h",
                "detectors": [{"function":"sum","field_name":"responsetime","by_field_name":"airline"}]
              },
              "data_description": {
                "time_field":"time"
              }
            }
          }
  - length: { $body: 1 }
  - match: { 0.time: 1487377800000 }
  - match: { 0.airline: foo }
  - match: { 0.responsetime: 1.0 }
