alert_groups_mapper.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /**
  19. * Mapper for <code>App.AlertGroup</code>
  20. * Save general information
  21. * Use <code>App.cache['previousAlertGroupsMap']</code> to store map alertDefinitions-alertGroups. This map is used
  22. * in the <code>App.AlertDefinitionsMapper</code> to correctly link alertDefinitions and alertGroups
  23. */
  24. App.alertGroupsMapper = App.QuickDataMapper.create({
  25. model: App.AlertGroup,
  26. config: {
  27. id: 'AlertGroup.id',
  28. name: 'AlertGroup.name',
  29. default: 'AlertGroup.default',
  30. targets_key: 'AlertGroup.targets',
  31. targets_type: 'array',
  32. targets: {
  33. item: 'id'
  34. }
  35. },
  36. /**
  37. * Map for alertGroup's alertDefinitions
  38. * Store alertDefinitions to alertGroup-properties basing on alertDefinitionType
  39. * Format: key - alertDefinitionType, value - alertGroup-property where alertDefinition should be saved
  40. * @type {object}
  41. */
  42. typesMap: {
  43. PORT: 'port_alert_definitions',
  44. METRIC: 'metrics_alert_definitions',
  45. WEB: 'web_alert_definitions',
  46. AGGREGATE: 'aggregate_alert_definitions',
  47. SCRIPT: 'script_alert_definitions'
  48. },
  49. map: function (json) {
  50. if (!Em.isNone(json, 'items')) {
  51. var alertGroups = [],
  52. self = this,
  53. groupsToDelete = App.AlertGroup.find().mapProperty('id'),
  54. typesMap = this.get('typesMap'),
  55. /**
  56. * AlertGroups-map for <code>App.AlertDefinitionsMappers</code>
  57. * Format:
  58. * <code>
  59. * {
  60. * alert_definition1_id: [alert_group1_id, alert_group2_id],
  61. * alert_definition2_id: [alert_group3_id, alert_group1_id],
  62. * ...
  63. * }
  64. * </code>
  65. * @type {object}
  66. */
  67. alertDefinitionsGroupsMap = {},
  68. alertNotificationsGroupsMap = {};
  69. json.items.forEach(function(item) {
  70. var group = self.parseIt(item, self.get('config'));
  71. groupsToDelete = groupsToDelete.without(group.id);
  72. Em.keys(typesMap).forEach(function(k) {
  73. group[typesMap[k]] = [];
  74. });
  75. group.targets = [];
  76. if (item.AlertGroup.definitions) {
  77. item.AlertGroup.definitions.forEach(function(definition) {
  78. var type = typesMap[definition.source_type];
  79. if (!group[type].contains(definition.id)) {
  80. group[type].push(definition.id);
  81. }
  82. if (Em.isNone(alertDefinitionsGroupsMap[definition.id])) {
  83. alertDefinitionsGroupsMap[definition.id] = [];
  84. }
  85. alertDefinitionsGroupsMap[definition.id].push(group.id);
  86. });
  87. }
  88. if (item.AlertGroup.targets) {
  89. item.AlertGroup.targets.forEach(function(target) {
  90. if (!group.targets.contains(target.id)) {
  91. group.targets.push(target.id);
  92. }
  93. if (Em.isNone(alertNotificationsGroupsMap[target.id])) {
  94. alertNotificationsGroupsMap[target.id] = [];
  95. }
  96. alertNotificationsGroupsMap[target.id].push(group.id);
  97. });
  98. }
  99. alertGroups.push(group);
  100. }, this);
  101. groupsToDelete.forEach(function(groupId) {
  102. self.deleteRecord(App.AlertGroup.find(groupId));
  103. });
  104. App.cache['previousAlertGroupsMap'] = alertDefinitionsGroupsMap;
  105. App.cache['alertNotificationsGroupsMap'] = alertNotificationsGroupsMap;
  106. App.store.loadMany(this.get('model'), alertGroups);
  107. App.store.commit();
  108. }
  109. }
  110. });