Skip to content

Commit

Permalink
fix(general): fix the size display using the filesize library (portai…
Browse files Browse the repository at this point in the history
…ner#246)

* fix(general): fix the size display using the filesize library

* refactor(humansize): use default value for filter
  • Loading branch information
deviantony authored Oct 1, 2016
1 parent 59e6522 commit 739a5ec
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 40 deletions.
2 changes: 1 addition & 1 deletion app/components/dashboard/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
</tr>
<tr>
<td>Total memory</td>
<td>{{ infoData.MemTotal|humansize }}</td>
<td>{{ infoData.MemTotal|humansize: 2 }}</td>
</tr>
</tbody>
</table>
Expand Down
4 changes: 2 additions & 2 deletions app/components/stats/statsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function (Settings, $scope, Messages, $timeout, Container, ContainerTop, $stateP
},
{
scaleLabel: function (valueObj) {
return humansizeFilter(parseInt(valueObj.value, 10));
return humansizeFilter(parseInt(valueObj.value, 10), 2);
},
responsive: true
//scaleOverride: true,
Expand All @@ -100,7 +100,7 @@ function (Settings, $scope, Messages, $timeout, Container, ContainerTop, $stateP
datasets: [networkRxDataset, networkTxDataset]
}, {
scaleLabel: function (valueObj) {
return humansizeFilter(parseInt(valueObj.value, 10));
return humansizeFilter(parseInt(valueObj.value, 10), 2);
},
responsive: true
});
Expand Down
4 changes: 2 additions & 2 deletions app/components/swarm/swarm.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
</tr>
<tr>
<td>Total memory</td>
<td ng-if="!swarm_mode">{{ info.MemTotal|humansize }}</td>
<td ng-if="swarm_mode">{{ totalMemory|humansize }}</td>
<td ng-if="!swarm_mode">{{ info.MemTotal|humansize: 2 }}</td>
<td ng-if="swarm_mode">{{ totalMemory|humansize: 2 }}</td>
</tr>
<tr ng-if="!swarm_mode">
<td>Operating system</td>
Expand Down
16 changes: 7 additions & 9 deletions app/shared/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,13 @@ angular.module('portainer.filters', [])
})
.filter('humansize', function () {
'use strict';
return function (bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes === 0) {
return 'n/a';
}
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10);
var value = bytes / Math.pow(1024, i);
var decimalPlaces = (i < 1) ? 0 : (i - 1);
return value.toFixed(decimalPlaces) + ' ' + sizes[[i]];
return function (bytes, round) {
if (!round) {
round = 1;
}
if (bytes || bytes === 0) {
return filesize(bytes, {base: 10, round: round});
}
};
})
.filter('containername', function () {
Expand Down
1 change: 1 addition & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"angular-ui-select": "~0.17.1",
"bootstrap": "~3.3.6",
"font-awesome": "~4.6.3",
"filesize": "~3.3.0",
"Hover": "2.0.2",
"jquery": "1.11.1",
"jquery.gritter": "1.7.4",
Expand Down
1 change: 1 addition & 0 deletions gruntFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ module.exports = function (grunt) {
'bower_components/bootstrap/dist/js/bootstrap.min.js',
'bower_components/Chart.js/Chart.min.js',
'bower_components/lodash/dist/lodash.min.js',
'bower_components/filesize/lib/filesize.min.js',
'bower_components/moment/min/moment.min.js',
'bower_components/xterm.js/src/xterm.js',
'assets/js/jquery.gritter.js', // Using custom version to fix error in minified build due to "use strict"
Expand Down
26 changes: 0 additions & 26 deletions test/unit/app/shared/filters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,32 +82,6 @@ describe('filters', function () {
}));
});

describe('humansize', function () {
it('should return n/a when size is zero', inject(function (humansizeFilter) {
expect(humansizeFilter(0)).toBe('n/a');
}));

it('should handle Bytes values', inject(function (humansizeFilter) {
expect(humansizeFilter(512)).toBe('512 Bytes');
}));

it('should handle KB values', inject(function (humansizeFilter) {
expect(humansizeFilter(5 * 1024)).toBe('5 KB');
}));

it('should handle MB values', inject(function (humansizeFilter) {
expect(humansizeFilter(5 * 1024 * 1024)).toBe('5.0 MB');
}));

it('should handle GB values', inject(function (humansizeFilter) {
expect(humansizeFilter(5 * 1024 * 1024 * 1024)).toBe('5.00 GB');
}));

it('should handle TB values', inject(function (humansizeFilter) {
expect(humansizeFilter(5 * 1024 * 1024 * 1024 * 1024)).toBe('5.000 TB');
}));
});

describe('containername', function () {
it('should strip the leading slash from container name', inject(function (containernameFilter) {
var container = {
Expand Down

0 comments on commit 739a5ec

Please sign in to comment.