2017-12-20 05:33:33 -06:00
|
|
|
import _ from 'lodash';
|
|
|
|
import { appEvents, coreModule } from 'app/core/core';
|
2016-06-16 07:29:20 -05:00
|
|
|
|
|
|
|
export class AlertNotificationEditCtrl {
|
2016-10-01 10:14:45 -05:00
|
|
|
theForm: any;
|
2017-06-02 07:00:42 -05:00
|
|
|
navModel: any;
|
2017-12-20 05:33:33 -06:00
|
|
|
testSeverity = 'critical';
|
2017-01-06 05:04:25 -06:00
|
|
|
notifiers: any;
|
|
|
|
notifierTemplateId: string;
|
2017-12-01 04:32:00 -06:00
|
|
|
isNew: boolean;
|
2017-01-06 05:04:25 -06:00
|
|
|
model: any;
|
|
|
|
defaults: any = {
|
2017-12-20 05:33:33 -06:00
|
|
|
type: 'email',
|
2018-06-05 03:27:29 -05:00
|
|
|
sendReminder: false,
|
2018-10-17 03:41:18 -05:00
|
|
|
disableResolveMessage: false,
|
2018-05-25 13:14:33 -05:00
|
|
|
frequency: '15m',
|
2017-01-06 05:04:25 -06:00
|
|
|
settings: {
|
2017-12-20 05:33:33 -06:00
|
|
|
httpMethod: 'POST',
|
2017-01-06 05:04:25 -06:00
|
|
|
autoResolve: true,
|
2017-12-20 05:33:33 -06:00
|
|
|
uploadImage: true,
|
2017-01-06 05:04:25 -06:00
|
|
|
},
|
2017-12-20 05:33:33 -06:00
|
|
|
isDefault: false,
|
2017-01-06 05:04:25 -06:00
|
|
|
};
|
2018-08-20 11:23:48 -05:00
|
|
|
getFrequencySuggestion: any;
|
2016-06-16 07:29:20 -05:00
|
|
|
|
|
|
|
/** @ngInject */
|
2017-12-21 01:39:31 -06:00
|
|
|
constructor(private $routeParams, private backendSrv, private $location, private $templateCache, navModelSrv) {
|
2017-12-20 05:33:33 -06:00
|
|
|
this.navModel = navModelSrv.getNav('alerting', 'channels', 0);
|
2017-12-01 04:32:00 -06:00
|
|
|
this.isNew = !this.$routeParams.id;
|
2017-06-02 07:00:42 -05:00
|
|
|
|
2018-08-20 11:23:48 -05:00
|
|
|
this.getFrequencySuggestion = () => {
|
|
|
|
return ['1m', '5m', '10m', '15m', '30m', '1h'];
|
|
|
|
};
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
this.backendSrv
|
|
|
|
.get(`/api/alert-notifiers`)
|
|
|
|
.then(notifiers => {
|
|
|
|
this.notifiers = notifiers;
|
|
|
|
|
|
|
|
// add option templates
|
2018-08-26 10:14:40 -05:00
|
|
|
for (const notifier of this.notifiers) {
|
2017-12-21 01:39:31 -06:00
|
|
|
this.$templateCache.put(this.getNotifierTemplateId(notifier.type), notifier.optionsTemplate);
|
2017-12-19 09:06:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.$routeParams.id) {
|
2017-12-20 05:33:33 -06:00
|
|
|
this.navModel.breadcrumbs.push({ text: 'New channel' });
|
|
|
|
this.navModel.node = { text: 'New channel' };
|
2017-12-19 09:06:54 -06:00
|
|
|
return _.defaults(this.model, this.defaults);
|
|
|
|
}
|
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
return this.backendSrv.get(`/api/alert-notifications/${this.$routeParams.id}`).then(result => {
|
|
|
|
this.navModel.breadcrumbs.push({ text: result.name });
|
|
|
|
this.navModel.node = { text: result.name };
|
2018-03-27 02:19:14 -05:00
|
|
|
result.settings = _.defaults(result.settings, this.defaults.settings);
|
2017-12-21 01:39:31 -06:00
|
|
|
return result;
|
|
|
|
});
|
2017-12-19 09:06:54 -06:00
|
|
|
})
|
|
|
|
.then(model => {
|
|
|
|
this.model = model;
|
|
|
|
this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
|
2017-01-06 05:04:25 -06:00
|
|
|
});
|
2016-06-16 07:29:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
2016-10-01 10:14:45 -05:00
|
|
|
if (!this.theForm.$valid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-25 09:26:28 -05:00
|
|
|
if (this.model.id) {
|
2018-02-15 09:47:30 -06:00
|
|
|
this.backendSrv
|
|
|
|
.put(`/api/alert-notifications/${this.model.id}`, this.model)
|
|
|
|
.then(res => {
|
|
|
|
this.model = res;
|
|
|
|
appEvents.emit('alert-success', ['Notification updated', '']);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
if (err.data && err.data.error) {
|
|
|
|
appEvents.emit('alert-error', [err.data.error]);
|
|
|
|
}
|
|
|
|
});
|
2016-06-16 07:29:20 -05:00
|
|
|
} else {
|
2018-02-15 09:47:30 -06:00
|
|
|
this.backendSrv
|
|
|
|
.post(`/api/alert-notifications`, this.model)
|
|
|
|
.then(res => {
|
|
|
|
appEvents.emit('alert-success', ['Notification created', '']);
|
|
|
|
this.$location.path('alerting/notifications');
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
if (err.data && err.data.error) {
|
|
|
|
appEvents.emit('alert-error', [err.data.error]);
|
|
|
|
}
|
|
|
|
});
|
2016-06-16 07:29:20 -05:00
|
|
|
}
|
|
|
|
}
|
2016-07-27 05:09:55 -05:00
|
|
|
|
2017-01-06 05:04:25 -06:00
|
|
|
getNotifierTemplateId(type) {
|
|
|
|
return `notifier-options-${type}`;
|
|
|
|
}
|
|
|
|
|
2016-07-27 05:09:55 -05:00
|
|
|
typeChanged() {
|
2018-03-27 02:19:14 -05:00
|
|
|
this.model.settings = _.defaults({}, this.defaults.settings);
|
2017-01-06 05:04:25 -06:00
|
|
|
this.notifierTemplateId = this.getNotifierTemplateId(this.model.type);
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|
2016-09-05 07:43:53 -05:00
|
|
|
|
|
|
|
testNotification() {
|
2016-10-01 10:14:45 -05:00
|
|
|
if (!this.theForm.$valid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-26 13:19:23 -05:00
|
|
|
const payload = {
|
2016-09-05 07:43:53 -05:00
|
|
|
name: this.model.name,
|
|
|
|
type: this.model.type,
|
2018-05-19 15:21:00 -05:00
|
|
|
frequency: this.model.frequency,
|
2017-12-20 05:33:33 -06:00
|
|
|
settings: this.model.settings,
|
2016-09-05 07:43:53 -05:00
|
|
|
};
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
this.backendSrv.post(`/api/alert-notifications/test`, payload).then(res => {
|
2017-12-20 05:33:33 -06:00
|
|
|
appEvents.emit('alert-success', ['Test notification sent', '']);
|
2017-01-06 05:04:25 -06:00
|
|
|
});
|
2016-09-05 07:43:53 -05:00
|
|
|
}
|
2016-06-16 07:29:20 -05:00
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
coreModule.controller('AlertNotificationEditCtrl', AlertNotificationEditCtrl);
|