Skip to content

Commit

Permalink
Adds fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Bipul Adhikari <[email protected]>
  • Loading branch information
bipuladh committed Dec 6, 2024
1 parent 4a61a83 commit 81fcee5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 46 deletions.
65 changes: 29 additions & 36 deletions packages/odf/components/s3-browser/upload-objects/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import {
import * as _ from 'lodash-es';
import { makeAutoObservable, toJS } from 'mobx';
import { ObjectID, UploadProgress, UploadStatus } from './types';
import {
isCancelledFile,
isCompletedFile,
isFailedFile,
isUploadingFile,
} from './utils';

export class UploadStore {
uploads: Record<string, UploadProgress> = {};
Expand All @@ -27,16 +33,16 @@ export class UploadStore {
};
}

get getUploadSpeed(): string {
const uploadingItems = Object.values(this.uploads).filter(
(upload) => upload.loaded > 0
private getSpeed(): ReturnType<typeof humanizeDecimalBytesPerSec> {
const totalDataTranserred = Object.values(this.uploads).reduce(
(acc, curr) => {
return (acc += curr.loaded);
},
0
);
const totalDataTranserred = uploadingItems.reduce((acc, curr) => {
return (acc += curr?.loaded);
}, 0);
const getTotalTimeElapsed = Object.values(this.uploads).reduce(
(acc, curr) => {
if (curr.startTime > acc && curr.startTime !== undefined) {
if (curr.startTime < acc && curr.startTime !== undefined) {
acc = curr.startTime;
}
return acc;
Expand All @@ -46,29 +52,25 @@ export class UploadStore {

const speed =
(totalDataTranserred * 1000) / (Date.now() - getTotalTimeElapsed);
return humanizeDecimalBytesPerSec(speed).string;
return humanizeDecimalBytesPerSec(speed);
}

get getUploadSpeed(): string {
return this.getSpeed().string;
}

get getUploadingFilesCount(): number {
return Object.values(this.uploads).filter(
(upload) =>
upload.uploadState === UploadStatus.UPLOAD_START ||
upload.uploadState === UploadStatus.INIT_STATE
).length;
return Object.values(this.uploads).filter(isUploadingFile).length;
}

get getFailedAndCancelledFilesCount(): number {
return Object.values(this.uploads).filter(
(upload) =>
upload.uploadState === UploadStatus.UPLOAD_FAILED ||
upload.uploadState === UploadStatus.UPLOAD_CANCELLED
(obj) => isCancelledFile(obj) || isFailedFile(obj)
).length;
}

get getCompletedFilesCount(): number {
return Object.values(this.uploads).filter(
(upload) => upload.uploadState === UploadStatus.UPLOAD_COMPLETE
).length;
return Object.values(this.uploads).filter(isCompletedFile).length;
}

get getTotalFilesCount(): number {
Expand All @@ -78,35 +80,26 @@ export class UploadStore {
get getTotalRemainingFilesAndSize(): string {
if (_.isEmpty(this.uploads)) return '';
const files = Object.values(this.uploads);
const uploadingFiles = files.filter(
(item) => item.uploadState === UploadStatus.UPLOAD_START
);
const uploadingFiles = files.filter(isUploadingFile);
const filesCount = uploadingFiles.length;
const filesSize = uploadingFiles.reduce(
const remainingDataSize = uploadingFiles.reduce(
(acc, curr) => (acc += curr.total - curr.loaded),
0
);
return `${filesCount} files (${humanizeBinaryBytes(filesSize).string})`;
return `${filesCount} files (${
humanizeBinaryBytes(remainingDataSize).string
})`;
}

get getTotalTimeRemaning(): string {
const files = Object.values(this.uploads);
const uploadingFiles = files.filter(
(item) =>
item.uploadState === UploadStatus.UPLOAD_START ||
item.uploadState === UploadStatus.INIT_STATE
);
const totalTimes = uploadingFiles.map(
(file) => Date.now() - file?.startTime
);
const totalUploaded = uploadingFiles.map((file) => file.loaded ?? 0);
const uploadSpeeds = totalUploaded.map((size, i) => size / totalTimes[i]);
const speed = _.sum(uploadSpeeds) / uploadSpeeds.length;
const uploadingFiles = files.filter(isUploadingFile);
const speed = this.getSpeed().value;
const totalRemainingBytes = uploadingFiles.reduce(
(acc, curr) => (acc += curr.total - curr.loaded),
0
);
const timeRemaining = totalRemainingBytes / speed;
const timeRemaining = (totalRemainingBytes * 1000) / speed;
if (timeRemaining < 1000) {
return humanizeSeconds(timeRemaining, 'ms').string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ export const convertFileToUploadProgress = (
name: file.name,
filePath: file.webkitRelativePath,
startTime: undefined,
webkitRelativePath: '',
size: 0,
type: '',
key,
total: file.size,
});
25 changes: 17 additions & 8 deletions packages/odf/components/s3-browser/upload-objects/utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { humanizeMinutes, humanizeSeconds } from '@odf/shared/utils';
import * as _ from 'lodash-es';
import { UploadProgressBatch, UploadStatus } from './types';
import { UploadProgress, UploadProgressBatch, UploadStatus } from './types';

export const getCompletedAndTotalUploadCount = (
objects: UploadProgressBatch
) => {
const totalObjects = Object.keys(objects).length;
const totalUploaded = Object.values(objects).filter(
(obj) => obj.uploadState === UploadStatus.UPLOAD_COMPLETE
).length;
const totalUploaded = Object.values(objects).filter(isCompletedFile).length;
return [totalUploaded, totalObjects];
};

Expand All @@ -18,14 +16,12 @@ export const getCompletedTotalFailedCount = (
const progressItems = Object.values(uploadProgress);
const [completedUploads, failedUploads] = progressItems.reduce(
(acc, curr) => {
const isCompleted = curr.uploadState === UploadStatus.UPLOAD_COMPLETE;
const isCompleted = isCompletedFile(curr);
if (isCompleted) {
acc = [acc[0] + 1, acc[1]];
return acc;
}
const isFailed =
curr.uploadState === UploadStatus.UPLOAD_FAILED ||
curr.uploadState === UploadStatus.UPLOAD_CANCELLED;
const isFailed = isFailedFile(curr) || isCancelledFile(curr);
if (isFailed) {
acc = [acc[0], acc[1] + 1];
return acc;
Expand Down Expand Up @@ -53,3 +49,16 @@ export const getTotalTimeElapsed = (
return minutes.string;
} else return humanizeSeconds(fromNow / 1000, 'seconds').string;
};

export const isUploadingFile = (upload: UploadProgress): boolean =>
upload.uploadState === UploadStatus.INIT_STATE ||
upload.uploadState === UploadStatus.UPLOAD_START;

export const isFailedFile = (upload: UploadProgress): boolean =>
upload.uploadState === UploadStatus.UPLOAD_FAILED;

export const isCancelledFile = (upload: UploadProgress): boolean =>
upload.uploadState === UploadStatus.UPLOAD_CANCELLED;

export const isCompletedFile = (upload: UploadProgress): boolean =>
upload.uploadState === UploadStatus.UPLOAD_COMPLETE;

0 comments on commit 81fcee5

Please sign in to comment.