-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Endpoint to retrieve filtered EUDR Alerts
- Loading branch information
Showing
6 changed files
with
195 additions
and
57 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
api/src/modules/eudr-alerts/alerts-query-builder/big-query-alerts-query.builder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { SelectQueryBuilder } from 'typeorm'; | ||
import { AlertsOutput } from '../dto/alerts-output.dto'; | ||
import { GetEUDRALertsDto } from '../dto/get-alerts.dto'; | ||
import { Query } from '@google-cloud/bigquery'; | ||
|
||
export class BigQueryAlertsQueryBuilder { | ||
queryBuilder: SelectQueryBuilder<AlertsOutput>; | ||
dto?: GetEUDRALertsDto; | ||
|
||
constructor( | ||
queryBuilder: SelectQueryBuilder<AlertsOutput>, | ||
getAlertsDto?: GetEUDRALertsDto, | ||
) { | ||
this.queryBuilder = queryBuilder; | ||
this.dto = getAlertsDto; | ||
} | ||
|
||
buildQuery(): Query { | ||
if (this.dto?.supplierIds) { | ||
this.queryBuilder.andWhere('supplierid IN (:...supplierIds)', { | ||
supplierIds: this.dto.supplierIds, | ||
}); | ||
} | ||
if (this.dto?.geoRegionIds) { | ||
this.queryBuilder.andWhere('georegionid IN (:...geoRegionIds)', { | ||
geoRegionIds: this.dto.geoRegionIds, | ||
}); | ||
} | ||
if (this.dto?.alertConfidence) { | ||
this.queryBuilder.andWhere('alertConfidence = :alertConfidence', { | ||
alertConfidence: this.dto.alertConfidence, | ||
}); | ||
} | ||
|
||
if (this.dto?.startYear && this.dto?.endYear) { | ||
this.addYearRange(); | ||
} else if (this.dto?.startYear) { | ||
this.addYearGreaterThanOrEqual(); | ||
} else if (this.dto?.endYear) { | ||
this.addYearLessThanOrEqual(); | ||
} | ||
|
||
if (this.dto?.startAlertDate && this.dto?.endAlertDate) { | ||
this.addAlertDateRange(); | ||
} else if (this.dto?.startAlertDate) { | ||
this.addAlertDateGreaterThanOrEqual(); | ||
} else if (this.dto?.endAlertDate) { | ||
this.addAlertDateLessThanOrEqual(); | ||
} | ||
|
||
this.queryBuilder.limit(this.dto?.limit); | ||
|
||
const [query, params] = this.queryBuilder.getQueryAndParameters(); | ||
|
||
return this.parseToBigQuery(query, params); | ||
} | ||
|
||
addYearRange(): void { | ||
this.queryBuilder.andWhere('year BETWEEN :startYear AND :endYear', { | ||
startYear: this.dto?.startYear, | ||
endYear: this.dto?.endYear, | ||
}); | ||
} | ||
|
||
addYearGreaterThanOrEqual(): void { | ||
this.queryBuilder.andWhere('year >= :startYear', { | ||
startYear: this.dto?.startYear, | ||
}); | ||
} | ||
|
||
addYearLessThanOrEqual(): void { | ||
this.queryBuilder.andWhere('year <= :endYear', { | ||
endYear: this.dto?.endYear, | ||
}); | ||
} | ||
|
||
addAlertDateRange(): void { | ||
this.queryBuilder.andWhere( | ||
'alertDate BETWEEN :startAlertDate AND :endAlertDate', | ||
{ | ||
startAlertDate: this.dto?.startAlertDate, | ||
endAlertDate: this.dto?.endAlertDate, | ||
}, | ||
); | ||
} | ||
|
||
addAlertDateGreaterThanOrEqual(): void { | ||
this.queryBuilder.andWhere('alertDate >= :startAlertDate', { | ||
startAlertDate: this.dto?.startAlertDate, | ||
}); | ||
} | ||
|
||
addAlertDateLessThanOrEqual(): void { | ||
this.queryBuilder.andWhere('alertDate <= :endAlertDate', { | ||
endAlertDate: this.dto?.endAlertDate, | ||
}); | ||
} | ||
|
||
parseToBigQuery(query: string, params: any[]): Query { | ||
return { | ||
query: this.removeDoubleQuotesAndReplacePositionalArguments(query), | ||
params, | ||
}; | ||
} | ||
|
||
/** | ||
* @description: BigQuery does not allow double quotes and the positional argument symbol must be a "?". | ||
* So there is a need to replace the way TypeORM handles the positional arguments, with $1, $2, etc. | ||
*/ | ||
|
||
private removeDoubleQuotesAndReplacePositionalArguments( | ||
query: string, | ||
): string { | ||
return query.replace(/\$\d+|"/g, (match: string) => | ||
match === '"' ? '' : '?', | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
import { GeoJSON } from 'geojson'; | ||
|
||
export class AlertsOutput { | ||
geoRegionId: string; | ||
supplierId: string; | ||
export type AlertsOutput = { | ||
alertCount: boolean; | ||
geometry: GeoJSON; | ||
date: Date; | ||
year: number; | ||
alertConfidence: 'low' | 'medium' | 'high' | 'very high'; | ||
} | ||
}; | ||
|
||
export type AlertGeometry = { | ||
geometry: { value: string }; | ||
}; | ||
|
||
export type AlertsWithGeom = AlertsOutput & AlertGeometry; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,43 @@ | ||
import { | ||
IsArray, | ||
IsDate, | ||
IsEnum, | ||
IsInt, | ||
IsNumber, | ||
IsOptional, | ||
IsUUID, | ||
} from 'class-validator'; | ||
|
||
export class GetEUDRALertsDto { | ||
supplierId: string; | ||
geoRegionId: string; | ||
geom: any; | ||
year: number; | ||
@IsOptional() | ||
@IsArray() | ||
@IsUUID('4', { each: true }) | ||
supplierIds: string[]; | ||
|
||
@IsOptional() | ||
@IsArray() | ||
@IsUUID('4', { each: true }) | ||
geoRegionIds: string[]; | ||
|
||
@IsOptional() | ||
@IsNumber() | ||
startYear: number; | ||
|
||
@IsOptional() | ||
@IsNumber() | ||
endYear: number; | ||
|
||
alertConfidence: 'high' | 'medium' | 'low'; | ||
|
||
@IsOptional() | ||
@IsDate() | ||
startAlertDate: Date; | ||
|
||
@IsOptional() | ||
@IsDate() | ||
endAlertDate: Date; | ||
|
||
@IsOptional() | ||
@IsInt() | ||
limit: number = 1000; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters