/*! grafana - v4.2.0 - 2017-03-24
 * 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.getFiltersAgg=function(a){for(var b={},c=0;c<a.settings.filters.length;c++){var d=a.settings.filters[c].query;b[d]={query_string:{query:d,analyze_wildcard:!0}}}return b},b.prototype.documentQuery=function(a){return a.size=500,a.sort={},a.sort[this.timeField]={order:"desc",unmapped_type:"boolean"},this.esVersion<5&&(a.fields=["*","_source"]),a.script_fields={},a.fielddata_fields=[this.timeField],a},b.prototype.addAdhocFilters=function(a,b){if(b){var c,d,e;for(c=0;c<b.length;c++)switch(d=b[c],e={},e[d.key]=d.value,d.operator){case"=":a.query.bool.filter.push({term:e});break;case"!=":a.query.bool.filter.push({bool:{must_not:{term:e}}});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"};return this.documentQuery(h,b)}for(f=h,e=0;e<b.bucketAggs.length;e++){var i=b.bucketAggs[e],j={};switch(i.type){case"date_histogram":j.date_histogram=this.getDateHistogramAgg(i);break;case"filters":j.filters={filters:this.getFiltersAgg(i)};break;case"terms":this.buildTermsAgg(i,j,b);break;case"geohash_grid":j.geohash_grid={field:i.field,precision:i.settings.precision}}f.aggs=f.aggs||{},f.aggs[i.id]=j,f=j}for(f.aggs={},e=0;e<b.metrics.length;e++)if(g=b.metrics[e],"count"!==g.type){var k={},l=null;if(a.isPipelineAgg(g.type)){if(!g.pipelineAgg||!/^\d*$/.test(g.pipelineAgg))continue;l={buckets_path:g.pipelineAgg}}else l={field:g.field};for(var m in g.settings)g.settings.hasOwnProperty(m)&&null!==g.settings[m]&&(l[m]=g.settings[m]);k[g.type]=l,f.aggs[g.id]=k}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});