-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Observability] Make Alerts page use shared Kibana time range #115192
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
miltonhultgren
merged 11 commits into
elastic:master
from
miltonhultgren:111348-sync-alerts-timeframe-with-observability
Oct 21, 2021
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
12c16d4
[Observability] Make Alerts page respect timefilter service range (#1…
miltonhultgren 030f306
Merge branch 'master' into 111348-sync-alerts-timeframe-with-observab…
kibanamachine 1e0886f
Merge branch 'master' of github.com:elastic/kibana into 111348-sync-a…
miltonhultgren 82465d3
[Observability] Add useHashQuery option in UrlStateStorage
miltonhultgren caab094
Remove unused
miltonhultgren 0cb1071
Add test for createKbnUrlStateStorage change
miltonhultgren d823e14
Add time range test
miltonhultgren efcb409
Merge branch 'master' of github.com:elastic/kibana into 111348-sync-a…
miltonhultgren a39dc0a
Add code comments
miltonhultgren 6e739e7
Clean up tests
miltonhultgren 77cdca7
Extend createKbnUrlStateStorage tests
miltonhultgren 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
14 changes: 14 additions & 0 deletions
14
x-pack/plugins/observability/public/hooks/use_timefilter_service.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,14 @@ | ||
| /* | ||
| * 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 { useKibana } from '../../../../../src/plugins/kibana_react/public'; | ||
| import { ObservabilityPublicPluginsStart } from '../'; | ||
|
|
||
| export function useTimefilterService() { | ||
| const { services } = useKibana<ObservabilityPublicPluginsStart>(); | ||
| return services.data.query.timefilter.timefilter; | ||
| } |
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 |
|---|---|---|
|
|
@@ -9,18 +9,18 @@ import { EuiButtonEmpty, EuiCallOut, EuiFlexGroup, EuiFlexItem, EuiLink } from ' | |
| import { IndexPatternBase } from '@kbn/es-query'; | ||
| import { i18n } from '@kbn/i18n'; | ||
| import React, { useCallback, useRef } from 'react'; | ||
| import { useHistory } from 'react-router-dom'; | ||
| import useAsync from 'react-use/lib/useAsync'; | ||
| import { ParsedTechnicalFields } from '../../../../rule_registry/common/parse_technical_fields'; | ||
| import type { AlertWorkflowStatus } from '../../../common/typings'; | ||
| import { ExperimentalBadge } from '../../components/shared/experimental_badge'; | ||
| import { useBreadcrumbs } from '../../hooks/use_breadcrumbs'; | ||
| import { useFetcher } from '../../hooks/use_fetcher'; | ||
| import { usePluginContext } from '../../hooks/use_plugin_context'; | ||
| import { RouteParams } from '../../routes'; | ||
| import { useTimefilterService } from '../../hooks/use_timefilter_service'; | ||
| import { callObservabilityApi } from '../../services/call_observability_api'; | ||
| import { AlertsSearchBar } from './alerts_search_bar'; | ||
| import { AlertsTableTGrid } from './alerts_table_t_grid'; | ||
| import { Provider, alertsPageStateContainer, useAlertsPageStateContainer } from './state_container'; | ||
| import './styles.scss'; | ||
| import { WorkflowStatusFilter } from './workflow_status_filter'; | ||
|
|
||
|
|
@@ -32,18 +32,24 @@ export interface TopAlert { | |
| active: boolean; | ||
| } | ||
|
|
||
| interface AlertsPageProps { | ||
| routeParams: RouteParams<'/alerts'>; | ||
| } | ||
| const NO_INDEX_NAMES: string[] = []; | ||
| const NO_INDEX_PATTERNS: IndexPatternBase[] = []; | ||
|
|
||
| export function AlertsPage({ routeParams }: AlertsPageProps) { | ||
| function AlertsPage() { | ||
| const { core, plugins, ObservabilityPageTemplate } = usePluginContext(); | ||
| const { prepend } = core.http.basePath; | ||
| const history = useHistory(); | ||
| const refetch = useRef<() => void>(); | ||
| const timefilterService = useTimefilterService(); | ||
| const { | ||
| query: { rangeFrom = 'now-15m', rangeTo = 'now', kuery = '', workflowStatus = 'open' }, | ||
| } = routeParams; | ||
| rangeFrom, | ||
| setRangeFrom, | ||
| rangeTo, | ||
| setRangeTo, | ||
| kuery, | ||
| setKuery, | ||
| workflowStatus, | ||
| setWorkflowStatus, | ||
| } = useAlertsPageStateContainer(); | ||
|
|
||
| useBreadcrumbs([ | ||
| { | ||
|
|
@@ -94,33 +100,23 @@ export function AlertsPage({ routeParams }: AlertsPageProps) { | |
|
|
||
| const setWorkflowStatusFilter = useCallback( | ||
| (value: AlertWorkflowStatus) => { | ||
| const nextSearchParams = new URLSearchParams(history.location.search); | ||
| nextSearchParams.set('workflowStatus', value); | ||
| history.push({ | ||
| ...history.location, | ||
| search: nextSearchParams.toString(), | ||
| }); | ||
| setWorkflowStatus(value); | ||
| }, | ||
| [history] | ||
| [setWorkflowStatus] | ||
| ); | ||
|
|
||
| const onQueryChange = useCallback( | ||
| ({ dateRange, query }) => { | ||
| if (rangeFrom === dateRange.from && rangeTo === dateRange.to && kuery === (query ?? '')) { | ||
| return refetch.current && refetch.current(); | ||
| } | ||
| const nextSearchParams = new URLSearchParams(history.location.search); | ||
|
|
||
| nextSearchParams.set('rangeFrom', dateRange.from); | ||
| nextSearchParams.set('rangeTo', dateRange.to); | ||
| nextSearchParams.set('kuery', query ?? ''); | ||
|
|
||
| history.push({ | ||
| ...history.location, | ||
| search: nextSearchParams.toString(), | ||
| }); | ||
| timefilterService.setTime(dateRange); | ||
| setRangeFrom(dateRange.from); | ||
| setRangeTo(dateRange.to); | ||
| setKuery(query); | ||
|
Comment on lines
+114
to
+117
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. Much cleaner 👍 |
||
| }, | ||
| [history, rangeFrom, rangeTo, kuery] | ||
| [rangeFrom, setRangeFrom, rangeTo, setRangeTo, kuery, setKuery, timefilterService] | ||
| ); | ||
|
|
||
| const addToQuery = useCallback( | ||
|
|
@@ -215,5 +211,12 @@ export function AlertsPage({ routeParams }: AlertsPageProps) { | |
| ); | ||
| } | ||
|
|
||
| const NO_INDEX_NAMES: string[] = []; | ||
| const NO_INDEX_PATTERNS: IndexPatternBase[] = []; | ||
| function WrappedAlertsPage() { | ||
| return ( | ||
| <Provider value={alertsPageStateContainer}> | ||
| <AlertsPage /> | ||
| </Provider> | ||
| ); | ||
| } | ||
|
|
||
| export { WrappedAlertsPage as AlertsPage }; | ||
9 changes: 9 additions & 0 deletions
9
x-pack/plugins/observability/public/pages/alerts/state_container/index.tsx
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,9 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| export { Provider, alertsPageStateContainer } from './state_container'; | ||
| export { useAlertsPageStateContainer } from './use_alerts_page_state_container'; |
53 changes: 53 additions & 0 deletions
53
x-pack/plugins/observability/public/pages/alerts/state_container/state_container.tsx
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,53 @@ | ||
| /* | ||
| * 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 { | ||
| createStateContainer, | ||
| createStateContainerReactHelpers, | ||
| } from '../../../../../../../src/plugins/kibana_utils/public'; | ||
| import type { AlertWorkflowStatus } from '../../../../common/typings'; | ||
|
|
||
| interface AlertsPageContainerState { | ||
| rangeFrom: string; | ||
| rangeTo: string; | ||
| kuery: string; | ||
| workflowStatus: AlertWorkflowStatus; | ||
| } | ||
|
|
||
| interface AlertsPageStateTransitions { | ||
| setRangeFrom: ( | ||
| state: AlertsPageContainerState | ||
| ) => (rangeFrom: string) => AlertsPageContainerState; | ||
| setRangeTo: (state: AlertsPageContainerState) => (rangeTo: string) => AlertsPageContainerState; | ||
| setKuery: (state: AlertsPageContainerState) => (kuery: string) => AlertsPageContainerState; | ||
| setWorkflowStatus: ( | ||
| state: AlertsPageContainerState | ||
| ) => (workflowStatus: AlertWorkflowStatus) => AlertsPageContainerState; | ||
| } | ||
|
|
||
| const defaultState: AlertsPageContainerState = { | ||
| rangeFrom: 'now-15m', | ||
| rangeTo: 'now', | ||
| kuery: '', | ||
| workflowStatus: 'open', | ||
| }; | ||
|
|
||
| const transitions: AlertsPageStateTransitions = { | ||
| setRangeFrom: (state) => (rangeFrom) => ({ ...state, rangeFrom }), | ||
| setRangeTo: (state) => (rangeTo) => ({ ...state, rangeTo }), | ||
| setKuery: (state) => (kuery) => ({ ...state, kuery }), | ||
| setWorkflowStatus: (state) => (workflowStatus) => ({ ...state, workflowStatus }), | ||
| }; | ||
|
|
||
| const alertsPageStateContainer = createStateContainer(defaultState, transitions); | ||
|
|
||
| type AlertsPageStateContainer = typeof alertsPageStateContainer; | ||
|
|
||
| const { Provider, useContainer } = createStateContainerReactHelpers<AlertsPageStateContainer>(); | ||
|
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. Nice 👍 (It's my first time reviewing the state container in a real world example, so it's nice to see there's some helpers and so on) |
||
|
|
||
| export { Provider, alertsPageStateContainer, useContainer, defaultState }; | ||
| export type { AlertsPageStateContainer, AlertsPageContainerState, AlertsPageStateTransitions }; | ||
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.
it is also possible to subscribe to state container changes and update global time there. This way timefilterService will stay in sync with state container even if dateRange is updated from other places
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 to know, there are some business requirements around defaults on page load that prevents us from doing that in this case though!