Skip to content

Commit

Permalink
add dfs * sda georegion ids for dashboard detail
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Mar 19, 2024
1 parent 2a48c9a commit 59bb2f0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { AffectedPlots } from './dashboard.types';

export class EUDRDashBoardDetail {
@ApiProperty()
Expand All @@ -11,6 +12,9 @@ export class EUDRDashBoardDetail {
type: () => DashBoardDetailSourcingInformation,
isArray: true,
})
@ApiProperty({ type: () => AffectedPlots })
plots: AffectedPlots;
@ApiProperty({ type: () => DashBoardDetailCountry, isArray: true })
sourcingInformation: DashBoardDetailSourcingInformation[];
@ApiProperty({ type: () => DashBoardDetailAlerts, isArray: true })
alerts: DashBoardDetailAlerts[];
Expand Down
2 changes: 1 addition & 1 deletion api/src/modules/eudr-alerts/dashboard/dashboard.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';

class AffectedPlots {
export class AffectedPlots {
@ApiProperty()
dfs: string[];
@ApiProperty()
Expand Down
16 changes: 16 additions & 0 deletions api/src/modules/eudr-alerts/dashboard/eudr-dashboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ export class EudrDashboardService {
const sourcingInformation: any = {};
let supplier: Supplier;
const geoRegionMap: Map<string, { plotName: string }> = new Map();
const allGeoRegionsBySupplier: string[] = [];

return this.datasource.transaction(async (manager: EntityManager) => {
supplier = await manager
Expand Down Expand Up @@ -514,7 +515,11 @@ export class EudrDashboardService {
plotName: string;
geoRegionId: string;
}[] = [];

for (const geoRegion of geoRegions) {
if (geoRegion.geoRegionId) {
allGeoRegionsBySupplier.push(geoRegion.geoRegionId);
}
geoRegion.geoRegionId = geoRegion.geoRegionId ?? null;
geoRegion.plotName = geoRegion.plotName ?? 'Unknown';
if (!geoRegionMap.get(geoRegion.geoRegionId)) {
Expand Down Expand Up @@ -583,13 +588,16 @@ export class EudrDashboardService {
endAlertDate: dto?.endAlertDate,
});

const affectedGeoRegionIds: Set<string> = new Set<string>();
const { totalAlerts, totalCarbonRemovals } = alertsOutput.reduce(
(
acc: { totalAlerts: number; totalCarbonRemovals: number },
cur: AlertsOutput,
) => {
acc.totalAlerts += cur.alertCount;
acc.totalCarbonRemovals += cur.carbonRemovals;
console.log(cur.geoRegionId);
affectedGeoRegionIds.add(cur.geoRegionId);
return acc;
},
{ totalAlerts: 0, totalCarbonRemovals: 0 },
Expand All @@ -608,6 +616,14 @@ export class EudrDashboardService {
values: groupAlertsByDate(alertsOutput, geoRegionMap),
};

const nonAlertedGeoRegions: string[] = allGeoRegionsBySupplier.filter(
(id: string) => ![...affectedGeoRegionIds].includes(id),
);
result.plots = {
dfs: nonAlertedGeoRegions,
sda: [...affectedGeoRegionIds],
};

result.alerts = alerts;

return result;
Expand Down

0 comments on commit 59bb2f0

Please sign in to comment.