From b9b2d3ed454aa2e6a721856a2fce4cab31340c55 Mon Sep 17 00:00:00 2001 From: Johannes Brandenburger <79154528+johannesbrandenburger@users.noreply.github.com> Date: Fri, 19 Jan 2024 12:45:26 +0100 Subject: [PATCH] fix heatmap (no views) --- services/heatmap-service/index.js | 73 +++++++++++----------------- services/heatmap-service/version.txt | 2 +- 2 files changed, 30 insertions(+), 45 deletions(-) diff --git a/services/heatmap-service/index.js b/services/heatmap-service/index.js index e8ed738..a2b6083 100644 --- a/services/heatmap-service/index.js +++ b/services/heatmap-service/index.js @@ -8,8 +8,12 @@ import dotenv from "dotenv"; import path from "path"; import { fileURLToPath } from "url"; dotenv.config({ path: path.join(path.dirname(fileURLToPath(import.meta.url)), "../.env") }); +const debug = (...args) => { console.log(...args); }; + const MONDODB_URI = process.env.MONGODB_URI || "mongodb://localhost:27017"; +debug("heatmap-service: connecting to MongoDB: ", MONDODB_URI); + // connect to MongoDB const client = new MongoClient(MONDODB_URI); await client.connect(); @@ -21,66 +25,47 @@ const polygonCollection = database.collection("polygon"); // heatRegionsState = { timestamp: string, heatRegions: [{ polygonname: string, density: number (0-1), count: number }] } const heatRegionStateCollection = database.collection("heatRegionState"); -// create a view that joins the heatRegion and polygon collections -try { await database.command({ drop: "heatRegionStateWithPolygonView" }); } catch (e) { } -// polygon: {polygonname: string, polygon: []} -// heatRegionWithPolygonView: { timestamp: string, heatRegions: [{ polygonname: string, density: number (0-1), count: number, polygon: [] }] } -await database.command({ - create: "heatRegionStateWithPolygonView", - viewOn: "heatRegionState", - pipeline: [ - { - $lookup: { - from: "polygon", - localField: "heatRegions.polygonname", - foreignField: "polygonname", - as: "polygondata", - }, - }, - { - $project: { - _id: 0, - timestamp: 1, - heatRegions: { - polygonname: 1, - density: 1, - count: 1, - polygon: "$polygondata.polygon", - }, - }, - }, - { - $unwind: "$heatRegions.polygon" - } - ], -}); -const heatRegionStateWithPolygonView = database.collection("heatRegionStateWithPolygonView"); - // start express server const app = express(); const port = 3003; app.use(express.json()); app.use(cookieParser()); -app.use(auth); +// app.use(auth); + // define routes app.get("/", async (req, res) => { + debug("heatmap-service: GET /"); + // get the current heat region state - const heatRegionState = await heatRegionStateCollection.find({}).sort({ timestamp: -1 }).limit(1).next(); + const heatRegionState = await heatRegionStateCollection.find({}).sort({ "timestamp": -1 }).limit(1).next(); if (!heatRegionState) { + debug("heatmap-service: no heat region state found"); res.status(400).send("No heat region state found"); return; } - // TEMP: add polygon data to the heat region state (later this should be done in the view) - for (let i = 0; i < heatRegionState.heatRegions.length; i++) { - const heatRegion = heatRegionState.heatRegions[i]; - const polygon = await polygonCollection.findOne({ polygonname: heatRegion.polygonname }); - if (polygon) { - heatRegion.polygon = polygon.polygon; + debug("heatmap-service: heat region state: ", heatRegionState); + + const polygonNames = heatRegionState.heatRegions.map(heatRegion => heatRegion.polygonname); + + debug("heatmap-service: polygon names: ", polygonNames); + + const polygons = await polygonCollection.find({ polygonname: { $in: polygonNames } }).toArray(); + + debug("heatmap-service: polygons: ", polygons); + + heatRegionState.heatRegions.forEach(heatRegion => { + const polygon = polygons.find(polygon => polygon.polygonname === heatRegion.polygonname); + if (!polygon) { + console.error(`Could not find polygon ${heatRegion.polygonname}`); + return; } - } + heatRegion.polygon = polygon.polygon; + }); + + debug("heatmap-service: sending heat region state: ", heatRegionState); // send the heat region state res.status(200).send(heatRegionState); diff --git a/services/heatmap-service/version.txt b/services/heatmap-service/version.txt index b482243..13637f4 100644 --- a/services/heatmap-service/version.txt +++ b/services/heatmap-service/version.txt @@ -1 +1 @@ -v1.0.2 \ No newline at end of file +v1.0.3 \ No newline at end of file