manage_alert_notifications_controller.js 14 KB

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