alert_instances_mapper.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.alertInstanceMapper = App.QuickDataMapper.create({
  19. model : App.AlertInstance,
  20. modelLocal: App.AlertInstanceLocal,
  21. config : {
  22. id: 'Alert.id',
  23. label: 'Alert.label',
  24. definition_name: 'Alert.definition_name',
  25. definition_id: 'Alert.definition_id',
  26. service_id: 'Alert.service_name',
  27. service_name: 'Alert.service_name',
  28. component_name: 'Alert.component_name',
  29. host_id: 'Alert.host_name',
  30. host_name: 'Alert.host_name',
  31. scope: 'Alert.scope',
  32. original_timestamp: 'Alert.original_timestamp',
  33. original_raw_timestamp: 'Alert.original_timestamp',
  34. latest_timestamp: 'Alert.latest_timestamp',
  35. maintenance_state: 'Alert.maintenance_state',
  36. instance: 'Alert.instance',
  37. state: 'Alert.state',
  38. text: 'Alert.text',
  39. repeat_tolerance: 'Alert.repeat_tolerance',
  40. repeat_tolerance_remaining: 'Alert.repeat_tolerance_remaining'
  41. },
  42. map: function(json) {
  43. return this.parse(json, this.get('model'));
  44. },
  45. mapLocal: function(json) {
  46. return this.parse(json, this.get('modelLocal'));
  47. },
  48. parse: function(json, model) {
  49. console.time('App.alertInstanceMapper execution time');
  50. if (json.items) {
  51. var alertInstances = [];
  52. var alertsToDelete = model.find().mapProperty('id');
  53. json.items.forEach(function (item) {
  54. var alert = this.parseIt(item, this.get('config'));
  55. alert.original_timestamp = App.dateTimeWithTimeZone(alert.original_timestamp);
  56. alert.latest_timestamp = App.dateTimeWithTimeZone(alert.latest_timestamp);
  57. alertInstances.push(alert);
  58. alertsToDelete = alertsToDelete.without(alert.id);
  59. }, this);
  60. if (alertsToDelete.length > 0) {
  61. model.find().clear();
  62. }
  63. App.store.loadMany(model, alertInstances);
  64. console.timeEnd('App.alertInstanceMapper execution time');
  65. }
  66. }
  67. });