Skip to content

Commit

Permalink
feat: Main Process Download Progress Tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanVicens committed Feb 1, 2024
1 parent 4762fe1 commit 8a8daea
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { MoveOfflineContentsOnContentsUploaded } from '../../../../../context/vi
import { LocalContentsMover } from '../../../../../context/virtual-drive/contents/application/LocalContentsMover';
import { AllLocalContentsDeleter } from '../../../../../context/virtual-drive/contents/application/AllLocalContentsDeleter';
import { MainProcessUploadProgressTracker } from '../../../../../context/shared/infrastructure/MainProcessUploadProgressTracker';
import { MainProcessDownloadProgressTracker } from '../../../../../context/shared/infrastructure/MainProcessDownloadProgressTracker';

export async function buildContentsContainer(
sharedContainer: SharedContainer
Expand Down Expand Up @@ -44,10 +45,13 @@ export async function buildContentsContainer(
'downloaded'
);

const tracker = new MainProcessDownloadProgressTracker();

const downloadContentsToPlainFile = new DownloadContentsToPlainFile(
contentsManagerFactory,
localFS,
eventBus
eventBus,
tracker
);

const localContentChecker = new LocalContentChecker(localFS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { FSLocalFileSystem } from '../../../../context/virtual-drive/contents/in
import { DependencyInjectionEventBus } from '../common/eventBus';
import { FuseAppDataLocalFileContentsDirectoryProvider } from '../../../../context/virtual-drive/shared/infrastructure/LocalFileContentsDirectoryProviders/FuseAppDataLocalFileContentsDirectoryProvider';
import { LocalContentsDeleter } from '../../../../context/virtual-drive/contents/application/LocalContentsDeleter';
import { MainProcessDownloadProgressTracker } from '../../../../context/shared/infrastructure/MainProcessDownloadProgressTracker';

export async function buildContentsContainer(): Promise<ContentsContainer> {
const user = DependencyInjectionUserProvider.get();
Expand All @@ -32,10 +33,13 @@ export async function buildContentsContainer(): Promise<ContentsContainer> {
'downloaded'
);

const tracker = new MainProcessDownloadProgressTracker();

const downloadContentsToPlainFile = new DownloadContentsToPlainFile(
contentsManagerFactory,
localFS,
eventBus
eventBus,
tracker
);

const localContentsDeleter = new LocalContentsDeleter(localFS);
Expand Down
23 changes: 19 additions & 4 deletions src/context/shared/domain/DownloadProgressTracker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
export interface DownloadProgressTracker {
downloadStarted(name: string): Promise<void>;
downloadUpdate(name: string, progress: number): Promise<void>;
downloadFinished(name: string): Promise<void>;
error(name: string): Promise<void>;
downloadStarted(name: string, extension: string, size: number): Promise<void>;
downloadUpdate(
name: string,
extension: string,
size: number,
progress: {
elapsedTime: number;
percentage: number;
}
): Promise<void>;
downloadFinished(
name: string,
extension: string,
size: number,
progress: {
elapsedTime: number;
}
): Promise<void>;
error(name: string, extension: string): Promise<void>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { trackError, trackEvent } from '../../../apps/main/analytics/service';
import { setTrayStatus } from '../../../apps/main/tray/tray';
import { broadcastToWindows } from '../../../apps/main/windows';
import { DownloadProgressTracker } from '../domain/DownloadProgressTracker';
import { SyncMessenger } from '../domain/SyncMessenger';

export class MainProcessDownloadProgressTracker
extends SyncMessenger
implements DownloadProgressTracker
{
async downloadStarted(
name: string,
extension: string,
size: number
): Promise<void> {
setTrayStatus('SYNCING');

trackEvent('Upload Started', {
file_name: name,
file_extension: extension,
file_size: size,
elapsedTimeMs: 0,
});

broadcastToWindows('sync-info-update', {
action: 'DOWNLOADING',
name: this.nameWithExtension(name, extension),
progress: 0,
});
}

async downloadUpdate(
name: string,
extension: string,
size: number,
progress: { elapsedTime: number; percentage: number }
): Promise<void> {
trackEvent('Upload Started', {
file_name: name,
file_extension: extension,
file_size: size,
elapsedTimeMs: progress.elapsedTime,
});

broadcastToWindows('sync-info-update', {
action: 'DOWNLOADING',
name: this.nameWithExtension(name, extension),
progress: progress.percentage,
});
}

async downloadFinished(
name: string,
extension: string,
size: number,
progress: {
elapsedTime: number;
}
): Promise<void> {
const nameWithExtension = this.nameWithExtension(name, extension);

setTrayStatus('IDLE');

trackEvent('Upload Completed', {
file_name: name,
file_extension: extension,
file_size: size,
elapsedTimeMs: progress.elapsedTime,
});

broadcastToWindows('sync-info-update', {
action: 'UPLOADED',
name: nameWithExtension,
});
}

async error(name: string, extension: string): Promise<void> {
const nameWithExtension = this.nameWithExtension(name, extension);

// TODO: finish this
trackError('Upload Error', new Error(), {
itemType: 'File',
root: '',
from: name,
action: 'Upload',
});

broadcastToWindows('sync-info-update', {
action: 'DOWNLOAD_ERROR',
name: nameWithExtension,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ export class DownloadContentsToPlainFile {

private async registerEvents(downloader: ContentFileDownloader, file: File) {
downloader.on('start', () => {
this.tracker.downloadStarted(file.nameWithExtension);
this.tracker.downloadStarted(file.name, file.type, file.size);
});

downloader.on('progress', (progress: number) => {
this.tracker.downloadUpdate(file.nameWithExtension, progress);
downloader.on('progress', (progress: number, elapsedTime: number) => {
this.tracker.downloadUpdate(file.name, file.type, file.size, {
elapsedTime,
percentage: progress,
});
});

downloader.on('error', () => {
this.tracker.error(file.nameWithExtension);
this.tracker.error(file.name, file.type);
});

downloader.on('finish', () => {
this.tracker.downloadFinished(file.nameWithExtension);
downloader.on('finish', (elapsedTime: number) => {
this.tracker.downloadFinished(file.name, file.type, file.size, {
elapsedTime,
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { File, FileAttributes } from '../../../files/domain/File';

export type FileDownloadEvents = {
start: () => void;
progress: (progress: number) => void;
progress: (progress: number, elapsedTime: number) => void;
finish: (fileId: FileAttributes['contentsId']) => void;
error: (error: Error) => void;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class EnvironmentContentFileDownloader implements ContentFileDownloader {
file.contentsId,
{
progressCallback: (progress: number) => {
this.eventEmitter.emit('progress', progress);
this.eventEmitter.emit('progress', progress, this.elapsedTime());
},
finishedCallback: async (err: Error, stream: Readable) => {
this.stopwatch.finish();
Expand All @@ -53,7 +53,7 @@ export class EnvironmentContentFileDownloader implements ContentFileDownloader {
this.eventEmitter.emit('error', err);
return reject(err);
}
this.eventEmitter.emit('finish');
this.eventEmitter.emit('finish', this.elapsedTime());

resolve(stream);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,33 @@ export class DownloadProgressTrackerMock implements DownloadProgressTracker {
readonly downloadFinishedMock = jest.fn();
readonly errorMock = jest.fn();

downloadStarted(name: string): Promise<void> {
return this.downloadStartedMock(name);
downloadStarted(
name: string,
extension: string,
size: number
): Promise<void> {
return this.downloadStartedMock(name, extension, size);
}
downloadUpdate(name: string, progress: number): Promise<void> {
return this.downloadUpdateMock(name, progress);

downloadUpdate(
name: string,
extension: string,
size: number,
progress: { elapsedTime: number; percentage: number }
): Promise<void> {
return this.downloadUpdateMock(name, extension, size, progress);
}
downloadFinished(name: string): Promise<void> {
return this.downloadFinishedMock(name);

downloadFinished(
name: string,
extension: string,
size: number,
progress: { elapsedTime: number }
): Promise<void> {
return this.downloadFinishedMock(name, extension, size, progress);
}
error(name: string): Promise<void> {
return this.errorMock(name);

error(name: string, extension: string): Promise<void> {
return this.errorMock(name, extension);
}
}

0 comments on commit 8a8daea

Please sign in to comment.