Skip to content

Commit 6085f7b

Browse files
Hari KiranHari Kiran
authored andcommitted
Fix #52 dataset page bugs
1 parent de19e63 commit 6085f7b

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

src/pages/DatasetDetailPage.tsx

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ const DatasetDetailPage: React.FC = () => {
9090
const [jsonViewerKey, setJsonViewerKey] = useState(0);
9191
const [jsonSize, setJsonSize] = useState<number>(0);
9292
const [transformedDataset, setTransformedDataset] = useState<any>(null);
93+
const [totalFileSize, setTotalFileSize] = useState<number>(0);
9394

9495

9596
// Recursive function to find `_DataLink_`
@@ -181,7 +182,18 @@ const DatasetDetailPage: React.FC = () => {
181182
}
182183

183184
return internalLinks;
184-
};
185+
};
186+
187+
const formatFileSize = (bytes: number): string => {
188+
if (bytes >= 1024 * 1024 * 1024) {
189+
return `${Math.floor(bytes / (1024 * 1024 * 1024))} GB`;
190+
} else if (bytes >= 1024 * 1024) {
191+
return `${Math.floor(bytes / (1024 * 1024))} MB`;
192+
} else {
193+
return `${Math.floor(bytes / 1024)} KB`;
194+
}
195+
};
196+
185197

186198
useEffect(() => {
187199
const fetchData = async () => {
@@ -215,8 +227,28 @@ const DatasetDetailPage: React.FC = () => {
215227
const transformed = transformJsonForDisplay(datasetDocument);
216228
setTransformedDataset(transformed);
217229

218-
const blob = new Blob([JSON.stringify(datasetDocument, null, 2)], { type: "application/json" });
219-
setJsonSize(blob.size);
230+
let totalSize = 0;
231+
232+
// 1️⃣ Sum external link sizes (from URL like ...?size=12345678)
233+
links.forEach((link) => {
234+
const sizeMatch = link.url.match(/size=(\d+)/);
235+
if (sizeMatch) {
236+
totalSize += parseInt(sizeMatch[1], 10);
237+
}
238+
});
239+
240+
// 2️⃣ Estimate internal size from _ArraySize_ (assume Float32 = 4 bytes)
241+
internalData.forEach((link) => {
242+
if (link.arraySize && Array.isArray(link.arraySize)) {
243+
const count = link.arraySize.reduce((acc, val) => acc * val, 1);
244+
totalSize += count * 4;
245+
}
246+
});
247+
248+
setTotalFileSize(totalSize);
249+
250+
const minifiedBlob = new Blob([JSON.stringify(datasetDocument)], { type: "application/json" });
251+
setJsonSize(minifiedBlob.size);
220252

221253
// // ✅ Construct download script dynamically
222254
let script = `curl -L --create-dirs "https://neurojson.io:7777/${dbName}/${docId}" -o "${docId}.json"\n`;
@@ -267,7 +299,7 @@ const DatasetDetailPage: React.FC = () => {
267299

268300
const handleDownloadDataset = () => {
269301
if (!datasetDocument) return;
270-
const jsonData = JSON.stringify(datasetDocument, null, 2);
302+
const jsonData = JSON.stringify(datasetDocument);
271303
const blob = new Blob([jsonData], { type: "application/json" });
272304
const link = document.createElement("a");
273305
link.href = URL.createObjectURL(blob);
@@ -580,8 +612,7 @@ const DatasetDetailPage: React.FC = () => {
580612
"&:hover": { backgroundColor: "#ff9100" },
581613
}}
582614
>
583-
{/* Download Dataset (1 Mb) */}
584-
Download Dataset ({(jsonSize / 1024).toFixed(0)} MB)
615+
Download Dataset ({(jsonSize / 1024).toFixed(0)} KB)
585616
</Button>
586617

587618
<Button
@@ -594,7 +625,10 @@ const DatasetDetailPage: React.FC = () => {
594625
"&:hover": { backgroundColor: "#ff9100" },
595626
}}
596627
>
597-
Script to Download All Files ({downloadScript.length} Bytes) (links: {externalLinks.length})
628+
{/* Script to Download All Files ({downloadScript.length} Bytes) (links: {externalLinks.length}) */}
629+
Script to Download All Files ({Math.floor(downloadScript.length / 1024)} KB)
630+
(links: {externalLinks.length}, total: {formatFileSize(totalFileSize)})
631+
598632
</Button>
599633

600634
<Box display="flex" alignItems="center" gap={1} sx={{ ml: "auto" }}>

0 commit comments

Comments
 (0)