From e532b3c37f3a4fbf277cb70e4a19a837fd5efa2b Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 31 Jan 2016 19:00:30 +0300 Subject: [PATCH] Added method for getting history from cache. --- plugins/datasource-zabbix/datasource.js | 2 +- plugins/datasource-zabbix/zabbixCache.js | 46 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/plugins/datasource-zabbix/datasource.js b/plugins/datasource-zabbix/datasource.js index facae7a..8d995fb 100644 --- a/plugins/datasource-zabbix/datasource.js +++ b/plugins/datasource-zabbix/datasource.js @@ -140,7 +140,7 @@ function (angular, _, dateMath, utils, metricFunctions) { } else { // Use history - getHistory = self.zabbixAPI.getHistory(items, from, to).then(function(history) { + getHistory = self.zabbixCache.getHistory(items, from, to).then(function(history) { return self.queryProcessor.handleHistory(history, addHostName); }); } diff --git a/plugins/datasource-zabbix/zabbixCache.js b/plugins/datasource-zabbix/zabbixCache.js index fb35b27..bc9f6ca 100644 --- a/plugins/datasource-zabbix/zabbixCache.js +++ b/plugins/datasource-zabbix/zabbixCache.js @@ -21,6 +21,10 @@ function (angular, _, utils) { this._hosts = undefined; this._applications = undefined; this._items = undefined; + this.storage = { + history: {}, + trends: {} + }; // Check is a service initialized or not this._initialized = undefined; @@ -115,6 +119,48 @@ function (angular, _, utils) { } } + p.getHistory = function(items, time_from, time_till) { + var itemids = _.map(arguments[0], 'itemid'); + var stamp = itemids.join() + arguments[1] + arguments[2]; + //console.log(arguments, stamp); + return this.zabbixAPI.getHistory(items, time_from, time_till); + }; + + p.getHistory_ = function(items, time_from, time_till) { + var deferred = $q.defer(); + var historyStorage = this.storage.history; + var full_history; + var expired = _.filter(_.indexBy(items, 'itemid'), function(item, itemid) { + return !historyStorage[itemid]; + }); + if (expired.length) { + this.zabbixAPI.getHistory(expired, time_from, time_till).then(function(history) { + var grouped_history = _.groupBy(history, 'itemid'); + _.forEach(expired, function(item) { + var itemid = item.itemid; + historyStorage[itemid] = item; + historyStorage[itemid].time_from = time_from; + historyStorage[itemid].time_till = time_till; + historyStorage[itemid].history = grouped_history[itemid]; + }); + full_history = _.map(items, function(item) { + return historyStorage[item.itemid].history; + }); + deferred.resolve(_.flatten(full_history, true)); + }); + } else { + full_history = _.map(items, function(item) { + return historyStorage[item.itemid].history; + }); + deferred.resolve(_.flatten(full_history, true)); + } + return deferred.promise; + }; + + p.getHistoryFromAPI = function(items, time_from, time_till) { + return this.zabbixAPI.getHistory(items, time_from, time_till); + }; + p.getHost = function(hostid) { return _.find(this._hosts, {'hostid': hostid}); };