Skip to content

Commit

Permalink
group by month and fill in missing months
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Mar 20, 2024
1 parent d2a7278 commit 97ba366
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
54 changes: 54 additions & 0 deletions api/src/modules/eudr-alerts/dashboard/dashboard-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,60 @@ export const groupAlertsByDate = (
}));
};

export const groupAndFillAlertsByMonth = (
alerts: AlertsOutput[],
geoRegionMap: Map<string, any>,
startDate: Date,
endDate: Date,
): any[] => {
const alertsByMonth: any = alerts.reduce((acc: any, cur: AlertsOutput) => {
const date = new Date(cur.alertDate.value);
const yearMonthKey = `${date.getFullYear()}-${(date.getMonth() + 1)
.toString()
.padStart(2, '0')}`;

if (!acc[yearMonthKey]) {
acc[yearMonthKey] = [];
}

acc[yearMonthKey].push({
plotName: geoRegionMap.get(cur.geoRegionId)?.plotName,
geoRegionId: cur.geoRegionId,
alertCount: cur.alertCount,
});

return acc;
}, {});

const start = new Date(startDate);
const end = new Date(endDate);
const filledMonths: any[] = [];
const existingMonths = new Set(Object.keys(alertsByMonth));

for (
let month = new Date(start);
month <= end;
month.setMonth(month.getMonth() + 1)
) {
const yearMonthKey = `${month.getFullYear()}-${(month.getMonth() + 1)
.toString()
.padStart(2, '0')}`;
if (!existingMonths.has(yearMonthKey)) {
filledMonths.push({
alertDate: yearMonthKey,
plots: [{ geoRegionId: null, plotName: null, alertCount: 0 }],
});
} else {
filledMonths.push({
alertDate: yearMonthKey,
plots: alertsByMonth[yearMonthKey],
});
}
}

return filledMonths.sort((a, b) => a.alertDate.localeCompare(b.alertDate));
};

export const findNonAlertedGeoRegions = (
geoRegionMapBySupplier: Record<string, string[]>,
alertMap: any,
Expand Down
18 changes: 14 additions & 4 deletions api/src/modules/eudr-alerts/dashboard/eudr-dashboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import { EUDRDashBoardDetail } from 'modules/eudr-alerts/dashboard/dashboard-det
import { MaterialsService } from 'modules/materials/materials.service';
import { AdminRegionsService } from 'modules/admin-regions/admin-regions.service';
import {
groupAlertsByDate,
aggregateAndCalculatePercentage,
findNonAlertedGeoRegions,
groupAndFillAlertsByMonth,
} from './dashboard-utils';

@Injectable()
Expand Down Expand Up @@ -607,12 +607,22 @@ export class EudrDashboardService {
alertsOutput[alertsOutput.length - 1]?.alertDate?.value.toString() ||
null;

const finalStartDate =
dto?.startAlertDate ?? (startAlertDate as unknown as Date);
const finalEndDate =
dto?.endAlertDate ?? (endAlertDate as unknown as Date);

const alerts = {
startAlertDate: startAlertDate,
endAlertDate: endAlertDate,
startAlertDate: dto?.startAlertDate ?? startAlertDate,
endAlertDate: dto?.endAlertDate ?? endAlertDate,
totalAlerts,
totalCarbonRemovals,
values: groupAlertsByDate(alertsOutput, geoRegionMap),
values: groupAndFillAlertsByMonth(
alertsOutput,
geoRegionMap,
finalStartDate,
finalEndDate,
),
};

const nonAlertedGeoRegions: string[] = allGeoRegionsBySupplier.filter(
Expand Down

0 comments on commit 97ba366

Please sign in to comment.