alert_instances_mapper.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. },
  40. map: function(json) {
  41. return this.parse(json, this.get('model'));
  42. },
  43. mapLocal: function(json) {
  44. return this.parse(json, this.get('modelLocal'));
  45. },
  46. parse: function(json, model) {
  47. console.time('App.alertInstanceMapper execution time');
  48. if (json.items) {
  49. var alertInstances = [];
  50. var alertsToDelete = model.find().mapProperty('id');
  51. json.items.forEach(function (item) {
  52. var alert = this.parseIt(item, this.get('config'));
  53. alert.original_timestamp = App.dateTimeWithTimeZone(alert.original_timestamp);
  54. alert.latest_timestamp = App.dateTimeWithTimeZone(alert.latest_timestamp);
  55. alertInstances.push(alert);
  56. alertsToDelete = alertsToDelete.without(alert.id);
  57. }, this);
  58. if (alertsToDelete.length > 0) {
  59. model.find().clear();
  60. }
  61. App.store.loadMany(model, alertInstances);
  62. console.timeEnd('App.alertInstanceMapper execution time');
  63. }
  64. }
  65. });