diff --git a/api/src/routes/text-messages.ts b/api/src/routes/text-messages.ts index c2693e3..923a003 100644 --- a/api/src/routes/text-messages.ts +++ b/api/src/routes/text-messages.ts @@ -3,95 +3,96 @@ import { prisma } from "../db.js"; import express from "../express.js"; express.get("/api/v1/text-messages", async (req, res) => { - try { - const directMessageIds = req.query.direct_message_node_ids as string; - const lastI = req.query.last_id as string; - const coun = req.query.count as string; + try { + const directMessageIds = req.query.direct_message_node_ids as string; + const lastI = req.query.last_id as string; + const coun = req.query.count as string; - // get query params - const to = req.query.to ?? undefined; - const from = req.query.from ?? undefined; - const channelId = (req.query.channel_id as string) ?? undefined; - const gatewayId = - Number.parseInt(req.query.gateway_id as string) ?? undefined; - const directMessageNodeIds = directMessageIds.split(",") ?? undefined; - const lastId = lastI ? Number.parseInt(lastI) : undefined; - const count = req.query.count ? Number.parseInt(coun) : 50; - const order = req.query.order ?? "asc"; + // get query params + const to = req.query.to ?? undefined; + const from = req.query.from ?? undefined; + const channelId = (req.query.channel_id as string) ?? undefined; + const gatewayId = + Number.parseInt(req.query.gateway_id as string) ?? undefined; + const directMessageNodeIds = directMessageIds.split(",") ?? undefined; + const lastId = lastI ? Number.parseInt(lastI) : undefined; + const count = req.query.count ? Number.parseInt(coun) : 50; + const order = req.query.order ?? "asc"; - // if direct message node ids are provided, there should be exactly two node ids - if ( - directMessageNodeIds !== undefined && - directMessageNodeIds.length !== 2 - ) { - res.status(400).json({ - message: - "direct_message_node_ids requires 2 node ids separated by a comma.", - }); - return; - } + // if direct message node ids are provided, there should be exactly two node ids + if ( + directMessageNodeIds !== undefined && + directMessageNodeIds.length !== 2 + ) { + res.status(400).json({ + message: + "direct_message_node_ids requires 2 node ids separated by a comma.", + }); + return; + } - // default where clauses that should always be used for filtering - let where: Prisma.TextMessageWhereInput = { - channel_id: channelId, - gateway_id: gatewayId, - // when ordered oldest to newest (asc), only get records after last id - // when ordered newest to oldest (desc), only get records before last id - id: - order === "asc" - ? { - gt: lastId, - } - : { - lt: lastId, - }, - }; + // default where clauses that should always be used for filtering + let where: Prisma.TextMessageWhereInput = { + channel_id: channelId, + gateway_id: gatewayId, + // when ordered oldest to newest (asc), only get records after last id + // when ordered newest to oldest (desc), only get records before last id + id: + order === "asc" + ? { + gt: lastId, + } + : { + lt: lastId, + }, + }; - // if direct message node ids are provided, we expect exactly 2 node ids - if ( - directMessageNodeIds !== undefined && - directMessageNodeIds.length === 2 - ) { - // filter message by "to -> from" or "from -> to" - const [firstNodeId, secondNodeId] = directMessageNodeIds; - where = { - AND: where, - OR: [ - { - to: Number.parseInt(firstNodeId), - from: Number.parseInt(secondNodeId), - }, - { - to: Number.parseInt(secondNodeId), - from: Number.parseInt(firstNodeId), - }, - ], - }; - } else { - // filter by to and from - where = { - ...where, - to: Number.parseInt(to as string), - from: Number.parseInt(from as string), - }; - } + // if direct message node ids are provided, we expect exactly 2 node ids + if ( + directMessageNodeIds !== undefined && + directMessageNodeIds.length === 2 + ) { + // filter message by "to -> from" or "from -> to" + const [firstNodeId, secondNodeId] = directMessageNodeIds; + where = { + AND: where, + OR: [ + { + to: Number.parseInt(firstNodeId), + from: Number.parseInt(secondNodeId), + }, + { + to: Number.parseInt(secondNodeId), + from: Number.parseInt(firstNodeId), + }, + ], + }; + } else { + // filter by to and from + where = { + ...where, + to: Number.parseInt(to as string), + from: Number.parseInt(from as string), + }; + } - // get text messages from db - const textMessages = await prisma.textMessage.findMany({ - where: where, - orderBy: { - id: order as Prisma.SortOrder, - }, - take: count, - }); + // get text messages from db + const textMessages = await prisma.textMessage.findMany({ + where: where, + orderBy: { + id: order as Prisma.SortOrder, + }, + take: count, + }); - res.json({ - text_messages: textMessages, - }); - } catch (err) { - res.status(500).json({ - message: "Something went wrong, try again later.", - }); - } + res.json({ + text_messages: textMessages, + }); + } catch (err) { + console.error(err); + res.status(500).json({ + message: "Something went wrong, try again later.", + }); + } }); console.log("API:EXPRESS registered route GET:/api/v1/text-messages"); diff --git a/api/src/routes/waypoints.ts b/api/src/routes/waypoints.ts index 712c30e..26d244d 100644 --- a/api/src/routes/waypoints.ts +++ b/api/src/routes/waypoints.ts @@ -2,45 +2,46 @@ import { prisma } from "../db.js"; import express from "../express.js"; express.get("/api/v1/waypoints", async (req, res) => { - try { - // get waypoints from db - const waypoints = await prisma.waypoint.findMany({ - orderBy: { - id: "desc", - }, - }); + try { + // get waypoints from db + const waypoints = await prisma.waypoint.findMany({ + orderBy: { + id: "desc", + }, + }); - // ensure we only have the latest unique waypoints - // since ordered by newest first, older entries will be ignored - const uniqueWaypoints: typeof waypoints = []; - for (const waypoint of waypoints) { - // skip if we already have a newer entry for this waypoint - if ( - uniqueWaypoints.find( - (w) => - w.from === waypoint.from && w.waypoint_id === waypoint.waypoint_id - ) - ) { - continue; - } + // ensure we only have the latest unique waypoints + // since ordered by newest first, older entries will be ignored + const uniqueWaypoints: typeof waypoints = []; + for (const waypoint of waypoints) { + // skip if we already have a newer entry for this waypoint + if ( + uniqueWaypoints.find( + (w) => + w.from === waypoint.from && w.waypoint_id === waypoint.waypoint_id + ) + ) { + continue; + } - // first time seeing this waypoint, add to unique list - uniqueWaypoints.push(waypoint); - } + // first time seeing this waypoint, add to unique list + uniqueWaypoints.push(waypoint); + } - // we only want waypoints that haven't expired yet - const nonExpiredWayPoints = uniqueWaypoints.filter((waypoint) => { - const nowInSeconds = Math.floor(Date.now() / 1000); - if (waypoint.expire) return waypoint.expire >= nowInSeconds; - }); + // we only want waypoints that haven't expired yet + const nonExpiredWayPoints = uniqueWaypoints.filter((waypoint) => { + const nowInSeconds = Math.floor(Date.now() / 1000); + if (waypoint.expire) return waypoint.expire >= nowInSeconds; + }); - res.json({ - waypoints: nonExpiredWayPoints, - }); - } catch (err) { - res.status(500).json({ - message: "Something went wrong, try again later.", - }); - } + res.json({ + waypoints: nonExpiredWayPoints, + }); + } catch (err) { + console.error(err); + res.status(500).json({ + message: "Something went wrong, try again later.", + }); + } }); console.log("API:EXPRESS registered route GET:/api/v1/waypoints"); diff --git a/app/public/index.html b/app/public/index.html index e26f347..9b3f091 100644 --- a/app/public/index.html +++ b/app/public/index.html @@ -2771,7 +2771,9 @@

if (value) { return JSON.parse(value); } - } catch (e) {} + } catch (e) { + console.error(e); + } // overlays enabled by default return ["Legend", "Position History"]; diff --git a/mqtt/src/messages/service_envelope.ts b/mqtt/src/messages/service_envelope.ts index e201773..addcf2b 100644 --- a/mqtt/src/messages/service_envelope.ts +++ b/mqtt/src/messages/service_envelope.ts @@ -42,6 +42,7 @@ export async function handleServiceEnvelope( }, }); } catch (e) { + console.error(e); // don't care if updating mqtt timestamp fails } diff --git a/mqtt/src/tools/purging.ts b/mqtt/src/tools/purging.ts index 4dd3ed1..472a8af 100644 --- a/mqtt/src/tools/purging.ts +++ b/mqtt/src/tools/purging.ts @@ -1,104 +1,106 @@ import { prisma } from "../db.js"; import { - PURGE_DEVICE_METRICS_AFTER_SECONDS, - PURGE_ENVIROMENT_METRICS_AFTER_SECONDS, - PURGE_POWER_METRICS_AFTER_SECONDS, - PURGE_MAP_REPORTS_AFTER_SECONDS, - PURGE_NEIGHBOUR_INFOS_AFTER_SECONDS, - PURGE_UNHEARD_NODES_FOR_SECONDS, - PURGE_POSITIONS_AFTER_SECONDS, - PURGE_SERVICE_ENVELOPES_AFTER_SECONDS, - PURGE_TEXT_MESSAGES_AFTER_SECONDS, - PURGE_TRACEROUTES_AFTER_SECONDS, - PURGE_WAYPOINTS_AFTER_SECONDS, + PURGE_DEVICE_METRICS_AFTER_SECONDS, + PURGE_ENVIROMENT_METRICS_AFTER_SECONDS, + PURGE_POWER_METRICS_AFTER_SECONDS, + PURGE_MAP_REPORTS_AFTER_SECONDS, + PURGE_NEIGHBOUR_INFOS_AFTER_SECONDS, + PURGE_UNHEARD_NODES_FOR_SECONDS, + PURGE_POSITIONS_AFTER_SECONDS, + PURGE_SERVICE_ENVELOPES_AFTER_SECONDS, + PURGE_TEXT_MESSAGES_AFTER_SECONDS, + PURGE_TRACEROUTES_AFTER_SECONDS, + PURGE_WAYPOINTS_AFTER_SECONDS, } from "../settings.js"; /** * Purges all nodes from the database that haven't been heard from within the configured timeframe. */ export async function purgeUnheardNodes() { - if (PURGE_UNHEARD_NODES_FOR_SECONDS === 0) return; - - // delete all nodes that were last updated before configured purge time - try { - await prisma.node.deleteMany({ - where: { - updated_at: { - // last updated before x seconds ago - lt: new Date(Date.now() - PURGE_UNHEARD_NODES_FOR_SECONDS * 1000), - }, - }, - }); - } catch (e) { - // do nothing - } + if (PURGE_UNHEARD_NODES_FOR_SECONDS === 0) return; + + // delete all nodes that were last updated before configured purge time + try { + await prisma.node.deleteMany({ + where: { + updated_at: { + // last updated before x seconds ago + lt: new Date(Date.now() - PURGE_UNHEARD_NODES_FOR_SECONDS * 1000), + }, + }, + }); + } catch (e) { + console.error(e); + // do nothing + } } /** * Purges all device metrics from the database that are older than the configured timeframe. */ export async function purgeOldDeviceMetrics() { - if (PURGE_DEVICE_METRICS_AFTER_SECONDS === 0) return; - - // delete all device metrics that are older than the configured purge time - try { - await prisma.deviceMetric.deleteMany({ - where: { - created_at: { - // last updated before x seconds ago - lt: new Date(Date.now() - PURGE_DEVICE_METRICS_AFTER_SECONDS * 1000), - }, - }, - }); - } catch (e) { - // do nothing - } + if (PURGE_DEVICE_METRICS_AFTER_SECONDS === 0) return; + + // delete all device metrics that are older than the configured purge time + try { + await prisma.deviceMetric.deleteMany({ + where: { + created_at: { + // last updated before x seconds ago + lt: new Date(Date.now() - PURGE_DEVICE_METRICS_AFTER_SECONDS * 1000), + }, + }, + }); + } catch (e) { + console.error(e); + // do nothing + } } /** * Purges all environment metrics from the database that are older than the configured timeframe. */ export async function purgeOldEnvironmentMetrics() { - if (PURGE_ENVIROMENT_METRICS_AFTER_SECONDS === 0) return; - - // delete all environment metrics that are older than the configured purge time - try { - await prisma.environmentMetric.deleteMany({ - where: { - created_at: { - // last updated before x seconds ago - lt: new Date( - Date.now() - PURGE_ENVIROMENT_METRICS_AFTER_SECONDS * 1000 - ), - }, - }, - }); - } catch (e) { - // do nothing - } + if (PURGE_ENVIROMENT_METRICS_AFTER_SECONDS === 0) return; + + // delete all environment metrics that are older than the configured purge time + try { + await prisma.environmentMetric.deleteMany({ + where: { + created_at: { + // last updated before x seconds ago + lt: new Date( + Date.now() - PURGE_ENVIROMENT_METRICS_AFTER_SECONDS * 1000 + ), + }, + }, + }); + } catch (e) { + console.error(e); + // do nothing + } } /** * Purges all map reports from the database that are older than the configured timeframe. */ export async function purgeOldMapReports() { - if (PURGE_MAP_REPORTS_AFTER_SECONDS === 0) return; - - // delete all map reports that are older than the configured purge time - try { - await prisma.mapReport.deleteMany({ - where: { - created_at: { - // last updated before x seconds ago - lt: new Date( - Date.now() - PURGE_MAP_REPORTS_AFTER_SECONDS * 1000 - ), - }, - }, - }); - } catch (e) { - // do nothing - } + if (PURGE_MAP_REPORTS_AFTER_SECONDS === 0) return; + + // delete all map reports that are older than the configured purge time + try { + await prisma.mapReport.deleteMany({ + where: { + created_at: { + // last updated before x seconds ago + lt: new Date(Date.now() - PURGE_MAP_REPORTS_AFTER_SECONDS * 1000), + }, + }, + }); + } catch (e) { + console.error(e); + // do nothing + } } /** @@ -119,6 +121,7 @@ export async function purgeOldNeighbourInfos() { }, }); } catch (e) { + console.error(e); // do nothing } } @@ -127,124 +130,132 @@ export async function purgeOldNeighbourInfos() { * Purges all power metrics from the database that are older than the configured timeframe. */ export async function purgeOldPowerMetrics() { - if (PURGE_POWER_METRICS_AFTER_SECONDS === 0) return; - - // delete all power metrics that are older than the configured purge time - try { - await prisma.powerMetric.deleteMany({ - where: { - created_at: { - // last updated before x seconds ago - lt: new Date(Date.now() - PURGE_POWER_METRICS_AFTER_SECONDS * 1000), - }, - }, - }); - } catch (e) { - // do nothing - } + if (PURGE_POWER_METRICS_AFTER_SECONDS === 0) return; + + // delete all power metrics that are older than the configured purge time + try { + await prisma.powerMetric.deleteMany({ + where: { + created_at: { + // last updated before x seconds ago + lt: new Date(Date.now() - PURGE_POWER_METRICS_AFTER_SECONDS * 1000), + }, + }, + }); + } catch (e) { + console.error(e); + // do nothing + } } /** * Purges all positions from the database that are older than the configured timeframe. */ export async function purgeOldPositions() { - if (PURGE_POSITIONS_AFTER_SECONDS === 0) return; - - // delete all positions that are older than the configured purge time - try { - await prisma.position.deleteMany({ - where: { - created_at: { - // last updated before x seconds ago - lt: new Date(Date.now() - PURGE_POSITIONS_AFTER_SECONDS * 1000), - }, - }, - }); - } catch (e) { - // do nothing - } + if (PURGE_POSITIONS_AFTER_SECONDS === 0) return; + + // delete all positions that are older than the configured purge time + try { + await prisma.position.deleteMany({ + where: { + created_at: { + // last updated before x seconds ago + lt: new Date(Date.now() - PURGE_POSITIONS_AFTER_SECONDS * 1000), + }, + }, + }); + } catch (e) { + console.error(e); + // do nothing + } } /** * Purges all service envelopes from the database that are older than the configured timeframe. */ export async function purgeOldServiceEnvelopes() { - if (PURGE_SERVICE_ENVELOPES_AFTER_SECONDS === 0) return; - - // delete all envelopes that are older than the configured purge time - try { - await prisma.serviceEnvelope.deleteMany({ - where: { - created_at: { - // last updated before x seconds ago - lt: new Date(Date.now() - PURGE_SERVICE_ENVELOPES_AFTER_SECONDS * 1000), - }, - }, - }); - } catch (e) { - // do nothing - } + if (PURGE_SERVICE_ENVELOPES_AFTER_SECONDS === 0) return; + + // delete all envelopes that are older than the configured purge time + try { + await prisma.serviceEnvelope.deleteMany({ + where: { + created_at: { + // last updated before x seconds ago + lt: new Date( + Date.now() - PURGE_SERVICE_ENVELOPES_AFTER_SECONDS * 1000 + ), + }, + }, + }); + } catch (e) { + console.error(e); + // do nothing + } } /** * Purges all text messages from the database that are older than the configured timeframe. */ export async function purgeOldTextMessages() { - if (PURGE_TEXT_MESSAGES_AFTER_SECONDS === 0) return; - - // delete all text messages that are older than the configured purge time - try { - await prisma.textMessage.deleteMany({ - where: { - created_at: { - // last updated before x seconds ago - lt: new Date(Date.now() - PURGE_TEXT_MESSAGES_AFTER_SECONDS * 1000), - }, - }, - }); - } catch (e) { - // do nothing - } + if (PURGE_TEXT_MESSAGES_AFTER_SECONDS === 0) return; + + // delete all text messages that are older than the configured purge time + try { + await prisma.textMessage.deleteMany({ + where: { + created_at: { + // last updated before x seconds ago + lt: new Date(Date.now() - PURGE_TEXT_MESSAGES_AFTER_SECONDS * 1000), + }, + }, + }); + } catch (e) { + console.error(e); + // do nothing + } } /** * Purges all traceroutes from the database that are older than the configured timeframe. */ export async function purgeOldTraceroutes() { - if (PURGE_TRACEROUTES_AFTER_SECONDS === 0) return; - - // delete all traceroutes that are older than the configured purge time - try { - await prisma.traceRoute.deleteMany({ - where: { - created_at: { - // last updated before x seconds ago - lt: new Date(Date.now() - PURGE_TRACEROUTES_AFTER_SECONDS * 1000), - }, - }, - }); - } catch (e) { - // do nothing - } + if (PURGE_TRACEROUTES_AFTER_SECONDS === 0) return; + + // delete all traceroutes that are older than the configured purge time + try { + await prisma.traceRoute.deleteMany({ + where: { + created_at: { + // last updated before x seconds ago + lt: new Date(Date.now() - PURGE_TRACEROUTES_AFTER_SECONDS * 1000), + }, + }, + }); + } catch (e) { + console.error(e); + // do nothing + } } /** * Purges all waypoints from the database that are older than the configured timeframe. */ export async function purgeOldWaypoints() { - if (PURGE_WAYPOINTS_AFTER_SECONDS === 0) return; - - // delete all waypoints that are older than the configured purge time - try { - await prisma.waypoint.deleteMany({ - where: { - created_at: { - // created before x seconds ago - lt: new Date(Date.now() - PURGE_WAYPOINTS_AFTER_SECONDS * 1000), - }, - }, - }); - } catch (e) { - // do nothing - } + if (PURGE_WAYPOINTS_AFTER_SECONDS === 0) return; + + // delete all waypoints that are older than the configured purge time + try { + await prisma.waypoint.deleteMany({ + where: { + created_at: { + // created before x seconds ago + lt: new Date(Date.now() - PURGE_WAYPOINTS_AFTER_SECONDS * 1000), + }, + }, + }); + } catch (e) { + console.error(e); + // do nothing + } }