Skip to content

Commit

Permalink
feat(eta): estimated time remaining
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuri committed Aug 6, 2017
1 parent 59043b2 commit 72e1731
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ export interface UploadProgress {
data?: {
percentage: number; // percentage of upload already completed
speed: number; // current upload speed per second in bytes
speedHuman: string; // current upload speed per second in human readable form
speedHuman: string; // current upload speed per second in human readable form,
eta: number; // estimated time remaining in seconds
etaHuman: string; // estimated time remaining in human readable format
};
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-uploader",
"version": "3.3.5",
"version": "3.3.6",
"main": "bundles/ngx-uploader.umd.js",
"module": "index.js",
"esnext": "index.js",
Expand Down
20 changes: 17 additions & 3 deletions src/ngx-uploader/classes/ngx-uploader.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface UploadProgress {
speedHuman: string;
startTime: number | null;
endTime: number | null;
eta: number | null;
etaHuman: string | null;
};
}

Expand Down Expand Up @@ -96,7 +98,9 @@ export class NgUploaderService {
speed: 0,
speedHuman: `${humanizeBytes(0)}/s`,
startTime: null,
endTime: null
endTime: null,
eta: null,
etaHuman: null
}
},
lastModifiedDate: file.lastModifiedDate,
Expand Down Expand Up @@ -193,13 +197,15 @@ export class NgUploaderService {
let time: number = new Date().getTime();
let progressStartTime: number = (file.progress.data && file.progress.data.startTime) || time;
let speed = 0;
let eta: number | null = null;

xhr.upload.addEventListener('progress', (e: ProgressEvent) => {
if (e.lengthComputable) {
const percentage = Math.round((e.loaded * 100) / e.total);
const diff = new Date().getTime() - time;
speed = Math.round(e.loaded / diff * 1000);
progressStartTime = (file.progress.data && file.progress.data.startTime) || new Date().getTime();
eta = Math.ceil((e.total - e.loaded) / speed);

file.progress = {
status: UploadStatus.Uploading,
Expand All @@ -208,7 +214,9 @@ export class NgUploaderService {
speed: speed,
speedHuman: `${humanizeBytes(speed)}/s`,
startTime: progressStartTime,
endTime: null
endTime: null,
eta: eta,
etaHuman: this.secondsToHuman(eta)
}
};

Expand All @@ -231,7 +239,9 @@ export class NgUploaderService {
speed: speedAverage,
speedHuman: `${humanizeBytes(speedAverage)}/s`,
startTime: progressStartTime,
endTime: new Date().getTime()
endTime: new Date().getTime(),
eta: eta,
etaHuman: this.secondsToHuman(eta || 0)
}
};

Expand Down Expand Up @@ -276,6 +286,10 @@ export class NgUploaderService {
});
}

secondsToHuman(sec: number): string {
return new Date(sec * 1000).toISOString().substr(11, 8);
}

generateId(): string {
return Math.random().toString(36).substring(7);
}
Expand Down

0 comments on commit 72e1731

Please sign in to comment.