alert_definitions_controller.js 6.7 KB

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