Cache and queryProcessor refactor.

This commit is contained in:
Alexander Zobnin
2016-02-18 23:47:31 +03:00
parent 637cf4c63d
commit 50c0764d01
3 changed files with 69 additions and 20 deletions

View File

@@ -49,7 +49,6 @@ function (angular, _, utils) {
this.filterHosts = function(groupFilter) { this.filterHosts = function(groupFilter) {
var groups = []; var groups = [];
var hosts = [];
return self.cache.getGroups().then(function(groupList) { return self.cache.getGroups().then(function(groupList) {
// Filter groups by regex // Filter groups by regex
@@ -69,15 +68,15 @@ function (angular, _, utils) {
} }
} }
if (groups) { var hostids = _.flatten(_.map(groups, 'hosts'));
var groupids = _.map(groups, 'groupid'); if (hostids.length) {
return self.cache.getHosts().then(function(hosts) { return self.cache.getHostsExtend().then(function(hosts) {
return _.filter(hosts, function (hostObj) { return _.map(hostids, function(hostid) {
return _.intersection(groupids, hostObj.groups).length; return hosts[hostid];
}); });
}); });
} else { } else {
return hosts; return [];
} }
}); });
}; };
@@ -129,14 +128,12 @@ function (angular, _, utils) {
var promises = [ var promises = [
this.filterHosts(groupFilter), this.filterHosts(groupFilter),
this.filterApplications(groupFilter, hostFilter), this.filterApplications(groupFilter, hostFilter)
this.cache.getItems(itemType),
]; ];
return $q.all(promises).then(function(results) { return $q.all(promises).then(function(results) {
var hostList = results[0]; var hostList = results[0];
var applicationList = results[1]; var applicationList = results[1];
var cachedItems = results[2];
// Filter hosts by regex // Filter hosts by regex
if (utils.isRegex(hostFilter)) { if (utils.isRegex(hostFilter)) {
@@ -144,8 +141,7 @@ function (angular, _, utils) {
hosts = _.filter(hostList, function (hostObj) { hosts = _.filter(hostList, function (hostObj) {
return hostFilterPattern.test(hostObj.name); return hostFilterPattern.test(hostObj.name);
}); });
} } else {
else {
var findedHosts = _.find(hostList, {'name': hostFilter}); var findedHosts = _.find(hostList, {'name': hostFilter});
if (findedHosts) { if (findedHosts) {
hosts.push(findedHosts); hosts.push(findedHosts);
@@ -172,20 +168,18 @@ function (angular, _, utils) {
} else { } else {
apps = undefined; apps = undefined;
if (hosts) { if (hosts) {
items = _.filter(cachedItems, function (itemObj) { items = _.flatten(_.map(hosts, 'items'), true);
return _.find(hosts, {'hostid': itemObj.hostid });
});
} }
} }
if (apps) { if (apps) {
var appids = _.flatten(_.map(apps, 'applicationids')); /*var appids = _.flatten(_.map(apps, 'applicationids'));
items = _.filter(cachedItems, function (itemObj) { items = _.filter(cachedItems, function (itemObj) {
return _.intersection(appids, itemObj.applications).length; return _.intersection(appids, itemObj.applications).length;
}); });
items = _.filter(items, function (itemObj) { items = _.filter(items, function (itemObj) {
return _.find(hosts, {'hostid': itemObj.hostid }); return _.find(hosts, {'hostid': itemObj.hostid });
}); });*/
} }
if (!showDisabledItems) { if (!showDisabledItems) {

View File

@@ -123,7 +123,8 @@ function (angular, _) {
p.getGroups = function() { p.getGroups = function() {
var params = { var params = {
output: ['name'], output: ['name'],
sortfield: 'name' sortfield: 'name',
selectHosts: []
}; };
return this.request('hostgroup.get', params); return this.request('hostgroup.get', params);
@@ -168,6 +169,27 @@ function (angular, _) {
return this.request('item.get', params); return this.request('item.get', params);
}; };
/**
* Get Hosts list with host's items.
* @return {[type]} [description]
*/
p.getHostsExtend = function() {
var params = {
output: ['name', 'host'],
sortfield: 'name',
selectGroups: [],
selectItems: [
'name', 'key_',
'value_type',
'hostid',
'status',
'state'
]
};
return this.request('host.get', params);
};
p.getLastValue = function(itemid) { p.getLastValue = function(itemid) {
var params = { var params = {
output: ['lastvalue'], output: ['lastvalue'],

View File

@@ -21,6 +21,7 @@ function (angular, _, utils) {
this._hosts = undefined; this._hosts = undefined;
this._applications = undefined; this._applications = undefined;
this._items = undefined; this._items = undefined;
this._hostsExtend = undefined;
this.storage = { this.storage = {
history: {}, history: {},
trends: {} trends: {}
@@ -51,15 +52,17 @@ function (angular, _, utils) {
this.zabbixAPI.getGroups(), this.zabbixAPI.getGroups(),
this.zabbixAPI.getHosts(), this.zabbixAPI.getHosts(),
this.zabbixAPI.getApplications(), this.zabbixAPI.getApplications(),
this.zabbixAPI.getItems() this.zabbixAPI.getItems(),
this.zabbixAPI.getHostsExtend()
]; ];
return $q.all(promises).then(function(results) { return $q.all(promises).then(function(results) {
if (results.length) { if (results.length) {
self._groups = results[0]; self._groups = convertGroups(results[0]);
self._hosts = convertHosts(results[1]); self._hosts = convertHosts(results[1]);
self._applications = convertApplications(results[2]); self._applications = convertApplications(results[2]);
self._items = convertItems(results[3]); self._items = convertItems(results[3]);
self._hostsExtend = convertHostsExtend(results[4]);
} }
self._initialized = true; self._initialized = true;
}); });
@@ -87,6 +90,17 @@ function (angular, _, utils) {
} }
}; };
p.getHostsExtend = function() {
var self = this;
if (this._hostsExtend) {
return $q.when(self._hostsExtend);
} else {
return this.refresh().then(function() {
return self._hostsExtend;
});
}
};
p.getApplications = function() { p.getApplications = function() {
var self = this; var self = this;
if (this._applications) { if (this._applications) {
@@ -181,6 +195,25 @@ function (angular, _, utils) {
}); });
} }
function convertGroups(groups) {
return _.forEach(groups, function(group) {
group.hosts = _.map(group.hosts, 'hostid');
return group;
});
}
function convertHostsExtend(hosts) {
return _.indexBy(_.map(hosts, function(host) {
host.items = _.forEach(host.items, function(item) {
item.applications = _.map(item.applications, 'applicationid');
item.item = item.name;
item.name = utils.expandItemName(item.item, item.key_);
return item;
});
return host;
}), 'hostid');
}
/** /**
* Group Zabbix applications by name * Group Zabbix applications by name
* host.hosts - array of host ids * host.hosts - array of host ids