Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Temp-Fix #103 findAround limit for performance (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
celian-rib authored Jul 21, 2022
1 parent 1576e74 commit 7b4f643
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 65 deletions.
52 changes: 3 additions & 49 deletions src/services/dropy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { DropyAround } from '@/interfaces/dropy.interface';
import { ChatConversation, Dropy, MediaType, User } from '@prisma/client';
import { UploadedFile } from 'express-fileupload';
import { sendPushNotification } from '@/notification';

const DISTANCE_FILTER_RADIUS = 0.004; // Environ 300m
import { getAvailableDropiesAroundLocation } from '@/utils/geolocation.utils';

export async function createDropy(user: User, latitude, longitude): Promise<Dropy> {
const dropy = client.dropy.create({ data: { emitterId: user.id, latitude, longitude } });
Expand Down Expand Up @@ -119,45 +118,6 @@ export async function getDropyById(dropyId: number): Promise<Dropy> {
return dropy;
}

export async function getAvailableDropiesAroundLocation(latitude: number, longitude: number, user: User = undefined): Promise<Dropy[]> {
const andQuery: Object[] = [
{
latitude: {
gt: latitude - DISTANCE_FILTER_RADIUS,
lt: latitude + DISTANCE_FILTER_RADIUS,
},
},
{
longitude: {
gt: longitude - DISTANCE_FILTER_RADIUS,
lt: longitude + DISTANCE_FILTER_RADIUS,
},
},
{
mediaType: {
not: MediaType.NONE,
},
},
{
retrieverId: {
equals: null,
},
},
];

if (user != undefined) {
andQuery.push({
emitterId: {
not: user.id,
},
});
}

return await client.dropy.findMany({
where: { AND: andQuery },
});
}

export async function getDropy(dropyId: number) {
const dropy = await client.dropy.findUnique({
where: { id: dropyId },
Expand All @@ -182,14 +142,8 @@ export async function getDropy(dropyId: number) {
return customDropy;
}

export async function findDropiesAround(): Promise<DropyAround[]> {
const dropies = await client.dropy.findMany({
where: {
retrieverId: {
equals: null,
},
},
});
export async function findDropiesAround(latitude: number, longitude: number): Promise<DropyAround[]> {
const dropies = await getAvailableDropiesAroundLocation(latitude, longitude);

const dropiesAround = dropies.map(dropy => {
return {
Expand Down
3 changes: 1 addition & 2 deletions src/services/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { User } from '@prisma/client';
import { getAvailableDropiesAroundLocation } from '@/services/dropy.service';
import { sendPushNotification } from '../notification';
import client from '@/prisma/client';
import { getDistanceFromLatLonInMeters } from '@/utils/notification.utils';
import { getAvailableDropiesAroundLocation, getDistanceFromLatLonInMeters } from '@/utils/geolocation.utils';

const DISTANCE_FILTER_RADIUS = 50;
const TIME_FILTER_MINUTES = 60 * 24;
Expand Down
4 changes: 2 additions & 2 deletions src/sockets/dropy.socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { SocketCallback } from '@/interfaces/socket.interface';

import * as dropyService from '@services/dropy.service';

export async function emitAllDropiesAround(callback: SocketCallback<DropyAround[]>) {
const dropies = await dropyService.findDropiesAround();
export async function emitAllDropiesAround(latitude: number, longitude: number, callback: SocketCallback<DropyAround[]>) {
const dropies = await dropyService.findDropiesAround(latitude, longitude);
callback({
status: 200,
data: dropies,
Expand Down
6 changes: 4 additions & 2 deletions src/sockets/listeners/dropy.socket.listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ export function startSocket() {

logger.log('Connected');

socket.on('all_dropies_around', async callback => {
socket.on('all_dropies_around', async (data: any, callback) => {
try {
const { latitude, longitude } = data;
throwIfNotNumber(latitude, longitude);
throwIfNotFunction(callback);

await dropySocket.emitAllDropiesAround(callback);
await dropySocket.emitAllDropiesAround(latitude, longitude, callback);
logger.log('All dropies around');
} catch (error) {
handleSocketRawError(callback, error);
Expand Down
54 changes: 54 additions & 0 deletions src/utils/geolocation.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Dropy, MediaType, User } from '@prisma/client';
import client from '@/prisma/client';

const DISTANCE_FILTER_RADIUS = 0.003; // Environ 300m

export function getDistanceFromLatLonInMeters(lat1, lon1, lat2, lon2) {
const deg2rad = deg => deg * (Math.PI / 180);
const R = 6371; // Radius of the earth in km
const dLat = deg2rad(lat2 - lat1); // deg2rad below
const dLon = deg2rad(lon2 - lon1);
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const d = R * c; // Distance in km
return d * 1000; // Distance in meters
}

export async function getAvailableDropiesAroundLocation(latitude: number, longitude: number, user: User = undefined): Promise<Dropy[]> {
const andQuery: Object[] = [
{
latitude: {
gt: latitude - DISTANCE_FILTER_RADIUS,
lt: latitude + DISTANCE_FILTER_RADIUS,
},
},
{
longitude: {
gt: longitude - DISTANCE_FILTER_RADIUS,
lt: longitude + DISTANCE_FILTER_RADIUS,
},
},
{
mediaType: {
not: MediaType.NONE,
},
},
{
retrieverId: {
equals: null,
},
},
];

if (user != undefined) {
andQuery.push({
emitterId: {
not: user.id,
},
});
}

return await client.dropy.findMany({
where: { AND: andQuery },
});
}
10 changes: 0 additions & 10 deletions src/utils/notification.utils.ts

This file was deleted.

0 comments on commit 7b4f643

Please sign in to comment.