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

View File

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

View File

@@ -17,14 +17,13 @@ describe('ZabbixDatasource', () => {
trendsFrom: '7d' trendsFrom: '7d'
} }
}; };
ctx.$q = Q;
ctx.templateSrv = {}; ctx.templateSrv = {};
ctx.alertSrv = {}; ctx.alertSrv = {};
ctx.zabbixAPIService = () => {}; ctx.zabbixAPIService = () => {};
ctx.ZabbixCachingProxy = () => {}; ctx.ZabbixCachingProxy = () => {};
ctx.queryBuilder = () => {}; 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); ctx.zabbixAPIService, ctx.ZabbixCachingProxy, ctx.queryBuilder);
}); });

View File

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

View File

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

View File

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

View File

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

View File

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