2022-05-23 21:29:33 +12:00
|
|
|
import { events } from "@budibase/backend-core"
|
2022-08-10 22:01:54 +12:00
|
|
|
import { AnalyticsPingRequest, PingSource } from "@budibase/types"
|
2022-08-12 00:50:05 +12:00
|
|
|
import { DocumentType, isDevAppID } from "../../db/utils"
|
2022-08-10 22:01:54 +12:00
|
|
|
import { context } from "@budibase/backend-core"
|
2022-05-23 21:29:33 +12:00
|
|
|
|
|
|
|
export const isEnabled = async (ctx: any) => {
|
2022-06-01 08:04:41 +12:00
|
|
|
const enabled = await events.analytics.enabled()
|
2022-05-23 21:29:33 +12:00
|
|
|
ctx.body = {
|
2022-06-01 08:04:41 +12:00
|
|
|
enabled,
|
2022-05-23 21:29:33 +12:00
|
|
|
}
|
|
|
|
}
|
2022-08-10 22:01:54 +12:00
|
|
|
|
|
|
|
export const ping = async (ctx: any) => {
|
|
|
|
const body = ctx.request.body as AnalyticsPingRequest
|
|
|
|
switch (body.source) {
|
|
|
|
case PingSource.APP: {
|
|
|
|
const db = context.getAppDB({ skip_setup: true })
|
2022-08-12 00:50:05 +12:00
|
|
|
const appInfo = await db.get(DocumentType.APP_METADATA)
|
2022-08-10 22:01:54 +12:00
|
|
|
let appId = context.getAppId()
|
|
|
|
|
|
|
|
if (isDevAppID(appId)) {
|
2022-08-10 22:29:11 +12:00
|
|
|
await events.serve.servedAppPreview(appInfo, body.timezone)
|
2022-08-10 22:01:54 +12:00
|
|
|
} else {
|
2022-08-10 22:29:11 +12:00
|
|
|
await events.serve.servedApp(appInfo, body.timezone)
|
2022-08-10 22:01:54 +12:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case PingSource.BUILDER: {
|
2022-08-10 22:29:11 +12:00
|
|
|
await events.serve.servedBuilder(body.timezone)
|
2022-08-10 22:01:54 +12:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.status = 200
|
|
|
|
}
|