heatmap_host.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 date = require('utils/date/date');
  18. var App = require('app');
  19. App.MainChartsHeatmapHostView = Em.View.extend({
  20. templateName: require('templates/main/charts/heatmap/heatmap_host'),
  21. didInsertElement: function() {
  22. $("#heatmapDetailsBlock").hide();
  23. },
  24. /** @private */
  25. hostClass: 'hostBlock',
  26. /**
  27. * link to host record in App.Host model
  28. */
  29. hostModelLink: function () {
  30. return App.Host.find(this.get('content.hostName'));
  31. }.property('content.hostName'),
  32. /**
  33. * show Host details block and move it near the cursor
  34. *
  35. * @param {Object} event
  36. * @this App.MainChartsHeatmapHostView
  37. */
  38. mouseEnter: function (event) {
  39. var host = this.get('content'),
  40. view = App.MainChartsHeatmapHostDetailView.create();
  41. Object.keys(view.get('details')).forEach(function (i) {
  42. var val = host[i];
  43. switch (i) {
  44. case 'diskUsage':
  45. val = this.getUsage(host['diskTotal'], host['diskFree']);
  46. break;
  47. case 'cpuUsage':
  48. val = this.getCpuUsage(host['cpuSystem'], host['cpuUser']);
  49. break;
  50. case 'memoryUsage':
  51. val = this.getUsage(host['memTotal'], host['memFree']);
  52. break;
  53. case 'hostComponents':
  54. val = this.getHostComponents(host[i]);
  55. }
  56. view.set('details.' + i, val);
  57. }, this);
  58. this.setMetric(view, host);
  59. this.openDetailsBlock(event);
  60. },
  61. /**
  62. * get relative usage of metric in percents
  63. * @param {number} total
  64. * @param {number} free
  65. * @return {string}
  66. */
  67. getUsage: function (total, free) {
  68. var result = 0;
  69. if (Number.isFinite(total) && Number.isFinite(free) && total > 0) {
  70. result = ((total - free) / total) * 100;
  71. }
  72. return result.toFixed(1);
  73. },
  74. /**
  75. * get CPU usage
  76. * @param {number} cpuSystem
  77. * @param {number} cpuUser
  78. * @returns {string}
  79. */
  80. getCpuUsage: function (cpuSystem, cpuUser) {
  81. var result = 0;
  82. if (Number.isFinite(cpuSystem) && Number.isFinite(cpuUser)) {
  83. result = cpuSystem + cpuUser;
  84. }
  85. return result.toFixed(1);
  86. },
  87. /**
  88. * get non-client host-components of host
  89. * @param {Array} components
  90. * @returns {string}
  91. */
  92. getHostComponents: function (components) {
  93. var nonClientComponents = App.get('components.slaves').concat(App.get('components.masters'));
  94. var result = [];
  95. components.forEach(function (componentName) {
  96. if (nonClientComponents.contains(componentName)) {
  97. result.push(App.format.role(componentName));
  98. }
  99. }, this);
  100. return result.join(', ')
  101. },
  102. /**
  103. * show tooltip with host's details
  104. */
  105. openDetailsBlock: function (event) {
  106. var detailsBlock = $("#heatmapDetailsBlock");
  107. detailsBlock.css('top', event.pageY + 10);
  108. detailsBlock.css('left', event.pageX + 10);
  109. detailsBlock.show();
  110. },
  111. /**
  112. * set name and value of selected metric
  113. * @param view
  114. * @param host
  115. */
  116. setMetric: function (view, host) {
  117. var selectedMetric = this.get('controller.selectedMetric');
  118. if (selectedMetric) {
  119. var metricName = selectedMetric.get('name');
  120. var h2vMap = selectedMetric.get('hostToValueMap');
  121. if (h2vMap && metricName) {
  122. var value = h2vMap[host.hostName];
  123. if (Em.isNone(value)) {
  124. value = this.t('charts.heatmap.unknown');
  125. } else {
  126. if (metricName === 'Garbage Collection Time') {
  127. value = date.timingFormat(parseInt(value));
  128. } else {
  129. if (isNaN(value)) {
  130. value = this.t('charts.heatmap.unknown');
  131. } else {
  132. value = value + selectedMetric.get('units');
  133. }
  134. }
  135. }
  136. view.set('details.metricName', metricName);
  137. view.set('details.metricValue', value);
  138. }
  139. }
  140. },
  141. /**
  142. * hide Host details block
  143. *
  144. * @param {Object} e
  145. * @this App.MainChartsHeatmapHostView
  146. */
  147. mouseLeave: function (e) {
  148. $("#heatmapDetailsBlock").hide();
  149. },
  150. hostTemperatureStyle: function () {
  151. var controller = this.get('controller');
  152. var h2sMap = controller.get('hostToSlotMap');
  153. if (h2sMap) {
  154. var hostname = this.get('content.hostName');
  155. if (hostname) {
  156. var slot = h2sMap[hostname];
  157. if (slot > -1) {
  158. var slotDefs = controller.get('selectedMetric.slotDefinitions');
  159. if (slotDefs && slotDefs.length > slot) {
  160. return slotDefs[slot].get('cssStyle');
  161. }
  162. }
  163. }
  164. }
  165. return '';
  166. }.property('controller.hostToSlotMap')
  167. });