forked from openshift/console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseTelemetry.ts
More file actions
128 lines (107 loc) · 4.46 KB
/
useTelemetry.ts
File metadata and controls
128 lines (107 loc) · 4.46 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { useEffect, useCallback } from 'react';
import {
useResolvedExtensions,
isTelemetryListener,
TelemetryListener,
TelemetryEventListener,
UserInfo,
} from '@console/dynamic-plugin-sdk';
import type { UserKind } from '@console/internal/module/k8s/types';
import {
CLUSTER_TELEMETRY_ANALYTICS,
PREFERRED_TELEMETRY_USER_SETTING_KEY,
USER_TELEMETRY_ANALYTICS,
} from '../constants';
import { useUser } from './useUser';
import { useUserSettings } from './useUserSettings';
export interface ClusterProperties {
clusterId?: string;
clusterType?: string;
consoleVersion?: string;
organizationId?: string;
accountMail?: string;
}
export type TelemetryEventProperties = {
user?: UserInfo;
userResource?: UserKind;
} & ClusterProperties &
Record<string, any>;
export interface TelemetryEvent {
eventType: string;
event: TelemetryEventProperties;
}
let telemetryEvents: TelemetryEvent[] = [];
export const getClusterProperties = () => {
const clusterProperties: ClusterProperties = {};
clusterProperties.clusterId = window.SERVER_FLAGS.telemetry?.CLUSTER_ID;
clusterProperties.clusterType = window.SERVER_FLAGS.telemetry?.CLUSTER_TYPE;
if (window.SERVER_FLAGS.telemetry?.DEVSANDBOX === 'true') {
clusterProperties.clusterType = 'DEVSANDBOX';
}
// Prefer to report the OCP version (releaseVersion) if available.
clusterProperties.consoleVersion =
window.SERVER_FLAGS.releaseVersion || window.SERVER_FLAGS.consoleVersion;
clusterProperties.organizationId = window.SERVER_FLAGS.telemetry?.ORGANIZATION_ID;
clusterProperties.accountMail = window.SERVER_FLAGS.telemetry?.ACCOUNT_MAIL;
return clusterProperties;
};
const clusterIsOptedInToTelemetry = () =>
window.SERVER_FLAGS.telemetry?.STATE === CLUSTER_TELEMETRY_ANALYTICS.OPTIN;
const isOptedOutFromTelemetry = (currentUserPreferenceTelemetryValue: USER_TELEMETRY_ANALYTICS) =>
window.SERVER_FLAGS.telemetry?.STATE === CLUSTER_TELEMETRY_ANALYTICS.DISABLED ||
(currentUserPreferenceTelemetryValue === USER_TELEMETRY_ANALYTICS.DENY &&
(clusterIsOptedInToTelemetry() ||
window.SERVER_FLAGS.telemetry?.STATE === CLUSTER_TELEMETRY_ANALYTICS.OPTOUT));
const userIsOptedInToTelemetry = (currentUserPreferenceTelemetryValue: USER_TELEMETRY_ANALYTICS) =>
currentUserPreferenceTelemetryValue === USER_TELEMETRY_ANALYTICS.ALLOW;
let clusterProperties = getClusterProperties();
export const updateClusterPropertiesFromTests = () => (clusterProperties = getClusterProperties());
export const useTelemetry = () => {
// TODO use usePluginInfo() hook to tell whether all dynamic plugins have been processed
// to avoid firing telemetry events multiple times whenever a dynamic plugin loads asynchronously
const [currentUserPreferenceTelemetryValue] = useUserSettings<USER_TELEMETRY_ANALYTICS>(
PREFERRED_TELEMETRY_USER_SETTING_KEY,
null,
true,
);
// Use centralized user data instead of fetching directly
const { userResource, userResourceLoaded: userResourceIsLoaded } = useUser();
const [extensions] = useResolvedExtensions<TelemetryListener>(isTelemetryListener);
useEffect(() => {
if (
userIsOptedInToTelemetry(currentUserPreferenceTelemetryValue) &&
clusterIsOptedInToTelemetry() &&
telemetryEvents.length > 0 &&
userResourceIsLoaded
) {
telemetryEvents.forEach(({ eventType, event }) => {
extensions.forEach((e) => e.properties.listener(eventType, { ...event, userResource }));
});
telemetryEvents = [];
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentUserPreferenceTelemetryValue, userResourceIsLoaded]);
return useCallback<TelemetryEventListener>(
(eventType, properties: Record<string, any>) => {
if (isOptedOutFromTelemetry(currentUserPreferenceTelemetryValue)) return;
const event = {
...clusterProperties,
...properties,
// This is required to ensure that the replayed events uses the right path.
path: properties?.pathname,
};
if (
(clusterIsOptedInToTelemetry() && !currentUserPreferenceTelemetryValue) ||
!userResourceIsLoaded
) {
telemetryEvents.push({ eventType, event });
if (telemetryEvents.length > 10) {
telemetryEvents.shift(); // Remove the first element
}
return;
}
extensions.forEach((e) => e.properties.listener(eventType, { ...event, userResource }));
},
[extensions, currentUserPreferenceTelemetryValue, userResource, userResourceIsLoaded],
);
};