manage_alert_notifications_controller.js 16 KB

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