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

@@ -14,7 +14,7 @@ import {ZabbixAPIError} from './zabbixAPICore.service.js';
class ZabbixAPIDatasource {
/** @ngInject */
constructor(instanceSettings, $q, templateSrv, alertSrv, zabbixAPIService, ZabbixCachingProxy, QueryBuilder) {
constructor(instanceSettings, templateSrv, alertSrv, zabbixAPIService, ZabbixCachingProxy, QueryBuilder) {
// General data source settings
this.name = instanceSettings.name;
@@ -45,7 +45,6 @@ class ZabbixAPIDatasource {
this.queryBuilder = new QueryBuilder(this.zabbixCache);
// Dependencies
this.q = $q;
this.templateSrv = templateSrv;
this.alertSrv = alertSrv;
@@ -129,7 +128,7 @@ class ZabbixAPIDatasource {
});
// Data for panel (all targets)
return this.q.all(_.flatten(promises))
return Promise.all(_.flatten(promises))
.then(_.flatten)
.then(timeseries_data => {
@@ -245,7 +244,7 @@ class ZabbixAPIDatasource {
});
});
} else {
return this.q.when([]);
return Promise.resolve([]);
}
});
}
@@ -318,18 +317,18 @@ class ZabbixAPIDatasource {
if (template.app === '/.*/') {
template.app = '';
}
result = this.queryBuilder.getItems(template.group, template.host, template.app);
result = this.queryBuilder.getItems(template.group, template.host, template.app, template.item);
} else if (parts.length === 3) {
// Get applications
result = this.queryBuilder.getApps(template.group, template.host);
result = this.queryBuilder.getApps(template.group, template.host, template.app);
} else if (parts.length === 2) {
// Get hosts
result = this.queryBuilder.getHosts(template.group);
result = this.queryBuilder.getHosts(template.group, template.host);
} else if (parts.length === 1) {
// Get groups
result = this.zabbixCache.getGroups(template.group);
} else {
result = this.q.when([]);
result = Promise.resolve([]);
}
return result.then(metrics => {

View File

@@ -13,7 +13,7 @@ import './css/query-editor.css!';
export class ZabbixQueryController extends QueryCtrl {
// ZabbixQueryCtrl constructor
constructor($scope, $injector, $rootScope, $sce, $q, templateSrv) {
constructor($scope, $injector, $rootScope, $sce, templateSrv) {
// Call superclass constructor
super($scope, $injector);
@@ -21,7 +21,6 @@ export class ZabbixQueryController extends QueryCtrl {
this.zabbix = this.datasource.zabbixAPI;
this.cache = this.datasource.zabbixCache;
this.queryBuilder = this.datasource.queryBuilder;
this.$q = $q;
// Use custom format for template variables
this.replaceTemplateVars = this.datasource.replaceTemplateVars;
@@ -107,12 +106,13 @@ export class ZabbixQueryController extends QueryCtrl {
}
initFilters() {
var self = this;
var itemtype = self.editorModes[self.target.mode].value;
return this.$q.when(this.suggestGroups())
.then(() => {return self.suggestHosts();})
.then(() => {return self.suggestApps();})
.then(() => {return self.suggestItems(itemtype);});
let itemtype = this.editorModes[this.target.mode].value;
return Promise.all([
this.suggestGroups(),
this.suggestHosts(),
this.suggestApps(),
this.suggestItems(itemtype)
]);
}
suggestGroups() {
@@ -303,7 +303,6 @@ export class ZabbixQueryController extends QueryCtrl {
this.panelCtrl.refresh();
}
}
}
// Set templateUrl as static property

View File

@@ -17,14 +17,13 @@ describe('ZabbixDatasource', () => {
trendsFrom: '7d'
}
};
ctx.$q = Q;
ctx.templateSrv = {};
ctx.alertSrv = {};
ctx.zabbixAPIService = () => {};
ctx.ZabbixCachingProxy = () => {};
ctx.queryBuilder = () => {};
ctx.ds = new Datasource(ctx.instanceSettings, ctx.$q, ctx.templateSrv, ctx.alertSrv,
ctx.ds = new Datasource(ctx.instanceSettings, ctx.templateSrv, ctx.alertSrv,
ctx.zabbixAPIService, ctx.ZabbixCachingProxy, ctx.queryBuilder);
});

View File

@@ -4,7 +4,7 @@ import * as utils from './utils';
import './zabbixAPICore.service';
/** @ngInject */
function ZabbixAPIServiceFactory($q, alertSrv, zabbixAPICoreService) {
function ZabbixAPIServiceFactory(alertSrv, zabbixAPICoreService) {
/**
* Zabbix API Wrapper.
@@ -28,7 +28,6 @@ function ZabbixAPIServiceFactory($q, alertSrv, zabbixAPICoreService) {
this.loginErrorCount = 0;
this.maxLoginAttempts = 3;
this.$q = $q;
this.alertSrv = alertSrv;
this.zabbixAPICore = zabbixAPICoreService;

View File

@@ -7,8 +7,7 @@ import angular from 'angular';
class ZabbixAPICoreService {
/** @ngInject */
constructor($q, backendSrv) {
this.$q = $q;
constructor(backendSrv) {
this.backendSrv = backendSrv;
}

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) {

View File

@@ -19,12 +19,11 @@ import '../datasource-zabbix/css/query-editor.css!';
class TriggerPanelEditorCtrl {
/** @ngInject */
constructor($scope, $rootScope, $q, uiSegmentSrv, datasourceSrv, templateSrv, popoverSrv) {
constructor($scope, $rootScope, uiSegmentSrv, datasourceSrv, templateSrv, popoverSrv) {
$scope.editor = this;
this.panelCtrl = $scope.ctrl;
this.panel = this.panelCtrl.panel;
this.$q = $q;
this.datasourceSrv = datasourceSrv;
this.templateSrv = templateSrv;
this.popoverSrv = popoverSrv;
@@ -83,11 +82,11 @@ class TriggerPanelEditorCtrl {
}
initFilters() {
var self = this;
return this.$q
.when(this.suggestGroups())
.then(() => {return self.suggestHosts();})
.then(() => {return self.suggestApps();});
return Promise.all([
this.suggestGroups(),
this.suggestHosts(),
this.suggestApps()
]);
}
suggestGroups() {

View File

@@ -61,7 +61,7 @@ var defaultTimeFormat = "DD MMM YYYY HH:mm:ss";
class TriggerPanelCtrl extends MetricsPanelCtrl {
/** @ngInject */
constructor($scope, $injector, $q, $element, datasourceSrv, templateSrv, contextSrv) {
constructor($scope, $injector, $element, datasourceSrv, templateSrv, contextSrv) {
super($scope, $injector);
this.datasourceSrv = datasourceSrv;
this.templateSrv = templateSrv;