feat: add telemetry event payload

This commit is contained in:
moonrailgun 2023-10-31 22:36:32 +08:00
parent 503321d43b
commit dc77c4f85a
4 changed files with 8 additions and 3 deletions

View File

@ -14,7 +14,6 @@
"build:openapi": "ts-node scripts/build-openapi-schema.ts", "build:openapi": "ts-node scripts/build-openapi-schema.ts",
"postinstall": "pnpm db:generate", "postinstall": "pnpm db:generate",
"check:type": "tsc --noEmit --skipLibCheck --module esnext", "check:type": "tsc --noEmit --skipLibCheck --module esnext",
"db:push": "prisma db push",
"db:generate": "prisma generate", "db:generate": "prisma generate",
"db:migrate:dev": "prisma migrate dev", "db:migrate:dev": "prisma migrate dev",
"db:migrate:apply": "prisma migrate deploy", "db:migrate:apply": "prisma migrate deploy",

View File

@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "TelemetryEvent" ADD COLUMN "payload" JSON;

View File

@ -205,6 +205,7 @@ model TelemetryEvent {
eventName String? @db.VarChar(100) eventName String? @db.VarChar(100)
urlOrigin String @db.VarChar(500) urlOrigin String @db.VarChar(500)
urlPath String @db.VarChar(500) urlPath String @db.VarChar(500)
payload Json? @db.Json // Other payload info get from query params, should be a object
createdAt DateTime @default(now()) @db.Timestamptz(6) createdAt DateTime @default(now()) @db.Timestamptz(6)
session TelemetrySession @relation(fields: [sessionId], references: [id], onUpdate: Cascade, onDelete: Cascade) session TelemetrySession @relation(fields: [sessionId], references: [id], onUpdate: Cascade, onDelete: Cascade)

View File

@ -5,11 +5,12 @@ import { getRequestInfo } from '../utils/detect';
import { prisma } from './_client'; import { prisma } from './_client';
export async function recordTelemetryEvent(req: Request) { export async function recordTelemetryEvent(req: Request) {
const url = req.query.url ?? req.headers.referer; const { url = req.headers.referer, name, ...others } = req.query;
if (!(url && typeof url === 'string')) { if (!(url && typeof url === 'string')) {
return; return;
} }
const eventName = req.query.name ? String(req.query.name) : undefined; const eventName = name ? String(name) : undefined;
const session = await findSession(req, url); const session = await findSession(req, url);
if (!session) { if (!session) {
@ -22,6 +23,7 @@ export async function recordTelemetryEvent(req: Request) {
} }
const { origin, pathname } = new URL(url); const { origin, pathname } = new URL(url);
const payload = Object.keys(others).length > 0 ? others : undefined;
await prisma.telemetryEvent.create({ await prisma.telemetryEvent.create({
data: { data: {
@ -30,6 +32,7 @@ export async function recordTelemetryEvent(req: Request) {
eventName, eventName,
urlOrigin: origin, urlOrigin: origin,
urlPath: pathname, urlPath: pathname,
payload,
}, },
}); });
} }