/*! grafana - v4.5.1 - 2017-09-19
 * Copyright (c) 2017 Torkel Ödegaard; Licensed Apache-2.0 */

define(["./query_def"],function(a){"use strict";function b(a){this.timeField=a.timeField,this.esVersion=a.esVersion}return b.prototype.getRangeFilter=function(){var a={};return a[this.timeField]={gte:"$timeFrom",lte:"$timeTo",format:"epoch_millis"},a},b.prototype.buildTermsAgg=function(a,b,c){var d,e,f;if(b.terms={field:a.field},!a.settings)return b;if(b.terms.size=0===parseInt(a.settings.size,10)?500:parseInt(a.settings.size,10),void 0!==a.settings.orderBy&&(b.terms.order={},b.terms.order[a.settings.orderBy]=a.settings.order,d=parseInt(a.settings.orderBy,10),!isNaN(d)))for(f=0;f<c.metrics.length;f++)if(e=c.metrics[f],e.id===a.settings.orderBy){b.aggs={},b.aggs[e.id]={},b.aggs[e.id][e.type]={field:e.field};break}return void 0!==a.settings.min_doc_count&&(b.terms.min_doc_count=parseInt(a.settings.min_doc_count,10)),a.settings.missing&&(b.terms.missing=a.settings.missing),b},b.prototype.getDateHistogramAgg=function(a){var b={},c=a.settings||{};return b.interval=c.interval,b.field=this.timeField,b.min_doc_count=c.min_doc_count||0,b.extended_bounds={min:"$timeFrom",max:"$timeTo"},b.format="epoch_millis","auto"===b.interval&&(b.interval="$__interval"),c.missing&&(b.missing=c.missing),b},b.prototype.getHistogramAgg=function(a){var b={},c=a.settings||{};return b.interval=c.interval,b.field=a.field,b.min_doc_count=c.min_doc_count||0,c.missing&&(b.missing=c.missing),b},b.prototype.getFiltersAgg=function(a){for(var b={},c=0;c<a.settings.filters.length;c++){var d=a.settings.filters[c].query,e=a.settings.filters[c].label;e=""===e||void 0===e?d:e,b[e]={query_string:{query:d,analyze_wildcard:!0}}}return b},b.prototype.documentQuery=function(a,b){return a.size=b,a.sort={},a.sort[this.timeField]={order:"desc",unmapped_type:"boolean"},this.esVersion<5&&(a.fields=["*","_source"]),a.script_fields={},this.esVersion<5?a.fielddata_fields=[this.timeField]:a.docvalue_fields=[this.timeField],a},b.prototype.addAdhocFilters=function(a,b){if(b){var c,d,e,f;for(c=0;c<b.length;c++)switch(d=b[c],e={},e[d.key]=d.value,f={},f[d.key]={query:d.value},d.operator){case"=":a.query.bool.must||(a.query.bool.must=[]),a.query.bool.must.push({match_phrase:f});break;case"!=":a.query.bool.must_not||(a.query.bool.must_not=[]),a.query.bool.must_not.push({match_phrase:f});break;case"<":e[d.key]={lt:d.value},a.query.bool.filter.push({range:e});break;case">":e[d.key]={gt:d.value},a.query.bool.filter.push({range:e});break;case"=~":a.query.bool.filter.push({regexp:e});break;case"!~":a.query.bool.filter.push({bool:{must_not:{regexp:e}}})}}},b.prototype.build=function(b,c,d){b.metrics=b.metrics||[{type:"count",id:"1"}],b.dsType="elasticsearch",b.bucketAggs=b.bucketAggs||[{type:"date_histogram",id:"2",settings:{interval:"auto"}}],b.timeField=this.timeField;var e,f,g,h={size:0,query:{bool:{filter:[{range:this.getRangeFilter()},{query_string:{analyze_wildcard:!0,query:d}}]}}};if(this.addAdhocFilters(h,c),0===b.bucketAggs.length){if(g=b.metrics[0],!g||"raw_document"!==g.type)throw{message:"Invalid query"};var i=g.settings&&g.settings.size||500;return this.documentQuery(h,i)}for(f=h,e=0;e<b.bucketAggs.length;e++){var j=b.bucketAggs[e],k={};switch(j.type){case"date_histogram":k.date_histogram=this.getDateHistogramAgg(j);break;case"histogram":k.histogram=this.getHistogramAgg(j);break;case"filters":k.filters={filters:this.getFiltersAgg(j)};break;case"terms":this.buildTermsAgg(j,k,b);break;case"geohash_grid":k.geohash_grid={field:j.field,precision:j.settings.precision}}f.aggs=f.aggs||{},f.aggs[j.id]=k,f=k}for(f.aggs={},e=0;e<b.metrics.length;e++)if(g=b.metrics[e],"count"!==g.type){var l={},m=null;if(a.isPipelineAgg(g.type)){if(!g.pipelineAgg||!/^\d*$/.test(g.pipelineAgg))continue;m={buckets_path:g.pipelineAgg}}else m={field:g.field};for(var n in g.settings)g.settings.hasOwnProperty(n)&&null!==g.settings[n]&&(m[n]=g.settings[n]);l[g.type]=m,f.aggs[g.id]=l}return h},b.prototype.getTermsQuery=function(a){var b={size:0,query:{bool:{filter:[{range:this.getRangeFilter()}]}}};a.query&&b.query.bool.filter.push({query_string:{analyze_wildcard:!0,query:a.query}});var c=500;return a.size&&(c=a.size),b.aggs={1:{terms:{field:a.field,size:c,order:{_term:"asc"}}}},b},b});