ZabbixAPI: improved requests caching. Cache groups, hosts, apps and items.
This commit is contained in:
@@ -18,7 +18,11 @@ function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy) {
|
|||||||
this.zabbixAPI = new ZabbixAPI(url, username, password, basicAuth, withCredentials);
|
this.zabbixAPI = new ZabbixAPI(url, username, password, basicAuth, withCredentials);
|
||||||
|
|
||||||
// Initialize caching proxy for requests
|
// Initialize caching proxy for requests
|
||||||
this.cachingProxy = new ZabbixCachingProxy(this.zabbixAPI, cacheTTL);
|
let cacheOptions = {
|
||||||
|
enabled: true,
|
||||||
|
ttl: cacheTTL
|
||||||
|
};
|
||||||
|
this.cachingProxy = new ZabbixCachingProxy(this.zabbixAPI, cacheOptions);
|
||||||
|
|
||||||
// Proxy methods
|
// Proxy methods
|
||||||
this.getHistory = this.cachingProxy.getHistory.bind(this.cachingProxy);
|
this.getHistory = this.cachingProxy.getHistory.bind(this.cachingProxy);
|
||||||
|
|||||||
@@ -5,12 +5,13 @@ import _ from 'lodash';
|
|||||||
// Each datasource instance must initialize its own cache.
|
// Each datasource instance must initialize its own cache.
|
||||||
|
|
||||||
/** @ngInject */
|
/** @ngInject */
|
||||||
function ZabbixCachingProxyFactory($interval) {
|
function ZabbixCachingProxyFactory() {
|
||||||
|
|
||||||
class ZabbixCachingProxy {
|
class ZabbixCachingProxy {
|
||||||
constructor(zabbixAPI, ttl) {
|
constructor(zabbixAPI, cacheOptions) {
|
||||||
this.zabbixAPI = zabbixAPI;
|
this.zabbixAPI = zabbixAPI;
|
||||||
this.ttl = ttl;
|
this.cacheEnabled = cacheOptions.enabled;
|
||||||
|
this.ttl = cacheOptions.ttl || 600000; // 10 minutes by default
|
||||||
|
|
||||||
// Internal objects for data storing
|
// Internal objects for data storing
|
||||||
this._groups = undefined;
|
this._groups = undefined;
|
||||||
@@ -22,6 +23,13 @@ function ZabbixCachingProxyFactory($interval) {
|
|||||||
trends: {}
|
trends: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.cache = {
|
||||||
|
groups: {},
|
||||||
|
hosts: {},
|
||||||
|
applications: {},
|
||||||
|
items: {}
|
||||||
|
};
|
||||||
|
|
||||||
// Check is a service initialized or not
|
// Check is a service initialized or not
|
||||||
this._initialized = undefined;
|
this._initialized = undefined;
|
||||||
|
|
||||||
@@ -32,7 +40,7 @@ function ZabbixCachingProxyFactory($interval) {
|
|||||||
this.refresh = callOnce(this._refresh, this.refreshPromise);
|
this.refresh = callOnce(this._refresh, this.refreshPromise);
|
||||||
|
|
||||||
// Update cache periodically
|
// Update cache periodically
|
||||||
$interval(_.bind(this.refresh, this), this.ttl);
|
// $interval(_.bind(this.refresh, this), this.ttl);
|
||||||
|
|
||||||
// Don't run duplicated history requests
|
// Don't run duplicated history requests
|
||||||
this.getHistory = callAPIRequestOnce(_.bind(this.zabbixAPI.getHistory, this.zabbixAPI),
|
this.getHistory = callAPIRequestOnce(_.bind(this.zabbixAPI.getHistory, this.zabbixAPI),
|
||||||
@@ -41,19 +49,19 @@ function ZabbixCachingProxyFactory($interval) {
|
|||||||
// Don't run duplicated requests
|
// Don't run duplicated requests
|
||||||
this.groupPromises = {};
|
this.groupPromises = {};
|
||||||
this.getGroupsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getGroups, this.zabbixAPI),
|
this.getGroupsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getGroups, this.zabbixAPI),
|
||||||
this.groupPromises, getAPIRequestHash);
|
this.groupPromises, getRequestHash);
|
||||||
|
|
||||||
this.hostPromises = {};
|
this.hostPromises = {};
|
||||||
this.getHostsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getHosts, this.zabbixAPI),
|
this.getHostsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getHosts, this.zabbixAPI),
|
||||||
this.hostPromises, getAPIRequestHash);
|
this.hostPromises, getRequestHash);
|
||||||
|
|
||||||
this.appPromises = {};
|
this.appPromises = {};
|
||||||
this.getAppsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getApps, this.zabbixAPI),
|
this.getAppsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getApps, this.zabbixAPI),
|
||||||
this.appPromises, getAPIRequestHash);
|
this.appPromises, getRequestHash);
|
||||||
|
|
||||||
this.itemPromises = {};
|
this.itemPromises = {};
|
||||||
this.getItemsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getItems, this.zabbixAPI),
|
this.getItemsOnce = callAPIRequestOnce(_.bind(this.zabbixAPI.getItems, this.zabbixAPI),
|
||||||
this.itemPromises, getAPIRequestHash);
|
this.itemPromises, getRequestHash);
|
||||||
}
|
}
|
||||||
|
|
||||||
_refresh() {
|
_refresh() {
|
||||||
@@ -70,41 +78,50 @@ function ZabbixCachingProxyFactory($interval) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getGroups() {
|
isExpired(cacheObject) {
|
||||||
if (this._groups) {
|
if (cacheObject) {
|
||||||
return Promise.resolve(this._groups);
|
let object_age = Date.now() - cacheObject.timestamp;
|
||||||
|
return !(cacheObject.timestamp && object_age < this.ttl);
|
||||||
} else {
|
} else {
|
||||||
return this.getGroupsOnce()
|
return true;
|
||||||
.then(groups => {
|
}
|
||||||
this._groups = groups;
|
}
|
||||||
return groups;
|
|
||||||
|
/**
|
||||||
|
* Check that result is present in cache and up to date
|
||||||
|
* or send request to API.
|
||||||
|
*/
|
||||||
|
proxyRequest(request, params, cacheObject) {
|
||||||
|
let hash = getRequestHash(params);
|
||||||
|
if (this.cacheEnabled && !this.isExpired(cacheObject[hash])) {
|
||||||
|
return Promise.resolve(cacheObject[hash].value);
|
||||||
|
} else {
|
||||||
|
return request(...params)
|
||||||
|
.then(result => {
|
||||||
|
cacheObject[hash] = {
|
||||||
|
value: result,
|
||||||
|
timestamp: Date.now()
|
||||||
|
};
|
||||||
|
return result;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getGroups() {
|
||||||
|
return this.proxyRequest(this.getGroupsOnce, [], this.cache.groups);
|
||||||
|
}
|
||||||
|
|
||||||
getHosts(groupids) {
|
getHosts(groupids) {
|
||||||
return this.getHostsOnce(groupids)
|
return this.proxyRequest(this.getHostsOnce, [groupids], this.cache.hosts);
|
||||||
.then(hosts => {
|
|
||||||
// iss #196 - disable caching due performance issues
|
|
||||||
//this._hosts = _.union(this._hosts, hosts);
|
|
||||||
return hosts;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getApps(hostids) {
|
getApps(hostids) {
|
||||||
return this.getAppsOnce(hostids)
|
return this.proxyRequest(this.getAppsOnce, [hostids], this.cache.applications);
|
||||||
.then(apps => {
|
|
||||||
return apps;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getItems(hostids, appids, itemtype) {
|
getItems(hostids, appids, itemtype) {
|
||||||
return this.getItemsOnce(hostids, appids, itemtype)
|
let params = [hostids, appids, itemtype];
|
||||||
.then(items => {
|
return this.proxyRequest(this.getItemsOnce, params, this.cache.items);
|
||||||
// iss #196 - disable caching due performance issues
|
|
||||||
//this._items = _.union(this._items, items);
|
|
||||||
return items;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getHistoryFromCache(items, time_from, time_till) {
|
getHistoryFromCache(items, time_from, time_till) {
|
||||||
@@ -195,12 +212,16 @@ function callAPIRequestOnce(func, promiseKeeper, argsHashFunc) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAPIRequestHash(args) {
|
function getRequestHash(args) {
|
||||||
var requestStamp = _.map(args, arg => {
|
var requestStamp = _.map(args, arg => {
|
||||||
if (arg === undefined) {
|
if (arg === undefined) {
|
||||||
return 'undefined';
|
return 'undefined';
|
||||||
} else {
|
} else {
|
||||||
return arg.toString();
|
if (_.isArray(arg)) {
|
||||||
|
return arg.sort().toString();
|
||||||
|
} else {
|
||||||
|
return arg.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}).join();
|
}).join();
|
||||||
return requestStamp.getHash();
|
return requestStamp.getHash();
|
||||||
|
|||||||
Reference in New Issue
Block a user