Skip to content

Commit

Permalink
Move bytesToSize to Utilities
Browse files Browse the repository at this point in the history
Ref #2484
  • Loading branch information
rushirajnenuji committed Aug 7, 2024
1 parent d040c1d commit 554eadc
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 134 deletions.
33 changes: 33 additions & 0 deletions src/js/common/Utilities.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
define([], () => {
"use strict";

const KIBIBYTE = 1024;
const MEBIBYTE = KIBIBYTE * 1024;
const GIBIBYTE = MEBIBYTE * 1024;
const TEBIBYTE = GIBIBYTE * 1024;

/**
* @namespace Utilities
* @description A generic utility object that contains functions used throughout MetacatUI to perform useful functions,
Expand Down Expand Up @@ -156,6 +161,34 @@ define([], () => {
}
return 0; // No decimal places
},

/**
* Convert number of bytes into human readable format
*
* @param integer bytes Number of bytes to convert
* @param integer precision Number of digits after the decimal separator
* @return string
*/
bytesToSize: function (bytes, precision = 0) {
if (typeof bytes === "undefined") return;

if (bytes >= 0 && bytes < KIBIBYTE) {
return bytes + " B";
} else if (bytes >= KIBIBYTE && bytes < MEBIBYTE) {
return (bytes / KIBIBYTE).toFixed(precision) + " KiB";
} else if (bytes >= MEBIBYTE && bytes < GIBIBYTE) {
precision = 2;
return (bytes / MEBIBYTE).toFixed(precision) + " MiB";
} else if (bytes >= GIBIBYTE && bytes < TEBIBYTE) {
precision = 2;
return (bytes / GIBIBYTE).toFixed(precision) + " GiB";
} else if (bytes >= TEBIBYTE) {
precision = 2;
return (bytes / TEBIBYTE).toFixed(precision) + " TiB";
} else {
return bytes + " B";
}
},
};

return Utilities;
Expand Down
57 changes: 21 additions & 36 deletions src/js/models/DataONEObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@ define([
"he",
"collections/AccessPolicy",
"collections/ObjectFormats",
"common/Utilities",
"md5",
], function ($, _, Backbone, uuid, he, AccessPolicy, ObjectFormats, md5) {
], function (
$,
_,
Backbone,
uuid,
he,
AccessPolicy,
ObjectFormats,
Utilities,
md5,
) {
/**
* @class DataONEObject
* @classdesc A DataONEObject represents a DataONE object, such as a data file,
Expand Down Expand Up @@ -124,8 +135,15 @@ define([

this.set("accessPolicy", this.createAccessPolicy());

this.on("change:size", this.bytesToSize);
if (attrs.size) this.bytesToSize();
var model = this;
this.on("change:size", function () {
var size = Utilities.bytesToSize(model.get("size"));
model.set("sizeStr", size);
});
if (attrs.size) {
var size = Utilities.bytesToSize(model.get("size"));
model.set("sizeStr", size);
}

// Cache an array of original attribute names to help in handleChange()
if (this.type == "DataONEObject")
Expand Down Expand Up @@ -1977,39 +1995,6 @@ define([
return xmlString;
},

/**
* Converts the number of bytes into a human readable format and
* updates the `sizeStr` attribute
* @returns: None
*
*/
bytesToSize: function () {
var kibibyte = 1024;
var mebibyte = kibibyte * 1024;
var gibibyte = mebibyte * 1024;
var tebibyte = gibibyte * 1024;
var precision = 0;

var bytes = this.get("size");

if (bytes >= 0 && bytes < kibibyte) {
this.set("sizeStr", bytes + " B");
} else if (bytes >= kibibyte && bytes < mebibyte) {
this.set("sizeStr", (bytes / kibibyte).toFixed(precision) + " KiB");
} else if (bytes >= mebibyte && bytes < gibibyte) {
precision = 2;
this.set("sizeStr", (bytes / mebibyte).toFixed(precision) + " MiB");
} else if (bytes >= gibibyte && bytes < tebibyte) {
precision = 2;
this.set("sizeStr", (bytes / gibibyte).toFixed(precision) + " GiB");
} else if (bytes >= tebibyte) {
precision = 2;
this.set("sizeStr", (bytes / tebibyte).toFixed(precision) + " TiB");
} else {
this.set("sizeStr", bytes + " B");
}
},

/**
* This method will download this object while
* sending the user's auth token in the request.
Expand Down
31 changes: 0 additions & 31 deletions src/js/models/PackageModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -1816,37 +1816,6 @@ define([
this.set("totalSize", totalSize);
return totalSize;
},

/****************************/
/**
* Convert number of bytes into human readable format
*
* @param integer bytes Number of bytes to convert
* @param integer precision Number of digits after the decimal separator
* @return string
*/
bytesToSize: function (bytes, precision) {
var kibibyte = 1024;
var mebibyte = kibibyte * 1024;
var gibibyte = mebibyte * 1024;
var tebibyte = gibibyte * 1024;

if (typeof bytes === "undefined") var bytes = this.get("size");

if (bytes >= 0 && bytes < kibibyte) {
return bytes + " B";
} else if (bytes >= kibibyte && bytes < mebibyte) {
return (bytes / kibibyte).toFixed(precision) + " KiB";
} else if (bytes >= mebibyte && bytes < gibibyte) {
return (bytes / mebibyte).toFixed(precision) + " MiB";
} else if (bytes >= gibibyte && bytes < tebibyte) {
return (bytes / gibibyte).toFixed(precision) + " GiB";
} else if (bytes >= tebibyte) {
return (bytes / tebibyte).toFixed(precision) + " TiB";
} else {
return bytes + " B";
}
},
},
);
return PackageModel;
Expand Down
32 changes: 0 additions & 32 deletions src/js/models/SolrResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -859,38 +859,6 @@ define(["jquery", "underscore", "backbone"], function ($, _, Backbone) {
//If nothing works so far, return an empty array
return [];
},

/****************************/

/**
* Convert number of bytes into human readable format
*
* @param integer bytes Number of bytes to convert
* @param integer precision Number of digits after the decimal separator
* @return string
*/
bytesToSize: function (bytes, precision) {
var kibibyte = 1024;
var mebibyte = kibibyte * 1024;
var gibibyte = mebibyte * 1024;
var tebibyte = gibibyte * 1024;

if (typeof bytes === "undefined") var bytes = this.get("size");

if (bytes >= 0 && bytes < kibibyte) {
return bytes + " B";
} else if (bytes >= kibibyte && bytes < mebibyte) {
return (bytes / kibibyte).toFixed(precision) + " KiB";
} else if (bytes >= mebibyte && bytes < gibibyte) {
return (bytes / mebibyte).toFixed(precision) + " MiB";
} else if (bytes >= gibibyte && bytes < tebibyte) {
return (bytes / gibibyte).toFixed(precision) + " GiB";
} else if (bytes >= tebibyte) {
return (bytes / tebibyte).toFixed(precision) + " TiB";
} else {
return bytes + " B";
}
},
},
);
return SolrResult;
Expand Down
4 changes: 3 additions & 1 deletion src/js/views/DataPackageView.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"backbone",
"localforage",
"collections/DataPackage",
"common/Utilities",
"models/DataONEObject",
"models/PackageModel",
"models/metadata/ScienceMetadata",
Expand All @@ -20,6 +21,7 @@
Backbone,
LocalForage,
DataPackage,
Utilities,
DataONEObject,
PackageModel,
ScienceMetadata,
Expand Down Expand Up @@ -1036,7 +1038,7 @@
var view = this;
_.each(members, function (m) {
// Update the size to bytes format
m.set({ size: m.bytesToSize(m.get("size")) });
m.set({ size: Utilities.bytesToSize(m.get("size")) });

// Add each item of this nested package to the package table view
view.addOne(m, dataPackage);
Expand Down
7 changes: 6 additions & 1 deletion src/js/views/MetadataIndexView.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ define([
"underscore",
"backbone",
"gmaps",
"common/Utilities",
"models/SolrResult",
"views/DownloadButtonView",
"text!templates/loading.html",
Expand All @@ -14,6 +15,7 @@ define([
_,
Backbone,
gmaps,
Utilities,
SolrResult,
DownloadButtonView,
LoadingTemplate,
Expand Down Expand Up @@ -434,7 +436,10 @@ define([
_.each(pkg.get("members"), function (solrResult, i) {
if (solrResult.get("formatType") != "DATA") return;

solrResult.set("formattedSize", solrResult.bytesToSize());
solrResult.set(
"formattedSize",
Utilities.bytesToSize(solrResult.get("size")),
);

//Add a section for the data details, just like the other attribute sections
var keys = [
Expand Down
6 changes: 4 additions & 2 deletions src/js/views/PackageTableView.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ define([
"jquery",
"underscore",
"backbone",
"common/Utilities",
"models/PackageModel",
"views/DownloadButtonView",
"text!templates/downloadContents.html",
], function ($, _, Backbone, Package, DownloadButtonView, Template) {
], function ($, _, Backbone, Utilities, Package, DownloadButtonView, Template) {
"use strict";

var PackageTable = Backbone.View.extend({
Expand Down Expand Up @@ -401,7 +402,8 @@ define([

//File size cell
var sizeCell = $(document.createElement("td")).addClass("size");
var size = memberModel.bytesToSize();
var size = Utilities.bytesToSize(memberModel.get("size"));
memberModel.set("sizeStr", size);
$(sizeCell).text(size);
$(tr).append(sizeCell);

Expand Down
35 changes: 4 additions & 31 deletions src/js/views/StatsView.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ define([
"DonutChart",
"CircleBadge",
"collections/Citations",
"common/Utilities",
"models/MetricsModel",
"models/Stats",
"MetricsChart",
Expand All @@ -27,6 +28,7 @@ define([
DonutChart,
CircleBadge,
Citations,
Utilities,
MetricsModel,
StatsModel,
MetricsChart,
Expand Down Expand Up @@ -923,12 +925,13 @@ define([
displayTotalSize: function () {
var className = "quick-stats-count";
var count = "";
var view = this;

if (!this.model.get("totalSize")) {
count = "0 bytes";
className += " no-activity";
} else {
count = this.bytesToSize(this.model.get("totalSize"));
count = Utilities.bytesToSize(view.model.get("totalSize"));
}

var countEl = $(document.createElement("p"))
Expand Down Expand Up @@ -1103,36 +1106,6 @@ define([
}
},

/**
* Convert number of bytes into human readable format
*
* @param integer bytes Number of bytes to convert
* @param integer precision Number of digits after the decimal separator
* @return string
*/
bytesToSize: function (bytes, precision) {
var kibibyte = 1024;
var mebibyte = kibibyte * 1024;
var gibibyte = mebibyte * 1024;
var tebibyte = gibibyte * 1024;

if (typeof bytes === "undefined") var bytes = this.get("size");

if (bytes >= 0 && bytes < kibibyte) {
return bytes + " B";
} else if (bytes >= kibibyte && bytes < mebibyte) {
return (bytes / kibibyte).toFixed(precision) + " KiB";
} else if (bytes >= mebibyte && bytes < gibibyte) {
return (bytes / mebibyte).toFixed(precision) + " MiB";
} else if (bytes >= gibibyte && bytes < tebibyte) {
return (bytes / gibibyte).toFixed(precision) + " GiB";
} else if (bytes >= tebibyte) {
return (bytes / tebibyte).toFixed(precision) + " TiB";
} else {
return bytes + " B";
}
},

renderUsageMetricsError: function () {
var message =
"<p class='check-back-message'><strong>This might take some time. Check back in 24 hours to see these results.</strong></p>";
Expand Down

0 comments on commit 554eadc

Please sign in to comment.