Skip to content

Commit

Permalink
Merge pull request #1447 from yaacov/units-ad-hoc
Browse files Browse the repository at this point in the history
Check for base unit when adjusting unit label
(cherry picked from commit e2865c1)

https://bugzilla.redhat.com/show_bug.cgi?id=1478379
  • Loading branch information
himdel authored and simaishi committed Aug 4, 2017
1 parent 10ea79f commit 820ad5e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ManageIQ.angular.app.controller('adHocMetricsController', ['$http', '$window', '
dash.refreshList = httpUtils.refreshList;
dash.refreshGraph = httpUtils.refreshGraph;
dash.setFilterOptions = utils.setFilterOptions;
dash.metricPrefix = utils.metricPrefix;
dash.setPage = httpUtils.setPage;

var pageSetup = function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,43 @@ angular.module('miq.util').factory('metricsUtilsFactory', function() {
data[0].value.toFixed(2) + '</div>';
};

var metricPrefix = function(maxValue) {
var exp = parseInt(Math.log10(maxValue) / 3, 10) * 3;
var metricPrefixes = {
3: {prefix: 'k', divisor: Math.pow(10, 3)},
6: {prefix: 'M', divisor: Math.pow(10, 6)},
9: {prefix: 'G', divisor: Math.pow(10, 9)},
12: {prefix: 'T', divisor: Math.pow(10, 12)},
15: {prefix: 'P', divisor: Math.pow(10, 15)}
var metricPrefix = function(maxValue, units) {
var metricPrefixes;
var baseUnit;
var baseUnitMultiplier;
var baseUnitMaxValue;
var exp;

// get base unit for special case units
switch (units) {
case 'millisecond':
case 'ms':
baseUnitMultiplier = Math.pow(10, -3);
baseUnit = 's';
break;
case 'ns':
baseUnitMultiplier = Math.pow(10, -9);
baseUnit = 's';
break;
default:
baseUnitMultiplier = 1;
baseUnit = units;
}

// adjust to base units and calc exponent
baseUnitMaxValue = maxValue * baseUnitMultiplier;
exp = ~~(Math.log10(baseUnitMaxValue) / 3) * 3;

// calc output unit label and multiplier
metricPrefixes = {
3: {unitLabel: 'K' + baseUnit, multiplier: baseUnitMultiplier * Math.pow(10, -3)},
6: {unitLabel: 'M' + baseUnit, multiplier: baseUnitMultiplier * Math.pow(10, -6)},
9: {unitLabel: 'G' + baseUnit, multiplier: baseUnitMultiplier * Math.pow(10, -9)},
12: {unitLabel: 'T' + baseUnit, multiplier: baseUnitMultiplier * Math.pow(10, -12)},
15: {unitLabel: 'P' + baseUnit, multiplier: baseUnitMultiplier * Math.pow(10, -15)}
};

return metricPrefixes[exp] || {prefix: '', divisor: 1};
return metricPrefixes[exp] || {unitLabel: baseUnit, multiplier: baseUnitMultiplier};
};

var getContainerDashboardData = function(item) {
Expand All @@ -124,11 +150,11 @@ angular.module('miq.util').factory('metricsUtilsFactory', function() {

item.data = item.data.sort(function(a, b) { return a.timestamp > b.timestamp; });
var maxValue = Math.max.apply(Math, item.data.map(function(o) { return o.value; }))
var m = metricPrefix(maxValue);
var m = metricPrefix(maxValue, item.tags.units || '');

var id = _.uniqueId('ChartId_');
var label = item.tags.descriptor_name || item.id;
var units = m.prefix + (item.tags.units || '');
var units = m.unitLabel;

item.lastValues = {
total: '100',
Expand All @@ -137,7 +163,7 @@ angular.module('miq.util').factory('metricsUtilsFactory', function() {
};
angular.forEach(item.data, function(d) {
item.lastValues.xData.push(new Date(d.timestamp));
item.lastValues.yData.push((d.value / m.divisor).toFixed(2));
item.lastValues.yData.push((d.value * m.multiplier).toFixed(2));
});
item.lastValue = '' + item.lastValues.yData[item.data.length] + ' ' + units

Expand All @@ -156,7 +182,8 @@ angular.module('miq.util').factory('metricsUtilsFactory', function() {
getContainerParamsData: getContainerParamsData,
getContainerDashboardData: getContainerDashboardData,
checkResponse: checkResponse,
setFilterOptions: setFilterOptions
setFilterOptions: setFilterOptions,
metricPrefix: metricPrefix
}
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,22 @@ describe('adHocMetricsController', function() {
expect($controller.timeFilter.range_count).toBe(9);
});
});

describe('utility functions', function() {
it('should calculate units correctly', function() {
var m;

m = $controller.metricPrefix(10000, 'ms');
expect(m.multiplier).toBe(Math.pow(10, -3));
expect(m.unitLable).toBe('s');

m = $controller.metricPrefix(10000, 'ns');
expect(m.multiplier).toBe(Math.pow(10, -9));
expect(m.unitLable).toBe('s');

m = $controller.metricPrefix(10000, 's');
expect(m.multiplier).toBe(Math.pow(10, -3));
expect(m.unitLable).toBe('Ks');
});
});
});

0 comments on commit 820ad5e

Please sign in to comment.