definition_details_controller.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. App.MainAlertDefinitionDetailsController = Em.Controller.extend({
  19. name: 'mainAlertDefinitionDetailsController',
  20. alerts: function () {
  21. return App.AlertInstanceLocal.find().toArray()
  22. .filterProperty('definitionId', this.get('content.id'));
  23. }.property('App.router.mainAlertInstancesController.isLoaded', 'App.router.mainAlertInstancesController.reload'),
  24. // stores object with editing form data (label)
  25. editing: Em.Object.create({
  26. label: Em.Object.create({
  27. name: 'label',
  28. isEditing: false,
  29. value: '',
  30. originalValue: '',
  31. isError: false,
  32. bindingValue: 'content.label'
  33. })
  34. }),
  35. /**
  36. * Host to count of alerts on this host during last day map
  37. * @type {Object}
  38. */
  39. lastDayAlertsCount: null,
  40. /**
  41. * Define if let user leave the page
  42. * @type {Boolean}
  43. */
  44. forceTransition: false,
  45. /**
  46. * List of all group names related to alert definition
  47. * @type {Array}
  48. */
  49. groupsList: Em.computed.mapBy('content.groups', 'displayName'),
  50. /**
  51. * Validation function to define if label field populated correctly
  52. * @method labelValidation
  53. */
  54. labelValidation: function () {
  55. this.set('editing.label.isError', !this.get('editing.label.value').trim());
  56. }.observes('editing.label.value'),
  57. /**
  58. * Set init values for variables
  59. */
  60. clearStep: function () {
  61. var editing = this.get('editing');
  62. Em.keys(editing).forEach(function (key) {
  63. editing.get(key).set('isEditing', false);
  64. });
  65. },
  66. /**
  67. * Load alert instances for current alertDefinition
  68. * Start updating loaded data
  69. * @method loadAlertInstances
  70. */
  71. loadAlertInstances: function () {
  72. App.router.get('mainAlertInstancesController').loadAlertInstancesByAlertDefinition(this.get('content.id'));
  73. App.router.set('mainAlertInstancesController.isUpdating', true);
  74. this.loadAlertInstancesHistory();
  75. },
  76. /**
  77. * Load alert instances history data
  78. * used to count instances number of the last 24 hour
  79. * @method loadAlertInstancesHistory
  80. */
  81. loadAlertInstancesHistory: function () {
  82. this.set('lastDayAlertsCount', null);
  83. return App.ajax.send({
  84. name: 'alerts.get_instances_history',
  85. sender: this,
  86. data: {
  87. definitionName: this.get('content.name'),
  88. timestamp: App.dateTime() - 86400000 // timestamp for time 24-hours ago
  89. },
  90. success: 'loadAlertInstancesHistorySuccess'
  91. });
  92. },
  93. /**
  94. * Success-callback for <code>loadAlertInstancesHistory</code>
  95. */
  96. loadAlertInstancesHistorySuccess: function (data) {
  97. var lastDayAlertsCount = {};
  98. data.items.forEach(function (alert) {
  99. if (!lastDayAlertsCount[alert.AlertHistory.host_name]) {
  100. lastDayAlertsCount[alert.AlertHistory.host_name] = 1;
  101. } else {
  102. lastDayAlertsCount[alert.AlertHistory.host_name] += 1;
  103. }
  104. });
  105. this.set('lastDayAlertsCount', lastDayAlertsCount);
  106. },
  107. /**
  108. * Edit button handler
  109. * @param {object} event
  110. * @method edit
  111. */
  112. edit: function (event) {
  113. var element = event.context;
  114. var value = this.get(element.get('bindingValue'));
  115. element.set('originalValue', value);
  116. element.set('value', value);
  117. element.set('isEditing', true);
  118. },
  119. /**
  120. * Cancel button handler
  121. * @param {object} event
  122. * @method cancelEdit
  123. */
  124. cancelEdit: function (event) {
  125. var element = event.context;
  126. element.set('value', element.get('originalValue'));
  127. element.set('isEditing', false);
  128. },
  129. /**
  130. * Save button handler, could save label of alert definition
  131. * @param {object} event
  132. * @returns {$.ajax}
  133. * @method saveEdit
  134. */
  135. saveEdit: function (event) {
  136. var element = event.context;
  137. this.set(element.get('bindingValue'), element.get('value'));
  138. element.set('isEditing', false);
  139. var data = Em.Object.create({});
  140. var property_name = "AlertDefinition/" + element.get('name');
  141. data.set(property_name, element.get('value'));
  142. var alertDefinition_id = this.get('content.id');
  143. return App.ajax.send({
  144. name: 'alerts.update_alert_definition',
  145. sender: this,
  146. data: {
  147. id: alertDefinition_id,
  148. data: data
  149. }
  150. });
  151. },
  152. /**
  153. * Onclick handler for save button on Save popup
  154. * Save changes of label and configs
  155. */
  156. saveLabelAndConfigs: function () {
  157. var configsController = App.router.get('mainAlertDefinitionConfigsController');
  158. if (configsController.get('canEdit')) {
  159. configsController.saveConfigs();
  160. }
  161. if (this.get('editing.label.isEditing')) {
  162. this.saveEdit({
  163. context: this.get('editing.label')
  164. });
  165. }
  166. },
  167. /**
  168. * "Delete" button handler
  169. * @param {object} event
  170. * @method deleteAlertDefinition
  171. */
  172. deleteAlertDefinition: function (event) {
  173. var alertDefinition = this.get('content');
  174. var self = this;
  175. App.showConfirmationPopup(function () {
  176. App.ajax.send({
  177. name: 'alerts.delete_alert_definition',
  178. sender: self,
  179. success: 'deleteAlertDefinitionSuccess',
  180. error: 'deleteAlertDefinitionError',
  181. data: {
  182. id: alertDefinition.get('id')
  183. }
  184. });
  185. }, null, function () {
  186. });
  187. },
  188. /**
  189. * Success-callback for <code>deleteAlertDefinition</code>
  190. * @method deleteAlertDefinitionSuccess
  191. */
  192. deleteAlertDefinitionSuccess: function () {
  193. App.router.transitionTo('main.alerts.index');
  194. },
  195. /**
  196. * Error-callback for <code>deleteAlertDefinition</code>
  197. * @method deleteAlertDefinitionError
  198. */
  199. deleteAlertDefinitionError: function (xhr, textStatus, errorThrown, opt) {
  200. xhr.responseText = "{\"message\": \"" + xhr.statusText + "\"}";
  201. App.ajax.defaultErrorHandler(xhr, opt.url, 'DELETE', xhr.status);
  202. },
  203. /**
  204. * "Disable / Enable" button handler
  205. * @method toggleState
  206. */
  207. toggleState: function () {
  208. var alertDefinition = this.get('content');
  209. var self = this;
  210. var bodyMessage = Em.Object.create({
  211. confirmMsg: alertDefinition.get('enabled') ? Em.I18n.t('alerts.table.state.enabled.confirm.msg') : Em.I18n.t('alerts.table.state.disabled.confirm.msg'),
  212. confirmButton: alertDefinition.get('enabled') ? Em.I18n.t('alerts.table.state.enabled.confirm.btn') : Em.I18n.t('alerts.table.state.disabled.confirm.btn')
  213. });
  214. return App.showConfirmationFeedBackPopup(function (query) {
  215. self.toggleDefinitionState(alertDefinition);
  216. }, bodyMessage);
  217. },
  218. /**
  219. * Enable/disable alertDefinition
  220. * @param {object} alertDefinition
  221. * @returns {$.ajax}
  222. * @method toggleDefinitionState
  223. */
  224. toggleDefinitionState: function (alertDefinition) {
  225. var newState = !alertDefinition.get('enabled');
  226. alertDefinition.set('enabled', newState);
  227. return App.ajax.send({
  228. name: 'alerts.update_alert_definition',
  229. sender: this,
  230. data: {
  231. id: alertDefinition.get('id'),
  232. data: {
  233. "AlertDefinition/enabled": newState
  234. }
  235. }
  236. });
  237. },
  238. /**
  239. * Define if label or configs are in edit mode
  240. * @type {Boolean}
  241. */
  242. isEditing: Em.computed.or('editing.label.isEditing', 'App.router.mainAlertDefinitionConfigsController.canEdit'),
  243. /**
  244. * If some configs or label are changed and user navigates away, show this popup with propose to save changes
  245. * @param {String} path
  246. * @method showSavePopup
  247. */
  248. showSavePopup: function (path) {
  249. var self = this;
  250. return App.ModalPopup.show({
  251. header: Em.I18n.t('common.warning'),
  252. bodyClass: Em.View.extend({
  253. template: Ember.Handlebars.compile('{{t alerts.saveChanges}}')
  254. }),
  255. primary: Em.I18n.t('common.save'),
  256. secondary: Em.I18n.t('common.discard'),
  257. third: Em.I18n.t('common.cancel'),
  258. disablePrimary: Em.computed.or('App.router.mainAlertDefinitionDetailsController.editing.label.isError', 'App.router.mainAlertDefinitionConfigsController.hasErrors'),
  259. onPrimary: function () {
  260. self.saveLabelAndConfigs();
  261. self.set('forceTransition', true);
  262. App.router.route(path);
  263. this.hide();
  264. },
  265. onSecondary: function () {
  266. self.set('forceTransition', true);
  267. App.router.route(path);
  268. this.hide();
  269. },
  270. onThird: function () {
  271. this.hide();
  272. }
  273. });
  274. }
  275. });