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 // fallback
app.get('/*', (req, res) => { app.use('/*', (req, res) => {
if (req.accepts('html')) { if (req.method === 'GET' && req.accepts('html')) {
res.sendFile(path.join(process.cwd(), 'public', 'index.html')); res.sendFile(path.join(process.cwd(), 'public', 'index.html'));
} }
}); });
app.use((err: any, req: any, res: any, next: any) => { app.use((err: any, req: any, res: any, next: any) => {
logger.error(err); logger.error('[express]', err);
res.status(500).json({ message: err.message }); res.status(500).json({ message: err.message });
}); });

View File

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

View File

@ -6,6 +6,11 @@ import { makeBadge } from 'badge-maker';
export const telemetryRouter = Router(); export const telemetryRouter = Router();
const blankGifBuffer = Buffer.from(
'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7',
'base64'
);
telemetryRouter.get( telemetryRouter.get(
'/:workspaceId/blank.gif', '/:workspaceId/blank.gif',
validate( validate(
@ -13,14 +18,9 @@ telemetryRouter.get(
query('url').optional().isURL() query('url').optional().isURL()
), ),
async (req, res) => { async (req, res) => {
const buffer = Buffer.from(
'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7',
'base64'
);
recordTelemetryEvent(req); 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); 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);
}
);