alert_notification_mapper.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. App.alertNotificationMapper = App.QuickDataMapper.create({
  19. model: App.AlertNotification,
  20. config: {
  21. id: 'AlertTarget.id',
  22. name: 'AlertTarget.name',
  23. type: 'AlertTarget.notification_type',
  24. description: 'AlertTarget.description',
  25. global: 'AlertTarget.global'
  26. },
  27. map: function (json) {
  28. if(Em.isNone(App.cache['previousAlertNotificationsFullMap'])) {
  29. App.cache['previousAlertNotificationsFullMap'] = {};
  30. }
  31. console.time('App.alertNotificationMapper execution time');
  32. if (json.items) {
  33. var result = [];
  34. var notificationsProperties = {};
  35. var notificationsAlertStates = {};
  36. var groupsMap = App.cache['alertNotificationsGroupsMap'];
  37. var notifications = {};
  38. var self = this;
  39. json.items.forEach(function (item) {
  40. var notification = this.parseIt(item, this.config);
  41. var groups = groupsMap && groupsMap[notification.id];
  42. if (groups) {
  43. notification.groups = groups;
  44. }
  45. var previousNotification = App.cache['previousAlertNotificationsFullMap'][notification.id] ? App.cache['previousAlertNotificationsFullMap'][notification.id] : {};
  46. var changedFields = self.getDiscrepancies(notification, previousNotification, ['name', 'type', 'description', 'global', 'groups']);
  47. if (Object.keys(changedFields).length) {
  48. result.push(notification);
  49. }
  50. notifications[notification.id] = notification;
  51. notificationsProperties[item.AlertTarget.id] = item.AlertTarget.properties;
  52. notificationsAlertStates[item.AlertTarget.id] = item.AlertTarget.alert_states;
  53. }, this);
  54. App.store.loadMany(this.get('model'), result);
  55. App.cache['previousAlertNotificationsFullMap'] = notifications;
  56. this._setPropertiesToEachModel('properties', notificationsProperties);
  57. this._setPropertiesToEachModel('alertStates', notificationsAlertStates);
  58. }
  59. console.timeEnd('App.alertNotificationMapper execution time');
  60. },
  61. /**
  62. * Set values from <code>propertyMap</code> for <code>propertyName</code> for each record in model
  63. * @param {string} propertyName
  64. * @param {object} propertiesMap record_id to value map
  65. * @method setPropertiesToEachModel
  66. * @private
  67. */
  68. _setPropertiesToEachModel: function (propertyName, propertiesMap) {
  69. var modelsMap = this.get('modelsMap');
  70. for (var recordId in propertiesMap) {
  71. if (propertiesMap.hasOwnProperty(recordId)) {
  72. App.AlertNotification.find(recordId).set(propertyName, propertiesMap[recordId]);
  73. }
  74. }
  75. }
  76. });