Refactor: removed unused code from zabbixCachingProxy.

This commit is contained in:
Alexander Zobnin
2016-11-14 22:36:21 +03:00
parent f56c4c66d6
commit 9392d45ad9
2 changed files with 23 additions and 62 deletions

View File

@@ -93,6 +93,25 @@ export function convertToZabbixAPIUrl(url) {
} }
} }
/**
* Wrap function to prevent multiple calls
* when waiting for result.
*/
export function callOnce(func, promiseKeeper) {
return function() {
if (!promiseKeeper) {
promiseKeeper = Promise.resolve(
func.apply(this, arguments)
.then(result => {
promiseKeeper = null;
return result;
})
);
}
return promiseKeeper;
};
}
// Fix for backward compatibility with lodash 2.4 // Fix for backward compatibility with lodash 2.4
if (!_.includes) { if (!_.includes) {
_.includes = _.contains; _.includes = _.contains;

View File

@@ -14,34 +14,17 @@ function ZabbixCachingProxyFactory() {
this.ttl = cacheOptions.ttl || 600000; // 10 minutes by default this.ttl = cacheOptions.ttl || 600000; // 10 minutes by default
// Internal objects for data storing // Internal objects for data storing
this._groups = undefined;
this._hosts = undefined;
this._applications = undefined;
this._items = undefined;
this.storage = {
history: {},
trends: {}
};
this.cache = { this.cache = {
groups: {}, groups: {},
hosts: {}, hosts: {},
applications: {}, applications: {},
items: {} items: {},
history: {},
trends: {}
}; };
// Check is a service initialized or not
this._initialized = undefined;
this.refreshPromise = false;
this.historyPromises = {}; this.historyPromises = {};
// Wrap _refresh() method to call it once.
this.refresh = callOnce(this._refresh, this.refreshPromise);
// Update cache periodically
// $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),
this.historyPromises, getHistoryRequestHash); this.historyPromises, getHistoryRequestHash);
@@ -64,20 +47,6 @@ function ZabbixCachingProxyFactory() {
this.itemPromises, getRequestHash); this.itemPromises, getRequestHash);
} }
_refresh() {
let promises = [
this.zabbixAPI.getGroups()
];
return Promise.all(promises)
.then(results => {
if (results.length) {
this._groups = results[0];
}
this._initialized = true;
});
}
isExpired(cacheObject) { isExpired(cacheObject) {
if (cacheObject) { if (cacheObject) {
let object_age = Date.now() - cacheObject.timestamp; let object_age = Date.now() - cacheObject.timestamp;
@@ -125,7 +94,7 @@ function ZabbixCachingProxyFactory() {
} }
getHistoryFromCache(items, time_from, time_till) { getHistoryFromCache(items, time_from, time_till) {
var historyStorage = this.storage.history; var historyStorage = this.cache.history;
var full_history; var full_history;
var expired = _.filter(_.keyBy(items, 'itemid'), (item, itemid) => { var expired = _.filter(_.keyBy(items, 'itemid'), (item, itemid) => {
return !historyStorage[itemid]; return !historyStorage[itemid];
@@ -156,14 +125,6 @@ function ZabbixCachingProxyFactory() {
getHistoryFromAPI(items, time_from, time_till) { getHistoryFromAPI(items, time_from, time_till) {
return this.zabbixAPI.getHistory(items, time_from, time_till); return this.zabbixAPI.getHistory(items, time_from, time_till);
} }
getHost(hostid) {
return _.find(this._hosts, {'hostid': hostid});
}
getItem(itemid) {
return _.find(this._items, {'itemid': itemid});
}
} }
return ZabbixCachingProxy; return ZabbixCachingProxy;
@@ -173,25 +134,6 @@ angular
.module('grafana.services') .module('grafana.services')
.factory('ZabbixCachingProxy', ZabbixCachingProxyFactory); .factory('ZabbixCachingProxy', ZabbixCachingProxyFactory);
/**
* Wrap function to prevent multiple calls
* when waiting for result.
*/
function callOnce(func, promiseKeeper) {
return function() {
if (!promiseKeeper) {
promiseKeeper = Promise.resolve(
func.apply(this, arguments)
.then(result => {
promiseKeeper = null;
return result;
})
);
}
return promiseKeeper;
};
}
/** /**
* Wrap zabbix API request to prevent multiple calls * Wrap zabbix API request to prevent multiple calls
* with same params when waiting for result. * with same params when waiting for result.