definition_details_controller.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.AlertInstance.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, description, thresholds)
  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. description: Em.Object.create({
  35. name: 'description',
  36. isEditing: false,
  37. value: '',
  38. originalValue: '',
  39. isError: false,
  40. bindingValue: 'content.description'
  41. })
  42. }),
  43. /**
  44. * Host to count of alerts on this host during last day map
  45. * @type {Object}
  46. */
  47. lastDayAlertsCount: null,
  48. /**
  49. * List of all group names related to alert definition
  50. * @type {Array}
  51. */
  52. groupsList: function () {
  53. return this.get('content.groups').mapProperty('displayName');
  54. }.property('content.groups.@each'),
  55. /**
  56. * Validation function to define if label field populated correctly
  57. * @method labelValidation
  58. */
  59. labelValidation: function () {
  60. this.set('editing.label.isError', !this.get('editing.label.value').trim());
  61. }.observes('editing.label.value'),
  62. /**
  63. * Validation function to define if description field populated correctly
  64. * @method descriptionValidation
  65. */
  66. descriptionValidation: function () {
  67. this.set('editing.description.isError', !this.get('editing.description.value').trim());
  68. }.observes('editing.description.value'),
  69. /**
  70. * Load alert instances for current alertDefinition
  71. * Start updating loaded data
  72. * @method loadAlertInstances
  73. */
  74. loadAlertInstances: function () {
  75. App.router.get('mainAlertInstancesController').loadAlertInstancesByAlertDefinition(this.get('content.id'));
  76. App.router.set('mainAlertInstancesController.isUpdating', true);
  77. this.loadAlertInstancesHistory();
  78. },
  79. /**
  80. * Load alert instances history data
  81. * used to count instances number of the last 24 hour
  82. * @method loadAlertInstancesHistory
  83. */
  84. loadAlertInstancesHistory: function () {
  85. this.set('lastDayAlertsCount', null);
  86. return App.ajax.send({
  87. name: 'alerts.get_instances_history',
  88. sender: this,
  89. data: {
  90. definitionName: this.get('content.name'),
  91. timestamp: App.dateTime() - 86400000 // timestamp for time 24-hours ago
  92. },
  93. success: 'loadAlertInstancesHistorySuccess'
  94. });
  95. },
  96. /**
  97. * Success-callback for <code>loadAlertInstancesHistory</code>
  98. */
  99. loadAlertInstancesHistorySuccess: function (data) {
  100. var lastDayAlertsCount = {};
  101. data.items.forEach(function (alert) {
  102. if (!lastDayAlertsCount[alert.AlertHistory.host_name]) {
  103. lastDayAlertsCount[alert.AlertHistory.host_name] = 1;
  104. } else {
  105. lastDayAlertsCount[alert.AlertHistory.host_name] += 1;
  106. }
  107. });
  108. this.set('lastDayAlertsCount', lastDayAlertsCount);
  109. },
  110. /**
  111. * Edit button handler
  112. * @param {object} event
  113. * @method edit
  114. */
  115. edit: function (event) {
  116. var element = event.context;
  117. var value = this.get(element.get('bindingValue'));
  118. element.set('originalValue', value);
  119. element.set('value', value);
  120. element.set('isEditing', true);
  121. },
  122. /**
  123. * Cancel button handler
  124. * @param {object} event
  125. * @method cancelEdit
  126. */
  127. cancelEdit: function (event) {
  128. var element = event.context;
  129. element.set('value', element.get('originalValue'));
  130. element.set('isEditing', false);
  131. },
  132. /**
  133. * Save button handler, could save label/description/thresholds of alert definition
  134. * @param {object} event
  135. * @returns {$.ajax}
  136. * @method saveEdit
  137. */
  138. saveEdit: function (event) {
  139. var element = event.context;
  140. this.set(element.get('bindingValue'), element.get('value'));
  141. element.set('isEditing', false);
  142. var data = Em.Object.create({});
  143. var property_name = "AlertDefinition/" + element.get('name');
  144. data.set(property_name, element.get('value'));
  145. var alertDefinition_id = this.get('content.id');
  146. return App.ajax.send({
  147. name: 'alerts.update_alert_definition',
  148. sender: this,
  149. data: {
  150. id: alertDefinition_id,
  151. data: data
  152. }
  153. });
  154. },
  155. /**
  156. * "Delete" button handler
  157. * @param {object} event
  158. * @method deleteAlertDefinition
  159. */
  160. deleteAlertDefinition: function (event) {
  161. var alertDefinition = this.get('content');
  162. var self = this;
  163. App.showConfirmationPopup(function () {
  164. App.ajax.send({
  165. name: 'alerts.delete_alert_definition',
  166. sender: self,
  167. success: 'deleteAlertDefinitionSuccess',
  168. error: 'deleteAlertDefinitionError',
  169. data: {
  170. id: alertDefinition.get('id')
  171. }
  172. });
  173. }, null, function () {
  174. });
  175. },
  176. /**
  177. * Success-callback for <code>deleteAlertDefinition</code>
  178. * @method deleteAlertDefinitionSuccess
  179. */
  180. deleteAlertDefinitionSuccess: function () {
  181. App.router.transitionTo('main.alerts.index');
  182. },
  183. /**
  184. * Error-callback for <code>deleteAlertDefinition</code>
  185. * @method deleteAlertDefinitionError
  186. */
  187. deleteAlertDefinitionError: function (xhr, textStatus, errorThrown, opt) {
  188. console.log(textStatus);
  189. console.log(errorThrown);
  190. xhr.responseText = "{\"message\": \"" + xhr.statusText + "\"}";
  191. App.ajax.defaultErrorHandler(xhr, opt.url, 'DELETE', xhr.status);
  192. },
  193. /**
  194. * "Disable / Enable" button handler
  195. * @method toggleState
  196. */
  197. toggleState: function () {
  198. var alertDefinition = this.get('content');
  199. var self = this;
  200. var bodyMessage = Em.Object.create({
  201. confirmMsg: alertDefinition.get('enabled') ? Em.I18n.t('alerts.table.state.enabled.confirm.msg') : Em.I18n.t('alerts.table.state.disabled.confirm.msg'),
  202. confirmButton: alertDefinition.get('enabled') ? Em.I18n.t('alerts.table.state.enabled.confirm.btn') : Em.I18n.t('alerts.table.state.disabled.confirm.btn')
  203. });
  204. return App.showConfirmationFeedBackPopup(function (query) {
  205. self.toggleDefinitionState(alertDefinition);
  206. }, bodyMessage);
  207. },
  208. /**
  209. * Enable/disable alertDefinition
  210. * @param {object} alertDefinition
  211. * @returns {$.ajax}
  212. * @method toggleDefinitionState
  213. */
  214. toggleDefinitionState: function (alertDefinition) {
  215. var newState = !alertDefinition.get('enabled');
  216. alertDefinition.set('enabled', newState);
  217. return App.ajax.send({
  218. name: 'alerts.update_alert_definition',
  219. sender: this,
  220. data: {
  221. id: alertDefinition.get('id'),
  222. data: {
  223. "AlertDefinition/enabled": newState
  224. }
  225. }
  226. });
  227. },
  228. /**
  229. * Router transition to service page
  230. * @param event
  231. */
  232. goToService: function (event) {
  233. if (event && event.context) {
  234. App.router.transitionTo('main.services.service', event.context);
  235. }
  236. },
  237. /**
  238. * Router transition to host level alerts page
  239. * @param {object} event
  240. * @method goToHostAlerts
  241. */
  242. goToHostAlerts: function (event) {
  243. if (event && event.context) {
  244. App.router.get('mainHostDetailsController').set('referer', App.router.location.lastSetURL);
  245. App.router.transitionTo('main.hosts.hostDetails.alerts', event.context);
  246. }
  247. }
  248. });