-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
50 lines (39 loc) · 1.64 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
export function logDuration(startTime: number, label: string): number {
const durationInMs = Date.now() - startTime;
const formattedDuration = getFormattedDuration(durationInMs);
console.log(`${label} | Duration: ${formattedDuration}`);
return durationInMs;
}
export function getFormattedDuration(durationInMs: number): string {
const hours = Math.floor(durationInMs / 3600000);
const minutes = Math.floor((durationInMs % 3600000) / 60000);
const seconds = Math.floor((durationInMs % 60000) / 1000);
const formattedMinutes = hours > 0 ? String(minutes).padStart(2, '0') : minutes;
const formattedSeconds = String(seconds).padStart(2, '0');
return hours > 0
? `${hours}:${formattedMinutes}:${formattedSeconds}`
: `${minutes}:${formattedSeconds}`;
}
export function calculateAverage(numbers: number[]): number {
if (numbers.length === 0) return 0;
const total = numbers.reduce((sum, num) => sum + num, 0);
return total / numbers.length;
}
export default class TimeTracker {
private durationArray: number[] = [];
private totalItems: number;
constructor(totalItemsCount: number) {
this.totalItems = totalItemsCount;
}
logProcess(startTime: number, itemName: string, currentCount: number): void {
const msDuration = logDuration(startTime, `Finished with ${itemName}`);
this.durationArray.push(msDuration);
const avgDuration = calculateAverage(this.durationArray);
const itemsLeft = this.totalItems - currentCount;
const estimatedDuration = getFormattedDuration(avgDuration * itemsLeft);
console.log(
`Total avg of ${(avgDuration / 1000).toFixed(1)}s per item\n` +
`*Estimated time till finished: ${estimatedDuration}*`
);
}
}