Commit f1660aa2 by Torkel Ödegaard

fix: updated backend srv to use appEvents and removed parts of alertsSrv

parent f34cbae2
...@@ -16,7 +16,6 @@ export class GrafanaCtrl { ...@@ -16,7 +16,6 @@ export class GrafanaCtrl {
/** @ngInject */ /** @ngInject */
constructor( constructor(
$scope, $scope,
alertSrv,
utilSrv, utilSrv,
$rootScope, $rootScope,
$controller, $controller,
...@@ -37,11 +36,8 @@ export class GrafanaCtrl { ...@@ -37,11 +36,8 @@ export class GrafanaCtrl {
$scope._ = _; $scope._ = _;
profiler.init(config, $rootScope); profiler.init(config, $rootScope);
alertSrv.init();
utilSrv.init(); utilSrv.init();
bridgeSrv.init(); bridgeSrv.init();
$scope.dashAlerts = alertSrv;
}; };
$rootScope.colors = colors; $rootScope.colors = colors;
......
import angular from 'angular';
import _ from 'lodash';
import coreModule from 'app/core/core_module'; import coreModule from 'app/core/core_module';
import appEvents from 'app/core/app_events';
export class AlertSrv { export class AlertSrv {
list: any[]; constructor() {}
/** @ngInject */ set() {
constructor(private $timeout, private $rootScope) { console.log('old depricated alert srv being used');
this.list = [];
}
init() {
this.$rootScope.onAppEvent(
'alert-error',
(e, alert) => {
this.set(alert[0], alert[1], 'error', 12000);
},
this.$rootScope
);
this.$rootScope.onAppEvent(
'alert-warning',
(e, alert) => {
this.set(alert[0], alert[1], 'warning', 5000);
},
this.$rootScope
);
this.$rootScope.onAppEvent(
'alert-success',
(e, alert) => {
this.set(alert[0], alert[1], 'success', 3000);
},
this.$rootScope
);
appEvents.on('alert-warning', options => this.set(options[0], options[1], 'warning', 5000));
appEvents.on('alert-success', options => this.set(options[0], options[1], 'success', 3000));
appEvents.on('alert-error', options => this.set(options[0], options[1], 'error', 7000));
}
getIconForSeverity(severity) {
switch (severity) {
case 'success':
return 'fa fa-check';
case 'error':
return 'fa fa-exclamation-triangle';
default:
return 'fa fa-exclamation';
}
}
set(title, text, severity, timeout) {
if (_.isObject(text)) {
console.log('alert error', text);
if (text.statusText) {
text = `HTTP Error (${text.status}) ${text.statusText}`;
}
}
const newAlert = {
title: title || '',
text: text || '',
severity: severity || 'info',
icon: this.getIconForSeverity(severity),
};
const newAlertJson = angular.toJson(newAlert);
// remove same alert if it already exists
_.remove(this.list, value => {
return angular.toJson(value) === newAlertJson;
});
this.list.push(newAlert);
if (timeout > 0) {
this.$timeout(() => {
this.list = _.without(this.list, newAlert);
}, timeout);
}
if (!this.$rootScope.$$phase) {
this.$rootScope.$digest();
}
return newAlert;
}
clear(alert) {
this.list = _.without(this.list, alert);
}
clearAll() {
this.list = [];
} }
} }
// this is just added to not break old plugins that might be using it
coreModule.service('alertSrv', AlertSrv); coreModule.service('alertSrv', AlertSrv);
...@@ -9,7 +9,7 @@ export class BackendSrv { ...@@ -9,7 +9,7 @@ export class BackendSrv {
private noBackendCache: boolean; private noBackendCache: boolean;
/** @ngInject */ /** @ngInject */
constructor(private $http, private alertSrv, private $q, private $timeout, private contextSrv) {} constructor(private $http, private $q, private $timeout, private contextSrv) {}
get(url, params?) { get(url, params?) {
return this.request({ method: 'GET', url: url, params: params }); return this.request({ method: 'GET', url: url, params: params });
...@@ -49,14 +49,14 @@ export class BackendSrv { ...@@ -49,14 +49,14 @@ export class BackendSrv {
} }
if (err.status === 422) { if (err.status === 422) {
this.alertSrv.set('Validation failed', data.message, 'warning', 4000); appEvents.emit('alert-warning', ['Validation failed', data.message]);
throw data; throw data;
} }
data.severity = 'error'; let severity = 'error';
if (err.status < 500) { if (err.status < 500) {
data.severity = 'warning'; severity = 'warning';
} }
if (data.message) { if (data.message) {
...@@ -66,7 +66,8 @@ export class BackendSrv { ...@@ -66,7 +66,8 @@ export class BackendSrv {
description = message; description = message;
message = 'Error'; message = 'Error';
} }
this.alertSrv.set(message, description, data.severity, 10000);
appEvents.emit('alert-' + severity, [message, description]);
} }
throw data; throw data;
...@@ -93,7 +94,7 @@ export class BackendSrv { ...@@ -93,7 +94,7 @@ export class BackendSrv {
if (options.method !== 'GET') { if (options.method !== 'GET') {
if (results && results.data.message) { if (results && results.data.message) {
if (options.showSuccessAlert !== false) { if (options.showSuccessAlert !== false) {
this.alertSrv.set(results.data.message, '', 'success', 3000); appEvents.emit('alert-success', [results.data.message]);
} }
} }
} }
......
...@@ -9,7 +9,7 @@ describe('backend_srv', () => { ...@@ -9,7 +9,7 @@ describe('backend_srv', () => {
return Promise.resolve({}); return Promise.resolve({});
}; };
const _backendSrv = new BackendSrv(_httpBackend, {}, {}, {}, {}); const _backendSrv = new BackendSrv(_httpBackend, {}, {}, {});
describe('when handling errors', () => { describe('when handling errors', () => {
it('should return the http status code', async () => { it('should return the http status code', async () => {
......
...@@ -11,7 +11,7 @@ const template = ` ...@@ -11,7 +11,7 @@ const template = `
`; `;
/** @ngInject */ /** @ngInject */
function uploadDashboardDirective(timer, alertSrv, $location) { function uploadDashboardDirective(timer, $location) {
return { return {
restrict: 'E', restrict: 'E',
template: template, template: template,
...@@ -59,7 +59,7 @@ function uploadDashboardDirective(timer, alertSrv, $location) { ...@@ -59,7 +59,7 @@ function uploadDashboardDirective(timer, alertSrv, $location) {
// Something // Something
elem[0].addEventListener('change', file_selected, false); elem[0].addEventListener('change', file_selected, false);
} else { } else {
alertSrv.set('Oops', 'Sorry, the HTML5 File APIs are not fully supported in this browser.', 'error'); appEvents.emit('alert-error', ['Oops', 'The HTML5 File APIs are not fully supported in this browser']);
} }
}, },
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment