-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support new granularities when determining x-axis timestamp format #1848
Open
filipgutica
wants to merge
6
commits into
main
Choose a base branch
from
fix/analytics-chart-time-label-formats
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+105
−56
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
21d468e
feat: support new granularities when determining x-axis timestamp format
filipgutica 60f4164
fix: improve day boundary calculation
filipgutica 763fce0
fix: better timestamp formatting
filipgutica 255b51a
fix: unit test
filipgutica 0d011d1
fix: improve options
filipgutica def924b
fix: update some comments
filipgutica File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some housekeeping... revisited this old code... thought I'd improve this explanation a bit