host.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. App.Host = DS.Model.extend({
  21. hostName: DS.attr('string'),
  22. publicHostName: DS.attr('string'),
  23. cluster: DS.belongsTo('App.Cluster'),
  24. hostComponents: DS.hasMany('App.HostComponent'),
  25. cpu: DS.attr('string'),
  26. memory: DS.attr('string'),
  27. diskTotal: DS.attr('number'),
  28. diskFree: DS.attr('number'),
  29. osArch: DS.attr('string'),
  30. ip: DS.attr('string'),
  31. rack: DS.attr('string'),
  32. healthStatus: DS.attr('string'),
  33. cpuUsage: DS.attr('number'),
  34. memoryUsage: DS.attr('number'),
  35. lastHeartBeatTime: DS.attr('number'),
  36. osType: DS.attr("string"),
  37. diskInfo: DS.attr('object'),
  38. loadOne:DS.attr('number'),
  39. loadFive:DS.attr('number'),
  40. loadFifteen:DS.attr('number'),
  41. criticalAlertsCount: function () {
  42. return App.router.get('clusterController.alerts').filterProperty('hostName', this.get('hostName')).filterProperty('isOk', false).filterProperty('ignoredForHosts', false).length;
  43. }.property('App.router.clusterController.alerts.length'),
  44. publicHostNameFormatted: function() {
  45. return this.get('publicHostName').substr(0, 20) + ' ...';
  46. }.property('publicHostName'),
  47. /**
  48. * API return diskTotal and diskFree. Need to save their different
  49. */
  50. diskUsed: function(){
  51. return this.get('diskTotal') - this.get('diskFree');
  52. }.property('diskFree', 'diskTotal'),
  53. /**
  54. * Format diskUsed value to float with 2 digits (also convert to GB)
  55. */
  56. diskUsedFormatted: function() {
  57. return Math.round(this.get('diskUsed') * Math.pow(10, 2)) / Math.pow(10, 2) + 'GB';
  58. }.property('diskUsed'),
  59. /**
  60. * Format diskTotal value to float with 2 digits (also convert to GB)
  61. */
  62. diskTotalFormatted: function() {
  63. return Math.round(this.get('diskTotal') * Math.pow(10, 2)) / Math.pow(10, 2) + 'GB';
  64. }.property('diskTotal'),
  65. /**
  66. * Percent value of used disk space
  67. */
  68. diskUsage: function() {
  69. return (this.get('diskUsed')) / this.get('diskTotal') * 100;
  70. }.property('diskUsed', 'diskTotal'),
  71. /**
  72. * Format diskUsage to float with 2 digits
  73. */
  74. diskUsageFormatted: function() {
  75. if (isNaN(this.get('diskUsage')) || this.get('diskUsage') < 0) {
  76. return 'Data Unavailable';
  77. }
  78. var s = Math.round(this.get('diskUsage') * Math.pow(10, 2)) / Math.pow(10, 2);
  79. if (isNaN(s)) {
  80. s = 0;
  81. }
  82. return s + '%';
  83. }.property('diskUsage'),
  84. diskInfoBar: function() {
  85. if (isNaN(this.get('diskUsage')) || this.get('diskUsage') < 0) {
  86. return this.get('diskUsageFormatted');
  87. }
  88. return this.get('diskUsedFormatted') + '/' + this.get('diskTotalFormatted') + ' (' + this.get('diskUsageFormatted')
  89. + ' ' + Em.I18n.t('services.service.summary.diskInfoBar.used') + ')';
  90. }.property('diskUsedFormatted', 'diskTotalFormatted'),
  91. /**
  92. * formatted bytes to appropriate value
  93. */
  94. memoryFormatted: function () {
  95. return misc.formatBandwidth(this.get('memory') * 1024);
  96. }.property('memory'),
  97. /**
  98. * Return true if the host has not sent heartbeat within the last 180 seconds
  99. */
  100. isNotHeartBeating : function() {
  101. return (App.testMode) ? false : ((new Date()).getTime() - this.get('lastHeartBeatTime')) > 180 * 1000;
  102. }.property('lastHeartBeatTime'),
  103. loadAvg: function() {
  104. if (this.get('loadOne') != null) return this.get('loadOne').toFixed(2);
  105. if (this.get('loadFive') != null) return this.get('loadFive').toFixed(2);
  106. if (this.get('loadFifteen') != null) return this.get('loadFifteen').toFixed(2);
  107. }.property('loadOne', 'loadFive', 'loadFifteen'),
  108. // Instead of making healthStatus a computed property that listens on hostComponents.@each.workStatus,
  109. // we are creating a separate observer _updateHealthStatus. This is so that healthStatus is updated
  110. // only once after the run loop. This is because Ember invokes the computed property every time
  111. // a property that it depends on changes. For example, App.statusMapper's map function would invoke
  112. // the computed property too many times and freezes the UI without this hack.
  113. // See http://stackoverflow.com/questions/12467345/ember-js-collapsing-deferring-expensive-observers-or-computed-properties
  114. healthClass: '',
  115. _updateHealthClass: function(){
  116. Ember.run.once(this, 'updateHealthClass');
  117. }.observes('healthStatus', 'hostComponents.@each.workStatus'),
  118. updateHealthClass: function(){
  119. var healthStatus = this.get('healthStatus');
  120. /**
  121. * Do nothing until load
  122. */
  123. if (!this.get('isLoaded') || this.get('isSaving')) {
  124. } else {
  125. var status;
  126. var masterComponents = this.get('hostComponents').filterProperty('isMaster');
  127. var masterComponentsRunning = masterComponents.everyProperty('workStatus', App.HostComponentStatus.started);
  128. var slaveComponents = this.get('hostComponents').filterProperty('isSlave');
  129. var slaveComponentsRunning = slaveComponents.everyProperty('workStatus', App.HostComponentStatus.started);
  130. if (this.get('isNotHeartBeating') || healthStatus == 'UNKNOWN') {
  131. status = 'DEAD-YELLOW';
  132. } else if (masterComponentsRunning && slaveComponentsRunning) {
  133. status = 'LIVE';
  134. } else if (masterComponents.length > 0 && !masterComponentsRunning) {
  135. status = 'DEAD-RED';
  136. } else {
  137. status = 'DEAD-ORANGE';
  138. }
  139. if (status) {
  140. healthStatus = status;
  141. }
  142. }
  143. this.set('healthClass', 'health-status-' + healthStatus);
  144. },
  145. healthToolTip: function(){
  146. var hostComponents = this.get('hostComponents').filter(function(item){
  147. if(item.get('workStatus') !== App.HostComponentStatus.started){
  148. return true;
  149. }
  150. });
  151. var output = '';
  152. switch (this.get('healthClass')){
  153. case 'health-status-DEAD-RED':
  154. hostComponents = hostComponents.filterProperty('isMaster', true);
  155. output = Em.I18n.t('hosts.host.healthStatus.mastersDown');
  156. hostComponents.forEach(function(hc, index){
  157. output += (index == (hostComponents.length-1)) ? hc.get('displayName') : (hc.get('displayName')+", ");
  158. }, this);
  159. break;
  160. case 'health-status-DEAD-YELLOW':
  161. output = Em.I18n.t('hosts.host.healthStatus.heartBeatNotReceived');
  162. break;
  163. case 'health-status-DEAD-ORANGE':
  164. hostComponents = hostComponents.filterProperty('isSlave', true);
  165. output = Em.I18n.t('hosts.host.healthStatus.slavesDown');
  166. hostComponents.forEach(function(hc, index){
  167. output += (index == (hostComponents.length-1)) ? hc.get('displayName') : (hc.get('displayName')+", ");
  168. }, this);
  169. break;
  170. case 'health-status-LIVE':
  171. output = Em.I18n.t('hosts.host.healthStatus.allUp');
  172. break;
  173. }
  174. return output;
  175. }.property('hostComponents.@each.workStatus')
  176. });
  177. App.Host.FIXTURES = [];