hosts_mapper.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 stringUtils = require('utils/string_utils');
  19. App.hostsMapper = App.QuickDataMapper.create({
  20. model: App.Host,
  21. config: {
  22. id: 'Hosts.host_name',
  23. host_name: 'Hosts.host_name',
  24. public_host_name: 'Hosts.public_host_name',
  25. cluster_id: 'cluster_id',// Hosts.cluster_name
  26. rack: 'Hosts.rack_info',
  27. host_components_key: 'host_components',
  28. host_components_type: 'array',
  29. host_components: {
  30. item: 'id'
  31. },
  32. alerts_summary: 'alerts_summary',
  33. critical_warning_alerts_count: 'critical_warning_alerts_count',
  34. cpu: 'Hosts.cpu_count',
  35. cpu_physical: 'Hosts.ph_cpu_count',
  36. memory: 'Hosts.total_mem',
  37. disk_info: 'Hosts.disk_info',
  38. disk_total: 'metrics.disk.disk_total',
  39. disk_free: 'metrics.disk.disk_free',
  40. health_status: 'Hosts.host_status',
  41. load_one: 'metrics.load.load_one',
  42. load_five: 'metrics.load.load_five',
  43. load_fifteen: 'metrics.load.load_fifteen',
  44. cpu_system: 'metrics.cpu.cpu_system',
  45. cpu_user: 'metrics.cpu.cpu_user',
  46. mem_total: 'metrics.memory.mem_total',
  47. mem_free: 'metrics.memory.mem_free',
  48. last_heart_beat_time: "Hosts.last_heartbeat_time",
  49. os_arch: 'Hosts.os_arch',
  50. os_type: 'Hosts.os_type',
  51. ip: 'Hosts.ip',
  52. passive_state: 'Hosts.maintenance_state',
  53. index: 'index'
  54. },
  55. hostComponentConfig: {
  56. component_name: 'HostRoles.component_name',
  57. service_id: 'HostRoles.service_name',
  58. passive_state: 'HostRoles.maintenance_state',
  59. work_status: 'HostRoles.state',
  60. stale_configs: 'HostRoles.stale_configs',
  61. host_name: 'host_name',
  62. admin_state: 'HostRoles.desired_admin_state'
  63. },
  64. stackVersionConfig: {
  65. id: 'HostStackVersions.id',
  66. stack: 'HostStackVersions.stack',
  67. repo_id: 'repository_versions[0].RepositoryVersions.id',
  68. repo_version: 'repository_versions[0].RepositoryVersions.repository_version',
  69. display_name: 'repository_versions[0].RepositoryVersions.display_name',
  70. version: 'HostStackVersions.version',
  71. status: 'HostStackVersions.state',
  72. host_name: 'host_name',
  73. host_id: 'host_name'
  74. },
  75. map: function (json, returnMapped) {
  76. returnMapped = !!returnMapped;
  77. console.time('App.hostsMapper execution time');
  78. if (json.items) {
  79. var hostsWithFullInfo = [];
  80. var hostIds = {};
  81. var components = [];
  82. var stackVersions = [];
  83. var componentsIdMap = {};
  84. var cacheServices = App.cache['services'];
  85. var loadedServiceComponentsMap = App.get('componentConfigMapper').buildServiceComponentMap(cacheServices);
  86. var serviceToHostComponentIdMap = {};
  87. json.items.forEach(function (item, index) {
  88. item.host_components = item.host_components || [];
  89. item.host_components.forEach(function (host_component) {
  90. host_component.id = host_component.HostRoles.component_name + "_" + item.Hosts.host_name;
  91. var component = this.parseIt(host_component, this.hostComponentConfig);
  92. var serviceName = host_component.HostRoles.service_name;
  93. component.id = host_component.HostRoles.component_name + "_" + item.Hosts.host_name;
  94. component.host_id = item.Hosts.host_name;
  95. component.host_name = item.Hosts.host_name;
  96. components.push(component);
  97. componentsIdMap[component.id] = component;
  98. if (!serviceToHostComponentIdMap[serviceName]) {
  99. serviceToHostComponentIdMap[serviceName] = [];
  100. }
  101. serviceToHostComponentIdMap[serviceName].push(component.id);
  102. }, this);
  103. if (App.get('supports.stackUpgrade')) {
  104. item.stack_versions.forEach(function (stackVersion) {
  105. stackVersion.host_name = item.Hosts.host_name;
  106. stackVersions.push(this.parseIt(stackVersion, this.stackVersionConfig));
  107. }, this);
  108. }
  109. var alertsSummary = item.alerts_summary;
  110. item.critical_warning_alerts_count = alertsSummary ? (alertsSummary.CRITICAL || 0) + (alertsSummary.WARNING || 0) : 0;
  111. item.cluster_id = App.get('clusterName');
  112. item.index = index;
  113. if (App.get('supports.stackUpgrade')) {
  114. this.config = $.extend(this.config, {
  115. stack_versions_key: 'stack_versions',
  116. stack_versions_type: 'array',
  117. stack_versions: {
  118. item: 'HostStackVersions.id'
  119. }
  120. })
  121. }
  122. var parsedItem = this.parseIt(item, this.config);
  123. parsedItem.is_requested = true;
  124. hostIds[item.Hosts.host_name] = parsedItem;
  125. hostsWithFullInfo.push(parsedItem);
  126. }, this);
  127. if(returnMapped){
  128. return hostsWithFullInfo;
  129. }
  130. App.Host.find().forEach(function (host) {
  131. if (!hostIds[host.get('hostName')]) {
  132. host.set('isRequested', false);
  133. }
  134. });
  135. App.HostComponent.find().filterProperty('isMaster').forEach(function(component) {
  136. if (componentsIdMap[component.get('id')]) componentsIdMap[component.get('id')].display_name_advanced = component.get('displayNameAdvanced');
  137. });
  138. App.store.commit();
  139. if (App.get('supports.stackUpgrade')) {
  140. App.store.loadMany(App.HostStackVersion, stackVersions);
  141. }
  142. App.store.loadMany(App.HostComponent, components);
  143. App.store.loadMany(App.Host, hostsWithFullInfo);
  144. var itemTotal = parseInt(json.itemTotal);
  145. if (!isNaN(itemTotal)) {
  146. App.router.set('mainHostController.filteredCount', itemTotal);
  147. }
  148. //bind host-components with service records
  149. App.get('componentConfigMapper').addNewHostComponents(loadedServiceComponentsMap, serviceToHostComponentIdMap, cacheServices);
  150. }
  151. console.timeEnd('App.hostsMapper execution time');
  152. }
  153. });