Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/telemetry/telemetry-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PrismaService } from '../global-services/prisma.service';
import { Logger } from "@nestjs/common";

const prisma = new PrismaService();
const logger = new Logger('TelemetryProcessor');
const logger = new Logger('TelemetryProcessor ');

const telemetryBatch: any[] = [];
const BATCH_INTERVAL = 60000; // 60 seconds
Expand Down
17 changes: 14 additions & 3 deletions src/telemetry/telemetry.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import * as si from "systeminformation";
import { PrismaService } from "../global-services/prisma.service";
import fetch from "node-fetch";
import { addToTelemetryBatch } from "./telemetry-processor";
import { Logger } from "@nestjs/common";


export async function telemetryMiddleware(
request: FastifyRequest,
reply: FastifyReply
) {
const prisma = new PrismaService();
const logger = new Logger('Telemetry');


try {
const userId = request.headers["user-id"] as string;
Expand Down Expand Up @@ -38,8 +42,8 @@ export async function telemetryMiddleware(
(/mobile/i.test(userAgent)
? "mobile"
: /tablet|iPad/i.test(userAgent)
? "tablet"
: "desktop");
? "tablet"
: "desktop");

const deviceInfo = {
did,
Expand All @@ -55,11 +59,18 @@ export async function telemetryMiddleware(
userId,
sessionId,
};
logger.log(`DEVICE INFO: ${JSON.stringify(deviceInfo)}`);
const userExists = await prisma.user.findUnique({
where: { id: userId },
});
if (userExists) {
await prisma.deviceMetrics.create({ data: deviceInfo });
const { userId, ...deviceInfoWithoutUserId } = deviceInfo;
await prisma.deviceMetrics.create({
data: {
...deviceInfoWithoutUserId,
user: { connect: { id: userId } }
}
});
} else {
addToTelemetryBatch(deviceInfo); // use batch
}
Expand Down