From 81fcee5913b8ecb71cdf9016819b18fecd627706 Mon Sep 17 00:00:00 2001 From: Bipul Adhikari Date: Fri, 6 Dec 2024 11:55:58 +0545 Subject: [PATCH] Adds fixes Signed-off-by: Bipul Adhikari --- .../s3-browser/upload-objects/store.ts | 65 +++++++++---------- .../upload-component/uploads.ts | 3 +- .../s3-browser/upload-objects/utils.ts | 25 ++++--- 3 files changed, 47 insertions(+), 46 deletions(-) diff --git a/packages/odf/components/s3-browser/upload-objects/store.ts b/packages/odf/components/s3-browser/upload-objects/store.ts index 555b5d07f..7d8d29838 100644 --- a/packages/odf/components/s3-browser/upload-objects/store.ts +++ b/packages/odf/components/s3-browser/upload-objects/store.ts @@ -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 = {}; @@ -27,16 +33,16 @@ export class UploadStore { }; } - get getUploadSpeed(): string { - const uploadingItems = Object.values(this.uploads).filter( - (upload) => upload.loaded > 0 + private getSpeed(): ReturnType { + 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; @@ -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 { @@ -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; } diff --git a/packages/odf/components/s3-browser/upload-objects/upload-component/uploads.ts b/packages/odf/components/s3-browser/upload-objects/upload-component/uploads.ts index 00082bcdd..6baba7c38 100644 --- a/packages/odf/components/s3-browser/upload-objects/upload-component/uploads.ts +++ b/packages/odf/components/s3-browser/upload-objects/upload-component/uploads.ts @@ -64,8 +64,7 @@ export const convertFileToUploadProgress = ( name: file.name, filePath: file.webkitRelativePath, startTime: undefined, - webkitRelativePath: '', - size: 0, type: '', key, + total: file.size, }); diff --git a/packages/odf/components/s3-browser/upload-objects/utils.ts b/packages/odf/components/s3-browser/upload-objects/utils.ts index de5e9cd60..03a585409 100644 --- a/packages/odf/components/s3-browser/upload-objects/utils.ts +++ b/packages/odf/components/s3-browser/upload-objects/utils.ts @@ -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]; }; @@ -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; @@ -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;