Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for base unit when adjusting unit label #1447

Merged
merged 1 commit into from
Jun 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});
});