alert_groups_mapper.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * Doesn't save not changed data (check it using <code>App.cache['previousAlertGroupsFullMap']</code>)
  22. * Use <code>App.cache['previousAlertGroupsMap']</code> to store map alertDefinitions-alertGroups. This map is used
  23. * in the <code>App.AlertDefinitionsMapper</code> to correctly link alertDefinitions and alertGroups
  24. */
  25. App.alertGroupsMapper = App.QuickDataMapper.create({
  26. model: App.AlertGroup,
  27. config: {
  28. id: 'AlertGroup.id',
  29. name: 'AlertGroup.name',
  30. default: 'AlertGroup.default',
  31. targets_key: 'AlertGroup.targets',
  32. targets_type: 'array',
  33. targets: {
  34. item: 'id'
  35. }
  36. },
  37. map: function (json) {
  38. if(Em.isNone(App.cache['previousAlertGroupsFullMap'])) {
  39. App.cache['previousAlertGroupsFullMap'] = {};
  40. }
  41. if (!Em.isNone(json, 'items')) {
  42. console.time('App.alertGroupsMapper execution time');
  43. var alertGroups = [],
  44. self = this,
  45. groupsMap = {},
  46. groupsToDelete = App.AlertGroup.find().mapProperty('id'),
  47. /**
  48. * AlertGroups-map for <code>App.AlertDefinitionsMappers</code>
  49. * Format:
  50. * <code>
  51. * {
  52. * alert_definition1_id: [alert_group1_id, alert_group2_id],
  53. * alert_definition2_id: [alert_group3_id, alert_group1_id],
  54. * ...
  55. * }
  56. * </code>
  57. * @type {object}
  58. */
  59. alertDefinitionsGroupsMap = {},
  60. alertNotificationsGroupsMap = {};
  61. json.items.forEach(function(item) {
  62. var group = self.parseIt(item, self.get('config'));
  63. groupsToDelete = groupsToDelete.without(group.id);
  64. group.targets = [];
  65. group.definitions = [];
  66. if (item.AlertGroup.definitions) {
  67. item.AlertGroup.definitions.forEach(function (definition) {
  68. if (!group.definitions.contains(definition.id)) {
  69. group.definitions.push(definition.id);
  70. }
  71. if (Em.isNone(alertDefinitionsGroupsMap[definition.id])) {
  72. alertDefinitionsGroupsMap[definition.id] = [];
  73. }
  74. alertDefinitionsGroupsMap[definition.id].push(group.id);
  75. });
  76. }
  77. if (item.AlertGroup.targets) {
  78. item.AlertGroup.targets.forEach(function (target) {
  79. if (!group.targets.contains(target.id)) {
  80. group.targets.push(target.id);
  81. }
  82. if (Em.isNone(alertNotificationsGroupsMap[target.id])) {
  83. alertNotificationsGroupsMap[target.id] = [];
  84. }
  85. alertNotificationsGroupsMap[target.id].push(group.id);
  86. });
  87. }
  88. groupsMap[group.id] = group;
  89. var previousGroup = App.cache['previousAlertGroupsFullMap'][group.id] ? App.cache['previousAlertGroupsFullMap'][group.id] : {};
  90. var changedFields = self.getDiscrepancies(group, previousGroup, ['name', 'description', 'default', 'targets', 'definitions']);
  91. if (Object.keys(changedFields).length) {
  92. alertGroups.push(group);
  93. }
  94. }, this);
  95. groupsToDelete.forEach(function(groupId) {
  96. self.deleteRecord(App.AlertGroup.find(groupId));
  97. });
  98. App.cache['previousAlertGroupsMap'] = alertDefinitionsGroupsMap;
  99. App.cache['previousAlertGroupsFullMap'] = groupsMap;
  100. App.cache['alertNotificationsGroupsMap'] = alertNotificationsGroupsMap;
  101. // initial load takes much more time than others, but it's OK (all data should be saved first time)
  102. App.store.loadMany(this.get('model'), alertGroups);
  103. App.store.commit();
  104. console.timeEnd('App.alertGroupsMapper execution time');
  105. }
  106. }
  107. });