Skip to content

Commit

Permalink
Merge pull request #809 from jpwhite4/format_functions
Browse files Browse the repository at this point in the history
 Move string format functions to XDMoD.utils.format namespace
  • Loading branch information
jpwhite4 authored Feb 23, 2019
2 parents c06a700 + afd036a commit 9f48a19
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 106 deletions.
106 changes: 106 additions & 0 deletions html/gui/js/CCR.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,112 @@ XDMoD.utils.syncWindowShadow = function (thisComponent) {
});
};

XDMoD.utils.format = {

/**
* Return an at-a-glance time showing the top two units
* i.e. days and hours, hours and minutes, etc. If you the full breakdown
* of days,hours,minutes,seconds then it is difficult to comprehend.
*
* @param s
* @returns {*}
*/
humanTime: function (seconds) {
var s = seconds;
var plural = function (val, unit, ignorezero) {
if (val > 1) {
return val + ' ' + unit + 's ';
} else if (val === 0) {
if (ignorezero) {
return '';
}
return val + ' ' + unit + 's ';
}
return val + ' ' + unit + ' ';
};

var days = Math.floor(s / (24 * 3600));
if (days > 0) {
s %= (24 * 3600);
return plural(days, 'day', 0) + plural((s / 3600.0).toFixed(1), 'hour', 1);
}
var hours = Math.floor(s / 3600);
if (hours > 0) {
s %= 3600;
return plural(hours, 'hour', 0) + plural((s / 60.0).toFixed(1), 'minute', 1);
}
var minutes = Math.floor(s / 60);
if (minutes > 0) {
return plural(minutes, 'minute', 0) + plural(s % 60, 'second', 1);
}

return plural(s, 'second', 0);
},

/**
* Converts a number to a string with SI prefix notation
*
* @param {Number} input the number to format.
* @param {String} units the unit name to append to the number.
* @param {Number} precision the number of significant figures to show.
* @returns {*}
*/
convertToSiPrefix: function (input, units, precision) {
if (isNaN(input)) {
return input;
}

var value = Number(Number(input).toPrecision(precision));
var result;

if (value >= 1.0e12) {
result = (value / 1.0e12) + ' T' + units;
} else if (value >= 1.0e9) {
result = (value / 1.0e9) + ' G' + units;
} else if (value >= 1.0e6) {
result = (value / 1.0e6) + ' M' + units;
} else if (value >= 1.0e3) {
result = (value / 1.0e3) + ' k' + units;
} else {
result = value + ' ' + units;
}

return result;
},

/**
* A helper display function that applies the correct human readable byte
* measurement to the provided value (val), given in the correct precision
* and formatted with the provided units.
*
* @param {Number} val
* @param {String} units
* @param {Number} precision
* @returns {*}
*/
convertToBinaryPrefix: function (val, units, precision) {
if (isNaN(val)) {
return val;
}

var outval = null;

if (val > 1099511627776.0) {
outval = (val / 1099511627776.0).toPrecision(precision) + ' Ti' + units;
} else if (val > 1073741824.0) {
outval = (val / 1073741824.0).toPrecision(precision) + ' Gi' + units;
} else if (val > 1048576.0) {
outval = (val / 1048576.0).toPrecision(precision) + ' Mi' + units;
} else if (val > 1024.0) {
outval = (val / 1024.0).toPrecision(precision) + ' Ki' + units;
} else {
outval = Number(val).toPrecision(precision) + ' ' + units;
}

return outval;
}
};

// =====================================================================

Ext.Ajax.timeout = 86400000;
Expand Down
112 changes: 6 additions & 106 deletions html/gui/js/modules/job_viewer/JobViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,105 +370,6 @@ XDMoD.Module.JobViewer = Ext.extend(XDMoD.PortalModule, {
);
}, // setupComponents

