Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ RUN npm install
EXPOSE 3000

# Command to run backend server (adjust as per your backend setup)
CMD ["node", "fetchCrimeData.js"]
CMD ["node","--env-file=.env", "fetchCrimeData.js"]
91 changes: 52 additions & 39 deletions fetchCrimeData.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,50 +135,67 @@ app.post("/closestCrimeData", async (req, res) => {

const closestCrimeDataList = [];

const crimeDataCursor = db.collection("boulderCrimeData").find();
const cacheKey = objectHash.sha1([req.latitude, req.longitude]);
console.log("Cached key" + cacheKey);
cachedValue = await readData(cacheKey);
console.log("Cached value found: " + cachedValue);
if (cachedValue) {
// No need to call DB as we have values in cache and can skip it.
const responseStatus = new ResponseStatus(200, "OK", "Success");
res.json(new Response(closestCrimeDataList, responseStatus));
} else {
const crimeDataCursor = db.collection("boulderCrimeData").find();

const allCrimeData = await crimeDataCursor.toArray();
const allCrimeData = await crimeDataCursor.toArray();

const routeProcessingPromises = routeList.map(async (route) => {
const closestCrimeData = [];
const routeProcessingPromises = routeList.map(async (route) => {
const closestCrimeData = [];

for (const [targetLat, targetLong] of route) {
let closestCrime = null;
let minDistance = Infinity;
for (const [targetLat, targetLong] of route) {
let closestCrime = null;
let minDistance = Infinity;

for (const crime of allCrimeData) {
const distance = calculateDistance(
targetLat,
targetLong,
crime.latitude,
crime.longitude,
);
if (distance < minDistance) {
minDistance = distance;
closestCrime = crime;
for (const crime of allCrimeData) {
const distance = calculateDistance(
targetLat,
targetLong,
crime.latitude,
crime.longitude,
);
if (distance < minDistance) {
minDistance = distance;
closestCrime = crime;
}
}
}

if (closestCrime) {
closestCrimeData.push({
latitude: targetLat,
longitude: targetLong,
crimeData: closestCrime.crimeData,
});
} else {
closestCrimeData.push({
message: "No crime data found near this location.",
});
if (closestCrime) {
closestCrimeData.push({
latitude: targetLat,
longitude: targetLong,
crimeData: closestCrime.crimeData,
});
} else {
closestCrimeData.push({
message: "No crime data found near this location.",
});
}
}
}
closestCrimeDataList.push(closestCrimeData);
});
closestCrimeDataList.push(closestCrimeData);
});

await Promise.all(routeProcessingPromises);
await client.close();
await Promise.all(routeProcessingPromises);
await client.close();

const responseStatus = new ResponseStatus(200, "OK", "Success");
res.json(new Response(closestCrimeDataList, responseStatus));
console.log(
"Saving in cache" +
cacheKey +
", value= " +
crimeDataResponse.data.crimes,
);
writeData(cacheKey, crimeDataResponse.data.crimes);
const responseStatus = new ResponseStatus(200, "OK", "Success");
res.json(new Response(closestCrimeDataList, responseStatus));
}
} catch (err) {
console.error(err);
ResponseUtils.setResponseError(res, 500, "Internal Server Error");
Expand Down Expand Up @@ -243,10 +260,6 @@ app.post("/closestCrimeData1", async (req, res) => {
}
});

// app.listen(port, () => {
// console.log(`Server is running on http://localhost:${port}`);
// });

async function initializeExpressServer() {
//initialize an Express application
app.use(express.json());
Expand Down