Refactor buildFromCache() method.

This commit is contained in:
Alexander Zobnin
2016-01-31 16:43:59 +03:00
parent 521e3a2a08
commit 1de4b4bc5c
2 changed files with 134 additions and 111 deletions

View File

@@ -31,8 +31,8 @@ function (angular, _, utils) {
this.filterHosts = function(groupFilter) {
var groups = [];
var hosts = [];
var groupList = self.cache.getGroups();
return self.cache.getGroups().then(function(groupList) {
// Filter groups by regex
if (utils.isRegex(groupFilter)) {
var filterPattern = utils.buildRegex(groupFilter);
@@ -52,11 +52,15 @@ function (angular, _, utils) {
if (groups) {
var groupids = _.map(groups, 'groupid');
hosts = _.filter(self.cache.getHosts(), function (hostObj) {
return self.cache.getHosts().then(function(hosts) {
return _.filter(hosts, function (hostObj) {
return _.intersection(groupids, hostObj.groups).length;
});
}
});
} else {
return hosts;
}
});
};
this.filterApplications = function(hostFilter) {
@@ -165,17 +169,29 @@ function (angular, _, utils) {
var hosts = [];
var apps = [];
var items = [];
var promises = [
this.cache.getGroups(),
this.cache.getHosts(),
this.cache.getApplications(),
this.cache.getItems()
];
return $q.all(promises).then(function(results) {
var cachedGroups = results[0];
var cachedHosts = results[1];
var cachedApps = results[2];
var cachedItems = results[3];
if (utils.isRegex(hostFilter)) {
// Filter groups
if (utils.isRegex(groupFilter)) {
var groupPattern = utils.buildRegex(groupFilter);
groups = _.filter(this.cache.getGroups(), function (groupObj) {
groups = _.filter(cachedGroups, function (groupObj) {
return groupPattern.test(groupObj.name);
});
} else {
var findedGroup = _.find(this.cache.getGroups(), {'name': groupFilter});
var findedGroup = _.find(cachedGroups, {'name': groupFilter});
if (findedGroup) {
groups.push(findedGroup);
} else {
@@ -184,7 +200,7 @@ function (angular, _, utils) {
}
if (groups) {
var groupids = _.map(groups, 'groupid');
hosts = _.filter(this.cache.getHosts(), function (hostObj) {
hosts = _.filter(cachedHosts, function (hostObj) {
return _.intersection(groupids, hostObj.groups).length;
});
} else {
@@ -198,7 +214,7 @@ function (angular, _, utils) {
return hostPattern.test(hostObj.name);
});
} else {
var findedHost = _.find(this.cache.getHosts(), {'name': hostFilter});
var findedHost = _.find(cachedHosts, {'name': hostFilter});
if (findedHost) {
hosts.push(findedHost);
} else {
@@ -208,7 +224,7 @@ function (angular, _, utils) {
}
// Find items belongs to selected hosts
items = _.filter(this.cache.getItems(), function (itemObj) {
items = _.filter(cachedItems, function (itemObj) {
return _.contains(_.map(hosts, 'hostid'), itemObj.hostid);
});
@@ -217,7 +233,7 @@ function (angular, _, utils) {
// Filter applications
if (utils.isRegex(appFilter)) {
var appPattern = utils.buildRegex(appFilter);
apps = _.filter(this.cache.getApplications(), function (appObj) {
apps = _.filter(cachedApps, function (appObj) {
return appPattern.test(appObj.name);
});
}
@@ -226,7 +242,7 @@ function (angular, _, utils) {
apps = undefined;
}
else {
var findedApp = _.find(this.cache.getApplications(), {'name': appFilter});
var findedApp = _.find(cachedApps, {'name': appFilter});
if (findedApp) {
apps.push(findedApp);
} else {
@@ -266,6 +282,7 @@ function (angular, _, utils) {
});
return items;
});
};
/**

View File

@@ -54,7 +54,13 @@ function (angular, _, utils) {
};
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() {