component_config_mapper.js 4.9 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 newHostComponentsMap = {};
  37. var cacheServices = App.cache['services'];
  38. var currentServiceComponentsMap = 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 hostComponentLoaded = {};
  44. if (json.items.length > 0 || this.get('model').find().someProperty('staleConfigs', true)) {
  45. json.items.forEach(function (item) {
  46. item.host_components.forEach(function (host_component) {
  47. host_component.id = host_component.HostRoles.component_name + '_' + host_component.HostRoles.host_name;
  48. hostComponentJsonIds.push(host_component.id);
  49. hostComponentJsonMap[host_component.id] = host_component;
  50. });
  51. });
  52. this.get('model').find().forEach(function (hostComponent) {
  53. var hostComponentJson = hostComponentJsonMap[hostComponent.get('id')];
  54. var currentStaleConfigsState = Boolean(hostComponentJson);
  55. var stateChanged = hostComponent.get('staleConfigs') !== currentStaleConfigsState;
  56. if (stateChanged && !hostComponent.get('isMaster')) {
  57. hostComponent.set('staleConfigs', currentStaleConfigsState);
  58. }
  59. //delete load host-components, so only new ones left
  60. delete hostComponentJsonMap[hostComponent.get('id')];
  61. });
  62. hostComponentJsonIds.forEach(function (hcId) {
  63. var newHostComponent = hostComponentJsonMap[hcId];
  64. if (newHostComponent) {
  65. var serviceName = newHostComponent.HostRoles.service_name;
  66. hostComponents.push(this.parseIt(newHostComponent, mapConfig));
  67. if (!newHostComponentsMap[serviceName]) {
  68. newHostComponentsMap[serviceName] = [];
  69. }
  70. if (!currentServiceComponentsMap[serviceName][newHostComponent.id]) {
  71. newHostComponentsMap[serviceName].push(newHostComponent.id);
  72. }
  73. }
  74. }, this);
  75. if (hostComponents.length > 0) {
  76. App.store.loadMany(this.get('model'), hostComponents);
  77. this.addNewHostComponents(newHostComponentsMap, cacheServices);
  78. }
  79. }
  80. console.timeEnd('App.componentConfigMapper execution time');
  81. },
  82. /**
  83. * build map that include loaded host-components to avoid duplicate loading
  84. * @param cacheServices
  85. * @return {Object}
  86. */
  87. buildServiceComponentMap: function (cacheServices) {
  88. var loadedServiceComponentsMap = {};
  89. cacheServices.forEach(function (cacheService) {
  90. var componentsMap = {};
  91. cacheService.host_components.forEach(function (componentId) {
  92. componentsMap[componentId] = true;
  93. });
  94. loadedServiceComponentsMap[cacheService.ServiceInfo.service_name] = componentsMap;
  95. });
  96. return loadedServiceComponentsMap;
  97. },
  98. /**
  99. * add only new host-components to every service
  100. * to update service - host-component relations in model
  101. * @param {object} newHostComponentsMap
  102. * @param {Array} cacheServices
  103. * @return {boolean}
  104. */
  105. addNewHostComponents: function (newHostComponentsMap, cacheServices) {
  106. if (!newHostComponentsMap || !cacheServices) return false;
  107. cacheServices.forEach(function (service) {
  108. if (newHostComponentsMap[service.ServiceInfo.service_name]) {
  109. newHostComponentsMap[service.ServiceInfo.service_name].forEach(function (componentId) {
  110. service.host_components.push(componentId)
  111. });
  112. }
  113. }, this);
  114. return true;
  115. }
  116. });