mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
fix: updated backend srv to use appEvents and removed parts of alertsSrv
This commit is contained in:
parent
f34cbae2dd
commit
f1660aa21a
@ -16,7 +16,6 @@ export class GrafanaCtrl {
|
||||
/** @ngInject */
|
||||
constructor(
|
||||
$scope,
|
||||
alertSrv,
|
||||
utilSrv,
|
||||
$rootScope,
|
||||
$controller,
|
||||
@ -37,11 +36,8 @@ export class GrafanaCtrl {
|
||||
$scope._ = _;
|
||||
|
||||
profiler.init(config, $rootScope);
|
||||
alertSrv.init();
|
||||
utilSrv.init();
|
||||
bridgeSrv.init();
|
||||
|
||||
$scope.dashAlerts = alertSrv;
|
||||
};
|
||||
|
||||
$rootScope.colors = colors;
|
||||
|
@ -1,100 +1,12 @@
|
||||
import angular from 'angular';
|
||||
import _ from 'lodash';
|
||||
import coreModule from 'app/core/core_module';
|
||||
import appEvents from 'app/core/app_events';
|
||||
|
||||
export class AlertSrv {
|
||||
list: any[];
|
||||
constructor() {}
|
||||
|
||||
/** @ngInject */
|
||||
constructor(private $timeout, private $rootScope) {
|
||||
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 = [];
|
||||
set() {
|
||||
console.log('old depricated alert srv being used');
|
||||
}
|
||||
}
|
||||
|
||||
// this is just added to not break old plugins that might be using it
|
||||
coreModule.service('alertSrv', AlertSrv);
|
||||
|
@ -9,7 +9,7 @@ export class BackendSrv {
|
||||
private noBackendCache: boolean;
|
||||
|
||||
/** @ngInject */
|
||||
constructor(private $http, private alertSrv, private $q, private $timeout, private contextSrv) {}
|
||||
constructor(private $http, private $q, private $timeout, private contextSrv) {}
|
||||
|
||||
get(url, params?) {
|
||||
return this.request({ method: 'GET', url: url, params: params });
|
||||
@ -49,14 +49,14 @@ export class BackendSrv {
|
||||
}
|
||||
|
||||
if (err.status === 422) {
|
||||
this.alertSrv.set('Validation failed', data.message, 'warning', 4000);
|
||||
appEvents.emit('alert-warning', ['Validation failed', data.message]);
|
||||
throw data;
|
||||
}
|
||||
|
||||
data.severity = 'error';
|
||||
let severity = 'error';
|
||||
|
||||
if (err.status < 500) {
|
||||
data.severity = 'warning';
|
||||
severity = 'warning';
|
||||
}
|
||||
|
||||
if (data.message) {
|
||||
@ -66,7 +66,8 @@ export class BackendSrv {
|
||||
description = message;
|
||||
message = 'Error';
|
||||
}
|
||||
this.alertSrv.set(message, description, data.severity, 10000);
|
||||
|
||||
appEvents.emit('alert-' + severity, [message, description]);
|
||||
}
|
||||
|
||||
throw data;
|
||||
@ -93,7 +94,7 @@ export class BackendSrv {
|
||||
if (options.method !== 'GET') {
|
||||
if (results && results.data.message) {
|
||||
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', () => {
|
||||
return Promise.resolve({});
|
||||
};
|
||||
|
||||
const _backendSrv = new BackendSrv(_httpBackend, {}, {}, {}, {});
|
||||
const _backendSrv = new BackendSrv(_httpBackend, {}, {}, {});
|
||||
|
||||
describe('when handling errors', () => {
|
||||
it('should return the http status code', async () => {
|
||||
|
@ -11,7 +11,7 @@ const template = `
|
||||
`;
|
||||
|
||||
/** @ngInject */
|
||||
function uploadDashboardDirective(timer, alertSrv, $location) {
|
||||
function uploadDashboardDirective(timer, $location) {
|
||||
return {
|
||||
restrict: 'E',
|
||||
template: template,
|
||||
@ -59,7 +59,7 @@ function uploadDashboardDirective(timer, alertSrv, $location) {
|
||||
// Something
|
||||
elem[0].addEventListener('change', file_selected, false);
|
||||
} 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']);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user