Merge branch 'cache-service' into develop

This commit is contained in:
Alexander Zobnin
2016-03-28 20:21:18 +03:00

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,89 +85,31 @@ 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;
}); });
} }
_getHosts() {
var self = this;
if (this._hosts) {
return this.$q.when(self._hosts);
} else {
return this.refresh().then(function() {
return self._hosts;
});
}
}
getIndexedHosts() {
var self = this;
if (this._idx_hosts) {
return this.$q.when(self._idx_hosts);
} else {
return this.refresh().then(function() {
return self._idx_hosts;
});
}
}
getIndexedApplications() {
var self = this;
if (this._idx_apps) {
return this.$q.when(self._idx_apps);
} else {
return this.refresh().then(function() {
return self._idx_apps;
});
}
}
getApplications() {
var self = this;
if (this._applications) {
return this.$q.when(self._applications);
} else {
return this.refresh().then(function() {
return self._applications;
});
}
}
_getItems(type) {
var self = this;
if (this._items) {
return this.$q.when(filterItems(self._items, type));
} else {
return this.refresh().then(function() {
return filterItems(self._items, type);
});
}
}
getHistoryFromCache(items, time_from, time_till) { getHistoryFromCache(items, time_from, time_till) {
var deferred = this.$q.defer(); var deferred = this.$q.defer();
var historyStorage = this.storage.history; var historyStorage = this.storage.history;
@@ -197,6 +154,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');
@@ -236,114 +213,6 @@ angular.module('grafana.services').factory('ZabbixCachingProxy', function($q, $i
return ZabbixCachingProxy; return ZabbixCachingProxy;
}); });
/**
* Convert host.get response to cache format
* host.groups - array of group ids
*/
function convertHosts(hosts) {
return _.forEach(hosts, function(host) {
host.groups = _.map(host.groups, 'groupid');
return host;
});
}
function convertGroups(groups) {
return _.forEach(groups, function(group) {
group.hosts = _.map(group.hosts, 'hostid');
return group;
});
}
/**
* Group Zabbix applications by name
* host.hosts - array of host ids
*/
function convertApplications(applications) {
return _.map(_.groupBy(applications, 'name'), function(value, key) {
//console.log(value);
// Hack for supporting different apis (2.2 vs 2.4 vs 3.0)
var hostField = 'host';
if (value[0] && value[0]['hosts']) {
// For Zabbix 2.2
hostField = 'hosts';
}
return {
name: key,
applicationids: _.map(value, 'applicationid'),
itemids: _.uniq(_.map(_.flatten(value, 'items'), 'itemid')),
hosts: _.uniq(_.map(_.flatten(value, hostField), 'hostid'))
};
});
}
function indexHosts(hosts) {
return _.indexBy(_.map(hosts, function(host) {
// Expand item names
host.items = _.forEach(host.items, function(item) {
item.item = item.name;
item.name = utils.expandItemName(item.item, item.key_);
return item;
});
host.applications = _.map(host.applications, 'applicationid');
host.idx_items = indexItems(host.items);
host.items = _.map(host.items, 'itemid');
return host;
}), 'hostid');
}
function indexApps(applications) {
return _.indexBy(_.map(applications, function(app) {
return {
name: app.name,
applicationid: app.applicationid,
host: _.first(_.map(app.hosts, 'hostid')),
itemids: _.map(app.items, 'itemid')
};
}), 'applicationid');
}
function indexItems(items) {
return _.indexBy(_.map(items, function(item) {
return item;
}), 'itemid');
}
/**
* Convert item.get response to cache format
* item.applications - array of application ids
* item.item - original item name returned by api (ie "CPU $2 time")
* item.name - expanded name (ie "CPU system time")
*/
function convertItems(items) {
return _.forEach(items, function(item) {
item.applications = _.map(item.applications, 'applicationid');
item.item = item.name;
item.name = utils.expandItemName(item.item, item.key_);
return item;
});
}
function filterItems(items, type) {
switch (type) {
case 'num':
return _.filter(items, function(item) {
return (item.value_type === '0' ||
item.value_type === '3');
});
case 'text':
return _.filter(items, function(item) {
return (item.value_type === '1' ||
item.value_type === '2' ||
item.value_type === '4');
});
default:
return items;
}
}
String.prototype.getHash = function() { String.prototype.getHash = function() {
var hash = 0, i, chr, len; var hash = 0, i, chr, len;
if (this.length === 0) { if (this.length === 0) {