Add optional parameter for filtering connections by node

This commit is contained in:
Anton Roslund 2026-01-08 18:32:55 +01:00
parent 556dde517b
commit 71d32d1cd0

View file

@ -159,6 +159,7 @@ app.get('/api', async (req, res) => {
"path": "/api/v1/connections", "path": "/api/v1/connections",
"description": "Aggregated edges between nodes from traceroutes", "description": "Aggregated edges between nodes from traceroutes",
"params": { "params": {
"node_id": "Only include connections involving this node id",
"time_from": "Only include edges created after this unix timestamp (milliseconds)", "time_from": "Only include edges created after this unix timestamp (milliseconds)",
"time_to": "Only include edges created before this unix timestamp (milliseconds)" "time_to": "Only include edges created before this unix timestamp (milliseconds)"
} }
@ -707,9 +708,10 @@ app.get('/api/v1/traceroutes', async (req, res) => {
}); });
// Aggregated edges endpoint // Aggregated edges endpoint
// GET /api/v1/connections?time_from=...&time_to=... // GET /api/v1/connections?node_id=...&time_from=...&time_to=...
app.get('/api/v1/connections', async (req, res) => { app.get('/api/v1/connections', async (req, res) => {
try { try {
const nodeId = req.query.node_id ? parseInt(req.query.node_id) : undefined;
const timeFrom = req.query.time_from ? parseInt(req.query.time_from) : undefined; const timeFrom = req.query.time_from ? parseInt(req.query.time_from) : undefined;
const timeTo = req.query.time_to ? parseInt(req.query.time_to) : undefined; const timeTo = req.query.time_to ? parseInt(req.query.time_to) : undefined;
@ -717,14 +719,21 @@ app.get('/api/v1/connections', async (req, res) => {
const edges = await prisma.edge.findMany({ const edges = await prisma.edge.findMany({
where: { where: {
created_at: { created_at: {
gte: timeFrom ? new Date(timeFrom) : undefined, ...(timeFrom && { gte: new Date(timeFrom) }),
lte: timeTo ? new Date(timeTo) : undefined, ...(timeTo && { lte: new Date(timeTo) }),
}, },
// Only include edges where both nodes have positions // Only include edges where both nodes have positions
from_latitude: { not: null }, from_latitude: { not: null },
from_longitude: { not: null }, from_longitude: { not: null },
to_latitude: { not: null }, to_latitude: { not: null },
to_longitude: { not: null }, to_longitude: { not: null },
// If node_id is provided, filter edges where either from_node_id or to_node_id matches
...(nodeId !== undefined && {
OR: [
{ from_node_id: nodeId },
{ to_node_id: nodeId },
],
}),
}, },
orderBy: [ orderBy: [
{ created_at: 'desc' }, { created_at: 'desc' },