Updated datasource.metricFindQuery() for working with cache.
This commit is contained in:
@@ -277,6 +277,8 @@ function (angular, _, dateMath, utils, metricFunctions) {
|
|||||||
* of metrics in "{metric1,metcic2,...,metricN}" format.
|
* of metrics in "{metric1,metcic2,...,metricN}" format.
|
||||||
*/
|
*/
|
||||||
this.metricFindQuery = function (query) {
|
this.metricFindQuery = function (query) {
|
||||||
|
var metrics;
|
||||||
|
|
||||||
// Split query. Query structure:
|
// Split query. Query structure:
|
||||||
// group.host.app.item
|
// group.host.app.item
|
||||||
var parts = [];
|
var parts = [];
|
||||||
@@ -294,49 +296,22 @@ function (angular, _, dateMath, utils, metricFunctions) {
|
|||||||
|
|
||||||
// Get items
|
// Get items
|
||||||
if (parts.length === 4) {
|
if (parts.length === 4) {
|
||||||
return this.zabbixAPI.itemFindQuery(template.group, template.host, template.app)
|
var items = this.queryProcessor.filterItems(template.host, template.app, true);
|
||||||
.then(function (result) {
|
metrics = _.map(items, formatMetric);
|
||||||
return _.map(result, function (item) {
|
|
||||||
var itemname = zabbixHelperSrv.expandItemName(item);
|
|
||||||
return {
|
|
||||||
text: itemname,
|
|
||||||
expandable: false
|
|
||||||
};
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
// Get applications
|
// Get applications
|
||||||
else if (parts.length === 3) {
|
else if (parts.length === 3) {
|
||||||
return this.zabbixAPI.appFindQuery(template.host, template.group).then(function (result) {
|
var apps = this.queryProcessor.filterApplications(template.host);
|
||||||
return _.map(result, function (app) {
|
metrics = _.map(apps, formatMetric);
|
||||||
return {
|
|
||||||
text: app.name,
|
|
||||||
expandable: false
|
|
||||||
};
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
// Get hosts
|
// Get hosts
|
||||||
else if (parts.length === 2) {
|
else if (parts.length === 2) {
|
||||||
return this.zabbixAPI.hostFindQuery(template.group).then(function (result) {
|
var hosts = this.queryProcessor.filterHosts(template.group);
|
||||||
return _.map(result, function (host) {
|
metrics = _.map(hosts, formatMetric);
|
||||||
return {
|
|
||||||
text: host.name,
|
|
||||||
expandable: false
|
|
||||||
};
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
// Get groups
|
// Get groups
|
||||||
else if (parts.length === 1) {
|
else if (parts.length === 1) {
|
||||||
return this.zabbixAPI.getGroupByName(template.group).then(function (result) {
|
metrics = _.map(this.zabbixCache.getGroups(template.group), formatMetric);
|
||||||
return _.map(result, function (hostgroup) {
|
|
||||||
return {
|
|
||||||
text: hostgroup.name,
|
|
||||||
expandable: false
|
|
||||||
};
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
// Return empty object for invalid request
|
// Return empty object for invalid request
|
||||||
else {
|
else {
|
||||||
@@ -344,8 +319,17 @@ function (angular, _, dateMath, utils, metricFunctions) {
|
|||||||
d.resolve([]);
|
d.resolve([]);
|
||||||
return d.promise;
|
return d.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $q.when(metrics);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function formatMetric(metricObj) {
|
||||||
|
return {
|
||||||
|
text: metricObj.name,
|
||||||
|
expandable: false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/////////////////
|
/////////////////
|
||||||
// Annotations //
|
// Annotations //
|
||||||
/////////////////
|
/////////////////
|
||||||
|
|||||||
@@ -28,6 +28,133 @@ function (angular, _, utils) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.filterHosts = function(groupFilter) {
|
||||||
|
var groups = [];
|
||||||
|
var hosts = [];
|
||||||
|
var groupList = self.cache.getGroups();
|
||||||
|
|
||||||
|
// Filter groups by regex
|
||||||
|
if (utils.isRegex(groupFilter)) {
|
||||||
|
var filterPattern = utils.buildRegex(groupFilter);
|
||||||
|
groups = _.filter(groupList, function (groupObj) {
|
||||||
|
return filterPattern.test(groupObj.name);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Find hosts in selected group
|
||||||
|
else {
|
||||||
|
var finded = _.find(groupList, {'name': groupFilter});
|
||||||
|
if (finded) {
|
||||||
|
groups.push(finded);
|
||||||
|
} else {
|
||||||
|
groups = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (groups) {
|
||||||
|
var groupids = _.map(groups, 'groupid');
|
||||||
|
hosts = _.filter(self.cache.getHosts(), function (hostObj) {
|
||||||
|
return _.intersection(groupids, hostObj.groups).length;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return hosts;
|
||||||
|
};
|
||||||
|
|
||||||
|
this.filterApplications = function(hostFilter) {
|
||||||
|
var hosts = [];
|
||||||
|
var apps = [];
|
||||||
|
var hostList = this.cache.getHosts();
|
||||||
|
|
||||||
|
// Filter hosts by regex
|
||||||
|
if (utils.isRegex(hostFilter)) {
|
||||||
|
var filterPattern = utils.buildRegex(hostFilter);
|
||||||
|
hosts = _.filter(hostList, function (hostObj) {
|
||||||
|
return filterPattern.test(hostObj.name);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Find applications in selected host
|
||||||
|
else {
|
||||||
|
var finded = _.find(hostList, {'name': hostFilter});
|
||||||
|
if (finded) {
|
||||||
|
hosts.push(finded);
|
||||||
|
} else {
|
||||||
|
hosts = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hosts) {
|
||||||
|
var hostsids = _.map(hosts, 'hostid');
|
||||||
|
apps = _.filter(this.cache.getApplications(), function (appObj) {
|
||||||
|
return _.intersection(hostsids, appObj.hosts).length;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return apps;
|
||||||
|
};
|
||||||
|
|
||||||
|
this.filterItems = function (hostFilter, appFilter, showDisabledItems) {
|
||||||
|
var hosts = [];
|
||||||
|
var apps = [];
|
||||||
|
var items = [];
|
||||||
|
var hostList = this.cache.getHosts();
|
||||||
|
var applicationList = this.cache.getApplications();
|
||||||
|
|
||||||
|
// Filter hosts by regex
|
||||||
|
if (utils.isRegex(hostFilter)) {
|
||||||
|
var hostFilterPattern = utils.buildRegex(hostFilter);
|
||||||
|
hosts = _.filter(hostList, function (hostObj) {
|
||||||
|
return hostFilterPattern.test(hostObj.name);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var findedHosts = _.find(hostList, {'name': hostFilter});
|
||||||
|
if (findedHosts) {
|
||||||
|
hosts.push(findedHosts);
|
||||||
|
} else {
|
||||||
|
hosts = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter applications by regex
|
||||||
|
if (utils.isRegex(appFilter)) {
|
||||||
|
var filterPattern = utils.buildRegex(appFilter);
|
||||||
|
apps = _.filter(applicationList, function (appObj) {
|
||||||
|
return filterPattern.test(appObj.name);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Find items in selected application
|
||||||
|
else if (appFilter) {
|
||||||
|
var finded = _.find(applicationList, {'name': appFilter});
|
||||||
|
if (finded) {
|
||||||
|
apps.push(finded);
|
||||||
|
} else {
|
||||||
|
apps = undefined;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
apps = undefined;
|
||||||
|
if (hosts) {
|
||||||
|
items = _.filter(this.cache.getItems(), function (itemObj) {
|
||||||
|
return _.find(hosts, {'hostid': itemObj.hostid });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (apps) {
|
||||||
|
var appids = _.flatten(_.map(apps, 'applicationids'));
|
||||||
|
items = _.filter(this.cache.getItems(), function (itemObj) {
|
||||||
|
return _.intersection(appids, itemObj.applications).length;
|
||||||
|
});
|
||||||
|
items = _.filter(items, function (itemObj) {
|
||||||
|
return _.find(hosts, {'hostid': itemObj.hostid });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!showDisabledItems) {
|
||||||
|
items = _.filter(items, {'status': '0'});
|
||||||
|
}
|
||||||
|
|
||||||
|
return items;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build query - convert target filters to array of Zabbix items
|
* Build query - convert target filters to array of Zabbix items
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ define([
|
|||||||
function (_, moment) {
|
function (_, moment) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
function utils() {
|
function Utils() {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expand Zabbix item name
|
* Expand Zabbix item name
|
||||||
@@ -49,5 +49,5 @@ function (_, moment) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new utils();
|
return new Utils();
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user