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';
|
|
|
|
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
|
|
|
|
|
|
|
$scope.selectSegments = _.map(target.select, function(select) {
|
|
|
|
return uiSegmentSrv.newSegment(select.agg);
|
|
|
|
});
|
2015-09-03 04:14:25 -05:00
|
|
|
|
|
|
|
$scope.groupBySegments.push(uiSegmentSrv.newPlusButton());
|
|
|
|
$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-02 10:45:41 -05:00
|
|
|
$scope.getFields = function() {
|
2015-09-03 04:14:25 -05:00
|
|
|
return $scope.datasource.metricFindQuery('fields()').then($scope.transformToSegments(true));
|
2015-09-02 10:45:41 -05:00
|
|
|
};
|
2015-07-01 13:45:55 -05:00
|
|
|
|
2015-09-02 10:45:41 -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-01 13:45:55 -05:00
|
|
|
|
2015-09-02 10:45:41 -05:00
|
|
|
if (addTemplateVars) {
|
|
|
|
_.each(templateSrv.variables, function(variable) {
|
|
|
|
segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
|
|
|
|
});
|
|
|
|
}
|
2015-07-01 13:45:55 -05:00
|
|
|
|
2015-09-02 10:45:41 -05:00
|
|
|
return segments;
|
|
|
|
};
|
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-07-02 09:54:39 -05:00
|
|
|
$scope.valueFieldChanged = function() {
|
|
|
|
$scope.target.valueField = $scope.valueFieldSegment.value;
|
|
|
|
$scope.$parent.get_data();
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.keyFieldChanged = function() {
|
|
|
|
$scope.target.keyField = $scope.keyFieldSegment.value;
|
|
|
|
$scope.$parent.get_data();
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.termValueSegmentChanged = function() {
|
|
|
|
$scope.target.termValue = $scope.termValueSegment.value;
|
|
|
|
$scope.$parent.get_data();
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.termKeySegmentChanged = function() {
|
|
|
|
$scope.target.termKey = $scope.termKeySegment.value;
|
|
|
|
$scope.$parent.get_data();
|
|
|
|
};
|
|
|
|
|
2015-08-31 07:25:05 -05:00
|
|
|
$scope.groupByFieldChanged = function() {
|
|
|
|
$scope.target.groupBy = $scope.groupByFieldSegment.value;
|
|
|
|
$scope.$parent.get_data();
|
|
|
|
};
|
|
|
|
|
2015-07-01 13:45:55 -05:00
|
|
|
$scope.changeFunction = function(func) {
|
|
|
|
$scope.target.function = func;
|
|
|
|
$scope.$parent.get_data();
|
|
|
|
};
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|