feat: count people around
This commit is contained in:
parent
74e1b5f10b
commit
1f449e9f0c
2 changed files with 81 additions and 0 deletions
46
services/api/src/api/v1/operations/radar/people-count.get.js
Normal file
46
services/api/src/api/v1/operations/radar/people-count.get.js
Normal file
|
@ -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]
|
||||
}
|
|
@ -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
|
Loading…
Add table
Reference in a new issue