service_mapper.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. var misc = require('utils/misc');
  19. App.serviceMapper = App.QuickDataMapper.create({
  20. model: App.Service,
  21. config: {
  22. id: 'ServiceInfo.service_name',
  23. service_name: 'ServiceInfo.service_name',
  24. work_status: 'ServiceInfo.state'
  25. },
  26. initialAppLoad: false,
  27. passiveStateMap: {},
  28. map: function (json) {
  29. console.time("App.serviceMapper execution time");
  30. var self = this;
  31. var passiveStateMap = this.get('passiveStateMap');
  32. json.items.forEach(function (service) {
  33. var cachedService = App.cache['services'].findProperty('ServiceInfo.service_name', service.ServiceInfo.service_name);
  34. if (cachedService) {
  35. // restore service workStatus
  36. App.Service.find(cachedService.ServiceInfo.service_name).set('workStatus', service.ServiceInfo.state);
  37. cachedService.ServiceInfo.state = service.ServiceInfo.state;
  38. } else {
  39. var serviceData = {
  40. ServiceInfo: {
  41. service_name: service.ServiceInfo.service_name,
  42. state: service.ServiceInfo.state
  43. },
  44. host_components: [],
  45. components: []
  46. };
  47. App.cache['services'].push(serviceData);
  48. }
  49. passiveStateMap[service.ServiceInfo.service_name] = service.ServiceInfo.maintenance_state;
  50. });
  51. if (!this.get('initialAppLoad')) {
  52. var parsedCacheServices = App.cache['services'].map(function(item){
  53. App.serviceMetricsMapper.mapExtendedModel(item);
  54. return self.parseIt(item, self.get('config'));
  55. });
  56. parsedCacheServices = misc.sortByOrder(App.StackService.find().mapProperty('serviceName'), parsedCacheServices);
  57. App.store.loadMany(this.get('model'), parsedCacheServices);
  58. App.store.commit();
  59. this.set('initialAppLoad', true);
  60. }
  61. for (var service in passiveStateMap) {
  62. if (passiveStateMap.hasOwnProperty(service)) {
  63. App.Service.find(service).set('passiveState', passiveStateMap[service]);
  64. }
  65. }
  66. console.timeEnd("App.serviceMapper execution time");
  67. }
  68. });