Skip to content

Commit

Permalink
Improve calculation of yearly average for risk by month
Browse files Browse the repository at this point in the history
- Add test for risk utils
  • Loading branch information
frozenhelium authored and samshara committed Dec 5, 2023
1 parent abab99e commit 7f2d573
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/utils/domain/risk.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, test } from 'vitest';

import { CATEGORY_RISK_LOW, CATEGORY_RISK_VERY_LOW } from '#utils/constants';

import { riskScoreToCategory } from './risk.ts';

test('Risk score to category', () => {
expect(
riskScoreToCategory(
0,
'FL',
),
).toEqual(CATEGORY_RISK_VERY_LOW);
expect(
riskScoreToCategory(
3,
'FL',
),
).toEqual(CATEGORY_RISK_LOW);
});
46 changes: 42 additions & 4 deletions src/utils/domain/risk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export interface RiskDataItem {
annual_average?: number | null,
}

export const monthNumberToNameMap: Record<number, keyof RiskDataItem> = {
const monthToKeyMap: Record<number, keyof RiskDataItem> = {
0: 'january',
1: 'february',
2: 'march',
Expand All @@ -90,6 +90,10 @@ export const monthNumberToNameMap: Record<number, keyof RiskDataItem> = {
9: 'october',
10: 'november',
11: 'december',
};

export const monthNumberToNameMap: Record<number, keyof RiskDataItem> = {
...monthToKeyMap,
// FIXME: we should not have these different
// class of data into same list
12: 'annual_average',
Expand All @@ -100,8 +104,42 @@ export function getValueForSelectedMonths(
riskDataItem: RiskDataItem | undefined,
aggregationMode: 'sum' | 'max' = 'sum',
) {
if (isNotDefined(selectedMonths)) {
return riskDataItem?.annual_average ?? undefined;
let annualValue;

if (aggregationMode === 'sum') {
annualValue = sumSafe([
riskDataItem?.january,
riskDataItem?.february,
riskDataItem?.march,
riskDataItem?.april,
riskDataItem?.may,
riskDataItem?.june,
riskDataItem?.july,
riskDataItem?.august,
riskDataItem?.september,
riskDataItem?.october,
riskDataItem?.november,
riskDataItem?.december,
]);
} else if (aggregationMode === 'max') {
annualValue = maxSafe([
riskDataItem?.january,
riskDataItem?.february,
riskDataItem?.march,
riskDataItem?.april,
riskDataItem?.may,
riskDataItem?.june,
riskDataItem?.july,
riskDataItem?.august,
riskDataItem?.september,
riskDataItem?.october,
riskDataItem?.november,
riskDataItem?.december,
]);
}

if (isNotDefined(selectedMonths) || selectedMonths[12] === true) {
return riskDataItem?.annual_average ?? annualValue ?? undefined;
}

const monthKeys = Object.keys(
Expand Down Expand Up @@ -269,7 +307,7 @@ export function riskScoreToCategory(
score: number | undefined | null,
hazardType: HazardType,
) {
if (isNotDefined(score) || score <= 0) {
if (isNotDefined(score) || score < 0) {
return undefined;
}

Expand Down

0 comments on commit 7f2d573

Please sign in to comment.