Refactor queryCtrl - filter metrics using queryProcessor and

support templating.
This commit is contained in:
Alexander Zobnin
2016-01-31 21:21:14 +03:00
parent 594bbbddb9
commit f696760c32
2 changed files with 20 additions and 124 deletions

View File

@@ -70,9 +70,9 @@ define([
}; };
$scope.initFilters = function () { $scope.initFilters = function () {
$scope.metric.filteredHosts = $scope.filterHosts(); $scope.filterHosts();
$scope.metric.filteredApplications = $scope.filterApplications(); $scope.filterApplications();
$scope.metric.filteredItems = $scope.filterItems(); $scope.filterItems();
}; };
$scope.getMetricsFromCache = function() { $scope.getMetricsFromCache = function() {
@@ -105,130 +105,26 @@ define([
$scope.getItemNames = _.partial(getMetricNames, $scope, 'filteredItems'); $scope.getItemNames = _.partial(getMetricNames, $scope, 'filteredItems');
$scope.filterHosts = function () { $scope.filterHosts = function () {
var group = $scope.target.group; var groupFilter = templateSrv.replace($scope.target.group.filter);
var groups = []; $scope.datasource.queryProcessor.filterHosts(groupFilter).then(function(hosts) {
var hosts = []; $scope.metric.filteredHosts = hosts;
// Filter groups by regex
if (group.isRegex) {
var filterPattern = Utils.buildRegex(group.filter);
groups = _.filter($scope.metric.groupList, function (groupObj) {
return filterPattern.test(groupObj.name);
}); });
}
// Find hosts in selected group
else {
var finded = _.find($scope.metric.groupList, {'name': group.filter});
if (finded) {
groups.push(finded);
} else {
groups = undefined;
}
}
if (groups) {
var groupids = _.map(groups, 'groupid');
hosts = _.filter($scope.metric.hostList, function (hostObj) {
return _.intersection(groupids, hostObj.groups).length;
});
}
return hosts;
}; };
$scope.filterApplications = function () { $scope.filterApplications = function () {
var host = $scope.target.host; var hostFilter = templateSrv.replace($scope.target.host.filter);
var hosts = []; $scope.datasource.queryProcessor.filterApplications(hostFilter).then(function(apps) {
var apps = []; $scope.metric.filteredApplications = apps;
// Filter hosts by regex
if (host.isRegex) {
var filterPattern = Utils.buildRegex(host.filter);
hosts = _.filter($scope.metric.hostList, function (hostObj) {
return filterPattern.test(hostObj.name);
}); });
}
// Find applications in selected host
else {
var finded = _.find($scope.metric.hostList, {'name': host.filter});
if (finded) {
hosts.push(finded);
} else {
hosts = undefined;
}
}
if (hosts) {
var hostsids = _.map(hosts, 'hostid');
apps = _.filter($scope.metric.applicationList, function (appObj) {
return _.intersection(hostsids, appObj.hosts).length;
});
}
return apps;
}; };
$scope.filterItems = function () { $scope.filterItems = function () {
var app = $scope.target.application; var hostFilter = templateSrv.replace($scope.target.host.filter);
var host = $scope.target.host; var appFilter = templateSrv.replace($scope.target.application.filter);
var hosts = []; $scope.datasource.queryProcessor.filterItems(hostFilter, appFilter, $scope.target.showDisabledItems)
var apps = []; .then(function(items) {
var items = []; $scope.metric.filteredItems = items;
// Filter hosts by regex
if (host.isRegex) {
var hostFilterPattern = Utils.buildRegex(host.filter);
hosts = _.filter($scope.metric.hostList, function (hostObj) {
return hostFilterPattern.test(hostObj.name);
}); });
}
else {
var findedHosts = _.find($scope.metric.hostList, {'name': host.filter});
if (findedHosts) {
hosts.push(findedHosts);
} else {
hosts = undefined;
}
}
// Filter applications by regex
if (app.isRegex) {
var filterPattern = Utils.buildRegex(app.filter);
apps = _.filter($scope.metric.applicationList, function (appObj) {
return filterPattern.test(appObj.name);
});
}
// Find items in selected application
else if (app.filter) {
var finded = _.find($scope.metric.applicationList, {'name': app.filter});
if (finded) {
apps.push(finded);
} else {
apps = undefined;
}
} else {
apps = undefined;
if (hosts) {
items = _.filter($scope.metric.itemList, function (itemObj) {
return _.find(hosts, {'hostid': itemObj.hostid });
});
}
}
if (apps) {
var appids = _.flatten(_.map(apps, 'applicationids'));
items = _.filter($scope.metric.itemList, function (itemObj) {
return _.intersection(appids, itemObj.applications).length;
});
items = _.filter(items, function (itemObj) {
return _.find(hosts, {'hostid': itemObj.hostid });
});
}
if (!$scope.target.showDisabledItems) {
items = _.filter(items, {'status': '0'});
}
return items;
}; };
$scope.onTargetPartChange = function (targetPart) { $scope.onTargetPartChange = function (targetPart) {
@@ -239,21 +135,21 @@ define([
// Handle group blur and filter hosts // Handle group blur and filter hosts
$scope.onGroupBlur = function() { $scope.onGroupBlur = function() {
$scope.metric.filteredHosts = $scope.filterHosts(); $scope.filterHosts();
$scope.parseTarget(); $scope.parseTarget();
$scope.get_data(); $scope.get_data();
}; };
// Handle host blur and filter applications // Handle host blur and filter applications
$scope.onHostBlur = function() { $scope.onHostBlur = function() {
$scope.metric.filteredApplications = $scope.filterApplications(); $scope.filterApplications();
$scope.parseTarget(); $scope.parseTarget();
$scope.get_data(); $scope.get_data();
}; };
// Handle application blur and filter items // Handle application blur and filter items
$scope.onApplicationBlur = function() { $scope.onApplicationBlur = function() {
$scope.metric.filteredItems = $scope.filterItems(); $scope.filterItems();
$scope.parseTarget(); $scope.parseTarget();
$scope.get_data(); $scope.get_data();
}; };

View File

@@ -153,7 +153,7 @@ function (angular, _, utils) {
} else { } else {
apps = undefined; apps = undefined;
if (hosts) { if (hosts) {
items = _.filter(this.cache.getItems(), function (itemObj) { items = _.filter(cachedItems, function (itemObj) {
return _.find(hosts, {'hostid': itemObj.hostid }); return _.find(hosts, {'hostid': itemObj.hostid });
}); });
} }