-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support new granularities when determining x-axis timestamp for…
…mat (#1848)
- Loading branch information
1 parent
fcabfa7
commit a4d46ee
Showing
6 changed files
with
105 additions
and
56 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
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
43 changes: 43 additions & 0 deletions
43
packages/analytics/analytics-chart/src/utils/format-timestamps.spec.ts
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,43 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { formatByGranularity } from './format-timestamps' | ||
|
||
describe('formatByGranularity', () => { | ||
const testDate = new Date('2024-12-10T15:30:45Z') | ||
|
||
it('formats correctly for second-based granularities in UTC', () => { | ||
expect(formatByGranularity(testDate, 'secondly', false, 'UTC')).toBe('3:30:45 PM') | ||
expect(formatByGranularity(testDate, 'secondly', true, 'UTC')).toBe('2024-12-10 3:30:45 PM') | ||
}) | ||
|
||
it('formats correctly for minute-based granularities in UTC', () => { | ||
expect(formatByGranularity(testDate, 'minutely', false, 'UTC')).toBe('3:30 PM') | ||
expect(formatByGranularity(testDate, 'hourly', true, 'UTC')).toBe('2024-12-10 3:30 PM') | ||
}) | ||
|
||
it('formats correctly for twelveHourly granularity in UTC', () => { | ||
expect(formatByGranularity(testDate, 'twelveHourly', false, 'UTC')).toBe('2024-12-10 3:30 PM') | ||
}) | ||
|
||
it('formats correctly for daily granularity in UTC', () => { | ||
expect(formatByGranularity(testDate, 'daily', false, 'UTC')).toBe('2024-12-10') | ||
}) | ||
|
||
it('formats correctly for weekly granularity in UTC', () => { | ||
expect(formatByGranularity(testDate, 'weekly', false, 'UTC')).toBe('2024 W50') | ||
}) | ||
|
||
it('formats with default format for unknown granularities in UTC', () => { | ||
// @ts-ignore - testing unknown granularity | ||
expect(formatByGranularity(testDate, 'unknownGranularity', false, 'UTC')).toBe('2024-12-10 3:30:45 PM') | ||
}) | ||
|
||
it('formats correctly for second-based granularities in America/New_York', () => { | ||
expect(formatByGranularity(testDate, 'secondly', false, 'America/New_York')).toBe('10:30:45 AM') | ||
expect(formatByGranularity(testDate, 'secondly', true, 'America/New_York')).toBe('2024-12-10 10:30:45 AM') | ||
}) | ||
|
||
it('formats correctly for minute-based granularities in America/New_York', () => { | ||
expect(formatByGranularity(testDate, 'minutely', false, 'America/New_York')).toBe('10:30 AM') | ||
expect(formatByGranularity(testDate, 'hourly', true, 'America/New_York')).toBe('2024-12-10 10:30 AM') | ||
}) | ||
}) |
31 changes: 31 additions & 0 deletions
31
packages/analytics/analytics-chart/src/utils/format-timestamps.ts
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,31 @@ | ||
import type { GranularityValues } from '@kong-ui-public/analytics-utilities' | ||
import { formatInTimeZone } from 'date-fns-tz' | ||
|
||
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone | ||
|
||
export const formatByGranularity = ( | ||
tickValue: Date, | ||
granularity: GranularityValues, | ||
dayBoundaryCrossed: boolean, | ||
timezone: string = tz, | ||
) => { | ||
if (['secondly', 'tenSecondly', 'thirtySecondly'].includes(granularity)) { | ||
return formatInTimeZone(tickValue, timezone, dayBoundaryCrossed ? 'yyyy-MM-dd h:mm:ss a' : 'h:mm:ss a') | ||
} | ||
if (['minutely', 'fiveMinutely', 'tenMinutely', 'thirtyMinutely', 'hourly', 'twoHourly'].includes(granularity)) { | ||
return formatInTimeZone(tickValue, timezone, dayBoundaryCrossed ? 'yyyy-MM-dd h:mm a' : 'h:mm a') | ||
} | ||
if (granularity === 'twelveHourly') { | ||
return formatInTimeZone(tickValue, timezone, 'yyyy-MM-dd h:mm a') | ||
} | ||
|
||
if (granularity === 'daily') { | ||
return formatInTimeZone(tickValue, timezone, 'yyyy-MM-dd') | ||
} | ||
|
||
if (granularity === 'weekly') { | ||
return `${formatInTimeZone(tickValue, timezone, 'yyyy')} W${formatInTimeZone(tickValue, timezone, 'II')}` | ||
} | ||
|
||
return formatInTimeZone(tickValue, timezone, 'yyyy-MM-dd h:mm:ss a') | ||
} |
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