manage_alert_notifications_controller.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. var validator = require('utils/validator');
  20. App.ManageAlertNotificationsController = Em.Controller.extend({
  21. name: 'manageAlertNotificationsController',
  22. /**
  23. * Are alert notifications loaded
  24. * @type {boolean}
  25. */
  26. isLoaded: false,
  27. /**
  28. * Create/Edit modal popup object
  29. * used to hide popup
  30. * @type {App.ModalPopup}
  31. */
  32. createEditPopup: null,
  33. /**
  34. * Map of edit inputs shown in Create/Edit Notification popup
  35. * @type {Object}
  36. */
  37. inputFields: Em.Object.create({
  38. name: {
  39. label: Em.I18n.t('common.name'),
  40. value: '',
  41. defaultValue: ''
  42. },
  43. groups: {
  44. label: Em.I18n.t('common.groups'),
  45. value: '',
  46. defaultValue: ''
  47. },
  48. method: {
  49. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.method'),
  50. value: '',
  51. defaultValue: ''
  52. },
  53. email: {
  54. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.email'),
  55. value: '',
  56. defaultValue: ''
  57. },
  58. severityFilter: {
  59. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.severityFilter'),
  60. value: [true, true, true, true],
  61. defaultValue: [true, true, true, true]
  62. },
  63. description: {
  64. label: Em.I18n.t('common.description'),
  65. value: '',
  66. defaultValue: ''
  67. },
  68. customProperties: Em.A([])
  69. }),
  70. /**
  71. * List of available Notification types
  72. * used in Type combobox
  73. * @type {Array}
  74. */
  75. methods: ['EMAIL', 'SNMP'],
  76. /**
  77. * List of all Alert Notifications
  78. * @type {App.AlertNotification[]}
  79. */
  80. alertNotifications: function () {
  81. return this.get('isLoaded') ? App.AlertNotification.find().toArray() : [];
  82. }.property('isLoaded'),
  83. /**
  84. * Selected Alert Notification
  85. * @type {App.AlertNotification}
  86. */
  87. selectedAlertNotification: null,
  88. /**
  89. * Addable to <code>selectedAlertNotification.properties</code> custom property
  90. * @type {{name: string, value: string}}
  91. */
  92. newCustomProperty: {name: '', value: ''},
  93. /**
  94. * List custom property names that shouldn't be displayed on Edit page
  95. * @type {string[]}
  96. */
  97. ignoredCustomProperties: ['ambari.dispatch.recipients'],
  98. /**
  99. * Load all Alert Notifications from server
  100. * Don't do anything if controller not isLoaded
  101. * @returns {$.ajax|null}
  102. */
  103. loadAlertNotifications: function () {
  104. this.set('isLoaded', false);
  105. return App.ajax.send({
  106. name: 'alerts.notifications',
  107. sender: this,
  108. success: 'getAlertNotificationsSuccessCallback',
  109. error: 'getAlertNotificationsErrorCallback'
  110. });
  111. },
  112. /**
  113. * Success-callback for load alert notifications request
  114. * @param {object} json
  115. * @method getAlertNotificationsSuccessCallback
  116. */
  117. getAlertNotificationsSuccessCallback: function (json) {
  118. App.alertNotificationMapper.map(json);
  119. this.set('isLoaded', true);
  120. },
  121. /**
  122. * Error-callback for load alert notifications request
  123. * @method getAlertNotificationsErrorCallback
  124. */
  125. getAlertNotificationsErrorCallback: function () {
  126. this.set('isLoaded', true);
  127. },
  128. /**
  129. * Add Notification button handler
  130. * @method addAlertNotification
  131. */
  132. addAlertNotification: function () {
  133. var inputFields = this.get('inputFields');
  134. Em.keys(inputFields).forEach(function (key) {
  135. inputFields.set(key + '.value', inputFields.get(key + '.defaultValue'));
  136. });
  137. this.showCreateEditPopup(false);
  138. },
  139. /**
  140. * Edit Notification button handler
  141. * @method editAlertNotification
  142. */
  143. editAlertNotification: function () {
  144. this.fillEditCreateInputs();
  145. this.showCreateEditPopup(true);
  146. },
  147. /**
  148. * Fill inputs of Create/Edit popup form
  149. * @param addCopyToName define whether add 'Copy of ' to name
  150. * @method fillEditCreateInputs
  151. */
  152. fillEditCreateInputs: function (addCopyToName) {
  153. var inputFields = this.get('inputFields');
  154. var selectedAlertNotification = this.get('selectedAlertNotification');
  155. inputFields.set('name.value', (addCopyToName ? 'Copy of ' : '') + selectedAlertNotification.get('name'));
  156. inputFields.set('email.value', selectedAlertNotification.get('properties')['ambari.dispatch.recipients'] ?
  157. selectedAlertNotification.get('properties')['ambari.dispatch.recipients'].join(', ') : '');
  158. inputFields.set('severityFilter.value', [
  159. selectedAlertNotification.get('alertStates').contains('OK'),
  160. selectedAlertNotification.get('alertStates').contains('WARNING'),
  161. selectedAlertNotification.get('alertStates').contains('CRITICAL'),
  162. selectedAlertNotification.get('alertStates').contains('UNKNOWN')
  163. ]);
  164. inputFields.set('description.value', selectedAlertNotification.get('description'));
  165. inputFields.set('method.value', selectedAlertNotification.get('type'));
  166. inputFields.get('customProperties').clear();
  167. var properties = selectedAlertNotification.get('properties');
  168. var ignoredCustomProperties = this.get('ignoredCustomProperties');
  169. Em.keys(properties).forEach(function (k) {
  170. if (ignoredCustomProperties.contains(k)) return;
  171. inputFields.get('customProperties').pushObject({
  172. name: k,
  173. value: properties[k],
  174. defaultValue: properties[k]
  175. });
  176. });
  177. },
  178. /**
  179. * Show Edit or Create Notification popup
  180. * @param {boolean} isEdit true - edit, false - create
  181. * @returns {App.ModalPopup}
  182. * @method showCreateEditPopup
  183. */
  184. showCreateEditPopup: function (isEdit) {
  185. var self = this;
  186. var createEditPopup = App.ModalPopup.show({
  187. header: isEdit ? Em.I18n.t('alerts.actions.manage_alert_notifications_popup.editHeader') : Em.I18n.t('alerts.actions.manage_alert_notifications_popup.addHeader'),
  188. bodyClass: Em.View.extend({
  189. controller: this,
  190. templateName: require('templates/main/alerts/create_alert_notification'),
  191. isEmailMethodSelected: function () {
  192. return this.get('controller.inputFields.method.value') === 'EMAIL';
  193. }.property('controller.inputFields.method.value')
  194. }),
  195. primary: Em.I18n.t('common.save'),
  196. onPrimary: function () {
  197. this.set('disablePrimary', true);
  198. var apiObject = self.formatNotificationAPIObject();
  199. if (isEdit) {
  200. self.updateAlertNotification(apiObject);
  201. } else {
  202. self.createAlertNotification(apiObject);
  203. }
  204. }
  205. });
  206. this.set('createEditPopup', createEditPopup);
  207. return createEditPopup;
  208. },
  209. /**
  210. * Create API-formatted object from data populate by user
  211. * @returns {Object}
  212. * @method formatNotificationAPIObject
  213. */
  214. formatNotificationAPIObject: function () {
  215. var inputFields = this.get('inputFields');
  216. var alertStates = [];
  217. var properties = {};
  218. if (inputFields.get('severityFilter.value')[0]) {
  219. alertStates.push('OK');
  220. }
  221. if (inputFields.get('severityFilter.value')[1]) {
  222. alertStates.push('WARNING');
  223. }
  224. if (inputFields.get('severityFilter.value')[2]) {
  225. alertStates.push('CRITICAL');
  226. }
  227. if (inputFields.get('severityFilter.value')[3]) {
  228. alertStates.push('UNKNOWN');
  229. }
  230. if (inputFields.get('method.value') === 'EMAIL') {
  231. properties['ambari.dispatch.recipients'] = inputFields.get('email.value').replace(/\s/g, '').split(',');
  232. }
  233. inputFields.get('customProperties').forEach(function (customProperty) {
  234. properties[customProperty.name] = customProperty.value;
  235. });
  236. return {
  237. AlertTarget: {
  238. name: inputFields.get('name.value'),
  239. description: inputFields.get('description.value'),
  240. notification_type: inputFields.get('method.value'),
  241. alert_states: alertStates,
  242. properties: properties
  243. }
  244. };
  245. },
  246. /**
  247. * Send request to server to create Alert Notification
  248. * @param {object} apiObject (@see formatNotificationAPIObject)
  249. * @returns {$.ajax}
  250. * @method createAlertNotification
  251. */
  252. createAlertNotification: function (apiObject) {
  253. return App.ajax.send({
  254. name: 'alerts.create_alert_notification',
  255. sender: this,
  256. data: {
  257. data: apiObject
  258. },
  259. success: 'createAlertNotificationSuccessCallback'
  260. });
  261. },
  262. /**
  263. * Success callback for <code>createAlertNotification</code>
  264. * @method createAlertNotificationSuccessCallback
  265. */
  266. createAlertNotificationSuccessCallback: function () {
  267. this.loadAlertNotifications();
  268. var createEditPopup = this.get('createEditPopup');
  269. if (createEditPopup) {
  270. createEditPopup.hide();
  271. }
  272. },
  273. /**
  274. * Send request to server to update Alert Notification
  275. * @param {object} apiObject (@see formatNotificationAPIObject)
  276. * @returns {$.ajax}
  277. * @method updateAlertNotification
  278. */
  279. updateAlertNotification: function (apiObject) {
  280. return App.ajax.send({
  281. name: 'alerts.update_alert_notification',
  282. sender: this,
  283. data: {
  284. data: apiObject,
  285. id: this.get('selectedAlertNotification.id')
  286. },
  287. success: 'updateAlertNotificationSuccessCallback'
  288. });
  289. },
  290. /**
  291. * Success callback for <code>updateAlertNotification</code>
  292. * @method updateAlertNotificationSuccessCallback
  293. */
  294. updateAlertNotificationSuccessCallback: function () {
  295. this.loadAlertNotifications();
  296. var createEditPopup = this.get('createEditPopup');
  297. if (createEditPopup) {
  298. createEditPopup.hide();
  299. }
  300. },
  301. /**
  302. * Delete Notification button handler
  303. * @return {App.ModalPopup}
  304. * @method deleteAlertNotification
  305. */
  306. deleteAlertNotification: function () {
  307. var self = this;
  308. return App.showConfirmationPopup(function () {
  309. App.ajax.send({
  310. name: 'alerts.delete_alert_notification',
  311. sender: self,
  312. data: {
  313. id: self.get('selectedAlertNotification.id')
  314. },
  315. success: 'deleteAlertNotificationSuccessCallback'
  316. });
  317. });
  318. },
  319. /**
  320. * Success callback for <code>deleteAlertNotification</code>
  321. * @method deleteAlertNotificationSuccessCallback
  322. */
  323. deleteAlertNotificationSuccessCallback: function () {
  324. this.loadAlertNotifications();
  325. var selectedAlertNotification = this.get('selectedAlertNotification');
  326. selectedAlertNotification.deleteRecord();
  327. this.set('selectedAlertNotification', null);
  328. },
  329. /**
  330. * Duplicate Notification button handler
  331. * @method duplicateAlertNotification
  332. */
  333. duplicateAlertNotification: function () {
  334. this.fillEditCreateInputs(true);
  335. this.showCreateEditPopup();
  336. },
  337. /**
  338. * Show popup with form for new custom property
  339. * @method addCustomPropertyHandler
  340. * @return {App.ModalPopup}
  341. */
  342. addCustomPropertyHandler: function () {
  343. var self = this;
  344. return App.ModalPopup.show({
  345. header: Em.I18n.t('alerts.notifications.addCustomPropertyPopup.header'),
  346. primary: Em.I18n.t('common.add'),
  347. bodyClass: Em.View.extend({
  348. /**
  349. * If some error with new custom property
  350. * @type {boolean}
  351. */
  352. isError: false,
  353. controller: this,
  354. /**
  355. * Error message for new custom property (invalid name, existed name etc)
  356. * @type {string}
  357. */
  358. errorMessage: '',
  359. /**
  360. * Check new custom property for errors with its name
  361. * @method errorHandler
  362. */
  363. errorsHandler: function () {
  364. var name = this.get('controller.newCustomProperty.name');
  365. var flag = validator.isValidConfigKey(name);
  366. if (flag) {
  367. if (this.get('controller.inputFields.customProperties').mapProperty('name').contains(name) ||
  368. this.get('controller.ignoredCustomProperties').contains(name)) {
  369. this.set('errorMessage', Em.I18n.t('alerts.notifications.addCustomPropertyPopup.error.propertyExists'));
  370. flag = false;
  371. }
  372. }
  373. else {
  374. this.set('errorMessage', Em.I18n.t('alerts.notifications.addCustomPropertyPopup.error.invalidPropertyName'));
  375. }
  376. this.set('isError', !flag);
  377. this.set('parentView.disablePrimary', !flag);
  378. }.observes('controller.newCustomProperty.name'),
  379. templateName: require('templates/main/alerts/add_custom_config_to_alert_notification_popup')
  380. }),
  381. onPrimary: function () {
  382. self.addCustomProperty();
  383. self.set('newCustomProperty', {name: '', value: ''}); // cleanup
  384. this.hide();
  385. }
  386. });
  387. },
  388. /**
  389. * Add Custom Property to <code>selectedAlertNotification</code> (push it to <code>properties</code>-field)
  390. * @method addCustomProperty
  391. */
  392. addCustomProperty: function () {
  393. var newCustomProperty = this.get('newCustomProperty');
  394. this.get('inputFields.customProperties').pushObject({
  395. name: newCustomProperty.name,
  396. value: newCustomProperty.value,
  397. defaultValue: newCustomProperty.value
  398. });
  399. },
  400. /**
  401. * "Remove"-button click handler
  402. * Delete customProperty from <code>inputFields.customProperties</code>
  403. * @param {object} e
  404. * @method removeCustomProperty
  405. */
  406. removeCustomPropertyHandler: function (e) {
  407. var customProperties = this.get('inputFields.customProperties');
  408. this.set('inputFields.customProperties', customProperties.without(e.context));
  409. }
  410. });