generated from PostHog/posthog-hello-world-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
71 lines (58 loc) · 2.06 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
export async function runEveryMinute(meta) {
const activeAlertKey = await meta.cache.get("active_alert")
const isInError = await isNoEventsInPeriod(meta)
if (activeAlertKey && !isInError) {
await resolveAlert(meta)
console.info('Resolved ingestion alert', activeAlertKey)
} else if (!activeAlertKey && isInError) {
const key = await triggerAlert(meta)
console.warn('Triggered ingesion alert', key)
} else if (isInError) {
console.warn('Ingestion alert is already active')
} else {
console.log('Ingestion OK')
}
}
async function isNoEventsInPeriod(meta) {
const events = await getTrend(meta)
return events.length == 0
}
async function getTrend(meta) {
const response = await fetch(eventsApiUrl(meta.config.posthogHost, meta.config.timeRange), {
headers: {
authorization: `Bearer ${meta.config.posthogApiKey}`
}
})
if (!response.ok) {
throw Error(`Error from PostHog API: status=${response.status} response=${await response.text()}`)
}
const results = await response.json()
return results.results
}
async function triggerAlert(meta) {
let key = "no_events"
await triggerWebHook(meta,'trigger')
await meta.cache.set("active_alert", key)
return key
}
async function resolveAlert(meta) {
await triggerWebHook(meta,'resolved')
await meta.cache.set("active_alert", null)
}
async function triggerWebHook(meta, status){
var webHookUrl = meta.config.webHookUrlTriggered
if(status === 'resolved'){
webHookUrl = meta.config.webHookUrlResolved
}
const response = await fetch(webHookUrl)
if (!response.ok) {
throw Error(`Error from WebHook: status=${response.status} response=${await response.text()}`)
}
return response
}
function eventsApiUrl(instanceURL, timeRange) {
let time_from = new Date(Date.now() - (parseInt(timeRange)*60*1000)).toISOString()
let url = new URL(`${instanceURL}/api/event?after=${time_from}`)
url.searchParams.set('refresh', 'true')
return url.href
}