feat: telemetry add telemetryId report

This commit is contained in:
moonrailgun 2024-02-22 23:51:52 +08:00
parent b02e0b75d6
commit 355690eb75
3 changed files with 50 additions and 9 deletions

View File

@ -73,14 +73,14 @@ if (env.allowOpenapi) {
}
// fallback
app.get('/*', (req, res) => {
if (req.accepts('html')) {
app.use('/*', (req, res) => {
if (req.method === 'GET' && req.accepts('html')) {
res.sendFile(path.join(process.cwd(), 'public', 'index.html'));
}
});
app.use((err: any, req: any, res: any, next: any) => {
logger.error(err);
logger.error('[express]', err);
res.status(500).json({ message: err.message });
});

View File

@ -22,6 +22,8 @@ export async function recordTelemetryEvent(req: Request) {
return;
}
const telemetryId = req.params.telemetryId;
const { origin, pathname } = new URL(url);
const payload = Object.keys(others).length > 0 ? others : undefined;
@ -29,6 +31,7 @@ export async function recordTelemetryEvent(req: Request) {
data: {
sessionId: session.id,
workspaceId,
telemetryId,
eventName,
urlOrigin: origin,
urlPath: pathname,

View File

@ -6,6 +6,11 @@ import { makeBadge } from 'badge-maker';
export const telemetryRouter = Router();
const blankGifBuffer = Buffer.from(
'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7',
'base64'
);
telemetryRouter.get(
'/:workspaceId/blank.gif',
validate(
@ -13,14 +18,9 @@ telemetryRouter.get(
query('url').optional().isURL()
),
async (req, res) => {
const buffer = Buffer.from(
'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7',
'base64'
);
recordTelemetryEvent(req);
res.header('Content-Type', 'image/gif').send(buffer);
res.header('Content-Type', 'image/gif').send(blankGifBuffer);
}
);
@ -46,3 +46,41 @@ telemetryRouter.get(
res.header('Content-Type', 'image/svg+xml').send(svg);
}
);
telemetryRouter.get(
'/:workspaceId/:telemetryId.gif',
validate(
query('name').optional().isString(),
query('url').optional().isURL()
),
async (req, res) => {
recordTelemetryEvent(req);
res.header('Content-Type', 'image/gif').status(200).send(blankGifBuffer);
}
);
telemetryRouter.get(
'/:workspaceId/:telemetryId/badge.svg',
validate(
query('name').optional().isString(),
query('url').optional().isURL(),
query('title').optional().isString(),
query('start').optional().isNumeric()
),
async (req, res) => {
const title = req.query.title || 'visitor';
const start = req.query.start ? Number(req.query.start) : 0;
recordTelemetryEvent(req);
const num = await sumTelemetryEvent(req);
const svg = makeBadge({
label: String(title),
message: numify(num + start),
color: 'green',
});
res.header('Content-Type', 'image/svg+xml').status(200).send(svg);
}
);