-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Task #218152 Created the cron job and function to calculate the distnace between camp and attendance location #1152
base: release-2.8.4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,135 @@ | ||||||
import { Injectable } from '@nestjs/common'; | ||||||
import { Cron } from '@nestjs/schedule'; | ||||||
import { HasuraService } from 'src/services/hasura/hasura.service'; | ||||||
|
||||||
@Injectable() | ||||||
export class DistanceCalculation { | ||||||
constructor(private hasuraServiceFromService: HasuraService) {} | ||||||
private async haversineFormula( | ||||||
campLat: any, | ||||||
campLong: any, | ||||||
attendanceLat: any, | ||||||
attendanceLong: any, | ||||||
) { | ||||||
const sql = `SELECT CASE WHEN COALESCE(NULLIF('${campLat}', ''), 'null') = 'null' | ||||||
OR COALESCE(NULLIF('${attendanceLat}', ''), 'null') = 'null' | ||||||
OR COALESCE(NULLIF('${campLong}', ''), 'null') = 'null' | ||||||
OR COALESCE(NULLIF('${attendanceLong}', ''), 'null') = 'null' THEN | ||||||
0::numeric | ||||||
ELSE round( cast( 6371 * 2 * asin( sqrt( power(sin(radians(COALESCE(NULLIF('${campLat}', '0')::DOUBLE PRECISION, 0) - COALESCE(NULLIF('${attendanceLat}', '0')::DOUBLE PRECISION, 0)) / 2), 2) + cos(radians(COALESCE(NULLIF('${campLat}', '0')::DOUBLE PRECISION, 0))) * cos(radians(COALESCE(NULLIF('${campLat}', '0')::DOUBLE PRECISION, 0))) * power(sin(radians(COALESCE(NULLIF('${campLong}', '0')::DOUBLE PRECISION, 0) - COALESCE(NULLIF('${attendanceLong}', '0')::DOUBLE PRECISION, 0)) / 2), 2) ) ) AS numeric ), 2 ) | ||||||
Comment on lines
+14
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure SQL queries are safe from injection vulnerabilities. The current implementation of the SQL query in |
||||||
END`; | ||||||
try { | ||||||
const response = ( | ||||||
await this.hasuraServiceFromService.executeRawSql(sql) | ||||||
).result; | ||||||
const formattedData = | ||||||
this.hasuraServiceFromService.getFormattedData(response); | ||||||
return formattedData[0]?.round; | ||||||
} catch { | ||||||
return []; | ||||||
} | ||||||
} | ||||||
private async fetchData(limit: number) { | ||||||
const gqlQuery = { | ||||||
query: `query MyQuery { | ||||||
result : camp_days_activities_tracker(where: {attendances: {context: {_eq: "camp_days_activities_tracker"}, camp_to_attendance_location_distance: {_is_null: true}}}, limit: ${limit}){ | ||||||
attendances{ | ||||||
id | ||||||
user_id | ||||||
lat | ||||||
long | ||||||
} | ||||||
camp{ | ||||||
properties{ | ||||||
id | ||||||
lat | ||||||
long | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
`, | ||||||
}; | ||||||
|
||||||
try { | ||||||
const result = await this.hasuraServiceFromService.getData( | ||||||
gqlQuery, | ||||||
); | ||||||
|
||||||
const data = result?.data?.result; | ||||||
|
||||||
if (data) { | ||||||
return data; | ||||||
} else { | ||||||
return []; | ||||||
} | ||||||
} catch (error) { | ||||||
return []; | ||||||
} | ||||||
} | ||||||
|
||||||
//cron runs for each hour's 30th minute | ||||||
@Cron('30 * * * *') | ||||||
private async processData() { | ||||||
const user = await this.fetchData(10); | ||||||
|
||||||
try { | ||||||
for (const userData of user) { | ||||||
const campData = userData.camp?.properties; | ||||||
const attendanceData = userData?.attendances?.[0]; | ||||||
const attendanceId = attendanceData.id; | ||||||
console.log('ATTENDANCEDATA ', attendanceData); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep console log only if neccessary otherwise remove it |
||||||
console.log('CAMPDATA ', campData); | ||||||
|
||||||
if (!campData || !attendanceData) { | ||||||
console.log('SKIP - Missing campData or attendanceData'); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here instead of console log is return statement expected? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we are skippin the records so we cannot return anything there |
||||||
continue; | ||||||
} | ||||||
const campLat = parseFloat(campData.lat); | ||||||
const campLong = parseFloat(campData.long); | ||||||
const attendanceLat = parseFloat(attendanceData.lat); | ||||||
const attendanceLong = parseFloat(attendanceData.long); | ||||||
if ( | ||||||
isNaN(campLat) || | ||||||
isNaN(campLong) || | ||||||
isNaN(attendanceLat) || | ||||||
isNaN(attendanceLong) | ||||||
) { | ||||||
console.log('SKIP - Invalid coordinates'); | ||||||
continue; | ||||||
} | ||||||
|
||||||
const calculatedDistance = await this.haversineFormula( | ||||||
campLat, | ||||||
campLong, | ||||||
attendanceLat, | ||||||
attendanceLong, | ||||||
); | ||||||
console.log( | ||||||
'Distance between attendance and camp location ', | ||||||
calculatedDistance, | ||||||
); | ||||||
const updateQuery = { | ||||||
query: `mutation MyMutation { | ||||||
update_attendance(where: {id: {_eq: ${attendanceId}}}, _set: {camp_to_attendance_location_distance: ${calculatedDistance}}) { | ||||||
returning { | ||||||
id | ||||||
camp_to_attendance_location_distance | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
`, | ||||||
}; | ||||||
|
||||||
const updatedResult = | ||||||
await this.hasuraServiceFromService.getData(updateQuery); | ||||||
console.log(JSON.stringify(updatedResult)); | ||||||
} | ||||||
} catch (error) { | ||||||
console.log('Error occurred while updating ', error); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enhance error handling by logging more detailed information. When logging errors, it's beneficial to log more detailed information about the error to aid in debugging. Consider logging the error message along with the stack trace. - console.log('Error occurred while updating ', error);
+ console.error('Error occurred while updating: ', error.message, error.stack); Committable suggestion
Suggested change
|
||||||
return []; | ||||||
} | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using more specific types for latitude and longitude parameters.
Using
any
type for latitude and longitude parameters reduces type safety. It's better to usenumber
type for these parameters to ensure that the values being passed are of the expected type.Committable suggestion