hosts_mapper.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.hostsMapper = App.QuickDataMapper.create({
  19. model: App.Host,
  20. config: {
  21. id: 'Hosts.host_name',
  22. host_name: 'Hosts.host_name',
  23. public_host_name: 'Hosts.public_host_name',
  24. cluster_id: 'Hosts.cluster_name',// 1
  25. rack: 'Hosts.rack_info',
  26. host_components_key: 'host_components',
  27. host_components_type: 'array',
  28. host_components: {
  29. item: 'id'
  30. },
  31. cpu: 'Hosts.cpu_count',
  32. memory: 'Hosts.total_mem',
  33. disk_info: 'Hosts.disk_info',
  34. disk_total: 'metrics.disk.disk_total',
  35. disk_free: 'metrics.disk.disk_free',
  36. health_status: 'Hosts.host_status',
  37. load_one: 'metrics.load.load_one',
  38. load_five: 'metrics.load.load_five',
  39. load_fifteen: 'metrics.load.load_fifteen',
  40. cpu_system: 'metrics.cpu.cpu_system',
  41. cpu_user: 'metrics.cpu.cpu_user',
  42. mem_total: 'metrics.memory.mem_total',
  43. mem_free: 'metrics.memory.mem_free',
  44. last_heart_beat_time: "Hosts.last_heartbeat_time",
  45. os_arch: 'Hosts.os_arch',
  46. os_type: 'Hosts.os_type',
  47. ip: 'Hosts.ip'
  48. },
  49. map: function (json) {
  50. console.time('App.hostsMapper execution time');
  51. if (json.items) {
  52. var result = [];
  53. var hostIds = {};
  54. var cacheData = App.cache['Hosts'];
  55. json.items.forEach(function (item) {
  56. //receive host_components when added hosts
  57. item.host_components = item.host_components || [];
  58. item.host_components.forEach(function (host_component) {
  59. host_component.id = host_component.HostRoles.component_name + "_" + host_component.HostRoles.host_name;
  60. }, this);
  61. hostIds[item.Hosts.host_name] = true;
  62. result.push(this.parseIt(item, this.config));
  63. }, this);
  64. result = this.sortByPublicHostName(result);
  65. var clientHosts = App.Host.find();
  66. if (clientHosts) {
  67. // hosts were added
  68. if (clientHosts.get('length') < result.length) {
  69. result.forEach(function (host) {
  70. cacheData[host.id] = {
  71. ip: host.ip,
  72. os_arch: host.os_arch,
  73. os_type: host.os_type,
  74. public_host_name: host.public_host_name,
  75. memory: host.memory,
  76. cpu: host.cpu,
  77. host_components: host.host_components
  78. };
  79. });
  80. }
  81. // hosts were deleted
  82. if (clientHosts.get('length') > result.length) {
  83. clientHosts.forEach(function (host) {
  84. if (host && !hostIds[host.get('hostName')]) {
  85. // Delete old ones as new ones will be
  86. // loaded by loadMany().
  87. host.deleteRecord();
  88. host.get('stateManager').transitionTo('loading');
  89. delete cacheData[host.get('id')];
  90. }
  91. });
  92. }
  93. }
  94. //restore properties from cache instead request them from server
  95. result.forEach(function (host) {
  96. var cacheHost = cacheData[host.id];
  97. if (cacheHost) {
  98. host.ip = cacheHost.ip;
  99. host.os_arch = cacheHost.os_arch;
  100. host.os_type = cacheHost.os_type;
  101. host.public_host_name = cacheHost.public_host_name;
  102. host.memory = cacheHost.memory;
  103. host.cpu = cacheHost.cpu;
  104. host.host_components = cacheHost.host_components;
  105. }
  106. });
  107. App.store.loadMany(this.get('model'), result);
  108. }
  109. console.timeEnd('App.hostsMapper execution time');
  110. },
  111. /**
  112. * Default data sorting by public_host_name field
  113. * @param data
  114. * @return {Array}
  115. */
  116. sortByPublicHostName: function(data) {
  117. data.sort(function(a, b) {
  118. var ap = a.public_host_name;
  119. var bp = b.public_host_name;
  120. if (ap > bp) return 1;
  121. if (ap < bp) return -1;
  122. return 0;
  123. });
  124. return data;
  125. }
  126. });