/**
* A helper display function that applies the correct human readable byte
* measurement to the provided value (val), given in the correct precision
* and formatted with the provided units.
*
* @param {Number} val
* @param {String} units
* @param {Number} precision
* @returns {*}
*/
change: function (val, units, precision) {
if (isNaN(val)) {
return val;
}

var outval = null;

if (val > 1099511627776.0) {
outval = (val / 1099511627776.0).toPrecision(precision) + " Ti" + units;
} else if (val > 1073741824.0) {
outval = (val / 1073741824.0).toPrecision(precision) + " Gi" + units;
} else if (val > 1048576.0) {
outval = (val / 1048576.0).toPrecision(precision) + " Mi" + units;
} else if (val > 1024.0) {
outval = (val / 1024.0).toPrecision(precision) + " Ki" + units;
} else {
outval = (1.0 * val).toPrecision(precision) + " " + units;
}

return outval;
}, // change

/**
* Return an at-a-glance time showing the top two units
* i.e. days and hours, hours and minutes, etc. If you the full breakdown
* of days,hours,minutes,seconds then it is difficult to comprehend.
*
* @param s
* @returns {*}
*/
humanTime: function (s) {
var plural = function (val, unit, ignorezero) {
if (val > 1) {
return val + " " + unit + "s ";
}
else if (val == 0) {
if (ignorezero) {
return "";
}
return val + " " + unit + "s ";
}
else {
return val + " " + unit + " ";
}
}; // plural

var days = Math.floor(s / (24 * 3600));
if (days > 0) {
s %= (24 * 3600);
return plural(days, "day", 0) + plural((s / 3600.0).toFixed(1), "hour", 1);
}
var hours = Math.floor(s / 3600);
if (hours > 0) {
s %= 3600;
return plural(hours, "hour", 0) + plural((s / 60.0).toFixed(1), "minute", 1);
}
var minutes = Math.floor(s / 60);
if (minutes > 0) {
return plural(minutes, "minute", 0) + plural(s % 60, "second", 1);
}
;
return plural(s, "second", 0);
}, // humanTime

/**
* Converts a number to a string with SI prefix notation
*/
convertToSiPrefix: function(value, units, precision) {
if (isNaN(value)) {
return value;
}

var outval = null;

if (value > 1.0e12) {
outval = (value / 1.0e12).toPrecision(precision) + " T" + units;
} else if (value > 1.0e9) {
outval = (value / 1.0e9).toPrecision(precision) + " G" + units;
} else if (value > 1.0e6) {
outval = (value / 1.0e6).toPrecision(precision) + " M" + units;
} else if (value > 1.0e3) {
outval = (value / 1.0e3).toPrecision(precision) + " k" + units;
} else {
outval = Number(value).toPrecision(precision) + " " + units;
}

return outval;
},

