/
runningTimer.ts
73 lines (62 loc) · 2.01 KB
/
runningTimer.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { IntegrationLogger } from '@jupiterone/integration-sdk-core';
class StepAnnouncer {
private stepId: string;
private announceEvery: number;
private intervalId: NodeJS.Timeout | null = null;
private logger: IntegrationLogger;
private startedAt: Date;
constructor(
stepId: string,
logger: IntegrationLogger,
announceEvery: number = 45,
) {
this.stepId = stepId;
this.announceEvery = announceEvery * 1000;
this.logger = logger;
this.startedAt = new Date();
this.start();
}
private getReadableHumanTime(): string {
const elapsedSeconds = Math.floor(
(new Date().getTime() - this.startedAt.getTime()) / 1000,
);
const hours = Math.floor(elapsedSeconds / 3600);
const remainingMinutes = Math.floor((elapsedSeconds % 3600) / 60);
const remainingSeconds = elapsedSeconds % 60;
const messageParts: string[] = [];
if (hours > 0) {
messageParts.push(`${hours} hour${hours > 1 ? 's' : ''}`);
}
if (remainingMinutes > 0) {
messageParts.push(
`${remainingMinutes} minute${remainingMinutes > 1 ? 's' : ''}`,
);
}
if (remainingSeconds > 0 || messageParts.length === 0) {
messageParts.push(
`${remainingSeconds} second${remainingSeconds > 1 ? 's' : ''}`,
);
}
return messageParts.join(' ');
}
public start(): void {
if (this.intervalId === null) {
this.intervalId = setInterval(() => this.announce(), this.announceEvery);
this.logger.info(`[${this.stepId}] has started.`);
}
}
private announce(): void {
const timeMessage = this.getReadableHumanTime();
const description = `[${this.stepId}] has been running for ${timeMessage}.`;
this.logger.info(description);
}
public finish(): void {
if (this.intervalId !== null) {
clearInterval(this.intervalId);
this.intervalId = null;
const description = `[${this.stepId}] has finished after ${this.getReadableHumanTime()}.`;
this.logger.info(description);
}
}
}
export { StepAnnouncer };