alert_definitions_controller.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. * @type {App.AlertDefinition[]}
  35. */
  36. content: App.AlertDefinition.find(),
  37. /**
  38. * Enable/disable alertDefinition confirmation popup
  39. * @param {object} event
  40. * @method toggleState
  41. * @return {App.ModalPopup}
  42. */
  43. toggleState: function (event) {
  44. var alertDefinition = event.context;
  45. var self = this;
  46. var bodyMessage = Em.Object.create({
  47. confirmMsg: alertDefinition.get('enabled') ? Em.I18n.t('alerts.table.state.enabled.confirm.msg') : Em.I18n.t('alerts.table.state.disabled.confirm.msg'),
  48. confirmButton: alertDefinition.get('enabled') ? Em.I18n.t('alerts.table.state.enabled.confirm.btn') : Em.I18n.t('alerts.table.state.disabled.confirm.btn')
  49. });
  50. return App.showConfirmationFeedBackPopup(function (query) {
  51. self.toggleDefinitionState(alertDefinition);
  52. }, bodyMessage);
  53. },
  54. /**
  55. * Enable/disable alertDefinition
  56. * @param {object} alertDefinition
  57. * @returns {$.ajax}
  58. * @method toggleDefinitionState
  59. */
  60. toggleDefinitionState: function (alertDefinition) {
  61. var newState = !alertDefinition.get('enabled');
  62. alertDefinition.set('enabled', newState);
  63. return App.ajax.send({
  64. name: 'alerts.update_alert_definition',
  65. sender: this,
  66. data: {
  67. id: alertDefinition.get('id'),
  68. data: {
  69. "AlertDefinition/enabled": newState
  70. }
  71. }
  72. });
  73. },
  74. /**
  75. * Calculate critical count for each service, to show up the label on services menu
  76. * @method getCriticalAlertsCountForService
  77. * @return {Number}
  78. */
  79. getCriticalAlertsCountForService: function (service) {
  80. return this.get('content').filterProperty('service.serviceName', service).
  81. invoke('getWithDefault', 'summary.CRITICAL.count', 0).
  82. reduce(Em.sum, 0);
  83. },
  84. /**
  85. * Calculate critical/warning count for each service, to show up the label on services menu
  86. * @method getCriticalOrWarningAlertsCountForService
  87. * @return {Number}
  88. */
  89. getCriticalOrWarningAlertsCountForService: function (service) {
  90. return this.get('content').filterProperty('service.serviceName', service).map(function (alertDefinition) {
  91. return alertDefinition.getWithDefault('summary.CRITICAL.count', 0) + alertDefinition.getWithDefault('summary.WARNING.count', 0);
  92. }).reduce(Em.sum, 0);
  93. },
  94. /**
  95. * ========================== alerts popup dialog =========================
  96. */
  97. /**
  98. * Number of all critical and warning alert instances
  99. * Calculation is based on each <code>alertDefinitions.summary</code>
  100. * @type {Number}
  101. */
  102. unhealthyAlertInstancesCount: function () {
  103. return this.get('content').map(function (alertDefinition) {
  104. return alertDefinition.getWithDefault('summary.CRITICAL.count', 0) + alertDefinition.getWithDefault('summary.WARNING.count', 0);
  105. }).reduce(Em.sum, 0);
  106. }.property('content.@each.summary'),
  107. /**
  108. * if critical alerts exist, the alert badge should be red.
  109. * @type {Boolean}
  110. */
  111. isCriticalAlerts: function () {
  112. return this.get('content').invoke('getWithDefault', 'summary.CRITICAL.count', 0).reduce(Em.sum, 0) !== 0;
  113. }.property('content.@each.summary')
  114. });