|
|
|
|
@ -8,7 +8,7 @@ function (angular, _, ElasticQueryBuilder) { |
|
|
|
|
|
|
|
|
|
var module = angular.module('grafana.controllers'); |
|
|
|
|
|
|
|
|
|
module.controller('ElasticQueryCtrl', function($scope, $timeout, $sce, templateSrv, $q) { |
|
|
|
|
module.controller('ElasticQueryCtrl', function($scope, $timeout, uiSegmentSrv, templateSrv, $q) { |
|
|
|
|
|
|
|
|
|
$scope.functionList = ['count', 'min', 'max', 'total', 'mean']; |
|
|
|
|
|
|
|
|
|
@ -17,63 +17,40 @@ function (angular, _, ElasticQueryBuilder) { |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
$scope.init = function() { |
|
|
|
|
$scope.queryBuilder = new ElasticQueryBuilder(target); |
|
|
|
|
|
|
|
|
|
var target = $scope.target; |
|
|
|
|
target.function = target.function || 'mean'; |
|
|
|
|
target.timestampField = target.timestampField || '@timestamp'; |
|
|
|
|
target.valueField = target.valueField || 'value' ; |
|
|
|
|
|
|
|
|
|
$scope.queryBuilder = new ElasticQueryBuilder(target); |
|
|
|
|
$scope.timestampSegment = uiSegmentSrv.newSegment(target.timestampField); |
|
|
|
|
$scope.valueFieldSegment = uiSegmentSrv.newSegment(target.valueField); |
|
|
|
|
|
|
|
|
|
if (!target.keyField) { |
|
|
|
|
target.keyField = '@timestamp'; |
|
|
|
|
} |
|
|
|
|
$scope.keyFieldSegment = new MetricSegment({value: target.keyField}); |
|
|
|
|
|
|
|
|
|
if (!target.valueField) { |
|
|
|
|
target.valueField = 'metric'; |
|
|
|
|
} |
|
|
|
|
$scope.valueFieldSegment = new MetricSegment({value: target.valueField}); |
|
|
|
|
|
|
|
|
|
if (!target.termKey) { |
|
|
|
|
target.termKey = 'service.raw'; |
|
|
|
|
} |
|
|
|
|
$scope.termKeySegment = new MetricSegment({value: target.termKey}); |
|
|
|
|
|
|
|
|
|
if (!target.termValue) { |
|
|
|
|
target.termValue = 'cpu-average/cpu-user'; |
|
|
|
|
} |
|
|
|
|
$scope.termValueSegment = new MetricSegment({value: target.termValue}); |
|
|
|
|
|
|
|
|
|
if (!target.groupByField) { |
|
|
|
|
target.groupByField = 'host.raw'; |
|
|
|
|
} |
|
|
|
|
$scope.groupByFieldSegment = new MetricSegment({value: target.groupByField}); |
|
|
|
|
|
|
|
|
|
if (!target.measurement) { |
|
|
|
|
$scope.measurementSegment = MetricSegment.newSelectMeasurement(); |
|
|
|
|
} else { |
|
|
|
|
$scope.measurementSegment = new MetricSegment(target.measurement); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$scope.tagSegments = []; |
|
|
|
|
_.each(target.tags, function(tag) { |
|
|
|
|
if (tag.condition) { |
|
|
|
|
$scope.tagSegments.push(MetricSegment.newCondition(tag.condition)); |
|
|
|
|
} |
|
|
|
|
$scope.tagSegments.push(new MetricSegment({value: tag.key, type: 'key', cssClass: 'query-segment-key' })); |
|
|
|
|
$scope.tagSegments.push(new MetricSegment.newOperator("=")); |
|
|
|
|
$scope.tagSegments.push(new MetricSegment({value: tag.value, type: 'value', cssClass: 'query-segment-value'})); |
|
|
|
|
}); |
|
|
|
|
$scope.termKeySegment = uiSegmentSrv.getSegmentForValue(target.termKey, 'select term field'); |
|
|
|
|
$scope.termValueSegment = uiSegmentSrv.getSegmentForValue(target.termValue, 'select term value'); |
|
|
|
|
$scope.groupByFieldSegment = uiSegmentSrv.getSegmentForValue(target.groupByField, 'add group by'); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
$scope.fixTagSegments(); |
|
|
|
|
$scope.getFields = function() { |
|
|
|
|
return $scope.datasource.metricFindQuery('fields()') |
|
|
|
|
.then($scope.transformToSegments(true)); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
$scope.groupBySegments = []; |
|
|
|
|
_.each(target.groupByTags, function(tag) { |
|
|
|
|
$scope.groupBySegments.push(new MetricSegment(tag)); |
|
|
|
|
}); |
|
|
|
|
$scope.transformToSegments = function(addTemplateVars) { |
|
|
|
|
return function(results) { |
|
|
|
|
var segments = _.map(results, function(segment) { |
|
|
|
|
return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable }); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
$scope.groupBySegments.push(MetricSegment.newPlusButton()); |
|
|
|
|
if (addTemplateVars) { |
|
|
|
|
_.each(templateSrv.variables, function(variable) { |
|
|
|
|
segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true })); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$scope.removeTagFilterSegment = new MetricSegment({fake: true, value: '-- remove tag filter --'}); |
|
|
|
|
$scope.removeGroupBySegment = new MetricSegment({fake: true, value: '-- remove group by --'}); |
|
|
|
|
return segments; |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
$scope.valueFieldChanged = function() { |
|
|
|
|
@ -101,48 +78,11 @@ function (angular, _, ElasticQueryBuilder) { |
|
|
|
|
$scope.$parent.get_data(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
$scope.fixTagSegments = function() { |
|
|
|
|
var count = $scope.tagSegments.length; |
|
|
|
|
var lastSegment = $scope.tagSegments[Math.max(count-1, 0)]; |
|
|
|
|
|
|
|
|
|
if (!lastSegment || lastSegment.type !== 'plus-button') { |
|
|
|
|
$scope.tagSegments.push(MetricSegment.newPlusButton()); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
$scope.groupByTagUpdated = function(segment, index) { |
|
|
|
|
if (segment.value === $scope.removeGroupBySegment.value) { |
|
|
|
|
$scope.target.groupByTags.splice(index, 1); |
|
|
|
|
$scope.groupBySegments.splice(index, 1); |
|
|
|
|
$scope.$parent.get_data(); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (index === $scope.groupBySegments.length-1) { |
|
|
|
|
$scope.groupBySegments.push(MetricSegment.newPlusButton()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
segment.type = 'group-by-key'; |
|
|
|
|
segment.fake = false; |
|
|
|
|
|
|
|
|
|
$scope.target.groupByTags[index] = segment.value; |
|
|
|
|
$scope.$parent.get_data(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
$scope.changeFunction = function(func) { |
|
|
|
|
$scope.target.function = func; |
|
|
|
|
$scope.$parent.get_data(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
$scope.measurementChanged = function() { |
|
|
|
|
$scope.target.measurement = $scope.measurementSegment.value; |
|
|
|
|
$scope.$parent.get_data(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
$scope.toggleQueryMode = function () { |
|
|
|
|
$scope.target.rawQuery = !$scope.target.rawQuery; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
$scope.handleQueryError = function(err) { |
|
|
|
|
$scope.parserError = err.message || 'Failed to issue metric query'; |
|
|
|
|
return []; |
|
|
|
|
|