Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions packages/analytics/src/Domain/Time/PeriodKeyGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,38 @@ describe('PeriodKeyGenerator', () => {
])
})

it('should generate January daily keys when the current month is different', () => {
jest.setSystemTime(new Date(2025, 4, 24, 12))

const keys = createGenerator().getDiscretePeriodKeys(Period.JanuaryThisYear)

expect(keys).toHaveLength(31)
expect(keys[0]).toBe('2025-1-1')
expect(keys[30]).toBe('2025-1-31')
})

it('should generate February keys on January 31 without rolling into March', () => {
jest.setSystemTime(new Date(2025, 0, 31, 12))

const generator = createGenerator()
const keys = generator.getDiscretePeriodKeys(Period.FebruaryThisYear)

expect(generator.getPeriodKey(Period.FebruaryThisYear)).toBe('2025-2')
expect(keys).toHaveLength(28)
expect(keys[0]).toBe('2025-2-1')
expect(keys[27]).toBe('2025-2-28')
})

it('should generate the Q2 monthly keys on May 31', () => {
jest.setSystemTime(new Date(2025, 4, 31, 12))

expect(createGenerator().getDiscretePeriodKeys(Period.Q2ThisYear)).toEqual([
'2025-4',
'2025-5',
'2025-6',
])
})

it('should generate period keys for specific months', () => {
for (const month of months) {
expect(createGenerator().getDiscretePeriodKeys(month).length >= 28).toBeTruthy()
Expand Down Expand Up @@ -295,4 +327,19 @@ describe('PeriodKeyGenerator', () => {
expect(createGenerator().convertPeriodKeyToPeriod('2022-11')).toEqual(Period.NovemberThisYear)
expect(createGenerator().convertPeriodKeyToPeriod('2022-12')).toEqual(Period.DecemberThisYear)
})

it('should convert a monthly period key independently of the local time zone', () => {
const originalTimeZone = process.env.TZ
process.env.TZ = 'America/Los_Angeles'

try {
expect(createGenerator().convertPeriodKeyToPeriod('2022-10')).toBe(Period.OctoberThisYear)
} finally {
if (originalTimeZone === undefined) {
delete process.env.TZ
} else {
process.env.TZ = originalTimeZone
}
}
})
})
23 changes: 8 additions & 15 deletions packages/analytics/src/Domain/Time/PeriodKeyGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ export class PeriodKeyGenerator implements PeriodKeyGeneratorInterface {
]

convertPeriodKeyToPeriod(periodKey: string): Period {
const date = new Date(periodKey)
const month = this.getMonth(date)
const month = Number(periodKey.split('-')[1])

return this.MONTHS[+month - 1]
return this.MONTHS[month - 1]
}

getDiscretePeriodKeys(period: Period): string[] {
Expand Down Expand Up @@ -208,30 +207,24 @@ export class PeriodKeyGenerator implements PeriodKeyGeneratorInterface {
}

private generateMonthlyKeysRange(startingMonthIndex: number, endingMonthIndex: number): string[] {
const today = new Date()
const year = new Date().getFullYear()
const keys = []
for (let i = startingMonthIndex; i < endingMonthIndex; i++) {
today.setMonth(i)
today.setDate(1)
keys.push(this.getMonthlyKey(today))
keys.push(this.getMonthlyKey(new Date(year, i, 1)))
}

return keys
}

private generateDailyKeysRange(month?: number): string[] {
const today = new Date()
if (month) {
today.setMonth(month)
}
const numberOfDays = new Date(today.getFullYear(), today.getMonth() + 1, 0).getDate()
const year = today.getFullYear()
const targetMonth = month ?? today.getMonth()
const numberOfDays = new Date(year, targetMonth + 1, 0).getDate()

const keys = []
for (let i = 1; i <= numberOfDays; i++) {
const date = new Date()
date.setMonth(today.getMonth())
date.setDate(i)
keys.push(this.getDailyKey(date))
keys.push(this.getDailyKey(new Date(year, targetMonth, i)))
}

return keys
Expand Down