Commit 525724cc by Torkel Ödegaard

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

parent b3bda020
......@@ -65,7 +65,7 @@
Select
</li>
<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>
</ul>
......
......@@ -72,20 +72,6 @@ function () {
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;
});
......@@ -22,7 +22,7 @@ function (angular, _, ElasticQueryBuilder) {
var target = $scope.target;
target.function = target.function || 'mean';
target.timeField = target.timeField || '@timestamp';
target.select = target.select || [{ agg: 'count' }];
target.select = target.select || [{ agg: 'Count' }];
target.groupByFields = target.groupByFields || [];
$scope.timeSegment = uiSegmentSrv.newSegment(target.timeField);
......@@ -31,32 +31,95 @@ function (angular, _, ElasticQueryBuilder) {
return uiSegmentSrv.newSegment(group.field);
});
$scope.selectSegments = _.map(target.select, function(select) {
return uiSegmentSrv.newSegment(select.agg);
$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' }));
}
});
$scope.groupBySegments.push(uiSegmentSrv.newPlusButton());
$scope.selectSegments.push(uiSegmentSrv.newPlusButton());
$scope.removeSelectSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove select --'});
$scope.removeGroupBySegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove group by --'});
};
$scope.getFields = function() {
return $scope.datasource.metricFindQuery('fields()').then($scope.transformToSegments(true));
$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);
};
$scope.transformToSegments = function(addTemplateVars) {
return function(results) {
var segments = _.map(results, function(segment) {
return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
});
$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;
}
if (addTemplateVars) {
_.each(templateSrv.variables, function(variable) {
segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
});
if (segment.type === 'plus-button' && index > 0) {
$scope.selectSegments.splice(index, 0, uiSegmentSrv.newCondition(' And '));
segment.type = 'agg';
index += 1;
}
return segments;
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'}));
}
}
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) {
$scope.$parent.get_data();
};
$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.transformToSegments = function(addTemplateVars) {
return function(results) {
var segments = _.map(results, function(segment) {
return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
});
$scope.termKeySegmentChanged = function() {
$scope.target.termKey = $scope.termKeySegment.value;
$scope.$parent.get_data();
};
if (addTemplateVars) {
_.each(templateSrv.variables, function(variable) {
segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
});
}
$scope.groupByFieldChanged = function() {
$scope.target.groupBy = $scope.groupByFieldSegment.value;
$scope.$parent.get_data();
return segments;
};
$scope.changeFunction = function(func) {
$scope.target.function = func;
$scope.$parent.get_data();
};
$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