Enhance traceroute retrieval by deduplicating results based on packet_id, ensuring only the latest entry for each packet_id is returned.

This commit is contained in:
Anton Roslund
2026-02-28 11:04:42 +01:00
parent 8fd6730e0d
commit 3cccf80d07

View File

@@ -547,10 +547,22 @@ app.get('/api/v1/nodes/:nodeId/traceroutes', async (req, res) => {
return;
}
// get latest traceroutes
// get latest traceroutes, deduplicated by packet_id
// We want replies where want_response is false and it will be "to" the
// requester.
const traceroutes = await prisma.$queryRaw`SELECT * FROM traceroutes WHERE want_response = false and \`to\` = ${node.node_id} and gateway_id is not null order by id desc limit ${count}`;
// Deduplicate by packet_id, keeping the latest traceroute (highest id) for each packet_id
const traceroutes = await prisma.$queryRaw`
SELECT t1.*
FROM traceroutes t1
INNER JOIN (
SELECT packet_id, MAX(id) as max_id
FROM traceroutes
WHERE want_response = false and \`to\` = ${node.node_id} and gateway_id is not null
GROUP BY packet_id
) t2 ON t1.packet_id = t2.packet_id AND t1.id = t2.max_id
ORDER BY t1.id DESC
LIMIT ${count}
`;
res.json({
traceroutes: traceroutes.map((trace) => {