alert_definitions_controller.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. contentUpdater: null,
  27. /**
  28. * List of all <code>App.AlertDefinition</code>
  29. * @type {App.AlertDefinition[]}
  30. */
  31. content: App.AlertDefinition.find(),
  32. /**
  33. * Generates key for alert summary that represents current state
  34. */
  35. getSummaryCache: function () {
  36. var res = '';
  37. this.get('content').forEach(function(o) {
  38. var summary = o.get('summary');
  39. o.get('order').forEach(function (state) {
  40. res += summary[state] ? summary[state].count + summary[state].maintenanceCount : 0;
  41. });
  42. });
  43. return res;
  44. },
  45. generateCacheByKey: function(key) {
  46. if (key === 'summary') {
  47. return this.getSummaryCache();
  48. }
  49. return this.get('content').mapProperty(key).join('');
  50. },
  51. contentWasChanged: function(key) {
  52. var updatedCache = this.generateCacheByKey(key);
  53. if (this.get('cache.' + key) !== updatedCache) {
  54. this.set('cache.' + key, updatedCache);
  55. this.propertyDidChange('contentUpdater');
  56. }
  57. },
  58. cache: {
  59. 'label': '',
  60. 'summary': '',
  61. 'serviceName': '',
  62. 'lastTriggered': '',
  63. 'enabled': ''
  64. },
  65. /**
  66. * Enable/disable alertDefinition confirmation popup
  67. * @param {object} event
  68. * @method toggleState
  69. * @return {App.ModalPopup}
  70. */
  71. toggleState: function (event) {
  72. var alertDefinition = event.context;
  73. var self = this;
  74. var bodyMessage = Em.Object.create({
  75. confirmMsg: alertDefinition.get('enabled') ? Em.I18n.t('alerts.table.state.enabled.confirm.msg') : Em.I18n.t('alerts.table.state.disabled.confirm.msg'),
  76. confirmButton: alertDefinition.get('enabled') ? Em.I18n.t('alerts.table.state.enabled.confirm.btn') : Em.I18n.t('alerts.table.state.disabled.confirm.btn')
  77. });
  78. return App.showConfirmationFeedBackPopup(function (query) {
  79. self.toggleDefinitionState(alertDefinition);
  80. }, bodyMessage);
  81. },
  82. /**
  83. * Enable/disable alertDefinition
  84. * @param {object} alertDefinition
  85. * @returns {$.ajax}
  86. * @method toggleDefinitionState
  87. */
  88. toggleDefinitionState: function (alertDefinition) {
  89. var newState = !alertDefinition.get('enabled');
  90. alertDefinition.set('enabled', newState);
  91. Em.run.next(function () {
  92. App.tooltip($('.enable-disable-button'));
  93. });
  94. return App.ajax.send({
  95. name: 'alerts.update_alert_definition',
  96. sender: this,
  97. data: {
  98. id: alertDefinition.get('id'),
  99. data: {
  100. "AlertDefinition/enabled": newState
  101. }
  102. }
  103. });
  104. },
  105. /**
  106. * ========================== alerts popup dialog =========================
  107. */
  108. /**
  109. * Number of all critical and warning alert instances
  110. * Calculation is based on each <code>alertDefinitions.summary</code>
  111. * @type {Number}
  112. */
  113. unhealthyAlertInstancesCount: function () {
  114. return this.get('content').map(function (alertDefinition) {
  115. return alertDefinition.getWithDefault('summary.CRITICAL.count', 0) + alertDefinition.getWithDefault('summary.WARNING.count', 0);
  116. }).reduce(Em.sum, 0);
  117. }.property('content.@each.summary'),
  118. /**
  119. * if critical alerts exist, the alert badge should be red.
  120. * @type {Boolean}
  121. */
  122. isCriticalAlerts: function () {
  123. return this.get('content').invoke('getWithDefault', 'summary.CRITICAL.count', 0).reduce(Em.sum, 0) !== 0;
  124. }.property('content.@each.summary')
  125. });