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([
'angular',
'config',
'lodash',
],
function (angular, config) {
function (angular, config, _) {
'use strict';
var module = angular.module('grafana.controllers');
......
......@@ -53,6 +53,8 @@ function (angular, _, queryDef) {
if ($scope.agg.size === '0') {
$scope.settingsLinkText += ' (' + $scope.agg.order + ')';
}
} else if ($scope.agg.type === 'date_histogram') {
delete $scope.agg.field;
}
return true;
......
......@@ -21,7 +21,11 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern) {
this.url = datasource.url;
this.name = datasource.name;
this.index = datasource.index;
this.timeField = datasource.jsonData.timeField;
this.indexPattern = new IndexPattern(datasource.index, datasource.jsonData.interval);
this.queryBuilder = new ElasticQueryBuilder({
timeField: this.timeField
});
}
ElasticDatasource.prototype._request = function(method, url, data) {
......@@ -145,25 +149,24 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern) {
};
ElasticDatasource.prototype.query = function(options) {
var queryBuilder = new ElasticQueryBuilder();
var payload = "";
var target;
var sentTargets = [];
var header = this.getQueryHeader(options.range);
var timeFrom = this.translateTime(options.range.from);
var timeTo = this.translateTime(options.range.to);
_.each(options.targets, function(target) {
if (target.hide) {
return;
}
for (var i = 0; i < options.targets.length; i++) {
target = options.targets[i];
if (target.hide) {return;}
var esQuery = queryBuilder.build(target, timeFrom, timeTo);
var esQuery = this.queryBuilder.build(target, timeFrom, timeTo);
payload += header + '\n';
payload += angular.toJson(esQuery) + '\n';
sentTargets.push(target);
});
}
payload = payload.replace(/\$interval/g, options.interval);
payload = payload.replace(/\$timeFrom/g, this.translateTime(options.range.from));
......
......@@ -41,7 +41,7 @@ function (angular, _, queryDef) {
}
case 'extended_stats': {
$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 @@
<span ng-hide="isFirst">Then by</span>
</li>
<li>
<metric-segment-model property="agg.type" options="bucketAggTypes" on-change="onChangeInternal()" custom="false"></metric-segment-model>
<metric-segment-model property="agg.field" get-options="getFields()" on-change="onChangeInternal()"></metric-segment>
<metric-segment-model property="agg.type" options="bucketAggTypes" on-change="onChangeInternal()" custom="false" css-class="tight-form-item-large"></metric-segment-model>
</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 class="tight-form-item tight-form-align" ng-if="settingsLinkText">
<a ng-click="toggleOptions()">{{settingsLinkText}}</a>
......
......@@ -4,10 +4,10 @@
Metric
</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 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 class="tight-form-item tight-form-align" ng-if="settingsLinkText">
<a ng-click="toggleOptions()">{{settingsLinkText}}</a>
......
......@@ -45,7 +45,7 @@
Query
</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>
</ul>
......
......@@ -4,11 +4,13 @@ define([
function (angular) {
'use strict';
function ElasticQueryBuilder() { }
function ElasticQueryBuilder(options) {
this.timeField = options.timeField;
}
ElasticQueryBuilder.prototype.getRangeFilter = function(timeField) {
ElasticQueryBuilder.prototype.getRangeFilter = function() {
var filter = {};
filter[timeField] = {"gte": "$timeFrom", "lte": "$timeTo"};
filter[this.timeField] = {"gte": "$timeFrom", "lte": "$timeTo"};
return filter;
};
......@@ -59,7 +61,7 @@ function (angular) {
},
"filter": {
"bool": {
"must": [{"range": this.getRangeFilter(target.timeField)}]
"must": [{"range": this.getRangeFilter()}]
}
}
}
......@@ -76,7 +78,7 @@ function (angular) {
case 'date_histogram': {
esAgg["date_histogram"] = {
"interval": target.interval || "$interval",
"field": aggDef.field,
"field": this.timeField,
"min_doc_count": 1,
"extended_bounds": { "min": "$timeFrom", "max": "$timeTo" }
};
......
define([
'angular',
'lodash',
'./queryBuilder',
],
function (angular, _, ElasticQueryBuilder) {
function (angular, _) {
'use strict';
var module = angular.module('grafana.controllers');
......@@ -14,12 +13,8 @@ function (angular, _, ElasticQueryBuilder) {
var target = $scope.target;
if (!target) { return; }
target.timeField = target.timeField || '@timestamp';
target.metrics = target.metrics || [{ type: 'count', id: '1' }];
target.bucketAggs = target.bucketAggs || [{ type: 'date_histogram', field: '@timestamp', id: '2'}];
$scope.queryBuilder = new ElasticQueryBuilder(target);
$scope.rawQueryOld = angular.toJson($scope.queryBuilder.build($scope.target), true);
target.bucketAggs = target.bucketAggs || [{ type: 'date_histogram', id: '2'}];
};
$scope.getFields = function() {
......@@ -29,7 +24,7 @@ function (angular, _, ElasticQueryBuilder) {
};
$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) {
$scope.rawQueryOld = newJson;
$scope.get_data();
......
......@@ -210,3 +210,6 @@ select.tight-form-input {
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([
'use strict';
describe('ElasticQueryBuilder', function() {
var builder;
it('with defaults', function() {
var builder = new ElasticQueryBuilder();
beforeEach(function() {
builder = new ElasticQueryBuilder({timeField: '@timestamp'});
});
it('with defaults', function() {
var query = builder.build({
metrics: [{type: 'Count', id: '0'}],
timeField: '@timestamp',
......@@ -19,8 +22,6 @@ define([
});
it('with multiple bucket aggs', function() {
var builder = new ElasticQueryBuilder();
var query = builder.build({
metrics: [{type: 'count', id: '1'}],
timeField: '@timestamp',
......@@ -36,8 +37,6 @@ define([
it('with select field', function() {
var builder = new ElasticQueryBuilder();
var query = builder.build({
metrics: [{type: 'avg', field: '@value', id: '1'}],
bucketAggs: [{type: 'date_histogram', field: '@timestamp', id: '2'}],
......@@ -48,8 +47,6 @@ define([
});
it('with term agg and order by metric agg', function() {
var builder = new ElasticQueryBuilder();
var query = builder.build({
metrics: [{type: 'count', id: '1'}, {type: 'avg', field: '@value', id: '5'}],
bucketAggs: [
......@@ -66,8 +63,6 @@ define([
});
it('with metric percentiles', function() {
var builder = new ElasticQueryBuilder();
var query = builder.build({
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