component_config_mapper.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. display_name: 'HostRoles.display_name',
  26. host_name: 'HostRoles.host_name',
  27. $ha_status: '',
  28. $display_name_advanced: '',
  29. stale_configs: 'HostRoles.stale_configs',
  30. host_id: 'HostRoles.host_name',
  31. service_id: 'HostRoles.service_name',
  32. admin_state: 'HostRoles.desired_admin_state'
  33. },
  34. map: function (json) {
  35. console.time('App.componentConfigMapper execution time');
  36. var hostComponents = [];
  37. var newHostComponentsMap = {};
  38. var cacheServices = App.cache['services'];
  39. var currentServiceComponentsMap = this.buildServiceComponentMap(cacheServices);
  40. var mapConfig = this.get('config');
  41. // We do not want to parse JSON if there is no need to
  42. var hostComponentJsonMap = {};
  43. var hostComponentJsonIds = [];
  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 id = hostComponent.get('id');
  54. var hostComponentJson = hostComponentJsonMap[id];
  55. var currentStaleConfigsState = Boolean(hostComponentJson);
  56. var stateChanged = hostComponent.get('staleConfigs') !== currentStaleConfigsState;
  57. if (stateChanged) {
  58. hostComponent.set('staleConfigs', currentStaleConfigsState);
  59. }
  60. //delete loaded host-components, so only new ones left
  61. delete hostComponentJsonMap[id];
  62. });
  63. hostComponentJsonIds.forEach(function (hcId) {
  64. var newHostComponent = hostComponentJsonMap[hcId];
  65. if (newHostComponent) {
  66. var serviceName = newHostComponent.HostRoles.service_name;
  67. hostComponents.push(this.parseIt(newHostComponent, mapConfig));
  68. if (!newHostComponentsMap[serviceName]) {
  69. newHostComponentsMap[serviceName] = [];
  70. }
  71. if (currentServiceComponentsMap[serviceName] && !currentServiceComponentsMap[serviceName][newHostComponent.id]) {
  72. newHostComponentsMap[serviceName].push(newHostComponent.id);
  73. }
  74. }
  75. }, this);
  76. if (hostComponents.length > 0) {
  77. App.store.commit();
  78. App.store.loadMany(this.get('model'), hostComponents);
  79. this.addNewHostComponents(newHostComponentsMap, cacheServices);
  80. }
  81. }
  82. console.timeEnd('App.componentConfigMapper execution time');
  83. },
  84. /**
  85. * build map that include loaded host-components to avoid duplicate loading
  86. * @param cacheServices
  87. * @return {Object}
  88. */
  89. buildServiceComponentMap: function (cacheServices) {
  90. var loadedServiceComponentsMap = {};
  91. cacheServices.forEach(function (cacheService) {
  92. var componentsMap = {};
  93. cacheService.host_components.forEach(function (componentId) {
  94. componentsMap[componentId] = true;
  95. });
  96. loadedServiceComponentsMap[cacheService.ServiceInfo.service_name] = componentsMap;
  97. });
  98. return loadedServiceComponentsMap;
  99. },
  100. /**
  101. * add only new host-components to every service
  102. * to update service - host-component relations in model
  103. * @param {object} newHostComponentsMap
  104. * @param {Array} cacheServices
  105. * @return {boolean}
  106. */
  107. addNewHostComponents: function (newHostComponentsMap, cacheServices) {
  108. if (!newHostComponentsMap || !cacheServices) return false;
  109. cacheServices.forEach(function (service) {
  110. if (newHostComponentsMap[service.ServiceInfo.service_name]) {
  111. newHostComponentsMap[service.ServiceInfo.service_name].forEach(function (componentId) {
  112. service.host_components.push(componentId)
  113. });
  114. }
  115. }, this);
  116. return true;
  117. }
  118. });