Commit 6c304924 by Torkel Ödegaard

feat(elastic_ds): moving time field name to dataasource option, it is no longer…

feat(elastic_ds): moving time field name to dataasource option, it is no longer specified for each query and date_histogram
parent f90714f8
define([ define([
'angular', 'angular',
'config', 'config',
'lodash',
], ],
function (angular, config) { function (angular, config, _) {
'use strict'; 'use strict';
var module = angular.module('grafana.controllers'); var module = angular.module('grafana.controllers');
......
...@@ -53,6 +53,8 @@ function (angular, _, queryDef) { ...@@ -53,6 +53,8 @@ function (angular, _, queryDef) {
if ($scope.agg.size === '0') { if ($scope.agg.size === '0') {
$scope.settingsLinkText += ' (' + $scope.agg.order + ')'; $scope.settingsLinkText += ' (' + $scope.agg.order + ')';
} }
} else if ($scope.agg.type === 'date_histogram') {
delete $scope.agg.field;
} }
return true; return true;
......
...@@ -21,7 +21,11 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern) { ...@@ -21,7 +21,11 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern) {
this.url = datasource.url; this.url = datasource.url;
this.name = datasource.name; this.name = datasource.name;
this.index = datasource.index; this.index = datasource.index;
this.timeField = datasource.jsonData.timeField;
this.indexPattern = new IndexPattern(datasource.index, datasource.jsonData.interval); this.indexPattern = new IndexPattern(datasource.index, datasource.jsonData.interval);
this.queryBuilder = new ElasticQueryBuilder({
timeField: this.timeField
});
} }
ElasticDatasource.prototype._request = function(method, url, data) { ElasticDatasource.prototype._request = function(method, url, data) {
...@@ -145,25 +149,24 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern) { ...@@ -145,25 +149,24 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern) {
}; };
ElasticDatasource.prototype.query = function(options) { ElasticDatasource.prototype.query = function(options) {
var queryBuilder = new ElasticQueryBuilder();
var payload = ""; var payload = "";
var target;
var sentTargets = []; var sentTargets = [];
var header = this.getQueryHeader(options.range); var header = this.getQueryHeader(options.range);
var timeFrom = this.translateTime(options.range.from); var timeFrom = this.translateTime(options.range.from);
var timeTo = this.translateTime(options.range.to); var timeTo = this.translateTime(options.range.to);
_.each(options.targets, function(target) { for (var i = 0; i < options.targets.length; i++) {
if (target.hide) { target = options.targets[i];
return; if (target.hide) {return;}
}
var esQuery = queryBuilder.build(target, timeFrom, timeTo); var esQuery = this.queryBuilder.build(target, timeFrom, timeTo);
payload += header + '\n'; payload += header + '\n';
payload += angular.toJson(esQuery) + '\n'; payload += angular.toJson(esQuery) + '\n';
sentTargets.push(target); sentTargets.push(target);
}); }
payload = payload.replace(/\$interval/g, options.interval); payload = payload.replace(/\$interval/g, options.interval);
payload = payload.replace(/\$timeFrom/g, this.translateTime(options.range.from)); payload = payload.replace(/\$timeFrom/g, this.translateTime(options.range.from));
......
...@@ -41,7 +41,7 @@ function (angular, _, queryDef) { ...@@ -41,7 +41,7 @@ function (angular, _, queryDef) {
} }
case 'extended_stats': { case 'extended_stats': {
$scope.agg.stats = $scope.agg.stats || ['std_deviation']; $scope.agg.stats = $scope.agg.stats || ['std_deviation'];
$scope.settingsLinkText = 'stats: ' + $scope.agg.stats.join(','); $scope.settingsLinkText = 'Stats: ' + $scope.agg.stats.join(',');
} }
} }
}; };
......
...@@ -5,8 +5,10 @@ ...@@ -5,8 +5,10 @@
<span ng-hide="isFirst">Then by</span> <span ng-hide="isFirst">Then by</span>
</li> </li>
<li> <li>
<metric-segment-model property="agg.type" options="bucketAggTypes" on-change="onChangeInternal()" custom="false"></metric-segment-model> <metric-segment-model property="agg.type" options="bucketAggTypes" on-change="onChangeInternal()" custom="false" css-class="tight-form-item-large"></metric-segment-model>
<metric-segment-model property="agg.field" get-options="getFields()" on-change="onChangeInternal()"></metric-segment> </li>
<li ng-if="agg.field">
<metric-segment-model property="agg.field" get-options="getFields()" on-change="onChangeInternal()" css-class="tight-form-item-xxlarge"></metric-segment>
</li> </li>
<li class="tight-form-item tight-form-align" ng-if="settingsLinkText"> <li class="tight-form-item tight-form-align" ng-if="settingsLinkText">
<a ng-click="toggleOptions()">{{settingsLinkText}}</a> <a ng-click="toggleOptions()">{{settingsLinkText}}</a>
......
...@@ -4,10 +4,10 @@ ...@@ -4,10 +4,10 @@
Metric Metric
</li> </li>
<li> <li>
<metric-segment-model property="agg.type" options="metricAggTypes" on-change="onTypeChange()" custom="false"></metric-segment-model> <metric-segment-model property="agg.type" options="metricAggTypes" on-change="onTypeChange()" custom="false" css-class="tight-form-item-large"></metric-segment-model>
</li> </li>
<li ng-if="agg.type !== 'count'"> <li ng-if="agg.type !== 'count'">
<metric-segment-model property="agg.field" get-options="getFields()" on-change="onChange()"></metric-segment> <metric-segment-model property="agg.field" get-options="getFields()" on-change="onChange()" css-class="tight-form-item-xxlarge"></metric-segment>
</li> </li>
<li class="tight-form-item tight-form-align" ng-if="settingsLinkText"> <li class="tight-form-item tight-form-align" ng-if="settingsLinkText">
<a ng-click="toggleOptions()">{{settingsLinkText}}</a> <a ng-click="toggleOptions()">{{settingsLinkText}}</a>
......
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
Query Query
</li> </li>
<li> <li>
<input type="text" class="input-xlarge tight-form-input" ng-model="target.query" spellcheck='false' placeholder="Lucence query" ng-blur="get_data()"> <input type="text" class="tight-form-input" style="width: 345px;" ng-model="target.query" spellcheck='false' placeholder="Lucence query" ng-blur="get_data()">
</li> </li>
</ul> </ul>
......
...@@ -4,11 +4,13 @@ define([ ...@@ -4,11 +4,13 @@ define([
function (angular) { function (angular) {
'use strict'; 'use strict';
function ElasticQueryBuilder() { } function ElasticQueryBuilder(options) {
this.timeField = options.timeField;
}
ElasticQueryBuilder.prototype.getRangeFilter = function(timeField) { ElasticQueryBuilder.prototype.getRangeFilter = function() {
var filter = {}; var filter = {};
filter[timeField] = {"gte": "$timeFrom", "lte": "$timeTo"}; filter[this.timeField] = {"gte": "$timeFrom", "lte": "$timeTo"};
return filter; return filter;
}; };
...@@ -59,7 +61,7 @@ function (angular) { ...@@ -59,7 +61,7 @@ function (angular) {
}, },
"filter": { "filter": {
"bool": { "bool": {
"must": [{"range": this.getRangeFilter(target.timeField)}] "must": [{"range": this.getRangeFilter()}]
} }
} }
} }
...@@ -76,7 +78,7 @@ function (angular) { ...@@ -76,7 +78,7 @@ function (angular) {
case 'date_histogram': { case 'date_histogram': {
esAgg["date_histogram"] = { esAgg["date_histogram"] = {
"interval": target.interval || "$interval", "interval": target.interval || "$interval",
"field": aggDef.field, "field": this.timeField,
"min_doc_count": 1, "min_doc_count": 1,
"extended_bounds": { "min": "$timeFrom", "max": "$timeTo" } "extended_bounds": { "min": "$timeFrom", "max": "$timeTo" }
}; };
......
define([ define([
'angular', 'angular',
'lodash', 'lodash',
'./queryBuilder',
], ],
function (angular, _, ElasticQueryBuilder) { function (angular, _) {
'use strict'; 'use strict';
var module = angular.module('grafana.controllers'); var module = angular.module('grafana.controllers');
...@@ -14,12 +13,8 @@ function (angular, _, ElasticQueryBuilder) { ...@@ -14,12 +13,8 @@ function (angular, _, ElasticQueryBuilder) {
var target = $scope.target; var target = $scope.target;
if (!target) { return; } if (!target) { return; }
target.timeField = target.timeField || '@timestamp';
target.metrics = target.metrics || [{ type: 'count', id: '1' }]; target.metrics = target.metrics || [{ type: 'count', id: '1' }];
target.bucketAggs = target.bucketAggs || [{ type: 'date_histogram', field: '@timestamp', id: '2'}]; target.bucketAggs = target.bucketAggs || [{ type: 'date_histogram', id: '2'}];
$scope.queryBuilder = new ElasticQueryBuilder(target);
$scope.rawQueryOld = angular.toJson($scope.queryBuilder.build($scope.target), true);
}; };
$scope.getFields = function() { $scope.getFields = function() {
...@@ -29,7 +24,7 @@ function (angular, _, ElasticQueryBuilder) { ...@@ -29,7 +24,7 @@ function (angular, _, ElasticQueryBuilder) {
}; };
$scope.queryUpdated = function() { $scope.queryUpdated = function() {
var newJson = angular.toJson($scope.queryBuilder.build($scope.target), true); var newJson = angular.toJson($scope.datasource.queryBuilder.build($scope.target), true);
if (newJson !== $scope.oldQueryRaw) { if (newJson !== $scope.oldQueryRaw) {
$scope.rawQueryOld = newJson; $scope.rawQueryOld = newJson;
$scope.get_data(); $scope.get_data();
......
...@@ -210,3 +210,6 @@ select.tight-form-input { ...@@ -210,3 +210,6 @@ select.tight-form-input {
padding-left: 66px; padding-left: 66px;
} }
.tight-form-item-large { width: 115px; }
.tight-form-item-xlarge { width: 150px; }
.tight-form-item-xxlarge { width: 200px; }
...@@ -4,10 +4,13 @@ define([ ...@@ -4,10 +4,13 @@ define([
'use strict'; 'use strict';
describe('ElasticQueryBuilder', function() { describe('ElasticQueryBuilder', function() {
var builder;
it('with defaults', function() { beforeEach(function() {
var builder = new ElasticQueryBuilder(); builder = new ElasticQueryBuilder({timeField: '@timestamp'});
});
it('with defaults', function() {
var query = builder.build({ var query = builder.build({
metrics: [{type: 'Count', id: '0'}], metrics: [{type: 'Count', id: '0'}],
timeField: '@timestamp', timeField: '@timestamp',
...@@ -19,8 +22,6 @@ define([ ...@@ -19,8 +22,6 @@ define([
}); });
it('with multiple bucket aggs', function() { it('with multiple bucket aggs', function() {
var builder = new ElasticQueryBuilder();
var query = builder.build({ var query = builder.build({
metrics: [{type: 'count', id: '1'}], metrics: [{type: 'count', id: '1'}],
timeField: '@timestamp', timeField: '@timestamp',
...@@ -36,8 +37,6 @@ define([ ...@@ -36,8 +37,6 @@ define([
it('with select field', function() { it('with select field', function() {
var builder = new ElasticQueryBuilder();
var query = builder.build({ var query = builder.build({
metrics: [{type: 'avg', field: '@value', id: '1'}], metrics: [{type: 'avg', field: '@value', id: '1'}],
bucketAggs: [{type: 'date_histogram', field: '@timestamp', id: '2'}], bucketAggs: [{type: 'date_histogram', field: '@timestamp', id: '2'}],
...@@ -48,8 +47,6 @@ define([ ...@@ -48,8 +47,6 @@ define([
}); });
it('with term agg and order by metric agg', function() { it('with term agg and order by metric agg', function() {
var builder = new ElasticQueryBuilder();
var query = builder.build({ var query = builder.build({
metrics: [{type: 'count', id: '1'}, {type: 'avg', field: '@value', id: '5'}], metrics: [{type: 'count', id: '1'}, {type: 'avg', field: '@value', id: '5'}],
bucketAggs: [ bucketAggs: [
...@@ -66,8 +63,6 @@ define([ ...@@ -66,8 +63,6 @@ define([
}); });
it('with metric percentiles', function() { it('with metric percentiles', function() {
var builder = new ElasticQueryBuilder();
var query = builder.build({ var query = builder.build({
metrics: [ metrics: [
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment