forked from supabase/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.layer.ts
More file actions
153 lines (139 loc) · 5.1 KB
/
Copy pathanalytics.layer.ts
File metadata and controls
153 lines (139 loc) · 5.1 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { PostHog } from "posthog-node";
import { Effect, Layer, Option } from "effect";
import type { ProjectLinkStateValue } from "../../next/config/project-link-state.service.ts";
import { ProjectLinkState } from "../../next/config/project-link-state.service.ts";
import { CliConfig } from "../../next/config/cli-config.service.ts";
import { aiToolLayer } from "./ai-tool.layer.ts";
import { CurrentAnalyticsContext, type AnalyticsContext } from "./analytics-context.ts";
import { Analytics } from "./analytics.service.ts";
import { AiTool } from "./ai-tool.service.ts";
import { telemetryRuntimeLayer } from "./runtime.layer.ts";
import { TelemetryRuntime } from "./runtime.service.ts";
function stripUndefined(properties: Record<string, unknown>): Record<string, unknown> {
return Object.fromEntries(Object.entries(properties).filter(([, value]) => value !== undefined));
}
function contextProperties(context: AnalyticsContext): Record<string, unknown> {
return stripUndefined({
command_run_id: context.command_run_id,
command: context.command,
flags_used: context.flags_used,
flag_values: context.flag_values,
});
}
function resolveGroups(
context: AnalyticsContext,
linkedProject: Option.Option<ProjectLinkStateValue>,
): { organization: string; project: string } | undefined {
if (context.groups?.organization !== undefined && context.groups.project !== undefined) {
return {
organization: context.groups.organization,
project: context.groups.project,
};
}
return Option.match(linkedProject, {
onNone: () => undefined,
onSome: (state) => ({
organization: state.project.organization_slug,
project: state.project.ref,
}),
});
}
export const analyticsLayer = Layer.effect(
Analytics,
Effect.gen(function* () {
const runtime = yield* TelemetryRuntime;
const cliConfig = yield* CliConfig;
const aiTool = yield* AiTool;
if (runtime.consent !== "granted" || Option.isNone(cliConfig.telemetryPosthogKey)) {
return Analytics.of({
capture: () => Effect.void,
identify: () => Effect.void,
alias: () => Effect.void,
groupIdentify: () => Effect.void,
});
}
const client = new PostHog(cliConfig.telemetryPosthogKey.value, {
host: cliConfig.telemetryPosthogHost,
flushAt: 1,
flushInterval: 0,
});
yield* Effect.addFinalizer(() =>
Effect.promise(() => client._shutdown(5_000)).pipe(Effect.ignore),
);
const baseProperties = stripUndefined({
platform: "cli",
schema_version: 1,
device_id: runtime.deviceId,
$session_id: runtime.sessionId,
is_first_run: runtime.isFirstRun,
is_tty: runtime.isTty,
is_ci: runtime.isCi,
ai_tool: Option.match(aiTool.name, {
onNone: () => (runtime.isCi ? "ci" : runtime.isTty ? undefined : "unknown_non_interactive"),
onSome: (name) => name,
}),
os: runtime.os,
arch: runtime.arch,
cli_version: runtime.cliVersion,
});
const capture = (event: string, properties: Record<string, unknown> = {}) =>
Effect.gen(function* () {
const context = yield* CurrentAnalyticsContext;
const maybeProjectLinkState = yield* Effect.serviceOption(ProjectLinkState);
const linkedProject = yield* Option.match(maybeProjectLinkState, {
onNone: () => Effect.succeed(Option.none<ProjectLinkStateValue>()),
onSome: (projectLinkState) =>
projectLinkState.load.pipe(
Effect.catch(() => Effect.succeed(Option.none<ProjectLinkStateValue>())),
),
});
const groups = resolveGroups(context, linkedProject);
client.capture({
event,
distinctId: context.distinct_id ?? runtime.identity.current() ?? runtime.deviceId,
...(groups === undefined ? {} : { groups }),
properties: {
...baseProperties,
...contextProperties(context),
...stripUndefined(properties),
},
});
});
const identify = (distinctId: string, properties: Record<string, unknown> = {}) =>
Effect.sync(() => {
client.identify({
distinctId,
properties: stripUndefined({
cli_version: runtime.cliVersion,
os: runtime.os,
arch: runtime.arch,
...properties,
}),
});
});
const alias = (distinctId: string, aliasValue: string) =>
Effect.sync(() => {
client.alias({ distinctId, alias: aliasValue });
});
const groupIdentify = (
groupType: string,
groupKey: string,
properties: Record<string, unknown> = {},
) =>
Effect.gen(function* () {
const context = yield* CurrentAnalyticsContext;
client.groupIdentify({
groupType,
groupKey,
distinctId: context.distinct_id ?? runtime.identity.current() ?? runtime.deviceId,
properties: stripUndefined(properties),
});
});
return Analytics.of({
capture,
identify,
alias,
groupIdentify,
});
}),
).pipe(Layer.provide(telemetryRuntimeLayer), Layer.provide(aiToolLayer));