/**
* Helper function that formats the provided val based on the provided units.
*
Expand All @@ -477,15 +378,14 @@ XDMoD.Module.JobViewer = Ext.extend(XDMoD.PortalModule, {
* @returns {*}
*/
formatData: function (val, units) {
var self = this;
switch (units) {
case "TODO":
case "":
case null:
return val;
break;
case "seconds":
return self.humanTime(val);
return XDMoD.utils.format.humanTime(val);
break;
case "boolean":
return (val == 1) ? "True" : "False";
Expand All @@ -496,21 +396,21 @@ XDMoD.Module.JobViewer = Ext.extend(XDMoD.PortalModule, {
break;
case "ratio":
case "1":
return self.convertToSiPrefix(val, "", 3);
return XDMoD.utils.format.convertToSiPrefix(val, '', 3);
break;
case 'bytes':
case 'B':
case 'B/s':
return self.change(val, units, 4);
return XDMoD.utils.format.convertToBinaryPrefix(val, units, 4);
break;
case 'kilobyte':
return self.change(val * 1024.0, 'byte', 4);
return XDMoD.utils.format.convertToBinaryPrefix(val * 1024.0, 'byte', 4);
break;
case 'megabyte':
return self.change(val * 1024.0 * 1024.0, 'byte', 4);
return XDMoD.utils.format.convertToBinaryPrefix(val * 1024.0 * 1024.0, 'byte', 4);
break;
default:
return self.convertToSiPrefix(val, units, 4);
return XDMoD.utils.format.convertToSiPrefix(val, units, 4);
break;
}
}, // formatData
Expand Down
1 change: 1 addition & 0 deletions html/unit_tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<script src="spec/ChangeStackSpec.js"></script>
<script src="spec/CCRTokenizeSpec.js"></script>
<script src="spec/JobViewerSpec.js"></script>
<script src="spec/XDMoDFormatSpec.js"></script>

<script>
mocha.checkLeaks();
Expand Down
6 changes: 6 additions & 0 deletions html/unit_tests/spec/JobViewerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,11 @@ describe('XDMoD.JobViewer', function () {

expect(jv.compareNodePath(node, path)).to.be.false;
});

it('data format functions', function () {
expect(jv.formatData(60, 'seconds')).to.equal('1 minute ');
expect(jv.formatData(10240, 'B/s')).to.equal('10.00 KiB/s');
expect(jv.formatData(11100000000, '1')).to.equal('11.1 G');
});
});
});
89 changes: 89 additions & 0 deletions html/unit_tests/spec/XDMoDFormatSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
describe('XDMoD.Format', function () {
describe('Check Format functions', function () {
it('SI formatting', function () {
var test_cases = [
[100001, 'B', 3, '100 kB'],
[10100001, 'B', 3, '10.1 MB'],
[0.0001, 'B', 2, '0.0001 B'],
[0.00033, 'B', 2, '0.00033 B'],
[1.00001, 'B', 1, '1 B'],
[1, '', 2, '1 '],
[10, '', 2, '10 '],
[100, '', 2, '100 '],
[1000, '', 2, '1 k'],
[10000, '', 2, '10 k'],
[100000, '', 2, '100 k'],
[1000000, '', 2, '1 M'],
[10000000, '', 2, '10 M'],
[100000000, '', 2, '100 M'],
[1000000000, '', 2, '1 G'],
[9, '', 2, '9 '],
[99, '', 2, '99 '],
[999, '', 2, '1 k'],
[9999, '', 2, '10 k'],
[99999, '', 2, '100 k'],
[999999, '', 2, '1 M'],
[9999999, '', 2, '10 M'],
[99999999, '', 2, '100 M'],
[999999999, '', 2, '1 G'],
[9999999999, '', 2, '10 G'],
[1, '', 4, '1 '],
[10, '', 4, '10 '],
[100, '', 4, '100 '],
[1000, '', 4, '1 k'],
[10000, '', 4, '10 k'],
[100000, '', 4, '100 k'],
[1000000, '', 4, '1 M'],
[10000000, '', 4, '10 M'],
[100000000, '', 4, '100 M'],
[1000000000, '', 4, '1 G'],
[9, '', 4, '9 '],
[99, '', 4, '99 '],
[999, '', 4, '999 '],
[9999, '', 4, '9.999 k'],
[99999, '', 4, '100 k'],
[999999, '', 4, '1 M'],
[9999999, '', 4, '10 M'],
[99999999, '', 4, '100 M'],
[999999999, '', 4, '1 G'],
[9999999999, '', 4, '10 G']
];

var i;
for (i = 0; i < test_cases.length; i++) {
expect(XDMoD.utils.format.convertToSiPrefix(test_cases[i][0], test_cases[i][1], test_cases[i][2])).to.equal(test_cases[i][3]);
}
});

it('Binary formatting', function () {
var test_cases = [
[1025, 'B', 3, '1.00 KiB'],
[10100001, 'B', 3, '9.63 MiB'],
[0.0001, 'B', 2, '0.00010 B'],
[1.00001, 'B', 1, '1 B']
];

var i;
for (i = 0; i < test_cases.length; i++) {
expect(XDMoD.utils.format.convertToBinaryPrefix(test_cases[i][0], test_cases[i][1], test_cases[i][2])).to.equal(test_cases[i][3]);
}
});

it('Elapsed time', function () {
var test_cases = [
[1, '1 second '],
[2, '2 seconds '],
[3600, '1 hour 0.0 minute '],
[3601, '1 hour 0.0 minute '],
[3600 + (5 * 60), '1 hour 5.0 minutes '],
[24 * 3600, '1 day 0.0 hour '],
[(3 * 24 * 3600) + 3600, '3 days 1.0 hour ']
];

var i;
for (i = 0; i < test_cases.length; i++) {
expect(XDMoD.utils.format.humanTime(test_cases[i][0])).to.equal(test_cases[i][1]);
}
});
});
});

0 comments on commit 9f48a19

Please sign in to comment.