alert_definitions_controller.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. App.MainAlertDefinitionsController = Em.ArrayController.extend({
  20. name: 'mainAlertDefinitionsController',
  21. /**
  22. * Timestamp when <code>App.alertDefinitionsMapper</code> run last time
  23. * Current <code>content</code> is updated on when it changed
  24. * @type {number|null}
  25. */
  26. mapperTimestamp: null,
  27. /**
  28. * Define whether restore filter conditions from local db
  29. * @type {Boolean}
  30. */
  31. showFilterConditionsFirstLoad: false,
  32. /**
  33. * List of all <code>App.AlertDefinition</code>
  34. * Consists of:
  35. * <ul>
  36. * <li>App.PortAlertDefinition</li>
  37. * <li>App.MetricsAlertDefinition</li>
  38. * <li>App.WebAlertDefinition</li>
  39. * <li>App.AggregateAlertDefinition</li>
  40. * <li>App.ScriptAlertDefinition</li>
  41. * </ul>
  42. * @type {App.AlertDefinition[]}
  43. */
  44. content: function () {
  45. var definitions = App.AlertDefinition.getAllDefinitions();
  46. definitions.sort(App.AlertDefinition.getSortDefinitionsByStatus(true));
  47. return definitions;
  48. }.property('mapperTimestamp'),
  49. /**
  50. * Enable/disable alertDefinition confirmation popup
  51. * @param {object} event
  52. * @method toggleState
  53. * @return {App.ModalPopup}
  54. */
  55. toggleState: function (event) {
  56. var alertDefinition = event.context;
  57. var self = this;
  58. var bodyMessage = Em.Object.create({
  59. confirmMsg: alertDefinition.get('enabled') ? Em.I18n.t('alerts.table.state.enabled.confirm.msg') : Em.I18n.t('alerts.table.state.disabled.confirm.msg'),
  60. confirmButton: alertDefinition.get('enabled') ? Em.I18n.t('alerts.table.state.enabled.confirm.btn') : Em.I18n.t('alerts.table.state.disabled.confirm.btn')
  61. });
  62. return App.showConfirmationFeedBackPopup(function (query) {
  63. self.toggleDefinitionState(alertDefinition);
  64. }, bodyMessage);
  65. },
  66. /**
  67. * Enable/disable alertDefinition
  68. * @param {object} alertDefinition
  69. * @returns {$.ajax}
  70. * @method toggleDefinitionState
  71. */
  72. toggleDefinitionState: function (alertDefinition) {
  73. var newState = !alertDefinition.get('enabled');
  74. alertDefinition.set('enabled', newState);
  75. return App.ajax.send({
  76. name: 'alerts.update_alert_definition',
  77. sender: this,
  78. data: {
  79. id: alertDefinition.get('id'),
  80. data: {
  81. "AlertDefinition/enabled": newState
  82. }
  83. }
  84. });
  85. },
  86. /**
  87. * Calculate critical/warning count for each service, to show up the label on services menu
  88. * @method getCriticalAlertsCountForService
  89. * @return {number}
  90. */
  91. getCriticalAlertsCountForService: function (service) {
  92. var alertsForService = this.get('content').filterProperty('service', service);
  93. return alertsForService.filterProperty('isCriticalOrWarning').get('length');
  94. },
  95. /**
  96. * ========================== alerts popup dialog =========================
  97. */
  98. /**
  99. * Alerts number to show up on top-nav bar: number of critical/warning alerts
  100. * @type {number}
  101. */
  102. allAlertsCount: function () {
  103. return this.get('unhealthyAlertInstances.length');
  104. }.property('unhealthyAlertInstances.length'),
  105. unhealthyAlertInstances: function () {
  106. return App.AlertInstance.find().toArray().filterProperty('state', 'CRITICAL').concat(
  107. App.AlertInstance.find().toArray().filterProperty('state', 'WARNING')
  108. );
  109. }.property('mapperTimestamp'),
  110. /**
  111. * if critical alerts exist, if true, the alert badge should be red.
  112. */
  113. isCriticalAlerts: function () {
  114. return this.get('unhealthyAlertInstances').someProperty('state', 'CRITICAL');
  115. }.property('unhealthyAlertInstances.@each.state'),
  116. /**
  117. * Onclick handler for alerts number located right to bg ops number (see application.hbs)
  118. * @method showPopup
  119. * @return {App.ModalPopup}
  120. */
  121. showPopup: function () {
  122. var self = this;
  123. return App.ModalPopup.show({
  124. header: function () {
  125. return Em.I18n.t('alerts.fastAccess.popup.header').format(this.get('definitionsController.allAlertsCount'));
  126. }.property('definitionsController.allAlertsCount'),
  127. definitionsController: this,
  128. classNames: ['sixty-percent-width-modal', 'alerts-popup'],
  129. secondary: Em.I18n.t('alerts.fastAccess.popup.body.showmore'),
  130. isHideBodyScroll: true,
  131. onSecondary: function () {
  132. this.hide();
  133. App.router.transitionTo('main.alerts.index');
  134. },
  135. bodyClass: Em.View.extend({
  136. templateName: require('templates/common/modal_popups/alerts_popup'),
  137. controller: self,
  138. contents: function () {
  139. return this.get('controller.unhealthyAlertInstances');
  140. }.property('controller.unhealthyAlertInstances.length', 'controller.unhealthyAlertInstances.@each.state'),
  141. isLoaded: function () {
  142. return !!this.get('controller.unhealthyAlertInstances');
  143. }.property('controller.unhealthyAlertInstances'),
  144. isAlertEmptyList: function () {
  145. return !this.get('contents.length');
  146. }.property('contents.length'),
  147. /**
  148. * Router transition to alert definition details page
  149. * @param event
  150. */
  151. gotoAlertDetails: function (event) {
  152. if (event && event.context) {
  153. this.get('parentView').hide();
  154. var definition = this.get('controller.content').findProperty('id', event.context.get('definitionId'));
  155. App.router.transitionTo('main.alerts.alertDetails', definition);
  156. }
  157. },
  158. /**
  159. * Router transition to service summary page
  160. * @param event
  161. */
  162. gotoService: function (event) {
  163. if (event && event.context) {
  164. this.get('parentView').hide();
  165. App.router.transitionTo('main.services.service', event.context);
  166. }
  167. },
  168. /**
  169. * Router transition to host level alerts page
  170. * @param event
  171. */
  172. goToHostAlerts: function (event) {
  173. if (event && event.context) {
  174. this.get('parentView').hide();
  175. App.router.transitionTo('main.hosts.hostDetails.alerts', event.context);
  176. }
  177. },
  178. didInsertElement: function () {
  179. Em.run.next(this, function () {
  180. App.tooltip($(".timeago"));
  181. });
  182. }
  183. })
  184. });
  185. }
  186. });