Refactor buildFromCache() method.
This commit is contained in:
@@ -31,32 +31,36 @@ function (angular, _, utils) {
|
|||||||
this.filterHosts = function(groupFilter) {
|
this.filterHosts = function(groupFilter) {
|
||||||
var groups = [];
|
var groups = [];
|
||||||
var hosts = [];
|
var hosts = [];
|
||||||
var groupList = self.cache.getGroups();
|
|
||||||
|
|
||||||
// Filter groups by regex
|
return self.cache.getGroups().then(function(groupList) {
|
||||||
if (utils.isRegex(groupFilter)) {
|
// Filter groups by regex
|
||||||
var filterPattern = utils.buildRegex(groupFilter);
|
if (utils.isRegex(groupFilter)) {
|
||||||
groups = _.filter(groupList, function (groupObj) {
|
var filterPattern = utils.buildRegex(groupFilter);
|
||||||
return filterPattern.test(groupObj.name);
|
groups = _.filter(groupList, function (groupObj) {
|
||||||
});
|
return filterPattern.test(groupObj.name);
|
||||||
}
|
});
|
||||||
// Find hosts in selected group
|
}
|
||||||
else {
|
// Find hosts in selected group
|
||||||
var finded = _.find(groupList, {'name': groupFilter});
|
else {
|
||||||
if (finded) {
|
var finded = _.find(groupList, {'name': groupFilter});
|
||||||
groups.push(finded);
|
if (finded) {
|
||||||
} else {
|
groups.push(finded);
|
||||||
groups = undefined;
|
} else {
|
||||||
|
groups = undefined;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (groups) {
|
if (groups) {
|
||||||
var groupids = _.map(groups, 'groupid');
|
var groupids = _.map(groups, 'groupid');
|
||||||
hosts = _.filter(self.cache.getHosts(), function (hostObj) {
|
return self.cache.getHosts().then(function(hosts) {
|
||||||
return _.intersection(groupids, hostObj.groups).length;
|
return _.filter(hosts, function (hostObj) {
|
||||||
});
|
return _.intersection(groupids, hostObj.groups).length;
|
||||||
}
|
});
|
||||||
return hosts;
|
});
|
||||||
|
} else {
|
||||||
|
return hosts;
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
this.filterApplications = function(hostFilter) {
|
this.filterApplications = function(hostFilter) {
|
||||||
@@ -165,107 +169,120 @@ function (angular, _, utils) {
|
|||||||
var hosts = [];
|
var hosts = [];
|
||||||
var apps = [];
|
var apps = [];
|
||||||
var items = [];
|
var items = [];
|
||||||
|
var promises = [
|
||||||
|
this.cache.getGroups(),
|
||||||
|
this.cache.getHosts(),
|
||||||
|
this.cache.getApplications(),
|
||||||
|
this.cache.getItems()
|
||||||
|
];
|
||||||
|
|
||||||
if (utils.isRegex(hostFilter)) {
|
return $q.all(promises).then(function(results) {
|
||||||
|
var cachedGroups = results[0];
|
||||||
|
var cachedHosts = results[1];
|
||||||
|
var cachedApps = results[2];
|
||||||
|
var cachedItems = results[3];
|
||||||
|
|
||||||
// Filter groups
|
if (utils.isRegex(hostFilter)) {
|
||||||
if (utils.isRegex(groupFilter)) {
|
|
||||||
var groupPattern = utils.buildRegex(groupFilter);
|
// Filter groups
|
||||||
groups = _.filter(this.cache.getGroups(), function (groupObj) {
|
if (utils.isRegex(groupFilter)) {
|
||||||
return groupPattern.test(groupObj.name);
|
var groupPattern = utils.buildRegex(groupFilter);
|
||||||
});
|
groups = _.filter(cachedGroups, function (groupObj) {
|
||||||
} else {
|
return groupPattern.test(groupObj.name);
|
||||||
var findedGroup = _.find(this.cache.getGroups(), {'name': groupFilter});
|
});
|
||||||
if (findedGroup) {
|
|
||||||
groups.push(findedGroup);
|
|
||||||
} else {
|
} else {
|
||||||
groups = undefined;
|
var findedGroup = _.find(cachedGroups, {'name': groupFilter});
|
||||||
|
if (findedGroup) {
|
||||||
|
groups.push(findedGroup);
|
||||||
|
} else {
|
||||||
|
groups = undefined;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
if (groups) {
|
||||||
if (groups) {
|
var groupids = _.map(groups, 'groupid');
|
||||||
var groupids = _.map(groups, 'groupid');
|
hosts = _.filter(cachedHosts, function (hostObj) {
|
||||||
hosts = _.filter(this.cache.getHosts(), function (hostObj) {
|
return _.intersection(groupids, hostObj.groups).length;
|
||||||
return _.intersection(groupids, hostObj.groups).length;
|
});
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// No groups finded
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter hosts
|
|
||||||
var hostPattern = utils.buildRegex(hostFilter);
|
|
||||||
hosts = _.filter(hosts, function (hostObj) {
|
|
||||||
return hostPattern.test(hostObj.name);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
var findedHost = _.find(this.cache.getHosts(), {'name': hostFilter});
|
|
||||||
if (findedHost) {
|
|
||||||
hosts.push(findedHost);
|
|
||||||
} else {
|
|
||||||
// No hosts finded
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find items belongs to selected hosts
|
|
||||||
items = _.filter(this.cache.getItems(), function (itemObj) {
|
|
||||||
return _.contains(_.map(hosts, 'hostid'), itemObj.hostid);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (utils.isRegex(itemFilter)) {
|
|
||||||
|
|
||||||
// Filter applications
|
|
||||||
if (utils.isRegex(appFilter)) {
|
|
||||||
var appPattern = utils.buildRegex(appFilter);
|
|
||||||
apps = _.filter(this.cache.getApplications(), function (appObj) {
|
|
||||||
return appPattern.test(appObj.name);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// Don't use application filter if it empty
|
|
||||||
else if (appFilter === "") {
|
|
||||||
apps = undefined;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var findedApp = _.find(this.cache.getApplications(), {'name': appFilter});
|
|
||||||
if (findedApp) {
|
|
||||||
apps.push(findedApp);
|
|
||||||
} else {
|
} else {
|
||||||
// No applications finded
|
// No groups finded
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter hosts
|
||||||
|
var hostPattern = utils.buildRegex(hostFilter);
|
||||||
|
hosts = _.filter(hosts, function (hostObj) {
|
||||||
|
return hostPattern.test(hostObj.name);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
var findedHost = _.find(cachedHosts, {'name': hostFilter});
|
||||||
|
if (findedHost) {
|
||||||
|
hosts.push(findedHost);
|
||||||
|
} else {
|
||||||
|
// No hosts finded
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find items belongs to selected applications
|
// Find items belongs to selected hosts
|
||||||
if (apps) {
|
items = _.filter(cachedItems, function (itemObj) {
|
||||||
var appids = _.flatten(_.map(apps, 'applicationids'));
|
return _.contains(_.map(hosts, 'hostid'), itemObj.hostid);
|
||||||
items = _.filter(items, function (itemObj) {
|
});
|
||||||
return _.intersection(appids, itemObj.applications).length;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (items) {
|
if (utils.isRegex(itemFilter)) {
|
||||||
var itemPattern = utils.buildRegex(itemFilter);
|
|
||||||
items = _.filter(items, function (itemObj) {
|
// Filter applications
|
||||||
return itemPattern.test(itemObj.name);
|
if (utils.isRegex(appFilter)) {
|
||||||
});
|
var appPattern = utils.buildRegex(appFilter);
|
||||||
|
apps = _.filter(cachedApps, function (appObj) {
|
||||||
|
return appPattern.test(appObj.name);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Don't use application filter if it empty
|
||||||
|
else if (appFilter === "") {
|
||||||
|
apps = undefined;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var findedApp = _.find(cachedApps, {'name': appFilter});
|
||||||
|
if (findedApp) {
|
||||||
|
apps.push(findedApp);
|
||||||
|
} else {
|
||||||
|
// No applications finded
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find items belongs to selected applications
|
||||||
|
if (apps) {
|
||||||
|
var appids = _.flatten(_.map(apps, 'applicationids'));
|
||||||
|
items = _.filter(items, function (itemObj) {
|
||||||
|
return _.intersection(appids, itemObj.applications).length;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (items) {
|
||||||
|
var itemPattern = utils.buildRegex(itemFilter);
|
||||||
|
items = _.filter(items, function (itemObj) {
|
||||||
|
return itemPattern.test(itemObj.name);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// No items finded
|
||||||
|
return [];
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// No items finded
|
items = _.filter(items, {'name': itemFilter});
|
||||||
return [];
|
if (!items.length) {
|
||||||
|
// No items finded
|
||||||
|
return [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
items = _.filter(items, {'name': itemFilter});
|
|
||||||
if (!items.length) {
|
|
||||||
// No items finded
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set host as host name for each item
|
// Set host as host name for each item
|
||||||
items = _.each(items, function (itemObj) {
|
items = _.each(items, function (itemObj) {
|
||||||
itemObj.host = _.find(hosts, {'hostid': itemObj.hostid}).name;
|
itemObj.host = _.find(hosts, {'hostid': itemObj.hostid}).name;
|
||||||
|
});
|
||||||
|
|
||||||
|
return items;
|
||||||
});
|
});
|
||||||
|
|
||||||
return items;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -54,7 +54,13 @@ function (angular, _, utils) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
p.getGroups = function() {
|
p.getGroups = function() {
|
||||||
return this._groups;
|
var self = this;
|
||||||
|
if (this._groups) {
|
||||||
|
return this.refresh().then(function() {
|
||||||
|
return self._groups;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return $q.when(this._groups);
|
||||||
};
|
};
|
||||||
|
|
||||||
p.getHosts = function() {
|
p.getHosts = function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user