-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[SLOs] bring back url state in history tab #244833
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
+238
−121
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
03bd085
SLOs: bring back url state in history tab
mgiota 99974f5
remove auto refresh on history tab
mgiota 6bca35f
bring back locator refactoring
mgiota 1d16014
remove unused TAB_ID_URL_PARAM
mgiota 8f6b511
Merge branch 'main' into slo_url_state_history_tab
mgiota d9a85c8
fix tests
mgiota e45f03c
fix calendar period picker
mgiota ee3fff3
Merge branch 'main' into slo_url_state_history_tab
mgiota c371d60
Merge branch 'main' of github.com:elastic/kibana into slo_url_state_h…
mgiota 6bbc637
fix types
mgiota 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
122 changes: 122 additions & 0 deletions
122
...bility/plugins/slo/public/pages/slo_details/components/history/hooks/use_url_app_state.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,122 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import DateMath from '@kbn/datemath'; | ||
| import { createKbnUrlStateStorage } from '@kbn/kibana-utils-plugin/public'; | ||
| import type { SLODefinitionResponse, SLOWithSummaryResponse } from '@kbn/slo-schema'; | ||
| import type { RecursivePartial } from '@kbn/utility-types'; | ||
| import deepmerge from 'deepmerge'; | ||
| import moment from 'moment'; | ||
| import { useEffect, useRef, useState } from 'react'; | ||
| import { useHistory } from 'react-router-dom'; | ||
| import { toDuration } from '../../../../../utils/slo/duration'; | ||
|
|
||
| const SLO_HISTORY_URL_STORAGE_KEY = '_a'; | ||
|
|
||
| interface AppState { | ||
| range: { | ||
| from: Date; | ||
| to: Date; | ||
| }; | ||
| } | ||
|
|
||
| interface SerializedAppState { | ||
| range: { | ||
| from: string; | ||
| to: string; | ||
| }; | ||
| } | ||
|
|
||
| const DEFAULT_STATE: AppState = { | ||
| range: { | ||
| from: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000), | ||
| to: new Date(), | ||
| }, | ||
| }; | ||
|
|
||
| export function useUrlAppState(slo: SLOWithSummaryResponse | SLODefinitionResponse): { | ||
| state: AppState; | ||
| updateState: (state: AppState) => void; | ||
| } { | ||
| const [state, setState] = useState<AppState>(() => { | ||
| const initialState = { | ||
| ...DEFAULT_STATE, | ||
| range: getDefaultRangeFromSlo(slo), | ||
| }; | ||
| return initialState; | ||
| }); | ||
|
|
||
| const history = useHistory(); | ||
| const urlStateStorage = useRef( | ||
| createKbnUrlStateStorage({ | ||
| history, | ||
| useHash: false, | ||
| useHashQuery: false, | ||
| }) | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| setState( | ||
| toAppState( | ||
| urlStateStorage.current?.get<SerializedAppState>(SLO_HISTORY_URL_STORAGE_KEY) ?? {}, | ||
| slo | ||
| ) | ||
| ); | ||
| }, [urlStateStorage, slo]); | ||
|
|
||
| const updateState = (newState: AppState) => { | ||
| setState((prevState) => { | ||
| const updatedState = deepmerge(prevState, newState); | ||
| urlStateStorage.current?.set(SLO_HISTORY_URL_STORAGE_KEY, updatedState, { | ||
| replace: true, | ||
| }); | ||
|
|
||
| return updatedState; | ||
| }); | ||
| }; | ||
|
|
||
| return { | ||
| state: deepmerge(DEFAULT_STATE, state), | ||
| updateState, | ||
| }; | ||
| } | ||
|
|
||
| function getDefaultRangeFromSlo( | ||
| slo: SLOWithSummaryResponse | SLODefinitionResponse | ||
| ): AppState['range'] { | ||
| if (slo.timeWindow.type === 'calendarAligned') { | ||
| const now = moment(); | ||
| const duration = toDuration(slo.timeWindow.duration); | ||
| const unit = duration.unit === 'w' ? 'isoWeek' : 'month'; | ||
|
|
||
| return { | ||
| from: moment.utc(now).startOf(unit).toDate(), | ||
| to: moment.utc(now).endOf(unit).toDate(), | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| from: new Date(DateMath.parse(`now-${slo.timeWindow.duration}`)!.valueOf()), | ||
| to: new Date(DateMath.parse('now', { roundUp: true })!.valueOf()), | ||
| }; | ||
| } | ||
|
|
||
| function toAppState( | ||
| urlState: RecursivePartial<SerializedAppState>, | ||
| slo: SLOWithSummaryResponse | SLODefinitionResponse | ||
| ): AppState { | ||
| return { | ||
| ...DEFAULT_STATE, | ||
| range: | ||
| urlState.range && urlState.range.from && urlState.range.to | ||
| ? { | ||
| from: new Date(urlState.range.from), | ||
| to: new Date(urlState.range.to), | ||
| } | ||
| : getDefaultRangeFromSlo(slo), | ||
| }; | ||
| } |
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 |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ | |
|
|
||
| import { EuiFlexGroup } from '@elastic/eui'; | ||
| import type { SLOWithSummaryResponse } from '@kbn/slo-schema'; | ||
| import type { SloTabId } from '@kbn/deeplinks-observability'; | ||
| import { ALERTS_TAB_ID, DEFINITION_TAB_ID, HISTORY_TAB_ID } from '@kbn/deeplinks-observability'; | ||
| import moment from 'moment'; | ||
| import React, { useEffect, useState } from 'react'; | ||
| import { BurnRatePanel } from './burn_rate_panel/burn_rate_panel'; | ||
|
|
@@ -19,18 +21,6 @@ import { SloHealthCallout } from './slo_health_callout'; | |
| import { SloRemoteCallout } from './slo_remote_callout'; | ||
| import { ActionModalProvider } from '../../../context/action_modal'; | ||
|
|
||
| export const TAB_ID_URL_PARAM = 'tabId'; | ||
|
Contributor
Author
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. TAB_ID_URL_PARAM is not used anywhere, so I removed it. Rest consts were moved to deeplinks package |
||
| export const OVERVIEW_TAB_ID = 'overview'; | ||
| export const HISTORY_TAB_ID = 'history'; | ||
| export const DEFINITION_TAB_ID = 'definition'; | ||
| export const ALERTS_TAB_ID = 'alerts'; | ||
|
|
||
| export type SloTabId = | ||
| | typeof OVERVIEW_TAB_ID | ||
| | typeof ALERTS_TAB_ID | ||
| | typeof HISTORY_TAB_ID | ||
| | typeof DEFINITION_TAB_ID; | ||
|
|
||
| export interface Props { | ||
| slo: SLOWithSummaryResponse; | ||
| isAutoRefreshing: boolean; | ||
|
|
@@ -58,7 +48,7 @@ export function SloDetails({ slo, isAutoRefreshing, selectedTabId }: Props) { | |
| }, [isAutoRefreshing]); | ||
|
|
||
| if (selectedTabId === HISTORY_TAB_ID) { | ||
| return <SloDetailsHistory slo={slo} isAutoRefreshing={isAutoRefreshing} />; | ||
| return <SloDetailsHistory slo={slo} />; | ||
| } | ||
|
|
||
| if (selectedTabId === DEFINITION_TAB_ID) { | ||
|
|
||
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
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.
@kdelemme I saw you had refactored the locator here, so I decided to bring that part as well