Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix/cache-modal-performance #411

Merged
merged 5 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 41 additions & 21 deletions packages/core/components/Modal/CopyFileManifest/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ function FileTable({ files, title }: { files: FileDetail[]; title: string }) {
};

const calculateTotalSize = (files: FileDetail[]) => {
if (files.length === 0) return "";
const totalBytes = files.reduce((acc, file) => acc + (file.size || 0), 0);
return totalBytes ? filesize(totalBytes) : "Calculating...";
};
Expand Down Expand Up @@ -72,9 +71,7 @@ function FileTable({ files, title }: { files: FileDetail[]; title: string }) {
{hasScroll && <div className={styles.gradientOverlay} />}
</div>
<div className={styles.summary}>
{files.length > 0 && (
<span className={styles.totalSize}>{calculateTotalSize(files)}</span>
)}
<span className={styles.totalSize}>{calculateTotalSize(files)}</span>
<span className={styles.fileCount}>{files.length.toLocaleString()} files</span>
</div>
</div>
Expand All @@ -92,13 +89,20 @@ export default function CopyFileManifest({ onDismiss }: ModalProps) {
);

const [fileDetails, setFileDetails] = React.useState<FileDetail[]>([]);
const [loading, setLoading] = React.useState<boolean>(true);

React.useEffect(() => {
async function fetchDetails() {
const details = await fileSelection.fetchAllDetails();
setFileDetails(details);
async function fetchFiles() {
try {
const details = await fileSelection.fetchAllDetails();
setFileDetails(details);
} catch (err: any) {
throw new Error(err.message || "Error fetching file details.");
BrianWhitneyAI marked this conversation as resolved.
Show resolved Hide resolved
} finally {
setLoading(false);
}
}
fetchDetails();
fetchFiles();
}, [fileSelection]);

const onMove = () => {
Expand All @@ -124,18 +128,36 @@ export default function CopyFileManifest({ onDismiss }: ModalProps) {

return (
<BaseModal
title="Copy files to local storage (VAST)"
onDismiss={onDismiss}
body={
<div className={styles.bodyContainer}>
<p className={styles.note}>
Files copied to the local storage (VAST) are stored with a 180-day
expiration, after which they revert to cloud-only storage. To extend the
expiration, reselect the files and confirm the update.
</p>
<FileTable
files={filesInLocalCache}
title="Files that are already on VAST: Extend expiration"
/>
<FileTable files={filesNotInLocalCache} title="Files to download to VAST" />
{loading && (
<div className={styles.loading}>
<p>Loading file details...</p>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thrilled to see this, I noticed the query time starts to get long even for hundreds of files, so this will really help users understand what's going on.

</div>
)}
{!loading && (
<>
<p className={styles.note}>
Files copied to the local storage (VAST) are stored with a 180-day
expiration, after which they revert to cloud-only storage. To extend
the expiration, reselect the files and confirm the update.
</p>
{filesInLocalCache.length > 0 && (
<FileTable
files={filesInLocalCache}
title="Files that are already on VAST: Extend expiration"
/>
)}
{filesNotInLocalCache.length > 0 && (
<FileTable
files={filesNotInLocalCache}
title="Files to download to VAST"
/>
)}
</>
)}
</div>
}
footer={
Expand All @@ -148,15 +170,13 @@ export default function CopyFileManifest({ onDismiss }: ModalProps) {
/>
<PrimaryButton
className={styles.confirmButton}
disabled={!fileDetails.length}
disabled={loading}
onClick={onMove}
text="CONFIRM"
title=""
/>
</div>
}
onDismiss={onDismiss}
title="Copy files to local storage (VAST)"
/>
);
}
18 changes: 2 additions & 16 deletions packages/core/entity/FileSelection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,27 +210,13 @@ export default class FileSelection {
* Fetch metadata for all items selected.
*/
public async fetchAllDetails(): Promise<FileDetail[]> {
const fileRangesByFileSets = this.groupByFileSet();
// Load file metadata for every file selected (however do to some performance enhancements
// the fetch will overshoot)
const fileRangePromises: Promise<FileDetail[]>[] = [];
fileRangesByFileSets.forEach((ranges, fileSet) => {
this.groupByFileSet().forEach((ranges, fileSet) => {
ranges.forEach((range) => {
fileRangePromises.push(fileSet.fetchFileRange(range.from, range.to));
});
});
await Promise.all(fileRangePromises);

// Collect the desired files from the fetched files
const files: FileDetail[] = [];
fileRangesByFileSets.forEach((ranges, fileSet) => {
ranges.forEach((range) => {
for (let i = range.from; i <= range.to; i++) {
files.push(fileSet.getFileByIndex(i) as FileDetail);
}
});
});
return files;
return (await Promise.all(fileRangePromises)).flat();
}

/**
Expand Down
27 changes: 19 additions & 8 deletions packages/core/entity/FileSet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,25 @@ export default class FileSet {
fileSet: this,
});

// Update cache for files fetched, due to overfetching the indexes updated
// in the cache will be inclusive of the range requested but may not necessarily start
// at the requested index, therefore here startIndexOfPage is used instead of startIndex
for (let i = 0; i < response.length; i++) {
this.cache.set(startIndexOfPage + i, response[i]);
}

return response;
// Grab responses and shove them into the cache
// while also filtering out the files that outside the range
// requested for the response to the caller of fetchFileRange()
return response.reduce((filesInRange, file, idxInResponses) => {
// Update cache for files fetched, due to overfetching the indexes updated
// in the cache will be inclusive of the range requested but may not necessarily start
// at the requested index, therefore here startIndexOfPage is used instead of startIndex
const idxInFileSet = startIndexOfPage + idxInResponses;
this.cache.set(idxInFileSet, file);

// If the file is within the range requested, add it to the files array
// for returning back to the caller of fetchFileRange().
// Ranges are inclusive of their bounds
if (idxInFileSet >= startIndex && idxInFileSet <= endIndex) {
filesInRange.push(file);
}

return filesInRange;
}, [] as FileDetail[]);
} finally {
// Clear the previously saved indexes as they are no longer loading
for (let i = startIndexOfPage; i < startIndexOfPage + limit; i++) {
Expand Down
Loading