Prechádzať zdrojové kódy

AMBARI-14022. YARN smart config: Slider shows values in MB even when it is over 10GB

Alex Antonenko 9 rokov pred
rodič
commit
702f832a27

+ 34 - 9
ambari-web/app/views/common/configs/widgets/slider_config_widget_view.js

@@ -354,8 +354,12 @@ App.SliderConfigWidgetView = App.ConfigWidgetView.extend({
     ticks.push(this.valueForTick(maxMirrorValue));
     ticks = ticks.uniq();
     ticks.forEach(function (tick, index, items) {
-      ticksLabels.push((items.length < 5 || index % 2 === 0 || items.length - 1 == index) ? tick + ' ' + self.get('unitLabel') : '');
-    });
+      var label = '';
+      if ((items.length < 5 || index % 2 === 0 || items.length - 1 == index)) {
+        label = this.formatTickLabel(tick, ' ');
+      }
+      ticksLabels.push(label);
+    }, this);
 
     ticks = ticks.uniq();
 
@@ -407,12 +411,9 @@ App.SliderConfigWidgetView = App.ConfigWidgetView.extend({
       tooltip: 'always',
       ticks_labels: ticksLabels,
       step: mirrorStep,
-      formatter: function(val) {
-        if (Em.isArray(val)) {
-          return val[0] + self.get('unitLabel');
-        } else {
-          return val + self.get('unitLabel');
-        }
+      formatter: function (val) {
+        var labelValue = Em.isArray(val) ? val[0] : val;
+        return self.formatTickLabel(labelValue);
       }
     });
 
@@ -562,7 +563,6 @@ App.SliderConfigWidgetView = App.ConfigWidgetView.extend({
    * @method changeBoundariesOnce
    */
   changeBoundariesOnce: function () {
-    var self = this;
     if ($.mocho) {
       //temp fix as it can broke test that doesn't have any connection with this method
       return;
@@ -648,6 +648,31 @@ App.SliderConfigWidgetView = App.ConfigWidgetView.extend({
       return true;
     }
     return false;
+  },
+
+  /**
+   * Returns formatted value of slider label
+   * @param tick - starting value
+   * @param separator - will be inserted between value and unit
+   * @returns {string}
+   */
+  formatTickLabel: function (tick, separator) {
+    var label,
+      separator = separator || '',
+      valueLabel = tick,
+      units = ['B', 'KB', 'MB', 'GB', 'TB'],
+      unitLabel = this.get('unitLabel'),
+      unitLabelIndex = units.indexOf(unitLabel);
+    if (unitLabelIndex > -1) {
+      while (tick > 9999 && unitLabelIndex < units.length - 1) {
+        tick /= 1024;
+        unitLabelIndex++;
+      }
+      unitLabel = units[unitLabelIndex];
+      valueLabel = this._extraRound(tick);
+    }
+    label = valueLabel + separator + unitLabel;
+    return label;
   }
 
 });

+ 56 - 0
ambari-web/test/views/common/configs/widgets/slider_config_widget_view_test.js

@@ -519,4 +519,60 @@ describe('App.SliderConfigWidgetView', function () {
     });
   });
 
+  describe('#formatTickLabel', function () {
+
+    var bytesView,
+      cases = [
+        {
+          unitLabel: 'B',
+          tick: 1024,
+          result: '1024B',
+          title: 'no conversion'
+        },
+        {
+          unitLabel: 'KB',
+          tick: 10240,
+          result: '10MB',
+          title: 'one exponent up conversion'
+        },
+        {
+          unitLabel: 'MB',
+          tick: 10000,
+          result: '9.766GB',
+          title: 'rounding to three decimals'
+        },
+        {
+          unitLabel: 'GB',
+          tick: 10752,
+          separator: ' ',
+          result: '10.5 TB',
+          title: 'rounding to less than three decimals, custom separator'
+        },
+        {
+          unitLabel: 'B',
+          tick: 20971520,
+          result: '20MB',
+          title: 'several exponents up conversion'
+        },
+        {
+          unitLabel: 'TB',
+          tick: 10000,
+          result: '10000TB',
+          title: 'no conversions for the highest exponent unit'
+        }
+      ];
+
+    beforeEach(function () {
+      bytesView = App.SliderConfigWidgetView.create();
+    });
+
+    cases.forEach(function (item) {
+      it(item.title, function () {
+        bytesView.set('unitLabel', item.unitLabel);
+        expect(bytesView.formatTickLabel(item.tick, item.separator)).to.equal(item.result);
+      });
+    });
+
+  });
+
 });