Merge pull request #54 from KomelT/feature/location-history
Implement position history feature
This commit is contained in:
84
src/index.js
84
src/index.js
@ -433,6 +433,90 @@ app.get('/api/v1/nodes/:nodeId/traceroutes', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/v1/nodes/:nodeId/position-history', async (req, res) => {
|
||||
try {
|
||||
|
||||
// defaults
|
||||
const nowInMilliseconds = new Date().getTime();
|
||||
const oneHourAgoInMilliseconds = new Date().getTime() - (3600 * 1000);
|
||||
|
||||
// get request params
|
||||
const nodeId = parseInt(req.params.nodeId);
|
||||
const timeFrom = req.query.time_from ? parseInt(req.query.time_from) : oneHourAgoInMilliseconds;
|
||||
const timeTo = req.query.time_to ? parseInt(req.query.time_to) : nowInMilliseconds;
|
||||
|
||||
// find node
|
||||
const node = await prisma.node.findFirst({
|
||||
where: {
|
||||
node_id: nodeId,
|
||||
},
|
||||
});
|
||||
|
||||
// make sure node exists
|
||||
if(!node){
|
||||
res.status(404).json({
|
||||
message: "Not Found",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const positions = await prisma.position.findMany({
|
||||
where: {
|
||||
node_id: nodeId,
|
||||
created_at: {
|
||||
gte: new Date(timeFrom),
|
||||
lte: new Date(timeTo),
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
const mapReports = await prisma.mapReport.findMany({
|
||||
where: {
|
||||
node_id: nodeId,
|
||||
created_at: {
|
||||
gte: new Date(timeFrom),
|
||||
lte: new Date(timeTo),
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
const positionHistory = []
|
||||
|
||||
positions.forEach((position) => {
|
||||
positionHistory.push({
|
||||
node_id: position.node_id,
|
||||
latitude: position.latitude,
|
||||
longitude: position.longitude,
|
||||
altitude: position.altitude,
|
||||
created_at: position.created_at,
|
||||
});
|
||||
});
|
||||
|
||||
mapReports.forEach((mapReport) => {
|
||||
positionHistory.push({
|
||||
node_id: mapReport.node_id,
|
||||
latitude: mapReport.latitude,
|
||||
longitude: mapReport.longitude,
|
||||
altitude: mapReport.altitude,
|
||||
created_at: mapReport.created_at,
|
||||
});
|
||||
});
|
||||
|
||||
// sort oldest to newest
|
||||
positionHistory.sort((a, b) => a.created_at - b.created_at);
|
||||
|
||||
res.json({
|
||||
position_history: positionHistory,
|
||||
});
|
||||
|
||||
} catch(err) {
|
||||
console.error(err);
|
||||
res.status(500).json({
|
||||
message: "Something went wrong, try again later.",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/v1/stats/hardware-models', async (req, res) => {
|
||||
try {
|
||||
|
||||
|
Reference in New Issue
Block a user