-
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.
calculate volume percentage by year, fix total plot area sum
- Loading branch information
Showing
3 changed files
with
108 additions
and
67 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { AlertsOutput } from '../dto/alerts-output.dto'; | ||
|
||
interface VolumeAndPlotByYear { | ||
year: number; | ||
volume: string; | ||
plotName?: string; | ||
geoRegionId?: string | null; | ||
} | ||
|
||
export interface AggregatedVoumeAndPlotByYear extends VolumeAndPlotByYear { | ||
percentage?: number; | ||
} | ||
|
||
export const aggregateAndCalculatePercentage = ( | ||
records: any[], | ||
): AggregatedVoumeAndPlotByYear[] => { | ||
const withGeoRegion: VolumeAndPlotByYear[] = records.filter( | ||
(record: VolumeAndPlotByYear) => record.geoRegionId !== null, | ||
); | ||
|
||
// Group and aggregate records for unknown GeoRegions | ||
const withoutGeoRegion: VolumeAndPlotByYear[] = records | ||
.filter((record: VolumeAndPlotByYear) => record.geoRegionId === null) | ||
.reduce<VolumeAndPlotByYear[]>( | ||
(acc: VolumeAndPlotByYear[], { year, volume }) => { | ||
const existingYearRecord: VolumeAndPlotByYear | undefined = acc.find( | ||
(record: VolumeAndPlotByYear) => record.year === year, | ||
); | ||
if (existingYearRecord) { | ||
existingYearRecord.volume = ( | ||
parseFloat(existingYearRecord.volume) + parseFloat(volume) | ||
).toString(); | ||
} else { | ||
acc.push({ year, volume, plotName: 'Unknown', geoRegionId: null }); | ||
} | ||
return acc; | ||
}, | ||
[], | ||
); | ||
|
||
// Merge records with known and unknown GeoRegions | ||
const combinedRecords: VolumeAndPlotByYear[] = [ | ||
...withGeoRegion, | ||
...withoutGeoRegion, | ||
]; | ||
|
||
// Calculate total volume per year | ||
const yearTotals: { [key: number]: number } = combinedRecords.reduce<{ | ||
[key: number]: number; | ||
}>((acc: { [p: number]: number }, { year, volume }) => { | ||
acc[year] = (acc[year] || 0) + parseFloat(volume); | ||
return acc; | ||
}, {}); | ||
|
||
return combinedRecords.map((record: VolumeAndPlotByYear) => ({ | ||
...record, | ||
percentage: (parseFloat(record.volume) / yearTotals[record.year]) * 100, | ||
})); | ||
}; | ||
|
||
export const groupAlertsByDate = ( | ||
alerts: AlertsOutput[], | ||
geoRegionMap: Map<string, any>, | ||
): any[] => { | ||
const alertsByDate: any = alerts.reduce((acc: any, cur: AlertsOutput) => { | ||
const date: string = cur.alertDate.value.toString(); | ||
if (!acc[date]) { | ||
acc[date] = []; | ||
} | ||
acc[date].push({ | ||
plotName: geoRegionMap.get(cur.geoRegionId)?.plotName, | ||
geoRegionId: cur.geoRegionId, | ||
alertCount: cur.alertCount, | ||
}); | ||
return acc; | ||
}, {}); | ||
return Object.keys(alertsByDate).map((key) => ({ | ||
alertDate: key, | ||
plots: alertsByDate[key], | ||
})); | ||
}; |
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