alert_definitions_controller.js 4.3 KB

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