component_config_mapper.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.componentConfigMapper = App.QuickDataMapper.create({
  19. model: App.HostComponent,
  20. config: {
  21. id: 'id',
  22. work_status: 'HostRoles.state',
  23. passive_state: 'HostRoles.maintenance_state',
  24. component_name: 'HostRoles.component_name',
  25. host_name: 'HostRoles.host_name',
  26. $ha_status: '',
  27. $display_name_advanced: '',
  28. stale_configs: 'HostRoles.stale_configs',
  29. host_id: 'HostRoles.host_name',
  30. service_id: 'HostRoles.service_name',
  31. admin_state: 'HostRoles.desired_admin_state'
  32. },
  33. map: function (json) {
  34. console.time('App.componentConfigMapper execution time');
  35. var hostComponents = [];
  36. var serviceToHostComponentIdMap = {};
  37. var cacheServices = App.cache['services'];
  38. var loadedServiceComponentsMap = this.buildServiceComponentMap(cacheServices);
  39. var mapConfig = this.get('config');
  40. // We do not want to parse JSON if there is no need to
  41. var hostComponentJsonMap = {};
  42. var hostComponentJsonIds = [];
  43. var hostComponentJsonsToRemove = {};
  44. json.items.forEach(function (item) {
  45. item.host_components.forEach(function (host_component) {
  46. host_component.id = host_component.HostRoles.component_name + '_' + host_component.HostRoles.host_name;
  47. hostComponentJsonIds.push(host_component.id);
  48. hostComponentJsonMap[host_component.id] = host_component;
  49. });
  50. });
  51. this.get('model').find().forEach(function (hostComponent) {
  52. var hostComponentJson = hostComponentJsonMap[hostComponent.get('id')];
  53. if (!hostComponentJson && !hostComponent.get('isMaster')) {
  54. hostComponent.set('staleConfigs', false);
  55. }
  56. if (hostComponentJson!=null && hostComponent.get('staleConfigs') &&
  57. hostComponentJson.HostRoles.state == hostComponent.get('workStatus') &&
  58. hostComponentJson.HostRoles.maintenance_state == hostComponent.get('passiveState')) {
  59. // A component already exists with correct stale_configs flag and other values - no need to load again
  60. hostComponentJsonsToRemove[hostComponentJson.id] = hostComponentJson;
  61. }
  62. });
  63. hostComponentJsonIds.forEach(function (hcId) {
  64. if(!hostComponentJsonsToRemove[hcId]){
  65. var host_component = hostComponentJsonMap[hcId];
  66. var serviceName = host_component.HostRoles.service_name;
  67. hostComponents.push(this.parseIt(host_component, mapConfig));
  68. if (!serviceToHostComponentIdMap[serviceName]) {
  69. serviceToHostComponentIdMap[serviceName] = [];
  70. }
  71. serviceToHostComponentIdMap[serviceName].push(host_component.id);
  72. }
  73. }, this);
  74. App.store.loadMany(this.get('model'), hostComponents);
  75. this.addNewHostComponents(loadedServiceComponentsMap, serviceToHostComponentIdMap, cacheServices);
  76. console.timeEnd('App.componentConfigMapper execution time');
  77. },
  78. /**
  79. * build map that include loaded host-components to avoid duplicate loading
  80. * @param cacheServices
  81. * @return {Object}
  82. */
  83. buildServiceComponentMap: function (cacheServices) {
  84. var loadedServiceComponentsMap = {};
  85. cacheServices.forEach(function (cacheService) {
  86. var componentsMap = {};
  87. cacheService.host_components.forEach(function (componentId) {
  88. componentsMap[componentId] = true;
  89. });
  90. loadedServiceComponentsMap[cacheService.ServiceInfo.service_name] = componentsMap;
  91. });
  92. return loadedServiceComponentsMap;
  93. },
  94. /**
  95. * add only new host-components to every service
  96. * to update service - host-component relations in model
  97. * @param loadedServiceComponentsMap
  98. * @param serviceToHostComponentIdMap
  99. * @param cacheServices
  100. * @return {boolean}
  101. */
  102. addNewHostComponents: function (loadedServiceComponentsMap, serviceToHostComponentIdMap, cacheServices) {
  103. if (!loadedServiceComponentsMap || !serviceToHostComponentIdMap || !cacheServices) return false;
  104. for (var serviceName in serviceToHostComponentIdMap) {
  105. var loadedService = cacheServices.findProperty('ServiceInfo.service_name', serviceName);
  106. if (serviceToHostComponentIdMap[serviceName] && loadedService) {
  107. serviceToHostComponentIdMap[serviceName].forEach(function (componentId) {
  108. if (!loadedServiceComponentsMap[serviceName][componentId]) {
  109. loadedService.host_components.push(componentId)
  110. }
  111. });
  112. }
  113. }
  114. return true;
  115. }
  116. });