generated from PostHog/posthog-plugin-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
250 lines (214 loc) · 8.52 KB
/
index.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
async function setupPlugin({ jobs, global, config }) {
global.url = `https://${config.DomainName}`
global.options = {
headers: {
Authorization: `Bearer ${config.ApiKey}`,
'Content-Type': 'application/json',
},
}
const createFileRequest = {
...global.options,
body: JSON.stringify({
path: `${config.pythonSupportFileUpload}`,
overwrite: `true`,
}),
}
const handle = await createFileForDBFS(createFileRequest, global)
const uploadFileRequest = {
...global.options,
body: JSON.stringify({
handle: handle,
data: `aW1wb3J0IHN5cwppbXBvcnQgcGFuZGFzIGFzIHBkCgpkZiA9IHBkLnJlYWRfY3N2KCcvZGJmcy97fScuZm9ybWF0KHN5cy5hcmd2WzJdKSwgc2VwPSd8JyxkZWxpbWl0ZXI9Tm9uZSkKZnJvbSBweXNwYXJrLnNxbC50eXBlcyBpbXBvcnQgKgoKZGVmIGVxdWl2YWxlbnRfdHlwZShmKToKICBpZiBmID09ICdkYXRldG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lcm1hbmVudF90YWJsZV9uYW1lLG92ZXJ3cml0ZT1GYWxzZSkKCg==`,
}),
}
await uploadFileForDBFS(uploadFileRequest, global)
const closeFileRequest = {
...global.options,
body: JSON.stringify({
handle: handle,
}),
}
await closeFileForDBFS(closeFileRequest, global)
global.eventsToIgnore = (config.eventsToIgnore || '').split(',').map((v) => v.trim())
}
function transformEventToRow(fullEvent) {
const { event, properties, $set, $set_once, distinct_id, team_id, site_url, now, sent_at, uuid, ...rest } =
fullEvent
const ip = properties?.['$ip'] || fullEvent.ip
const timestamp = fullEvent.timestamp || properties?.timestamp || now || sent_at
let ingestedProperties = null
let elements = null
if (event === '$autocapture' && properties?.['$elements']) {
const { ...props } = properties
elements = props
}
return {
event,
distinct_id,
team_id,
ip,
site_url,
timestamp,
uuid: uuid,
properties: JSON.stringify(ingestedProperties ?? {}),
elements: JSON.stringify(elements ?? []),
people_set: JSON.stringify($set ?? {}),
people_set_once: JSON.stringify($set_once ?? {}),
}
}
async function exportEvents(events, { global, storage }) {
let rows = events
.filter((event) => !global.eventsToIgnore.includes(event.event))
.map(transformEventToRow)
.map((row) => {
const keys = Object.keys(row)
const values = keys.map((key) => row[key])
return values.join('|') + '\n'
})
rows = rows.join().split('\n')
let data = null
if (!global.tests) {
data = await storage.get('data', null)
}
if (data === null) {
data = []
}
rows.forEach((row) => {
if (row.length >= 0) {
if (row[0] === ',' || row[0] === '|') {
row = row.substring(1)
}
data.push(row)
}
})
if (!global.tests) {
await storage.set('data', data)
}
return data
}
async function fetchWithRetry(url, options = {}, method = 'GET', isRetry = false) {
try {
const res = await fetch(url, { method: method, ...options })
return res
} catch {
if (isRetry) {
throw new Error(`${method} request to ${url} failed.`)
}
const res = await fetchWithRetry(url, options, (method = method), (isRetry = true))
return res
}
}
async function createFileForDBFS(request, global) {
const response = await fetchWithRetry(`${global.url}/api/2.0/dbfs/create`, request, 'POST')
const result = await response.json()
return result.handle
}
async function uploadFileForDBFS(request, global) {
const response = await fetchWithRetry(`${global.url}/api/2.0/dbfs/add-block`, request, 'POST')
await response.json()
}
async function closeFileForDBFS(request, global) {
const response = await fetchWithRetry(`${global.url}/api/2.0/dbfs/close`, request, 'POST')
await response.json()
}
async function runEveryMinute({ jobs, global, storage, config, cache }) {
let request = global.options
let isDataNull = false
let job_id = null
request.body = JSON.stringify({
path: `${config.fileName}`,
overwrite: `true`,
})
const handle = await createFileForDBFS(request, global)
let data = await storage.get('data', null)
if (!data) {
data = []
} else {
data.unshift(
`event|distinct_id|team_id|ip|site_url|timestamp|uuid|properties|elements|people_set|people_set_once`
)
}
for (const content of data) {
isDataNull = true
const contentBase64 = Buffer.from(content).toString('base64') + 'Cg=='
request = global.options
request.body = JSON.stringify({
handle: handle,
data: contentBase64,
})
await uploadFileForDBFS(request, global)
}
await storage.set('data', [])
request.body = JSON.stringify({
handle: handle,
})
await closeFileForDBFS(request, global)
if (isDataNull) {
request.body = JSON.stringify({
name: 'A python job to push data into db',
tasks: [
{
task_key: 'python',
description: 'Extracts session data from events',
depends_on: [],
existing_cluster_id: `${config.clusterId}`,
spark_python_task: {
python_file: `dbfs:${config.pythonSupportFileUpload}`,
},
libraries: [],
timeout_seconds: 86400,
max_retries: 3,
min_retry_interval_millis: 2000,
retry_on_timeout: false,
},
],
job_clusters: [
{
job_cluster_key: 'auto_scaling_cluster',
new_cluster: {
spark_version: '7.3.x-scala2.12',
node_type_id: 'i3.xlarge',
spark_conf: {
'spark.speculation': true,
},
aws_attributes: {
availability: 'SPOT',
zone_id: 'us-west-2a',
},
autoscale: {
min_workers: 2,
max_workers: 16,
},
},
},
],
timeout_seconds: 86400,
max_concurrent_runs: 10,
format: 'MULTI_TASK',
})
const jobIdToDrop = await cache.get('job_id')
if (jobIdToDrop) {
request.body = JSON.stringify({
job_id: jobIdToDrop,
})
const response = await fetchWithRetry(`${global.url}/api/2.1/jobs/delete`, request, 'POST')
const result = await response.json()
}
let response = await fetchWithRetry(`${global.url}/api/2.1/jobs/create`, request, 'POST')
let result = await response.json()
job_id = result.job_id
request.body = JSON.stringify({
job_id: job_id,
python_params: [`${config.dbName}`, `${config.fileName}`],
})
response = await fetchWithRetry(`${global.url}/api/2.1/jobs/run-now`, request, 'POST')
result = await response.json()
await cache.set('job_id', job_id)
}
}
module.exports = {
setupPlugin,
exportEvents,
runEveryMinute,
transformEventToRow,
}