Skip to content

Commit

Permalink
dashboard: move common formatBytes operation to base class
Browse files Browse the repository at this point in the history
  • Loading branch information
swhite2 committed Apr 30, 2024
1 parent d1962f8 commit 5edad25
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
14 changes: 14 additions & 0 deletions src/opnsense/www/js/widgets/BaseWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,18 @@ export default class BaseWidget {
const op = Math.round(Math.min(Math.max(opacity || 1, 0), 1) * 255);
return color + op.toString(16).toUpperCase();
}

_formatBytes(value, decimals = 2) {
if (!isNaN(value) && value > 0) {
let fileSizeTypes = ["", "K", "M", "G", "T", "P", "E", "Z", "Y"];
let ndx = Math.floor(Math.log(value) / Math.log(1000) );
if (ndx > 0) {
return (value / Math.pow(1000, ndx)).toFixed(2) + ' ' + fileSizeTypes[ndx];
} else {
return value.toFixed(2);
}
} else {
return "";
}
}
}
18 changes: 2 additions & 16 deletions src/opnsense/www/js/widgets/Traffic.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,6 @@ export default class Traffic extends BaseWidget {
return color + op.toString(16).toUpperCase();
}

_formatField(value) {
if (!isNaN(value) && value > 0) {
let fileSizeTypes = ["", "K", "M", "G", "T", "P", "E", "Z", "Y"];
let ndx = Math.floor(Math.log(value) / Math.log(1000) );
if (ndx > 0) {
return (value / Math.pow(1000, ndx)).toFixed(2) + ' ' + fileSizeTypes[ndx];
} else {
return value.toFixed(2);
}
} else {
return "";
}
}

_chartConfig(dataset) {
return {
type: 'line',
Expand Down Expand Up @@ -105,7 +91,7 @@ export default class Traffic extends BaseWidget {
y: {
ticks: {
callback: (value, index, values) => {
return this._formatField(value);
return this._formatBytes(value);
}
}
}
Expand All @@ -123,7 +109,7 @@ export default class Traffic extends BaseWidget {
intersect: false,
callbacks: {
label: (context) => {
return context.dataset.label + ": " + this._formatField(context.dataset.data[context.dataIndex].y).toString();
return context.dataset.label + ": " + this._formatBytes(context.dataset.data[context.dataIndex].y).toString();
}
}
},
Expand Down

0 comments on commit 5edad25

Please sign in to comment.