-
Notifications
You must be signed in to change notification settings - Fork 252
area/ui: Support typing multiple units in relative range picker #5006
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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
107 changes: 107 additions & 0 deletions
107
ui/packages/shared/components/src/DateTimeRangePicker/utils.test.ts
This file contains hidden or 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,107 @@ | ||
| // Copyright 2022 The Parca Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import {describe, expect, it} from 'vitest'; | ||
|
|
||
| import {UNITS, formatRange, getRelativeTimeRangeBetweenDates, parseInput} from './utils'; | ||
|
|
||
| describe('parseInput', () => { | ||
| it('should parse single unit inputs correctly', () => { | ||
| expect(parseInput('30s')).toEqual({value: 30, unit: UNITS.SECOND}); | ||
| expect(parseInput('45m')).toEqual({value: 45, unit: UNITS.MINUTE}); | ||
| expect(parseInput('2h')).toEqual({value: 2, unit: UNITS.HOUR}); | ||
| expect(parseInput('3d')).toEqual({value: 3, unit: UNITS.DAY}); | ||
| expect(parseInput('1w')).toEqual({value: 1, unit: UNITS.WEEK}); | ||
| expect(parseInput('2y')).toEqual({value: 2, unit: UNITS.YEAR}); | ||
| }); | ||
|
|
||
| it('should parse multi-unit inputs correctly', () => { | ||
| expect(parseInput('1h30m')).toEqual({value: 1.5, unit: UNITS.HOUR}); | ||
| expect(parseInput('2d12h')).toEqual({value: 2.5, unit: UNITS.DAY}); | ||
| expect(parseInput('1w2d')).toEqual({value: 1.2857142857142858, unit: UNITS.WEEK}); | ||
| }); | ||
|
|
||
| it('should return null for invalid inputs', () => { | ||
| expect(parseInput('')).toBeNull(); | ||
| expect(parseInput('invalid')).toBeNull(); | ||
| expect(parseInput('1x')).toBeNull(); | ||
| }); | ||
|
|
||
| it('should return null for inputs longer than 100 characters', () => { | ||
| const longInput = '1h'.repeat(51); // 102 characters | ||
| expect(parseInput(longInput)).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getRelativeTimeRangeBetweenDates', () => { | ||
| it('should return correct unit and value for various time ranges', () => { | ||
| expect(getRelativeTimeRangeBetweenDates(30 * 1000)).toEqual({unit: UNITS.MINUTE, value: 0.5}); | ||
| expect(getRelativeTimeRangeBetweenDates(45 * 60 * 1000)).toEqual({ | ||
| unit: UNITS.MINUTE, | ||
| value: 45, | ||
| }); | ||
| expect(getRelativeTimeRangeBetweenDates(2 * 60 * 60 * 1000)).toEqual({ | ||
| unit: UNITS.HOUR, | ||
| value: 2, | ||
| }); | ||
| expect(getRelativeTimeRangeBetweenDates(3 * 24 * 60 * 60 * 1000)).toEqual({ | ||
| unit: UNITS.DAY, | ||
| value: 3, | ||
| }); | ||
| expect(getRelativeTimeRangeBetweenDates(10 * 24 * 60 * 60 * 1000)).toEqual({ | ||
| unit: UNITS.WEEK, | ||
| value: 1.43, | ||
| }); | ||
| }); | ||
|
|
||
| it('should round values to two decimal places', () => { | ||
| expect(getRelativeTimeRangeBetweenDates(2 * 60 * 60 * 1000 + 5 * 60 * 1000)).toEqual({ | ||
| unit: UNITS.HOUR, | ||
| value: 2.08, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('formatRange', () => { | ||
| it('should format single unit ranges correctly', () => { | ||
| expect(formatRange(30, UNITS.SECOND)).toBe('30s'); | ||
| expect(formatRange(45, UNITS.MINUTE)).toBe('45m'); | ||
| expect(formatRange(2, UNITS.HOUR)).toBe('2h'); | ||
| expect(formatRange(3, UNITS.DAY)).toBe('3d'); | ||
| expect(formatRange(1, UNITS.WEEK)).toBe('1w'); | ||
| expect(formatRange(2, UNITS.YEAR)).toBe('2y'); | ||
| }); | ||
|
|
||
| it('should preserve original units when possible', () => { | ||
| expect(formatRange(25, UNITS.HOUR)).toBe('25h'); | ||
| expect(formatRange(36, UNITS.HOUR)).toBe('36h'); | ||
| expect(formatRange(1.5, UNITS.DAY)).toBe('1d12h'); | ||
| expect(formatRange(1.25, UNITS.HOUR)).toBe('1h15m'); | ||
| }); | ||
|
|
||
| it('should handle fractional values correctly', () => { | ||
| expect(formatRange(1.75, UNITS.HOUR)).toBe('1h45m'); | ||
| expect(formatRange(2.5, UNITS.DAY)).toBe('2d12h'); | ||
| }); | ||
|
|
||
| it('should return 0s for zero values', () => { | ||
| expect(formatRange(0, UNITS.SECOND)).toBe('0s'); | ||
| expect(formatRange(0, UNITS.HOUR)).toBe('0s'); | ||
| }); | ||
|
|
||
| it('should handle edge cases correctly', () => { | ||
| expect(formatRange(0.1, UNITS.MINUTE)).toBe('0m6s'); | ||
| expect(formatRange(1.99, UNITS.DAY)).toBe('1d23h45m'); | ||
| expect(formatRange(1.001, UNITS.HOUR)).toBe('1h'); | ||
| }); | ||
| }); | ||
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.
What's going to happen for something like 90s vs 1m30s? Are both parsed as 90s?
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.
Good questions.
To answer the test question (and maybe the first one) internally, and as a request to the backend they are both processed and parsed as
90sbut in the UI component, it will be parsed as1m30s, and this is because the logic favours the higher unit over the lower ones.Regarding the suggestion to work with seconds or milliseconds instead of floating-point numbers, the floating point numbers are only being used to format the human readable text that shows up in the
RelativeDatepickercomponent. This allows us to preserve the user's input format when possible. But as mentioned, we still send the timerange in milliseconds to the backend.If you think there might be advantages to using milliseconds throughout the entire component, I'm open to discussing potential improvements or alternative implementations.
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.
I don't have a strong opinion on how it gets represented internally. I was thinking that maybe there was an easier way. Seems like it's already pretty good, so please ignore my suggestion.
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.
Feel free to merge this as is too.