manage_alert_notifications_controller.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. App.ManageAlertNotificationsController = Em.Controller.extend({
  20. name: 'manageAlertNotificationsController',
  21. isLoaded: false,
  22. /**
  23. * Create/Edit modal popup object
  24. * used to hide popup
  25. * @type {App.ModalPopup}
  26. */
  27. createEditPopup: null,
  28. /**
  29. * Map of edit inputs shown in Create/Edit Notification popup
  30. * @type {Object}
  31. */
  32. inputFields: Em.Object.create({
  33. name: {
  34. label: Em.I18n.t('common.name'),
  35. value: '',
  36. defaultValue: ''
  37. },
  38. groups: {
  39. label: Em.I18n.t('common.groups'),
  40. value: '',
  41. defaultValue: ''
  42. },
  43. method: {
  44. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.method'),
  45. value: '',
  46. defaultValue: ''
  47. },
  48. email: {
  49. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.email'),
  50. value: '',
  51. defaultValue: ''
  52. },
  53. severityFilter: {
  54. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.severityFilter'),
  55. value: [true, true, true, true],
  56. defaultValue: [true, true, true, true]
  57. },
  58. description: {
  59. label: Em.I18n.t('common.description'),
  60. value: '',
  61. defaultValue: ''
  62. }
  63. }),
  64. /**
  65. * List of available Notification types
  66. * used in Type combobox
  67. * @type {Array}
  68. */
  69. methods: ['EMAIL', 'SNMP'],
  70. /**
  71. * List of all Alert Notifications
  72. * @type {App.AlertNotification[]}
  73. */
  74. alertNotifications: function () {
  75. if (this.get('isLoaded')) {
  76. return App.AlertNotification.find().toArray();
  77. }
  78. return [];
  79. }.property('isLoaded'),
  80. /**
  81. * Selected Alert Notification
  82. * @type {App.AlertNotification}
  83. */
  84. selectedAlertNotification: null,
  85. /**
  86. * Load all Alert Notifications from server
  87. * Don't do anything if controller not isLoaded
  88. * @returns {$.ajax|null}
  89. */
  90. loadAlertNotifications: function () {
  91. this.set('isLoaded', false);
  92. return App.ajax.send({
  93. name: 'alerts.notifications',
  94. sender: this,
  95. success: 'getAlertNotificationsSuccessCallback',
  96. error: 'getAlertNotificationsErrorCallback'
  97. });
  98. },
  99. /**
  100. * Success-callback for load alert notifications request
  101. * @param {object} json
  102. * @method getAlertNotificationsSuccessCallback
  103. */
  104. getAlertNotificationsSuccessCallback: function (json) {
  105. App.alertNotificationMapper.map(json);
  106. this.set('isLoaded', true);
  107. },
  108. /**
  109. * Error-callback for load alert notifications request
  110. * @method getAlertNotificationsErrorCallback
  111. */
  112. getAlertNotificationsErrorCallback: function () {
  113. this.set('isLoaded', true);
  114. },
  115. /**
  116. * Add Notification button handler
  117. */
  118. addAlertNotification: function () {
  119. var inputFields = this.get('inputFields');
  120. Em.keys(inputFields).forEach(function (key) {
  121. inputFields.set(key + '.value', inputFields.get(key + '.defaultValue'));
  122. });
  123. this.showCreateEditPopup(false);
  124. },
  125. /**
  126. * Edit Notification button handler
  127. */
  128. editAlertNotification: function () {
  129. this.fillEditCreateInputs();
  130. this.showCreateEditPopup(true);
  131. },
  132. /**
  133. * Fill inputs of Create/Edit popup form
  134. * @param addCopyToName define whether add 'Copy of ' to name
  135. */
  136. fillEditCreateInputs: function (addCopyToName) {
  137. var inputFields = this.get('inputFields');
  138. var selectedAlertNotification = this.get('selectedAlertNotification');
  139. inputFields.set('name.value', (addCopyToName ? 'Copy of ' : '') + selectedAlertNotification.get('name'));
  140. inputFields.set('email.value', selectedAlertNotification.get('properties')['ambari.dispatch.recipients'] ?
  141. selectedAlertNotification.get('properties')['ambari.dispatch.recipients'].join(', ') : '');
  142. inputFields.set('severityFilter.value', [
  143. selectedAlertNotification.get('alertStates').contains('OK'),
  144. selectedAlertNotification.get('alertStates').contains('WARNING'),
  145. selectedAlertNotification.get('alertStates').contains('CRITICAL'),
  146. selectedAlertNotification.get('alertStates').contains('UNKNOWN')
  147. ]);
  148. inputFields.set('description.value', selectedAlertNotification.get('description'));
  149. inputFields.set('method.value', selectedAlertNotification.get('type'));
  150. },
  151. /**
  152. * Show Edit or Create Notification popup
  153. * @param isEdit
  154. * @returns {App.ModalPopup}
  155. */
  156. showCreateEditPopup: function (isEdit) {
  157. var self = this;
  158. var createEditPopup = App.ModalPopup.show({
  159. header: isEdit ? Em.I18n.t('alerts.actions.manage_alert_notifications_popup.editHeader') : Em.I18n.t('alerts.actions.manage_alert_notifications_popup.addHeader'),
  160. bodyClass: Em.View.extend({
  161. controller: this,
  162. templateName: require('templates/main/alerts/create_alert_notification'),
  163. isEmailMethodSelected: function () {
  164. return this.get('controller.inputFields.method.value') === 'EMAIL';
  165. }.property('controller.inputFields.method.value')
  166. }),
  167. primary: Em.I18n.t('common.save'),
  168. onPrimary: function () {
  169. this.set('disablePrimary', true);
  170. var apiObject = self.formatNotificationAPIObject();
  171. if (isEdit) {
  172. self.updateAlertNotification(apiObject);
  173. } else {
  174. self.createAlertNotification(apiObject);
  175. }
  176. }
  177. });
  178. this.set('createEditPopup', createEditPopup);
  179. return createEditPopup;
  180. },
  181. /**
  182. * Create API-formatted object from data populate by user
  183. * @returns {Object}
  184. */
  185. formatNotificationAPIObject: function () {
  186. var inputFields = this.get('inputFields');
  187. var alertStates = [];
  188. var properties = {};
  189. if (inputFields.severityFilter.value[0]) {
  190. alertStates.push('OK');
  191. }
  192. if (inputFields.severityFilter.value[1]) {
  193. alertStates.push('WARNING');
  194. }
  195. if (inputFields.severityFilter.value[2]) {
  196. alertStates.push('CRITICAL');
  197. }
  198. if (inputFields.severityFilter.value[3]) {
  199. alertStates.push('UNKNOWN');
  200. }
  201. if (inputFields.method.value === 'EMAIL') {
  202. properties['ambari.dispatch.recipients'] = inputFields.email.value.replace(/\s/g, '').split(',');
  203. }
  204. return {
  205. AlertTarget: {
  206. name: inputFields.name.value,
  207. description: inputFields.description.value,
  208. notification_type: inputFields.method.value,
  209. alert_states: alertStates,
  210. properties: properties
  211. }
  212. };
  213. },
  214. /**
  215. * Send request to server to create Alert Notification
  216. * @param apiObject
  217. * @returns {$.ajax}
  218. */
  219. createAlertNotification: function (apiObject) {
  220. return App.ajax.send({
  221. name: 'alerts.create_alert_notification',
  222. sender: this,
  223. data: {
  224. data: apiObject
  225. },
  226. success: 'createAlertNotificationSuccessCallback'
  227. });
  228. },
  229. /**
  230. * Success callback for <code>createAlertNotification</code>
  231. */
  232. createAlertNotificationSuccessCallback: function () {
  233. this.loadAlertNotifications();
  234. var createEditPopup = this.get('createEditPopup');
  235. if (createEditPopup) {
  236. createEditPopup.hide();
  237. }
  238. },
  239. /**
  240. * Send request to server to update Alert Notification
  241. * @param apiObject
  242. * @returns {$.ajax}
  243. */
  244. updateAlertNotification: function (apiObject) {
  245. return App.ajax.send({
  246. name: 'alerts.update_alert_notification',
  247. sender: this,
  248. data: {
  249. data: apiObject,
  250. id: this.get('selectedAlertNotification.id')
  251. },
  252. success: 'updateAlertNotificationSuccessCallback'
  253. });
  254. },
  255. /**
  256. * Success callback for <code>updateAlertNotification</code>
  257. */
  258. updateAlertNotificationSuccessCallback: function () {
  259. this.loadAlertNotifications();
  260. var createEditPopup = this.get('createEditPopup');
  261. if (createEditPopup) {
  262. createEditPopup.hide();
  263. }
  264. },
  265. /**
  266. * Delete Notification button handler
  267. */
  268. deleteAlertNotification: function () {
  269. var self = this;
  270. return App.showConfirmationPopup(function () {
  271. App.ajax.send({
  272. name: 'alerts.delete_alert_notification',
  273. sender: self,
  274. data: {
  275. id: self.get('selectedAlertNotification.id')
  276. },
  277. success: 'deleteAlertNotificationSuccessCallback'
  278. });
  279. });
  280. },
  281. /**
  282. * Success callback for <code>deleteAlertNotification</code>
  283. */
  284. deleteAlertNotificationSuccessCallback: function () {
  285. this.loadAlertNotifications();
  286. var selectedAlertNotification = this.get('selectedAlertNotification');
  287. selectedAlertNotification.deleteRecord();
  288. this.set('selectedAlertNotification', null);
  289. },
  290. /**
  291. * Duplicate Notification button handler
  292. */
  293. duplicateAlertNotification: function () {
  294. this.fillEditCreateInputs(true);
  295. this.showCreateEditPopup();
  296. }
  297. });