hosts_mapper.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. tmp_result:[],
  21. config: {
  22. id: 'Hosts.host_name',
  23. host_name: 'Hosts.host_name',
  24. public_host_name: 'Hosts.public_host_name',
  25. cluster_id: 'Hosts.cluster_name',// 1
  26. rack: 'Hosts.rack_info',
  27. host_components_key: 'host_components',
  28. host_components_type: 'array',
  29. host_components: {
  30. item: 'id'
  31. },
  32. cpu: 'Hosts.cpu_count',
  33. memory: 'Hosts.total_mem',
  34. disk_info: 'Hosts.disk_info',
  35. disk_total: 'metrics.disk.disk_total',
  36. disk_free: 'metrics.disk.disk_free',
  37. health_status: 'Hosts.host_status',
  38. load_one: 'metrics.load.load_one',
  39. load_five: 'metrics.load.load_five',
  40. load_fifteen: 'metrics.load.load_fifteen',
  41. cpu_usage: 'cpu_usage',
  42. memory_usage: 'memory_usage',
  43. $network_usage: 36,
  44. $io_usage: 39,
  45. last_heart_beat_time: "Hosts.last_heartbeat_time",
  46. os_arch: 'Hosts.os_arch',
  47. os_type: 'Hosts.os_type',
  48. ip: 'Hosts.ip'
  49. },
  50. map: function (json) {
  51. var self = this;
  52. if (!this.get('model')) {
  53. return;
  54. }
  55. if (json.items) {
  56. json.items.forEach(function (item) {
  57. var result = [];
  58. self.tmp_result=[];
  59. // Disk Usage
  60. if (item.metrics && item.metrics.disk && item.metrics.disk.disk_total && item.metrics.disk.disk_free) {
  61. var diskUsed = item.metrics.disk.disk_total - item.metrics.disk.disk_free;
  62. var diskUsedPercent = (100 * diskUsed) / item.metrics.disk.disk_total;
  63. item.disk_usage = diskUsedPercent.toFixed(1);
  64. }
  65. // CPU Usage
  66. if (item.metrics && item.metrics.cpu && item.metrics.cpu.cpu_system && item.metrics.cpu.cpu_user) {
  67. var cpuUsedPercent = item.metrics.cpu.cpu_system + item.metrics.cpu.cpu_user;
  68. item.cpu_usage = cpuUsedPercent.toFixed(1);
  69. }
  70. // Memory Usage
  71. if (item.metrics && item.metrics.memory && item.metrics.memory.mem_free && item.metrics.memory.mem_total) {
  72. var memUsed = item.metrics.memory.mem_total - item.metrics.memory.mem_free;
  73. var memUsedPercent = (100 * memUsed) / item.metrics.memory.mem_total;
  74. item.memory_usage = memUsedPercent.toFixed(1);
  75. }
  76. if(App.Host.find(item.Hosts.host_name).get("hostName") == item.Hosts.host_name){ // UPDATE
  77. App.Host.find(item.Hosts.host_name).set("cpuUsage", item.cpu_usage);
  78. App.Host.find(item.Hosts.host_name).set("diskUsage", item.disk_usage);
  79. App.Host.find(item.Hosts.host_name).set("memoryUsage", item.memory_usage);
  80. self.tmp_result.push(this.parseIt(item, this.config));
  81. $.map(self.tmp_result[0], function (e,a){
  82. //console.log(a, "------------------", self.tmp_result[0][a])
  83. if(typeof(e) === "string" || typeof(e) === "number")
  84. {
  85. var modelName=self.parseName(a);
  86. if(typeof(App.Host.find(self.tmp_result[0].host_name).get(modelName)) !== "undefined"){
  87. App.Host.find(self.tmp_result[0].host_name).set(modelName, self.tmp_result[0][a]);
  88. // console.log(modelName, "------------------", self.tmp_result[0][a])
  89. }
  90. }
  91. })
  92. }else{ // ADD
  93. item.host_components.forEach(function (host_component) {
  94. host_component.id = host_component.HostRoles.component_name + "_" + host_component.HostRoles.host_name;
  95. }, this);
  96. result.push(this.parseIt(item, this.config));
  97. App.store.loadMany(this.get('model'), result);
  98. }
  99. }, this);
  100. //console.log(this.get('model'), result);
  101. }
  102. },
  103. parseName:function(name){
  104. var new_name = name.replace(/_\w/g,replacer);
  105. function replacer(str, p1, p2, offset, s)
  106. {
  107. return str[1].toUpperCase();
  108. }
  109. return new_name;
  110. }
  111. });