alert_definition_summary_mapper.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. var App = require('app');
  18. var dataManipulation = require('utils/data_manipulation');
  19. App.alertDefinitionSummaryMapper = App.QuickDataMapper.create({
  20. config: {},
  21. map: function(data) {
  22. console.time('App.alertDefinitionSummaryMapper execution time');
  23. if (!data.alerts_summary_grouped) return;
  24. var alertDefinitions = App.AlertDefinition.find();
  25. var alertDefinitionsMap = alertDefinitions.toArray().toMapByProperty('id');
  26. var summaryMap = {};
  27. data.alerts_summary_grouped.forEach(function(alertDefinitionSummary) {
  28. var alertDefinition = alertDefinitionsMap[alertDefinitionSummary.definition_id];
  29. if (alertDefinition) {
  30. var summary = {},
  31. timestamp = 0;
  32. Em.keys(alertDefinitionSummary.summary).forEach(function(status) {
  33. summary[status] = {
  34. count: alertDefinitionSummary.summary[status].count,
  35. maintenanceCount: alertDefinitionSummary.summary[status].maintenance_count
  36. };
  37. if (alertDefinitionSummary.summary[status].latest_text) {
  38. summary[status].latestText = alertDefinitionSummary.summary[status].latest_text;
  39. }
  40. if (alertDefinitionSummary.summary[status].original_timestamp > timestamp) {
  41. timestamp = alertDefinitionSummary.summary[status].original_timestamp;
  42. }
  43. });
  44. summaryMap[alertDefinitionSummary.definition_id] = {
  45. summary: summary,
  46. lastTriggered: App.dateTimeWithTimeZone(parseInt(timestamp, 10)),
  47. lastTriggeredRaw: timestamp
  48. };
  49. }
  50. });
  51. alertDefinitions.forEach(function (d) {
  52. var id = d.get('id');
  53. if (alertDefinitionsMap[id].get('stateManager.currentState.name') !== 'saved') {
  54. alertDefinitionsMap[id].get('stateManager').transitionTo('saved');
  55. }
  56. alertDefinitionsMap[id].setProperties(summaryMap[id]);
  57. if (!alertDefinitionsMap[id].get('enabled')) {
  58. // clear summary for disabled alert definitions
  59. alertDefinitionsMap[id].set('summary', {});
  60. }
  61. });
  62. // set alertsCount and hasCriticalAlerts for each service
  63. var groupedByServiceName = dataManipulation.groupPropertyValues(alertDefinitions, 'service.serviceName');
  64. var groupedByComponentName = dataManipulation.groupPropertyValues(alertDefinitions, 'componentName');
  65. var services = App.Service.find();
  66. var servicesMap = services.toArray().toMapByProperty('id');
  67. Object.keys(groupedByServiceName).forEach(function(serviceName) {
  68. var service = servicesMap[serviceName];
  69. if (service) {
  70. var hasCriticalAlerts = false;
  71. var alertsCount = groupedByServiceName[serviceName].map(function (alertDefinition) {
  72. var criticalCount = alertDefinition.getWithDefault('summary.CRITICAL.count', 0);
  73. var warningCount = alertDefinition.getWithDefault('summary.WARNING.count', 0);
  74. if (criticalCount) {
  75. hasCriticalAlerts = true;
  76. }
  77. return criticalCount + warningCount;
  78. }).reduce(Em.sum, 0);
  79. service.setProperties({
  80. alertsCount: alertsCount,
  81. hasCriticalAlerts: hasCriticalAlerts
  82. });
  83. service.get('hostComponents').filterProperty('isMaster').forEach(function (master) {
  84. hasCriticalAlerts = false;
  85. alertsCount = (groupedByComponentName[master.get('componentName')] || []).map(function (alertDefinition) {
  86. var criticalCount = alertDefinition.getWithDefault('summary.CRITICAL.count', 0);
  87. var warningCount = alertDefinition.getWithDefault('summary.WARNING.count', 0);
  88. if (criticalCount) {
  89. hasCriticalAlerts = true;
  90. }
  91. return criticalCount + warningCount;
  92. }).reduce(Em.sum, 0);
  93. master.setProperties({
  94. alertsCount: alertsCount,
  95. hasCriticalAlerts: hasCriticalAlerts
  96. });
  97. });
  98. }
  99. });
  100. if (!$.mocho) {
  101. //for some reasons this causing error in unit test
  102. App.store.commit();
  103. }
  104. console.timeEnd('App.alertDefinitionSummaryMapper execution time');
  105. }
  106. });