host.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. var misc = require('utils/misc');
  20. DS.attr.transforms.object = {
  21. from: function(serialized) {
  22. return Ember.none(serialized) ? null : Object(serialized);
  23. },
  24. to: function(deserialized) {
  25. return Ember.none(deserialized) ? null : Object(deserialized);
  26. }
  27. };
  28. App.Host = DS.Model.extend({
  29. hostName: DS.attr('string'),
  30. publicHostName: DS.attr('string'),
  31. cluster: DS.belongsTo('App.Cluster'),
  32. hostComponents: DS.hasMany('App.HostComponent'),
  33. cpu: DS.attr('string'),
  34. memory: DS.attr('string'),
  35. diskTotal: DS.attr('number'),
  36. diskFree: DS.attr('number'),
  37. osArch: DS.attr('string'),
  38. ip: DS.attr('string'),
  39. rack: DS.attr('string'),
  40. healthStatus: DS.attr('string'),
  41. cpuUsage: DS.attr('number'),
  42. memoryUsage: DS.attr('number'),
  43. lastHeartBeatTime: DS.attr('number'),
  44. osType: DS.attr("string"),
  45. diskInfo: DS.attr('object'),
  46. loadOne:DS.attr('number'),
  47. loadFive:DS.attr('number'),
  48. loadFifteen:DS.attr('number'),
  49. publicHostNameFormatted: function() {
  50. return this.get('publicHostName').substr(0, 25) + ' ...';
  51. }.property('publicHostName'),
  52. /**
  53. * API return diskTotal and diskFree. Need to save their different
  54. */
  55. diskUsed: function(){
  56. return this.get('diskTotal') - this.get('diskFree');
  57. }.property('diskFree', 'diskTotal'),
  58. /**
  59. * Format diskUsed value to float with 2 digits (also convert to GB)
  60. */
  61. diskUsedFormatted: function() {
  62. return Math.round(this.get('diskUsed') * Math.pow(10, 2)) / Math.pow(10, 2) + 'GB';
  63. }.property('diskUsed'),
  64. /**
  65. * Format diskTotal value to float with 2 digits (also convert to GB)
  66. */
  67. diskTotalFormatted: function() {
  68. return Math.round(this.get('diskTotal') * Math.pow(10, 2)) / Math.pow(10, 2) + 'GB';
  69. }.property('diskTotal'),
  70. /**
  71. * Percent value of used disk space
  72. */
  73. diskUsage: function() {
  74. return (this.get('diskUsed')) / this.get('diskTotal') * 100;
  75. }.property('diskUsed', 'diskTotal'),
  76. /**
  77. * Format diskUsage to float with 2 digits
  78. */
  79. diskUsageFormatted: function() {
  80. if (isNaN(this.get('diskUsage')) || this.get('diskUsage') < 0) {
  81. return 'Data Unavailable';
  82. }
  83. var s = Math.round(this.get('diskUsage') * Math.pow(10, 2)) / Math.pow(10, 2);
  84. if (isNaN(s)) {
  85. s = 0;
  86. }
  87. return s + '%';
  88. }.property('diskUsage'),
  89. diskInfoBar: function() {
  90. if (isNaN(this.get('diskUsage')) || this.get('diskUsage') < 0) {
  91. return this.get('diskUsageFormatted');
  92. }
  93. return this.get('diskUsedFormatted') + '/' + this.get('diskTotalFormatted') + ' (' + this.get('diskUsageFormatted') + ' used)';
  94. }.property('diskUsedFormatted', 'diskTotalFormatted'),
  95. /**
  96. * formatted bytes to appropriate value
  97. */
  98. memoryFormatted: function () {
  99. return misc.formatBandwidth(this.get('memory') * 1000);
  100. }.property('memory'),
  101. /**
  102. * Return true if host not heartbeating last 180 seconds
  103. */
  104. isNotHeartBeating : function(){
  105. return ((new Date()).getTime() - this.get('lastHeartBeatTime')) > 180 * 1000;
  106. }.property('lastHeartBeatTime'),
  107. loadAvg: function() {
  108. if (this.get('loadOne') != null) return this.get('loadOne').toFixed(2);
  109. if (this.get('loadFive') != null) return this.get('loadFive').toFixed(2);
  110. if (this.get('loadFifteen') != null) return this.get('loadFifteen').toFixed(2);
  111. }.property('loadOne', 'loadFive', 'loadFifteen'),
  112. healthClass: function(){
  113. var healthStatus = this.get('healthStatus');
  114. /**
  115. * Do nothing until load
  116. */
  117. if (!this.get('isLoaded') || this.get('isSaving')) {
  118. } else {
  119. var status;
  120. var masterComponents = this.get('hostComponents').filterProperty('isMaster');
  121. var masterComponentsRunning = masterComponents.everyProperty('workStatus', App.HostComponentStatus.started);
  122. var slaveComponents = this.get('hostComponents').filterProperty('isSlave');
  123. var slaveComponentsRunning = slaveComponents.everyProperty('workStatus', App.HostComponentStatus.started);
  124. if (this.get('isNotHeartBeating')) {
  125. status = 'DEAD-YELLOW';
  126. } else if (masterComponentsRunning && slaveComponentsRunning) {
  127. status = 'LIVE';
  128. } else if (masterComponents.length > 0 && !masterComponentsRunning) {
  129. status = 'DEAD';
  130. } else {
  131. status = 'DEAD-ORANGE';
  132. }
  133. if (status) {
  134. healthStatus = status;
  135. }
  136. }
  137. return 'health-status-' + healthStatus;
  138. }.property('healthStatus', 'hostComponents.@each.workStatus')
  139. });
  140. App.Host.FIXTURES = [];