Commit 525724cc by Torkel Ödegaard

feat(elasticsearch): lots of work on elasticsearch metrics query editor, #1034

parent b3bda020
...@@ -65,7 +65,7 @@ ...@@ -65,7 +65,7 @@
Select Select
</li> </li>
<li ng-repeat="segment in selectSegments"> <li ng-repeat="segment in selectSegments">
<metric-segment segment="segment" get-alt-segments="getSelectSegments(segment, $index)" on-value-changed="selectSegmentChanged()"></metric-segment> <metric-segment segment="segment" get-alt-segments="getSelectSegments(segment, $index)" on-value-changed="selectChanged(segment, $index)"></metric-segment>
</li> </li>
</ul> </ul>
......
...@@ -72,20 +72,6 @@ function () { ...@@ -72,20 +72,6 @@ function () {
return query; return query;
}; };
ElasticQueryBuilder.prototype._buildRangeFilter = function(target) {
var filter = {"range":{}};
filter["range"][target.timestampField] = {
"gte": "$rangeFrom",
"lte": "$rangeTo"
};
return filter;
};
ElasticQueryBuilder.prototype._buildTermFilter = function(target) {
var filter = {"term":{}};
filter["term"][target.termKey] = target.termValue;
return filter;
};
return ElasticQueryBuilder; return ElasticQueryBuilder;
}); });
...@@ -22,7 +22,7 @@ function (angular, _, ElasticQueryBuilder) { ...@@ -22,7 +22,7 @@ function (angular, _, ElasticQueryBuilder) {
var target = $scope.target; var target = $scope.target;
target.function = target.function || 'mean'; target.function = target.function || 'mean';
target.timeField = target.timeField || '@timestamp'; target.timeField = target.timeField || '@timestamp';
target.select = target.select || [{ agg: 'count' }]; target.select = target.select || [{ agg: 'Count' }];
target.groupByFields = target.groupByFields || []; target.groupByFields = target.groupByFields || [];
$scope.timeSegment = uiSegmentSrv.newSegment(target.timeField); $scope.timeSegment = uiSegmentSrv.newSegment(target.timeField);
...@@ -31,32 +31,95 @@ function (angular, _, ElasticQueryBuilder) { ...@@ -31,32 +31,95 @@ function (angular, _, ElasticQueryBuilder) {
return uiSegmentSrv.newSegment(group.field); return uiSegmentSrv.newSegment(group.field);
}); });
$scope.selectSegments = _.map(target.select, function(select) { $scope.selectSegments = [];
return uiSegmentSrv.newSegment(select.agg); _.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' }));
}
}); });
$scope.groupBySegments.push(uiSegmentSrv.newPlusButton()); $scope.groupBySegments.push(uiSegmentSrv.newPlusButton());
$scope.selectSegments.push(uiSegmentSrv.newPlusButton());
$scope.removeSelectSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove select --'}); $scope.removeSelectSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove select --'});
$scope.removeGroupBySegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove group by --'}); $scope.removeGroupBySegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove group by --'});
}; };
$scope.getFields = function() { $scope.getSelectSegments = function(segment, index) {
return $scope.datasource.metricFindQuery('fields()').then($scope.transformToSegments(true)); 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);
}; };
$scope.transformToSegments = function(addTemplateVars) { $scope.selectChanged = function(segment, index) {
return function(results) { if (segment.value === $scope.removeSelectSegment.value) {
var segments = _.map(results, function(segment) { var nextSegment = $scope.selectSegments[index + 1];
return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable }); var remove = 2;
}); if (nextSegment && nextSegment.type === 'field') {
remove += 1;
}
$scope.selectSegments.splice(index-1, remove);
$scope.rebuildTargetSelects();
return;
}
if (addTemplateVars) { if (segment.type === 'plus-button' && index > 0) {
_.each(templateSrv.variables, function(variable) { $scope.selectSegments.splice(index, 0, uiSegmentSrv.newCondition(' And '));
segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true })); 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'}));
} }
}
return segments; 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);
}; };
}; };
...@@ -90,34 +153,20 @@ function (angular, _, ElasticQueryBuilder) { ...@@ -90,34 +153,20 @@ function (angular, _, ElasticQueryBuilder) {
$scope.$parent.get_data(); $scope.$parent.get_data();
}; };
$scope.valueFieldChanged = function() { $scope.transformToSegments = function(addTemplateVars) {
$scope.target.valueField = $scope.valueFieldSegment.value; return function(results) {
$scope.$parent.get_data(); var segments = _.map(results, function(segment) {
}; return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
});
$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();
};
$scope.groupByFieldChanged = function() { if (addTemplateVars) {
$scope.target.groupBy = $scope.groupByFieldSegment.value; _.each(templateSrv.variables, function(variable) {
$scope.$parent.get_data(); segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
}; });
}
$scope.changeFunction = function(func) { return segments;
$scope.target.function = func; };
$scope.$parent.get_data();
}; };
$scope.handleQueryError = function(err) { $scope.handleQueryError = function(err) {
......
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