definition_details_controller.js 9.9 KB

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