Skip to content

Commit

Permalink
fix heatmap (no views)
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesbrandenburger committed Jan 19, 2024
1 parent 18fcba4 commit b9b2d3e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 45 deletions.
73 changes: 29 additions & 44 deletions services/heatmap-service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion services/heatmap-service/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.0.2
v1.0.3

0 comments on commit b9b2d3e

Please sign in to comment.