alert_definitions_actions_controller.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 validator = require('utils/validator');
  19. App.MainAlertDefinitionActionsController = Em.ArrayController.extend({
  20. name: 'mainAlertDefinitionActionsController',
  21. /**
  22. * List of available actions for alert definitions
  23. * @type {{title: string, icon: string, action: string, showDivider: boolean}[]}
  24. */
  25. content: [
  26. /*{
  27. title: Em.I18n.t('alerts.actions.create'),
  28. icon: 'icon-plus',
  29. action: 'createNewAlertDefinition',
  30. showDivider: true
  31. },*/
  32. {
  33. title: Em.I18n.t('alerts.actions.manageGroups'),
  34. icon: 'icon-th-large',
  35. action: 'manageAlertGroups',
  36. showDivider: false
  37. },
  38. {
  39. title: Em.I18n.t('alerts.actions.manageNotifications'),
  40. icon: 'icon-envelope-alt',
  41. action: 'manageNotifications',
  42. showDivider: false
  43. },
  44. {
  45. title: Em.I18n.t('alerts.actions.manageSettings'),
  46. icon: 'icon-cogs',
  47. action: 'manageSettings',
  48. showDivider: false
  49. }
  50. ],
  51. /**
  52. * Common handler for menu item click
  53. * Call proper controller's method described in <code>action</code>-field (see <code>content</code>)
  54. * @param {object} event
  55. * @method actionHandler
  56. */
  57. actionHandler: function(event) {
  58. var menuElement = event.context,
  59. action = menuElement.action;
  60. if ('function' === Em.typeOf(Em.get(this, action))) {
  61. this[action]();
  62. }
  63. else {
  64. Em.assert('Invalid action provided - ' + action, false);
  65. }
  66. },
  67. /**
  68. * Start "Create Alert Definition" wizard
  69. * @method createNewAlertDefinition
  70. */
  71. createNewAlertDefinition: function() {
  72. App.router.transitionTo('alertAdd');
  73. },
  74. /**
  75. * Handler when clicking on "Manage Alert Groups", a popup will show up
  76. * @method manageAlertGroups
  77. * @return {App.ModalPopup}
  78. */
  79. manageAlertGroups: function () {
  80. return App.ModalPopup.show({
  81. header: Em.I18n.t('alerts.actions.manage_alert_groups_popup.header'),
  82. bodyClass: App.MainAlertsManageAlertGroupView.extend({
  83. controllerBinding: 'App.router.manageAlertGroupsController'
  84. }),
  85. classNames: ['sixty-percent-width-modal', 'manage-alert-group-popup'],
  86. primary: Em.I18n.t('common.save'),
  87. onPrimary: function () {
  88. var modifiedAlertGroups = this.get('subViewController.defsModifiedAlertGroups');
  89. var dataForSuccessPopup = {
  90. created: modifiedAlertGroups.toCreate.length,
  91. deleted: modifiedAlertGroups.toDelete.length,
  92. updated: modifiedAlertGroups.toSet.length
  93. };
  94. var showSuccessPopup = dataForSuccessPopup.created + dataForSuccessPopup.deleted + dataForSuccessPopup.updated > 0;
  95. // Save modified Alert-groups
  96. var self = this;
  97. var errors = [];
  98. var deleteQueriesCounter = modifiedAlertGroups.toDelete.length;
  99. var createQueriesCounter = modifiedAlertGroups.toCreate.length;
  100. var deleteQueriesRun = false;
  101. var createQueriesRun = false;
  102. var runNextQuery = function () {
  103. if (!deleteQueriesRun && deleteQueriesCounter > 0) {
  104. deleteQueriesRun = true;
  105. modifiedAlertGroups.toDelete.forEach(function (group) {
  106. self.get('subViewController').removeAlertGroup(group, finishFunction, finishFunction);
  107. }, this);
  108. } else if (!createQueriesRun && deleteQueriesCounter < 1) {
  109. createQueriesRun = true;
  110. modifiedAlertGroups.toSet.forEach(function (group) {
  111. self.get('subViewController').updateAlertGroup(group, finishFunction, finishFunction);
  112. }, this);
  113. modifiedAlertGroups.toCreate.forEach(function (group) {
  114. self.get('subViewController').postNewAlertGroup(group, finishFunction);
  115. }, this);
  116. }
  117. };
  118. var finishFunction = function (xhr, text, errorThrown) {
  119. if (xhr && errorThrown) {
  120. var error = xhr.status + "(" + errorThrown + ") ";
  121. try {
  122. var json = $.parseJSON(xhr.responseText);
  123. error += json.message;
  124. } catch (err) {
  125. }
  126. errors.push(error);
  127. }
  128. if (createQueriesRun) {
  129. createQueriesCounter--;
  130. } else {
  131. deleteQueriesCounter--;
  132. }
  133. if (deleteQueriesCounter + createQueriesCounter < 1) {
  134. if (errors.length > 0) {
  135. self.get('subViewController').set('errorMessage', errors.join(". "));
  136. }
  137. else {
  138. self.hide();
  139. if (showSuccessPopup) {
  140. App.ModalPopup.show({
  141. secondary: null,
  142. header: Em.I18n.t('alerts.groups.successPopup.header'),
  143. bodyClass: Em.View.extend({
  144. dataForSuccessPopup: dataForSuccessPopup,
  145. templateName: require('templates/main/alerts/alert_groups/success_popup_body')
  146. })
  147. });
  148. }
  149. App.router.get('updateController').updateAlertGroups(function () {
  150. App.router.get('manageAlertGroupsController').toggleProperty('changeTrigger');
  151. App.router.get('updateController').updateAlertDefinitions(function() {
  152. App.router.get('updateController').updateAlertNotifications(Em.K);
  153. });
  154. });
  155. }
  156. } else {
  157. runNextQuery();
  158. }
  159. };
  160. runNextQuery();
  161. },
  162. subViewController: function () {
  163. return App.router.get('manageAlertGroupsController');
  164. }.property('App.router.manageAlertGroupsController'),
  165. disablePrimary: Em.computed.not('subViewController.isDefsModified'),
  166. didInsertElement: function () {
  167. this.fitZIndex();
  168. }
  169. });
  170. },
  171. /**
  172. * "Manage Alert Notifications" handler
  173. * @method manageNotifications
  174. * @return {App.ModalPopup}
  175. */
  176. manageNotifications: function () {
  177. return App.ModalPopup.show({
  178. header: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.header'),
  179. bodyClass: App.ManageAlertNotificationsView.extend({
  180. controllerBinding: 'App.router.manageAlertNotificationsController'
  181. }),
  182. classNames: ['sixty-percent-width-modal', 'manage-configuration-group-popup'],
  183. secondary: null,
  184. primary: Em.I18n.t('common.close'),
  185. autoHeight: false
  186. });
  187. },
  188. /**
  189. * "Manage Alert Settings" handler
  190. * @method manageSettings
  191. * @return {App.ModalPopup}
  192. */
  193. manageSettings: function () {
  194. var controller = this;
  195. var configProperties = App.router.get('clusterController.clusterEnv.properties');
  196. return App.ModalPopup.show({
  197. classNames: ['fourty-percent-width-modal'],
  198. header: Em.I18n.t('alerts.actions.manageSettings'),
  199. primary: Em.I18n.t('common.save'),
  200. secondary: Em.I18n.t('common.cancel'),
  201. inputValue: configProperties.alerts_repeat_tolerance || '1',
  202. errorMessage: Em.I18n.t('alerts.actions.editRepeatTolerance.error'),
  203. isInvalid: function () {
  204. var intValue = Number(this.get('inputValue'));
  205. return this.get('inputValue') !== 'DEBUG' && (!validator.isValidInt(intValue) || intValue < 1 || intValue > 99);
  206. }.property('inputValue'),
  207. disablePrimary: Em.computed.alias('isInvalid'),
  208. onPrimary: function () {
  209. if (this.get('isInvalid')) {
  210. return;
  211. }
  212. configProperties.alerts_repeat_tolerance = this.get('inputValue');
  213. App.ajax.send({
  214. name: 'admin.save_configs',
  215. sender: controller,
  216. data: {
  217. siteName: 'cluster-env',
  218. properties: configProperties
  219. },
  220. error: 'manageSettingsErrorCallback'
  221. });
  222. this.hide();
  223. },
  224. bodyClass: Ember.View.extend({
  225. templateName: require('templates/common/modal_popups/prompt_popup'),
  226. title: Em.I18n.t('alerts.actions.editRepeatTolerance.title'),
  227. description: Em.I18n.t('alerts.actions.editRepeatTolerance.description'),
  228. label: Em.I18n.t('alerts.actions.editRepeatTolerance.label')
  229. })
  230. });
  231. },
  232. manageSettingsErrorCallback: function(data) {
  233. var error = Em.I18n.t('alerts.actions.manageSettings.error');
  234. if(data && data.responseText){
  235. try {
  236. var json = $.parseJSON(data.responseText);
  237. error += json.message;
  238. } catch (err) {
  239. }
  240. }
  241. App.showAlertPopup(Em.I18n.t('alerts.actions.manageSettings.error'), error);
  242. }
  243. });