Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / applications / calendar / src / app / metrics / useCalendarNESTMetric.tsx
blob7db67a4cbece2150b59223ba1b2972d809475a7e
1 import { useRef } from 'react';
3 import metrics from '@proton/metrics';
4 import useFlag from '@proton/unleash/useFlag';
6 import type { CalendarViewEventTemporaryEvent } from '../containers/calendar/interface';
7 import { getNESTData } from './calendarMetricsHelper';
9 interface NESTMetric {
10     startTime: DOMHighResTimeStamp;
11     isRecurring: boolean;
12     hasInvitees: boolean;
13     hasConferenceData: boolean;
16 export const useCalendarNESTMetric = () => {
17     const metricRef = useRef<NESTMetric | undefined>(undefined);
18     const calendarMetricsEnabled = useFlag('CalendarMetrics');
20     const startNESTMetric = (event: CalendarViewEventTemporaryEvent, isCreatingEvent: boolean) => {
21         if (!calendarMetricsEnabled || !isCreatingEvent) {
22             return;
23         }
25         const startTime = performance.now();
27         metricRef.current = {
28             startTime: startTime,
29             ...getNESTData(event),
30         };
31     };
33     const stopNESTMetric = (isCreatingEvent: boolean) => {
34         if (!calendarMetricsEnabled || !isCreatingEvent || !metricRef.current) {
35             return;
36         }
38         const endTime = performance.now();
39         const duration = (endTime - metricRef.current.startTime) / 1000;
41         void metrics.calendar_new_event_setup_time_histogram.observe({
42             Value: duration,
43             Labels: {
44                 recurring: metricRef.current.isRecurring ? 'true' : 'false',
45                 has_conference_data: metricRef.current.hasConferenceData ? 'true' : 'false',
46                 has_invitees: metricRef.current.hasInvitees ? 'true' : 'false',
47             },
48         });
50         metricRef.current = undefined;
51     };
53     return {
54         startNESTMetric,
55         stopNESTMetric,
56     };