Skip to content

Commit

Permalink
fix: add getPeriodsDurationByType util and test
Browse files Browse the repository at this point in the history
  • Loading branch information
BRaimbault committed Dec 10, 2024
1 parent 245848a commit e744748
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/constants/periods.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const LAST_YEAR = 'LAST_YEAR'

export const PREDEFINED_PERIODS = 'PREDEFINED_PERIODS'
export const RELATIVE_PERIODS = 'RELATIVE_PERIODS'
export const FIXED_PERIODS = 'FIXED_PERIODS'
export const START_END_DATES = 'START_END_DATES'

export const periodTypes = (includeRelativePeriods) => [
Expand Down
34 changes: 33 additions & 1 deletion src/util/__tests__/periods.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { getFixedPeriodsByType } from '../periods.js'
import { getFixedPeriodsByType, getPeriodsDurationByType } from '../periods.js'

const periods = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }]

const periodsDetails = {
1: { type: 'TYPE_A', offset: -1, duration: 3 },
2: { type: 'TYPE_B', offset: -1, duration: 2 },
3: { type: 'TYPE_A', offset: 0, duration: 4 },
}

describe('util/periods', () => {
test('getFixedPeriodsByType - RELATIVE', () => {
Expand Down Expand Up @@ -316,6 +324,7 @@ describe('util/periods', () => {
},
])
})

test('getFixedPeriodsByType - MONTHLY 2020', () => {
expect(
getFixedPeriodsByType({
Expand Down Expand Up @@ -437,4 +446,27 @@ describe('util/periods', () => {
},
])
})

it('getPeriodsDurationByType - should correctly calculate the duration by type with deduplication', () => {
const result = getPeriodsDurationByType(periods, periodsDetails)
expect(result).toEqual({
FIXED_PERIODS: {
any: 1,
first: 0,
last: 0,
},
TYPE_A: { first: 1, last: 3 },
TYPE_B: { first: 0, last: 2 },
})
})

it('getPeriodsDurationByType - should correctly calculate the duration by type without deduplication', () => {
const result = getPeriodsDurationByType(periods, periodsDetails, false)
expect(result).toEqual({
1: { any: 3 },
2: { any: 2 },
3: { any: 4 },
4: { any: 1 },
})
})
})
55 changes: 54 additions & 1 deletion src/util/periods.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import {
getFixedPeriodsOptionsById,
getRelativePeriodsOptionsById,
getRelativePeriodsDetails,
} from '@dhis2/analytics'
import { periodTypes, periodGroups } from '../constants/periods.js'
import {
periodTypes,
periodGroups,
FIXED_PERIODS,
} from '../constants/periods.js'
import { sumObjectValues } from '../util/helpers.js'

const getYearOffsetFromNow = (year) => year - new Date(Date.now()).getFullYear()

Expand Down Expand Up @@ -80,3 +86,50 @@ export const getHiddenPeriods = (systemSettings) => {
)
.map((setting) => setting.match(periodSetting)[1].toUpperCase())
}

// Count maximum number of periods returned by analytics api

export const getPeriodsDurationByType = (
periods,
periodsDetails,
deduplication = true
) => {
if (!deduplication) {
return periods.reduce((acc, { id }) => {
acc[id] = acc[id] || {}
acc[id].any = periodsDetails[id]?.duration || 1
return acc
}, {})
} else {
return periods.reduce((acc, { id }) => {
const {
type = FIXED_PERIODS,
offset = 0,
duration = 1,
} = periodsDetails[id] || {}

acc[type] = acc[type] || { first: 0, last: 0 }

if (type === FIXED_PERIODS && !periodsDetails[id]) {
acc[type].any = (acc[type].any || 0) + 1
} else {
acc[type].first = Math.max(acc[type].first, 1 + offset)
acc[type].last = Math.max(
acc[type].last,
duration - (1 + offset)
)
}
return acc
}, {})
}
}

export const countPeriods = (periods, deduplication) => {
const periodsDetails = getRelativePeriodsDetails()
const periodsDurationByType = getPeriodsDurationByType(
periods,
periodsDetails,
deduplication
)
return sumObjectValues(periodsDurationByType)
}

0 comments on commit e744748

Please sign in to comment.