Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / utils / availability.ts
blob5a6379d7164ef28c5e749b6fc4f1f54a86add1b4
1 const FIVE_MINUTES_IN_MILLISECONDS = 5 * 60 * 1000;
3 export enum AvailabilityTypes {
4     SENTRY = 'SENTRY',
5     ERROR = 'ERROR',
6     CRITICAL = 'CRITICAL',
9 export type AvailabilityReport = {
10     [AvailabilityTypes.SENTRY]: boolean;
11     [AvailabilityTypes.ERROR]: boolean;
12     [AvailabilityTypes.CRITICAL]: boolean;
14 export class Availability {
15     static status: Map<AvailabilityTypes, number> = new Map();
17     static interval: ReturnType<typeof setInterval>;
19     static init = (report: (status: AvailabilityReport) => void, interval: number = FIVE_MINUTES_IN_MILLISECONDS) => {
20         this.clear();
21         this.interval = setInterval(() => {
22             const sentry = this.status.get(AvailabilityTypes.SENTRY);
23             const error = this.status.get(AvailabilityTypes.ERROR);
24             const critical = this.status.get(AvailabilityTypes.CRITICAL);
25             report({
26                 [AvailabilityTypes.SENTRY]: sentry !== undefined && Date.now() - sentry > FIVE_MINUTES_IN_MILLISECONDS,
27                 [AvailabilityTypes.ERROR]: error !== undefined && Date.now() - error > FIVE_MINUTES_IN_MILLISECONDS,
28                 [AvailabilityTypes.CRITICAL]:
29                     critical !== undefined && Date.now() - critical > FIVE_MINUTES_IN_MILLISECONDS,
30             });
31         }, interval);
32     };
34     static mark = (type: AvailabilityTypes) => {
35         this.status.set(type, Date.now());
36     };
38     static clear = () => {
39         clearInterval(this.interval);
40         this.status = new Map();
41     };