Refactor: replace angular $q by native Promise.

This commit is contained in:
Alexander Zobnin
2016-11-13 15:55:23 +03:00
parent 775a422c84
commit ce8d9f4be8
8 changed files with 32 additions and 42 deletions

View File

@@ -5,15 +5,13 @@ import _ from 'lodash';
// Each datasource instance must initialize its own cache.
/** @ngInject */
function ZabbixCachingProxyFactory($q, $interval) {
function ZabbixCachingProxyFactory($interval) {
class ZabbixCachingProxy {
constructor(zabbixAPI, ttl) {
this.zabbixAPI = zabbixAPI;
this.ttl = ttl;
this.$q = $q;
// Internal objects for data storing
this._groups = undefined;
this._hosts = undefined;
@@ -110,34 +108,32 @@ function ZabbixCachingProxyFactory($q, $interval) {
}
getHistoryFromCache(items, time_from, time_till) {
var deferred = this.$q.defer();
var historyStorage = this.storage.history;
var full_history;
var expired = _.filter(_.keyBy(items, 'itemid'), function(item, itemid) {
var expired = _.filter(_.keyBy(items, 'itemid'), (item, itemid) => {
return !historyStorage[itemid];
});
if (expired.length) {
this.zabbixAPI.getHistory(expired, time_from, time_till).then(function(history) {
return this.zabbixAPI.getHistory(expired, time_from, time_till).then(function(history) {
var grouped_history = _.groupBy(history, 'itemid');
_.forEach(expired, function(item) {
_.forEach(expired, 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) {
full_history = _.map(items, item => {
return historyStorage[item.itemid].history;
});
deferred.resolve(_.flatten(full_history, true));
return _.flatten(full_history, true);
});
} else {
full_history = _.map(items, function(item) {
return historyStorage[item.itemid].history;
});
deferred.resolve(_.flatten(full_history, true));
return Promise.resolve(_.flatten(full_history, true));
}
return deferred.promise;
}
getHistoryFromAPI(items, time_from, time_till) {