-
Notifications
You must be signed in to change notification settings - Fork 11
refactor: re-structure of page time range to be query param centric #1514
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
Show all changes
21 commits
Select commit
Hold shift + click to select a range
1de4d4d
refactor: re-structure of page time range to be query param centric
Christian862 9f10747
Merge branch 'main' of github.com:hypertrace/hypertrace-ui into NavIt…
Christian862 90aca7d
test: support for recent changes
Christian862 40f55da
refactor: requested changes
Christian862 31d4f0f
refactor: added refresh query param
Christian862 bdb7a8b
Merge branch 'main' of github.com:hypertrace/hypertrace-ui into NavIt…
Christian862 595017a
refactor: name change
Christian862 e70957d
refactor: requested changes
Christian862 898dc90
refactor: option param type for refresh flag
Christian862 0c88198
Merge branch 'main' of github.com:hypertrace/hypertrace-ui into NavIt…
Christian862 be3d30f
refactor: testing support
Christian862 7f4a4f1
Merge branch 'main' of github.com:hypertrace/hypertrace-ui into NavIt…
Christian862 8a9660e
Merge branch 'main' of github.com:hypertrace/hypertrace-ui into NavIt…
Christian862 17fa1b3
fix: nit requested changes
Christian862 c03ed28
Merge branch 'main' of github.com:hypertrace/hypertrace-ui into NavIt…
Christian862 9680f55
refactor: aarons changes
Christian862 75f5be9
refactor: requested changes
Christian862 c8f632e
refactor: remove htMemoize from nav item
Christian862 a2e33fc
test: added and fixed testing for new time range
Christian862 e542a30
Merge branch 'main' of github.com:hypertrace/hypertrace-ui into NavIt…
Christian862 1321830
refactor: restore momoization for nav item
Christian862 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
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
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 |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import { Injectable } from '@angular/core'; | ||
| import { isEmpty, isNil } from 'lodash-es'; | ||
| import { EMPTY, ReplaySubject } from 'rxjs'; | ||
| import { catchError, filter, map, switchMap, take } from 'rxjs/operators'; | ||
| import { ParamMap } from '@angular/router'; | ||
| import { isEmpty, isNil, omit } from 'lodash-es'; | ||
| import { concat, EMPTY, Observable, ReplaySubject } from 'rxjs'; | ||
| import { catchError, distinctUntilChanged, filter, map, switchMap, take } from 'rxjs/operators'; | ||
| import { NavigationService, QueryParamObject } from '../navigation/navigation.service'; | ||
| import { ReplayObservable } from '../utilities/rxjs/rxjs-utils'; | ||
| import { FixedTimeRange } from './fixed-time-range'; | ||
|
|
@@ -16,6 +17,7 @@ import { TimeUnit } from './time-unit.type'; | |
| }) | ||
| export class TimeRangeService { | ||
| private static readonly TIME_RANGE_QUERY_PARAM: string = 'time'; | ||
| private static readonly REFRESH_ON_NAVIGATION: string = 'refresh'; | ||
|
|
||
| private readonly timeRangeSubject$: ReplaySubject<TimeRange> = new ReplaySubject(1); | ||
| private currentTimeRange?: TimeRange; | ||
|
|
@@ -51,31 +53,65 @@ export class TimeRangeService { | |
| } | ||
|
|
||
| public setRelativeRange(value: number, unit: TimeUnit): this { | ||
| return this.setTimeRange(TimeRangeService.toRelativeTimeRange(value, unit)); | ||
| return this.setTimeRangeInUrl(TimeRangeService.toRelativeTimeRange(value, unit)); | ||
| } | ||
|
|
||
| public setFixedRange(startTime: Date, endTime: Date): this { | ||
| return this.setTimeRange(TimeRangeService.toFixedTimeRange(startTime, endTime)); | ||
| return this.setTimeRangeInUrl(TimeRangeService.toFixedTimeRange(startTime, endTime)); | ||
| } | ||
|
|
||
| public refresh(): void { | ||
| this.setTimeRange(this.getCurrentTimeRange()); | ||
| const currentStringTimeRange = this.getCurrentTimeRange().toUrlString(); | ||
| this.applyTimeRangeChange(this.timeRangeFromUrlString(currentStringTimeRange)); | ||
| } | ||
|
|
||
| private getInitialTimeRange(): Observable<TimeRange> { | ||
| return this.navigationService.navigation$.pipe( | ||
| take(1), // Wait for first navigation | ||
| switchMap(activatedRoute => activatedRoute.queryParamMap), // Get the params from it | ||
| take(1), // Only the first set of params | ||
| map(paramMap => paramMap.get(TimeRangeService.TIME_RANGE_QUERY_PARAM)), // Extract the time range value from it | ||
| filter((timeRangeString): timeRangeString is string => !isEmpty(timeRangeString)), // Only valid time ranges | ||
| map(timeRangeString => this.timeRangeFromUrlString(timeRangeString)), | ||
| catchError(() => EMPTY) | ||
| ); | ||
| } | ||
|
|
||
| private getPageTimeRangeChanges(): Observable<TimeRange> { | ||
| return this.navigationService.navigation$.pipe( | ||
| map(activeRoute => activeRoute.snapshot.queryParamMap), | ||
| filter(queryParamMap => !isNil(queryParamMap.get(TimeRangeService.TIME_RANGE_QUERY_PARAM))), | ||
| distinctUntilChanged((prevParamMap, currParamMap) => this.shouldNotUpdateTimeRange(prevParamMap, currParamMap)), | ||
| map(currQueryParamMap => { | ||
| const timeRangeQueryParamString = currQueryParamMap.get(TimeRangeService.TIME_RANGE_QUERY_PARAM); | ||
|
|
||
| return this.timeRangeFromUrlString(timeRangeQueryParamString!); | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| private initializeTimeRange(): void { | ||
| this.navigationService.navigation$ | ||
| .pipe( | ||
| take(1), // Wait for first navigation | ||
| switchMap(activatedRoute => activatedRoute.queryParamMap), // Get the params from it | ||
| take(1), // Only the first set of params | ||
| map(paramMap => paramMap.get(TimeRangeService.TIME_RANGE_QUERY_PARAM)), // Extract the time range value from it | ||
| filter((timeRangeString): timeRangeString is string => !isEmpty(timeRangeString)), // Only valid time ranges | ||
| map(timeRangeString => this.timeRangeFromUrlString(timeRangeString)), | ||
| catchError(() => EMPTY) | ||
| ) | ||
| .subscribe(timeRange => { | ||
| this.setTimeRange(timeRange); | ||
| }); | ||
| concat(this.getInitialTimeRange(), this.getPageTimeRangeChanges()).subscribe(timeRange => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one option discussed offline - the two branches of the subscribe basically handle the respective emissions of these two observables. We could do it as two separate subscriptions and it would probably be easier to understand. |
||
| if (!this.timeRangeMatchesCurrentUrl(timeRange)) { | ||
| this.setTimeRangeInUrl(timeRange); | ||
| } else { | ||
| this.applyTimeRangeChange(timeRange); | ||
|
|
||
| const queryParams = this.navigationService.getCurrentActivatedRoute().snapshot.queryParams; | ||
| if (TimeRangeService.REFRESH_ON_NAVIGATION in queryParams) { | ||
| this.navigationService.replaceQueryParametersInUrl(omit(queryParams, TimeRangeService.REFRESH_ON_NAVIGATION)); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private shouldNotUpdateTimeRange(prevParamMap: ParamMap, currParamMap: ParamMap): boolean { | ||
| const refreshQueryParam = currParamMap.get(TimeRangeService.REFRESH_ON_NAVIGATION); | ||
|
|
||
| return ( | ||
| prevParamMap.get(TimeRangeService.TIME_RANGE_QUERY_PARAM) === | ||
| currParamMap.get(TimeRangeService.TIME_RANGE_QUERY_PARAM) && isEmpty(refreshQueryParam) | ||
| ); | ||
| } | ||
|
|
||
| public timeRangeFromUrlString(timeRangeFromUrl: string): TimeRange { | ||
|
|
@@ -91,19 +127,24 @@ export class TimeRangeService { | |
| throw new Error(); // Caught in observable | ||
| } | ||
|
|
||
| private setTimeRange(newTimeRange: TimeRange): this { | ||
| private applyTimeRangeChange(newTimeRange: TimeRange): this { | ||
| this.currentTimeRange = newTimeRange; | ||
| this.timeRangeSubject$.next(newTimeRange); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| private setTimeRangeInUrl(timeRange: TimeRange): this { | ||
| this.navigationService.addQueryParametersToUrl({ | ||
| [TimeRangeService.TIME_RANGE_QUERY_PARAM]: newTimeRange.toUrlString() | ||
| [TimeRangeService.TIME_RANGE_QUERY_PARAM]: timeRange.toUrlString() | ||
| }); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public setDefaultTimeRange(timeRange: TimeRange): void { | ||
| if (!this.currentTimeRange) { | ||
| this.setTimeRange(timeRange); | ||
| this.setTimeRangeInUrl(timeRange); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -115,12 +156,22 @@ export class TimeRangeService { | |
| return new FixedTimeRange(startTime, endTime); | ||
| } | ||
|
|
||
| public toQueryParams(startTime: Date, endTime: Date): QueryParamObject { | ||
| const newTimeRange = new FixedTimeRange(startTime, endTime); | ||
|
|
||
| return { | ||
| [TimeRangeService.TIME_RANGE_QUERY_PARAM]: newTimeRange.toUrlString() | ||
| public toQueryParams(timeRange: TimeRange, refreshTimeOnNavigationParam?: boolean): QueryParamObject { | ||
| const queryParams: QueryParamObject = { | ||
| [TimeRangeService.TIME_RANGE_QUERY_PARAM]: timeRange.toUrlString() | ||
| }; | ||
|
|
||
| if (refreshTimeOnNavigationParam) { | ||
| return { ...queryParams, [TimeRangeService.REFRESH_ON_NAVIGATION]: true }; | ||
| } | ||
|
|
||
| return queryParams; | ||
| } | ||
|
|
||
| private timeRangeMatchesCurrentUrl(timeRange: TimeRange): boolean { | ||
| return ( | ||
| this.navigationService.getQueryParameter(TimeRangeService.TIME_RANGE_QUERY_PARAM, '') === timeRange.toUrlString() | ||
| ); | ||
| } | ||
|
|
||
| public isInitialized(): boolean { | ||
|
|
||
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.
So when do we want to emit?
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.
No this only emits when we navigate and have a time query param. Drill behaviour still works as expected.