Code coverage report for app/views/main/charts/heatmap/heatmap_host.js

Statements: 91.55% (65 / 71)      Branches: 100% (43 / 43)      Functions: 78.57% (11 / 14)      Lines: 91.55% (65 / 71)      Ignored: none     

All files » app/views/main/charts/heatmap/ » heatmap_host.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206    1                                   1 1 1   1                           1                   19     19 19   19   4 4   4 4   4 4   4     19   19 19                   5 4 1 3 1   2     1                   5   5 1   5                   4   4 1   4                 1 1   1 3 2     1                                       6 6   6 5 5 5 5 4 4 2   2   4 4                               6 6 6 5 5 4 4 3 3 1         5      
'use strict';
 
;require.register("views/main/charts/heatmap/heatmap_host", function (exports, require, module) {
  /**
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements. See the NOTICE file distributed with this
   * work for additional information regarding copyright ownership. The ASF
   * licenses this file to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   * License for the specific language governing permissions and limitations under
   * the License.
   */
 
  var date = require('utils/date/date');
  var numberUtils = require('utils/number_utils');
  var App = require('app');
 
  App.MainChartsHeatmapHostView = Em.View.extend({
    templateName: require('templates/main/charts/heatmap/heatmap_host'),
 
    didInsertElement: function didInsertElement() {
      $("#heatmapDetailsBlock").hide();
    },
 
    /** @private */
    hostClass: 'hostBlock',
 
    /**
     * link to host record in App.Host model
     */
    hostModelLink: function () {
      return App.Host.find(this.get('content.hostName'));
    }.property('content.hostName'),
 
    /**
     * show Host details block and move it near the cursor
     *
     * @param {Object} event
     * @this App.MainChartsHeatmapHostView
     */
    mouseEnter: function mouseEnter(event) {
      var host = this.get('content'),
          view = App.MainChartsHeatmapHostDetailView.create();
 
      Object.keys(view.get('details')).forEach(function (i) {
        var val = host[i];
 
        switch (i) {
          case 'diskUsage':
            val = this.getUsage(host['diskTotal'], host['diskFree']);
            break;
          case 'cpuUsage':
            val = this.getCpuUsage(host['cpuSystem'], host['cpuUser']);
            break;
          case 'memoryUsage':
            val = this.getUsage(host['memTotal'], host['memFree']);
            break;
          case 'hostComponents':
            val = this.getHostComponents(host[i]);
        }
 
        view.set('details.' + i, val);
      }, this);
      this.setMetric(view, host);
      this.openDetailsBlock(event);
    },
 
    /**
     *
     * @param {string} number
     * @param {string} units
     * @returns {string}
     */
    convertValue: function convertValue(number, units) {
      if (Number.isFinite(Number(number)) && Number(number) > 0) {
        if (units === 'MB') {
          return (Number(number) / numberUtils.BYTES_IN_MB).toFixed(2) + units;
        } else if (units === 'ms') {
          return date.timingFormat(number);
        } else {
          return number + units;
        }
      }
      return number;
    },
 
    /**
     * get relative usage of metric in percents
     * @param {number} total
     * @param {number} free
     * @return {string}
     */
    getUsage: function getUsage(total, free) {
      var result = 0;
 
      if (Number.isFinite(total) && Number.isFinite(free) && total > 0) {
        result = (total - free) / total * 100;
      }
      return result.toFixed(1);
    },
 
    /**
     * get CPU usage
     * @param {number} cpuSystem
     * @param {number} cpuUser
     * @returns {string}
     */
    getCpuUsage: function getCpuUsage(cpuSystem, cpuUser) {
      var result = 0;
 
      if (Number.isFinite(cpuSystem) && Number.isFinite(cpuUser)) {
        result = cpuSystem + cpuUser;
      }
      return result.toFixed(1);
    },
 
    /**
     * get non-client host-components of host
     * @param {Array} components
     * @returns {string}
     */
    getHostComponents: function getHostComponents(components) {
      var nonClientComponents = App.get('components.slaves').concat(App.get('components.masters'));
      var result = [];
 
      components.forEach(function (componentName) {
        if (nonClientComponents.contains(componentName)) {
          result.push(App.format.role(componentName, false));
        }
      }, this);
      return result.join(', ');
    },
 
    /**
     * show tooltip with host's details
     */
    openDetailsBlock: function openDetailsBlock(event) {
      var detailsBlock = $("#heatmapDetailsBlock");
 
      detailsBlock.css('top', event.pageY + 10);
      detailsBlock.css('left', event.pageX + 10);
      detailsBlock.show();
    },
 
    /**
     * set name and value of selected metric
     * @param view
     * @param host
     */
    setMetric: function setMetric(view, host) {
      var selectedMetric = this.get('controller.selectedMetric');
      var hostToSlotMap = this.get('controller.hostToSlotMap');
 
      if (selectedMetric) {
        var slotDefinitions = selectedMetric.get('slotDefinitions');
        var metricName = selectedMetric.get('name');
        var h2vMap = selectedMetric.get('hostToValueMap');
        if (h2vMap && metricName) {
          var value = h2vMap[host.hostName];
          if (Em.isNone(value) || isNaN(Number(value))) {
            value = slotDefinitions[hostToSlotMap[host.hostName]].get('label');
          } else {
            value = this.convertValue(value, selectedMetric.get('units'));
          }
          view.set('details.metricName', metricName);
          view.set('details.metricValue', value);
        }
      }
    },
 
    /**
     * hide Host details block
     *
     * @param {Object} e
     * @this App.MainChartsHeatmapHostView
     */
    mouseLeave: function mouseLeave(e) {
      $("#heatmapDetailsBlock").hide();
    },
 
    hostTemperatureStyle: function () {
      var controller = this.get('controller');
      var h2sMap = controller.get('hostToSlotMap');
      if (h2sMap) {
        var hostname = this.get('content.hostName');
        if (hostname) {
          var slot = h2sMap[hostname];
          if (slot > -1) {
            var slotDefs = controller.get('selectedMetric.slotDefinitions');
            if (slotDefs && slotDefs.length > slot) {
              return slotDefs[slot].get('cssStyle');
            }
          }
        }
      }
      return '';
    }.property('controller.hostToSlotMap')
  });
});