Optimized groups, hosts, apps and items requests (call once).

This commit is contained in:
Alexander Zobnin
2016-03-27 20:17:53 +03:00
parent be79dfd448
commit ca3a0c22ac

View File

@@ -40,6 +40,22 @@ angular.module('grafana.services').factory('ZabbixCachingProxy', function($q, $i
// Don't run duplicated history requests // Don't run duplicated history requests
this.getHistory = callHistoryOnce(_.bind(this.zabbixAPI.getHistory, this.zabbixAPI), this.getHistory = callHistoryOnce(_.bind(this.zabbixAPI.getHistory, this.zabbixAPI),
this.historyPromises); this.historyPromises);
this.groupPromises = {};
this.getGroupsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getGroups, this.zabbixAPI),
this.groupPromises);
this.hostPromises = {};
this.getHostsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getHosts, this.zabbixAPI),
this.hostPromises);
this.appPromises = {};
this.getAppsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getApps, this.zabbixAPI),
this.appPromises);
this.itemPromises = {};
this.getItemsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getItems, this.zabbixAPI),
this.itemPromises);
} }
_refresh() { _refresh() {
@@ -61,8 +77,7 @@ angular.module('grafana.services').factory('ZabbixCachingProxy', function($q, $i
if (this._groups) { if (this._groups) {
return this.$q.when(self._groups); return this.$q.when(self._groups);
} else { } else {
return this.zabbixAPI return this.getGroupsOnce()
.getGroups()
.then(groups => { .then(groups => {
self._groups = groups; self._groups = groups;
return self._groups; return self._groups;
@@ -70,28 +85,25 @@ angular.module('grafana.services').factory('ZabbixCachingProxy', function($q, $i
} }
} }
getApps(hostids) {
return this.zabbixAPI
.getApps(hostids)
.then(apps => {
return apps;
});
}
getHosts(groupids) { getHosts(groupids) {
var self = this; var self = this;
return this.zabbixAPI return this.getHostsOnce(groupids)
.getHosts(groupids)
.then(hosts => { .then(hosts => {
self._hosts = _.union(self._hosts, hosts); self._hosts = _.union(self._hosts, hosts);
return hosts; return hosts;
}); });
} }
getApps(hostids) {
return this.getAppsOnce(hostids)
.then(apps => {
return apps;
});
}
getItems(hostids, appids) { getItems(hostids, appids) {
var self = this; var self = this;
return this.zabbixAPI return this.getItemsOnce(hostids, appids)
.getItems(hostids, appids)
.then(items => { .then(items => {
self._items = _.union(self._items, items); self._items = _.union(self._items, items);
return items; return items;
@@ -197,6 +209,26 @@ angular.module('grafana.services').factory('ZabbixCachingProxy', function($q, $i
} }
} }
function callAPIRequestOnce(func, promiseKeeper) {
return function() {
var itemids = _.map(arguments[0], 'itemid');
var stamp = itemids.join() + arguments[1] + arguments[2];
var hash = stamp.getHash();
var deferred = $q.defer();
if (!promiseKeeper[hash]) {
promiseKeeper[hash] = deferred.promise;
func.apply(this, arguments).then(function(result) {
deferred.resolve(result);
promiseKeeper[hash] = null;
});
} else {
return promiseKeeper[hash];
}
return deferred.promise;
};
}
function callHistoryOnce(func, promiseKeeper) { function callHistoryOnce(func, promiseKeeper) {
return function() { return function() {
var itemids = _.map(arguments[0], 'itemid'); var itemids = _.map(arguments[0], 'itemid');