From 1f449e9f0c2bf3592875eb00233dc80a1b8cdcbb Mon Sep 17 00:00:00 2001 From: devthejo Date: Thu, 11 Sep 2025 11:53:44 +0200 Subject: [PATCH] feat: count people around --- .../v1/operations/radar/people-count.get.js | 46 +++++++++++++++++++ .../radar/people-count.get.spec.yaml | 35 ++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 services/api/src/api/v1/operations/radar/people-count.get.js create mode 100644 services/api/src/api/v1/operations/radar/people-count.get.spec.yaml diff --git a/services/api/src/api/v1/operations/radar/people-count.get.js b/services/api/src/api/v1/operations/radar/people-count.get.js new file mode 100644 index 0000000..83168a5 --- /dev/null +++ b/services/api/src/api/v1/operations/radar/people-count.get.js @@ -0,0 +1,46 @@ +const { ctx } = require("@modjo/core") +const { reqCtx } = require("@modjo/express/ctx") + +const RADAR_DISTANCE_KM = 25000 // 25km in meters + +module.exports = function () { + const redis = ctx.require("redisHotGeodata") + + async function doRadarPeopleCount(_req) { + const { deviceId } = reqCtx.get("session") + + if (!deviceId) { + throw new Error("No device found in session") + } + + // Get current device location from Redis + const devicePosition = await redis.geopos("device", deviceId) + + if ( + !devicePosition || + !devicePosition[0] || + devicePosition[0].length !== 2 + ) { + throw new Error("Device location not available in Redis") + } + + const [longitude, latitude] = devicePosition[0] + + // Use Redis GEORADIUS to get all devices within 25km + const devicesData = await redis.georadius( + "device", + longitude, + latitude, + RADAR_DISTANCE_KM, + "m", + "WITHDIST" + ) + const count = devicesData.filter( + ([id]) => parseInt(id, 10) !== deviceId + ).length + + return { count } + } + + return [doRadarPeopleCount] +} diff --git a/services/api/src/api/v1/operations/radar/people-count.get.spec.yaml b/services/api/src/api/v1/operations/radar/people-count.get.spec.yaml new file mode 100644 index 0000000..9e7c6cb --- /dev/null +++ b/services/api/src/api/v1/operations/radar/people-count.get.spec.yaml @@ -0,0 +1,35 @@ +# description: Get count of people within 25km radius of authenticated user's device location +x-security: + - auth: ["user"] +responses: + 200: + # description: People count within 25km radius + content: + application/json: + schema: + type: object + properties: + count: + type: integer + minimum: 0 + description: Number of people within 25km radius + required: + - count + 400: + # description: Invalid coordinates provided + content: + application/json: + schema: + type: object + properties: + error: + type: string + 500: + # description: Internal server error + content: + application/json: + schema: + type: object + properties: + error: + type: string \ No newline at end of file