2015-09-05 15:05:09 +08:00
|
|
|
define([
|
|
|
|
'angular',
|
|
|
|
'lodash',
|
2015-09-28 22:28:19 +08:00
|
|
|
'./query_def'
|
2015-09-05 15:05:09 +08:00
|
|
|
],
|
2015-09-05 21:41:04 +08:00
|
|
|
function (angular, _, queryDef) {
|
2015-09-05 15:05:09 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var module = angular.module('grafana.directives');
|
|
|
|
|
2015-09-06 00:31:42 +08:00
|
|
|
module.controller('ElasticMetricAggCtrl', function($scope, uiSegmentSrv, $q, $rootScope) {
|
2015-09-05 21:41:04 +08:00
|
|
|
var metricAggs = $scope.target.metrics;
|
2015-09-05 15:05:09 +08:00
|
|
|
|
2015-09-05 21:41:04 +08:00
|
|
|
$scope.metricAggTypes = queryDef.metricAggTypes;
|
2015-09-07 19:13:19 +08:00
|
|
|
$scope.extendedStats = queryDef.extendedStats;
|
2015-12-10 17:43:00 +08:00
|
|
|
$scope.pipelineAggOptions = [];
|
2015-09-05 15:05:09 +08:00
|
|
|
|
2015-09-05 21:41:04 +08:00
|
|
|
$scope.init = function() {
|
|
|
|
$scope.agg = metricAggs[$scope.index];
|
2015-09-06 00:31:42 +08:00
|
|
|
$scope.validateModel();
|
2015-12-10 17:43:00 +08:00
|
|
|
$scope.updatePipelineAggOptions();
|
2015-12-09 20:55:06 +08:00
|
|
|
};
|
|
|
|
|
2015-12-10 17:43:00 +08:00
|
|
|
$scope.updatePipelineAggOptions = function() {
|
2015-12-11 00:12:52 +08:00
|
|
|
$scope.pipelineAggOptions = queryDef.getPipelineAggOptions($scope.target);
|
2015-09-06 20:45:12 +08:00
|
|
|
};
|
2015-09-05 15:05:09 +08:00
|
|
|
|
2015-09-06 00:31:42 +08:00
|
|
|
$rootScope.onAppEvent('elastic-query-updated', function() {
|
|
|
|
$scope.index = _.indexOf(metricAggs, $scope.agg);
|
2015-12-10 17:43:00 +08:00
|
|
|
$scope.updatePipelineAggOptions();
|
2015-09-06 00:31:42 +08:00
|
|
|
$scope.validateModel();
|
2015-09-18 18:15:06 +08:00
|
|
|
}, $scope);
|
2015-09-05 15:05:09 +08:00
|
|
|
|
2015-09-06 00:31:42 +08:00
|
|
|
$scope.validateModel = function() {
|
2015-09-06 02:22:54 +08:00
|
|
|
$scope.isFirst = $scope.index === 0;
|
|
|
|
$scope.isSingle = metricAggs.length === 1;
|
2015-09-06 01:55:58 +08:00
|
|
|
$scope.settingsLinkText = '';
|
2015-11-05 15:36:51 +08:00
|
|
|
$scope.aggDef = _.findWhere($scope.metricAggTypes, {value: $scope.agg.type});
|
2015-09-06 01:55:58 +08:00
|
|
|
|
2015-12-11 00:42:31 +08:00
|
|
|
if (queryDef.isPipelineAgg($scope.agg.type)) {
|
2015-12-10 18:17:14 +08:00
|
|
|
$scope.agg.pipelineAgg = $scope.agg.pipelineAgg || 'select metric';
|
|
|
|
$scope.agg.field = $scope.agg.pipelineAgg;
|
2015-12-11 00:03:46 +08:00
|
|
|
|
2015-12-11 19:41:40 +08:00
|
|
|
var pipelineOptions = queryDef.getPipelineOptions($scope.agg);
|
|
|
|
if (pipelineOptions.length > 0) {
|
|
|
|
_.each(pipelineOptions, function(opt) {
|
|
|
|
$scope.agg.settings[opt.text] = $scope.agg.settings[opt.text] || opt.default;
|
|
|
|
});
|
2015-12-11 16:44:37 +08:00
|
|
|
$scope.settingsLinkText = 'Options';
|
|
|
|
}
|
2015-12-10 18:46:19 +08:00
|
|
|
} else if (!$scope.agg.field) {
|
|
|
|
$scope.agg.field = 'select field';
|
2015-12-10 18:17:14 +08:00
|
|
|
}
|
|
|
|
|
2015-09-06 02:22:54 +08:00
|
|
|
switch($scope.agg.type) {
|
|
|
|
case 'percentiles': {
|
|
|
|
$scope.agg.settings.percents = $scope.agg.settings.percents || [25,50,75,95,99];
|
2015-12-12 01:20:53 +08:00
|
|
|
$scope.settingsLinkText = 'Values: ' + $scope.agg.settings.percents.join(',');
|
2015-09-06 02:22:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'extended_stats': {
|
2015-12-12 01:20:53 +08:00
|
|
|
if (_.keys($scope.agg.meta).length === 0) {
|
|
|
|
$scope.agg.meta.std_deviation_bounds_lower = true;
|
|
|
|
$scope.agg.meta.std_deviation_bounds_upper = true;
|
|
|
|
}
|
|
|
|
|
2015-09-07 19:13:19 +08:00
|
|
|
var stats = _.reduce($scope.agg.meta, function(memo, val, key) {
|
|
|
|
if (val) {
|
|
|
|
var def = _.findWhere($scope.extendedStats, {value: key});
|
|
|
|
memo.push(def.text);
|
|
|
|
}
|
|
|
|
return memo;
|
|
|
|
}, []);
|
2015-09-08 15:10:26 +08:00
|
|
|
|
2015-12-12 01:20:53 +08:00
|
|
|
$scope.settingsLinkText = 'Stats: ' + stats.join(', ');
|
2015-11-05 15:36:51 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'raw_document': {
|
|
|
|
$scope.target.metrics = [$scope.agg];
|
|
|
|
$scope.target.bucketAggs = [];
|
2015-12-12 01:20:53 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($scope.aggDef.supportsInlineScript) {
|
|
|
|
// I know this stores the inline script twice
|
|
|
|
// but having it like this simplifes the query_builder
|
|
|
|
var inlineScript = $scope.agg.inlineScript;
|
|
|
|
if (inlineScript) {
|
|
|
|
$scope.agg.settings.script = {inline: inlineScript};
|
|
|
|
} else {
|
|
|
|
delete $scope.agg.settings.script;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($scope.settingsLinkText === '') {
|
|
|
|
$scope.settingsLinkText = 'Options';
|
2015-09-06 02:22:54 +08:00
|
|
|
}
|
2015-09-06 00:31:42 +08:00
|
|
|
}
|
2015-09-06 20:45:12 +08:00
|
|
|
};
|
2015-09-06 00:31:42 +08:00
|
|
|
|
2015-09-05 21:41:04 +08:00
|
|
|
$scope.toggleOptions = function() {
|
|
|
|
$scope.showOptions = !$scope.showOptions;
|
2015-12-10 17:43:00 +08:00
|
|
|
$scope.updatePipelineAggOptions();
|
2015-12-09 16:04:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.onChangeInternal = function() {
|
|
|
|
$scope.onChange();
|
2015-09-06 00:31:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.onTypeChange = function() {
|
|
|
|
$scope.agg.settings = {};
|
2015-09-07 19:13:19 +08:00
|
|
|
$scope.agg.meta = {};
|
|
|
|
$scope.showOptions = false;
|
2015-12-11 00:01:29 +08:00
|
|
|
$scope.updatePipelineAggOptions();
|
2015-09-06 00:31:42 +08:00
|
|
|
$scope.onChange();
|
|
|
|
};
|
2015-09-05 15:05:09 +08:00
|
|
|
|
2015-09-22 15:31:58 +08:00
|
|
|
$scope.getFieldsInternal = function() {
|
|
|
|
return $scope.getFields({$fieldType: 'number'});
|
|
|
|
};
|
|
|
|
|
2015-09-05 21:41:04 +08:00
|
|
|
$scope.addMetricAgg = function() {
|
|
|
|
var addIndex = metricAggs.length;
|
2015-09-05 18:24:14 +08:00
|
|
|
|
2015-09-05 21:41:04 +08:00
|
|
|
var id = _.reduce($scope.target.bucketAggs.concat($scope.target.metrics), function(max, val) {
|
|
|
|
return parseInt(val.id) > max ? parseInt(val.id) : max;
|
|
|
|
}, 0);
|
2015-09-05 18:24:14 +08:00
|
|
|
|
2015-09-05 21:41:04 +08:00
|
|
|
metricAggs.splice(addIndex, 0, {type: "count", field: "select field", id: (id+1).toString()});
|
2015-09-06 02:22:54 +08:00
|
|
|
$scope.onChange();
|
2015-09-05 21:41:04 +08:00
|
|
|
};
|
2015-09-05 15:05:09 +08:00
|
|
|
|
2015-09-05 21:41:04 +08:00
|
|
|
$scope.removeMetricAgg = function() {
|
|
|
|
metricAggs.splice($scope.index, 1);
|
|
|
|
$scope.onChange();
|
|
|
|
};
|
2015-09-05 15:05:09 +08:00
|
|
|
|
2015-12-11 19:41:40 +08:00
|
|
|
$scope.toggleShowMetric = function() {
|
|
|
|
$scope.agg.hide = !$scope.agg.hide;
|
|
|
|
if (!$scope.agg.hide) {
|
|
|
|
delete $scope.agg.hide;
|
|
|
|
}
|
|
|
|
$scope.onChange();
|
|
|
|
};
|
|
|
|
|
2015-09-05 21:41:04 +08:00
|
|
|
$scope.init();
|
2015-09-05 15:05:09 +08:00
|
|
|
|
2015-09-05 21:41:04 +08:00
|
|
|
});
|
2015-09-05 15:05:09 +08:00
|
|
|
|
|
|
|
});
|