2015-07-01 13:45:55 -05:00
|
|
|
define([
|
|
|
|
'angular',
|
|
|
|
'lodash',
|
|
|
|
'./queryBuilder',
|
|
|
|
],
|
|
|
|
function (angular, _, ElasticQueryBuilder) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var module = angular.module('grafana.controllers');
|
|
|
|
|
2015-09-02 10:45:41 -05:00
|
|
|
module.controller('ElasticQueryCtrl', function($scope, $timeout, uiSegmentSrv, templateSrv, $q) {
|
2015-07-01 13:45:55 -05:00
|
|
|
|
|
|
|
$scope.functionList = ['count', 'min', 'max', 'total', 'mean'];
|
|
|
|
|
|
|
|
$scope.functionMenu = _.map($scope.functionList, function(func) {
|
|
|
|
return { text: func, click: "changeFunction('" + func + "');" };
|
|
|
|
});
|
|
|
|
|
|
|
|
$scope.init = function() {
|
2015-09-02 10:45:41 -05:00
|
|
|
$scope.queryBuilder = new ElasticQueryBuilder(target);
|
|
|
|
|
2015-07-01 13:45:55 -05:00
|
|
|
var target = $scope.target;
|
|
|
|
target.function = target.function || 'mean';
|
2015-09-03 01:18:00 -05:00
|
|
|
target.timeField = target.timeField || '@timestamp';
|
2015-09-03 07:02:31 -05:00
|
|
|
target.select = target.select || [{ agg: 'Count' }];
|
2015-09-03 04:14:25 -05:00
|
|
|
target.groupByFields = target.groupByFields || [];
|
2015-07-01 13:45:55 -05:00
|
|
|
|
2015-09-03 01:18:00 -05:00
|
|
|
$scope.timeSegment = uiSegmentSrv.newSegment(target.timeField);
|
2015-09-03 04:14:25 -05:00
|
|
|
|
|
|
|
$scope.groupBySegments = _.map(target.groupByFields, function(group) {
|
|
|
|
return uiSegmentSrv.newSegment(group.field);
|
|
|
|
});
|
2015-09-03 01:18:00 -05:00
|
|
|
|
2015-09-03 07:02:31 -05:00
|
|
|
$scope.selectSegments = [];
|
|
|
|
_.each(target.select, function(select) {
|
|
|
|
if ($scope.selectSegments.length > 0) {
|
|
|
|
$scope.selectSegments.push(uiSegmentSrv.newCondition(" and "));
|
|
|
|
}
|
|
|
|
if (select.agg === 'Count') {
|
|
|
|
$scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.agg, type: 'agg'}));
|
|
|
|
} else {
|
|
|
|
$scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.agg, type: 'agg'}));
|
|
|
|
$scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.field, type: 'field' }));
|
|
|
|
}
|
2015-09-03 01:18:00 -05:00
|
|
|
});
|
2015-09-03 04:14:25 -05:00
|
|
|
|
|
|
|
$scope.groupBySegments.push(uiSegmentSrv.newPlusButton());
|
2015-09-03 07:02:31 -05:00
|
|
|
$scope.selectSegments.push(uiSegmentSrv.newPlusButton());
|
2015-09-03 04:14:25 -05:00
|
|
|
$scope.removeSelectSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove select --'});
|
|
|
|
$scope.removeGroupBySegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove group by --'});
|
2015-09-02 10:45:41 -05:00
|
|
|
};
|
2015-07-01 13:45:55 -05:00
|
|
|
|
2015-09-03 07:02:31 -05:00
|
|
|
$scope.getSelectSegments = function(segment, index) {
|
|
|
|
if (segment.type === 'agg' || segment.type === 'plus-button') {
|
|
|
|
var options = [
|
|
|
|
uiSegmentSrv.newSegment({value: 'Count', type: 'agg'}),
|
|
|
|
uiSegmentSrv.newSegment({value: 'Min', type: 'agg'}),
|
|
|
|
uiSegmentSrv.newSegment({value: 'Max', type: 'agg'}),
|
|
|
|
uiSegmentSrv.newSegment({value: 'Avg', type: 'agg'}),
|
|
|
|
];
|
|
|
|
if (index > 0) {
|
|
|
|
options.splice(0, 0, angular.copy($scope.removeSelectSegment));
|
|
|
|
}
|
|
|
|
return $q.when(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $scope.datasource.metricFindQuery('fields()')
|
|
|
|
.then($scope.transformToSegments(false))
|
|
|
|
.then(null, $scope.handleQueryError);
|
2015-09-02 10:45:41 -05:00
|
|
|
};
|
2015-07-01 13:45:55 -05:00
|
|
|
|
2015-09-03 07:02:31 -05:00
|
|
|
$scope.selectChanged = function(segment, index) {
|
|
|
|
if (segment.value === $scope.removeSelectSegment.value) {
|
|
|
|
var nextSegment = $scope.selectSegments[index + 1];
|
|
|
|
var remove = 2;
|
|
|
|
if (nextSegment && nextSegment.type === 'field') {
|
|
|
|
remove += 1;
|
|
|
|
}
|
|
|
|
$scope.selectSegments.splice(index-1, remove);
|
|
|
|
$scope.rebuildTargetSelects();
|
|
|
|
return;
|
|
|
|
}
|
2015-07-01 13:45:55 -05:00
|
|
|
|
2015-09-03 07:02:31 -05:00
|
|
|
if (segment.type === 'plus-button' && index > 0) {
|
|
|
|
$scope.selectSegments.splice(index, 0, uiSegmentSrv.newCondition(' And '));
|
|
|
|
segment.type = 'agg';
|
|
|
|
index += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (segment.type === 'agg') {
|
|
|
|
var nextSegment = $scope.selectSegments[index + 1];
|
|
|
|
|
|
|
|
if (segment.value === 'Count') {
|
|
|
|
if (nextSegment && nextSegment.type === 'field') {
|
|
|
|
$scope.selectSegments.splice(index + 1, 1);
|
|
|
|
}
|
|
|
|
} else if (!nextSegment || nextSegment.type !== 'field') {
|
|
|
|
$scope.selectSegments.splice(index + 1, 0, uiSegmentSrv.newSegment({value: 'select field', fake: true, type: 'field'}));
|
2015-09-02 10:45:41 -05:00
|
|
|
}
|
2015-09-03 07:02:31 -05:00
|
|
|
}
|
2015-07-01 13:45:55 -05:00
|
|
|
|
2015-09-03 07:02:31 -05:00
|
|
|
if ((index+1) === $scope.selectSegments.length) {
|
|
|
|
$scope.selectSegments.push(uiSegmentSrv.newPlusButton());
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.rebuildTargetSelects();
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.rebuildTargetSelects = function() {
|
|
|
|
$scope.target.select = [];
|
|
|
|
for (var i = 0; i < $scope.selectSegments.length; i++) {
|
|
|
|
var segment = $scope.selectSegments[i];
|
|
|
|
var select = {agg: segment.value };
|
|
|
|
|
|
|
|
if (segment.type === 'agg' && segment.value !== 'Count') {
|
|
|
|
select.field = $scope.selectSegments[i+1].value;
|
|
|
|
i += 2;
|
|
|
|
} else {
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.target.select.push(select);
|
2015-09-02 10:45:41 -05:00
|
|
|
};
|
2015-07-01 13:45:55 -05:00
|
|
|
};
|
|
|
|
|
2015-09-03 04:14:25 -05:00
|
|
|
$scope.getGroupByFields = function(segment) {
|
|
|
|
return $scope.datasource.metricFindQuery('fields()').then($scope.transformToSegments(false))
|
|
|
|
.then(function(results) {
|
|
|
|
if (segment.type !== 'plus-button') {
|
|
|
|
results.splice(0, 0, angular.copy($scope.removeGroupBySegment));
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
})
|
|
|
|
.then(null, $scope.handleQueryError);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.groupByChanged = function(segment, index) {
|
|
|
|
if (segment.value === $scope.removeGroupBySegment.value) {
|
|
|
|
$scope.target.groupByFields.splice(index, 1);
|
|
|
|
$scope.groupBySegments.splice(index, 1);
|
|
|
|
$scope.$parent.get_data();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index === $scope.groupBySegments.length-1) {
|
|
|
|
$scope.groupBySegments.push(uiSegmentSrv.newPlusButton());
|
|
|
|
}
|
|
|
|
|
|
|
|
segment.type = 'group-by-key';
|
|
|
|
segment.fake = false;
|
|
|
|
|
|
|
|
$scope.target.groupByFields[index] = {field: segment.value};
|
|
|
|
$scope.$parent.get_data();
|
|
|
|
};
|
|
|
|
|
2015-09-03 07:02:31 -05:00
|
|
|
$scope.transformToSegments = function(addTemplateVars) {
|
|
|
|
return function(results) {
|
|
|
|
var segments = _.map(results, function(segment) {
|
|
|
|
return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
|
|
|
|
});
|
2015-07-02 09:54:39 -05:00
|
|
|
|
2015-09-03 07:02:31 -05:00
|
|
|
if (addTemplateVars) {
|
|
|
|
_.each(templateSrv.variables, function(variable) {
|
|
|
|
segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
|
|
|
|
});
|
|
|
|
}
|
2015-08-31 07:25:05 -05:00
|
|
|
|
2015-09-03 07:02:31 -05:00
|
|
|
return segments;
|
|
|
|
};
|
2015-07-01 13:45:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.handleQueryError = function(err) {
|
|
|
|
$scope.parserError = err.message || 'Failed to issue metric query';
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
2015-09-03 01:18:00 -05:00
|
|
|
$scope.toggleQueryMode = function () {
|
|
|
|
$scope.target.rawQuery = !$scope.target.rawQuery;
|
|
|
|
};
|
|
|
|
|
2015-09-02 08:25:40 -05:00
|
|
|
$scope.init();
|
2015-07-01 13:45:55 -05:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|