-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Infrastructure UI] Add unified search to hosts table #143850
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
crespocarlos
merged 6 commits into
elastic:main
from
crespocarlos:139594-unified-search
Oct 27, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8d4ad0f
Add unified search to hosts table
crespocarlos 074359a
Add saved query support
crespocarlos 94050af
Adjust error handling
crespocarlos 31d9d16
Minor refactoring and unit tests
crespocarlos 76bd54f
Revert changes to translations
crespocarlos a13c020
CR fixes
crespocarlos 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
39 changes: 39 additions & 0 deletions
39
x-pack/plugins/infra/public/pages/metrics/hosts/components/hosts_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,39 @@ | ||
| /* | ||
| * 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 React from 'react'; | ||
| import { EuiSpacer } from '@elastic/eui'; | ||
|
|
||
| import { i18n } from '@kbn/i18n'; | ||
| import { InfraLoadingPanel } from '../../../../components/loading'; | ||
| import { useMetricsDataViewContext } from '../hooks/use_data_view'; | ||
| import { UnifiedSearchBar } from './unified_search_bar'; | ||
| import { HostsTable } from './hosts_table'; | ||
|
|
||
| export const HostContainer = () => { | ||
| const { metricsDataView, isDataViewLoading, hasFailedLoadingDataView } = | ||
| useMetricsDataViewContext(); | ||
|
|
||
| if (isDataViewLoading) { | ||
| return ( | ||
| <InfraLoadingPanel | ||
| height="100%" | ||
| width="auto" | ||
| text={i18n.translate('xpack.infra.waffle.loadingDataText', { | ||
| defaultMessage: 'Loading data', | ||
| })} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| return hasFailedLoadingDataView || !metricsDataView ? null : ( | ||
| <> | ||
| <UnifiedSearchBar dataView={metricsDataView} /> | ||
| <EuiSpacer /> | ||
| <HostsTable /> | ||
| </> | ||
| ); | ||
| }; |
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
77 changes: 77 additions & 0 deletions
77
x-pack/plugins/infra/public/pages/metrics/hosts/components/unified_search_bar.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,77 @@ | ||
| /* | ||
| * 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 React from 'react'; | ||
| import { useKibana } from '@kbn/kibana-react-plugin/public'; | ||
| import type { Filter, Query, TimeRange } from '@kbn/es-query'; | ||
| import type { DataView } from '@kbn/data-views-plugin/public'; | ||
| import type { SavedQuery } from '@kbn/data-plugin/public'; | ||
| import type { InfraClientStartDeps } from '../../../../types'; | ||
| import { useUnifiedSearchContext } from '../hooks/use_unified_search'; | ||
|
|
||
| interface Props { | ||
| dataView: DataView; | ||
| } | ||
|
|
||
| export const UnifiedSearchBar = ({ dataView }: Props) => { | ||
| const { | ||
| services: { unifiedSearch }, | ||
| } = useKibana<InfraClientStartDeps>(); | ||
| const { | ||
| unifiedSearchDateRange, | ||
| unifiedSearchQuery, | ||
| submitFilterChange, | ||
| saveQuery, | ||
| clearSavedQUery, | ||
| } = useUnifiedSearchContext(); | ||
|
|
||
| const { SearchBar } = unifiedSearch.ui; | ||
|
|
||
| const onFilterChange = (filters: Filter[]) => { | ||
| onQueryChange({ filters }); | ||
| }; | ||
|
|
||
| const onQuerySubmit = (payload: { dateRange: TimeRange; query?: Query }) => { | ||
| onQueryChange({ payload }); | ||
| }; | ||
|
|
||
| const onClearSavedQuery = () => { | ||
| clearSavedQUery(); | ||
| }; | ||
|
|
||
| const onQuerySave = (savedQuery: SavedQuery) => { | ||
| saveQuery(savedQuery); | ||
| }; | ||
|
|
||
| const onQueryChange = ({ | ||
| payload, | ||
| filters, | ||
| }: { | ||
| payload?: { dateRange: TimeRange; query?: Query }; | ||
| filters?: Filter[]; | ||
| }) => { | ||
| submitFilterChange(payload?.query, payload?.dateRange, filters); | ||
| }; | ||
|
|
||
| return ( | ||
| <SearchBar | ||
| appName={'Infra Hosts'} | ||
| indexPatterns={[dataView]} | ||
| query={unifiedSearchQuery} | ||
| dateRangeFrom={unifiedSearchDateRange.from} | ||
| dateRangeTo={unifiedSearchDateRange.to} | ||
| onQuerySubmit={onQuerySubmit} | ||
| onSaved={onQuerySave} | ||
| onSavedQueryUpdated={onQuerySave} | ||
| onClearSavedQuery={onClearSavedQuery} | ||
| showSaveQuery | ||
| showQueryInput | ||
| // @ts-expect-error onFiltersUpdated is a valid prop on SearchBar | ||
| onFiltersUpdated={onFilterChange} | ||
| /> | ||
| ); | ||
| }; | ||
85 changes: 85 additions & 0 deletions
85
x-pack/plugins/infra/public/pages/metrics/hosts/hooks/use_data_view.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,85 @@ | ||
| /* | ||
| * 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 { useDataView } from './use_data_view'; | ||
| import { renderHook } from '@testing-library/react-hooks'; | ||
| import { KibanaReactContextValue, useKibana } from '@kbn/kibana-react-plugin/public'; | ||
| import { coreMock, notificationServiceMock } from '@kbn/core/public/mocks'; | ||
| import type { DataView } from '@kbn/data-views-plugin/public'; | ||
| import { DataViewsServicePublic } from '@kbn/data-views-plugin/public/types'; | ||
| import { InfraClientStartDeps } from '../../../../types'; | ||
| import { CoreStart } from '@kbn/core/public'; | ||
|
|
||
| jest.mock('@kbn/i18n'); | ||
| jest.mock('@kbn/kibana-react-plugin/public'); | ||
|
|
||
| let dataViewMock: jest.Mocked<DataViewsServicePublic>; | ||
| const useKibanaMock = useKibana as jest.MockedFunction<typeof useKibana>; | ||
| const notificationMock = notificationServiceMock.createStartContract(); | ||
| const prop = { metricAlias: 'test' }; | ||
|
|
||
| const mockUseKibana = () => { | ||
| useKibanaMock.mockReturnValue({ | ||
| services: { | ||
| ...coreMock.createStart(), | ||
| notifications: notificationMock, | ||
| dataViews: dataViewMock, | ||
| } as Partial<CoreStart> & Partial<InfraClientStartDeps>, | ||
| } as unknown as KibanaReactContextValue<Partial<CoreStart> & Partial<InfraClientStartDeps>>); | ||
| }; | ||
|
|
||
| const mockDataView = { | ||
| id: 'mock-id', | ||
| title: 'mock-title', | ||
| timeFieldName: 'mock-time-field-name', | ||
| isPersisted: () => false, | ||
| getName: () => 'mock-data-view', | ||
| toSpec: () => ({}), | ||
| } as jest.Mocked<DataView>; | ||
|
|
||
| describe('useHostTable hook', () => { | ||
| beforeEach(() => { | ||
| dataViewMock = { | ||
| createAndSave: jest.fn(), | ||
| find: jest.fn(), | ||
| } as Partial<DataViewsServicePublic> as jest.Mocked<DataViewsServicePublic>; | ||
|
|
||
| mockUseKibana(); | ||
| }); | ||
|
|
||
| it('should find an existing Data view', async () => { | ||
| dataViewMock.find.mockReturnValue(Promise.resolve([mockDataView])); | ||
| const { result, waitForNextUpdate } = renderHook(() => useDataView(prop)); | ||
|
|
||
| await waitForNextUpdate(); | ||
| expect(result.current.isDataViewLoading).toEqual(false); | ||
| expect(result.current.hasFailedLoadingDataView).toEqual(false); | ||
| expect(result.current.metricsDataView).toEqual(mockDataView); | ||
| }); | ||
|
|
||
| it('should create a new Data view', async () => { | ||
| dataViewMock.find.mockReturnValue(Promise.resolve([])); | ||
| dataViewMock.createAndSave.mockReturnValue(Promise.resolve(mockDataView)); | ||
| const { result, waitForNextUpdate } = renderHook(() => useDataView(prop)); | ||
|
|
||
| await waitForNextUpdate(); | ||
| expect(result.current.isDataViewLoading).toEqual(false); | ||
| expect(result.current.hasFailedLoadingDataView).toEqual(false); | ||
| expect(result.current.metricsDataView).toEqual(mockDataView); | ||
| }); | ||
|
|
||
| it('should display a toast when it fails to load the data view', async () => { | ||
| dataViewMock.find.mockReturnValue(Promise.reject()); | ||
| const { result, waitForNextUpdate } = renderHook(() => useDataView(prop)); | ||
|
|
||
| await waitForNextUpdate(); | ||
| expect(result.current.isDataViewLoading).toEqual(false); | ||
| expect(result.current.hasFailedLoadingDataView).toEqual(true); | ||
| expect(result.current.metricsDataView).toBeUndefined(); | ||
| expect(notificationMock.toasts.addDanger).toBeCalledTimes(1); | ||
| }); | ||
| }); |
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 |
|---|---|---|
|
|
@@ -5,7 +5,8 @@ | |
| * 2.0. | ||
| */ | ||
|
|
||
| import { useCallback, useState, useEffect } from 'react'; | ||
| import { i18n } from '@kbn/i18n'; | ||
| import { useCallback, useState, useEffect, useMemo } from 'react'; | ||
| import { useKibana } from '@kbn/kibana-react-plugin/public'; | ||
| import createContainer from 'constate'; | ||
| import type { DataView } from '@kbn/data-views-plugin/public'; | ||
|
|
@@ -15,7 +16,7 @@ import { useTrackedPromise } from '../../../../utils/use_tracked_promise'; | |
| export const useDataView = ({ metricAlias }: { metricAlias: string }) => { | ||
| const [metricsDataView, setMetricsDataView] = useState<DataView>(); | ||
| const { | ||
| services: { dataViews }, | ||
| services: { dataViews, notifications }, | ||
| } = useKibana<InfraClientStartDeps>(); | ||
|
|
||
| const [createDataViewRequest, createDataView] = useTrackedPromise( | ||
|
|
@@ -33,7 +34,7 @@ export const useDataView = ({ metricAlias }: { metricAlias: string }) => { | |
|
|
||
| const [getDataViewRequest, getDataView] = useTrackedPromise( | ||
| { | ||
| createPromise: (indexPattern: string): Promise<DataView[]> => { | ||
| createPromise: (_indexPattern: string): Promise<DataView[]> => { | ||
| return dataViews.find(metricAlias, 1); | ||
| }, | ||
| onResolve: (response: DataView[]) => { | ||
|
|
@@ -58,17 +59,36 @@ export const useDataView = ({ metricAlias }: { metricAlias: string }) => { | |
| } | ||
| }, [metricAlias, createDataView, getDataView]); | ||
|
|
||
| const hasFailedFetchingDataView = getDataViewRequest.state === 'rejected'; | ||
| const hasFailedCreatingDataView = createDataViewRequest.state === 'rejected'; | ||
| const isDataViewLoading = useMemo( | ||
| () => getDataViewRequest.state === 'pending' || createDataViewRequest.state === 'pending', | ||
| [getDataViewRequest.state, createDataViewRequest.state] | ||
| ); | ||
|
|
||
| const hasFailedLoadingDataView = useMemo( | ||
| () => getDataViewRequest.state === 'rejected' || createDataViewRequest.state === 'rejected', | ||
| [getDataViewRequest.state, createDataViewRequest.state] | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| loadDataView(); | ||
| }, [metricAlias, loadDataView]); | ||
|
|
||
| useEffect(() => { | ||
| if (hasFailedLoadingDataView && notifications) { | ||
| notifications.toasts.addDanger( | ||
| i18n.translate('xpack.infra.hostsTable.errorOnCreateOrLoadDataview', { | ||
| defaultMessage: | ||
| 'There was an error trying to load or create the Data View: {metricAlias}', | ||
| values: { metricAlias }, | ||
| }) | ||
| ); | ||
| } | ||
| }, [hasFailedLoadingDataView, notifications, metricAlias]); | ||
|
|
||
| return { | ||
| metricsDataView, | ||
| hasFailedCreatingDataView, | ||
| hasFailedFetchingDataView, | ||
| isDataViewLoading, | ||
| hasFailedLoadingDataView, | ||
|
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. thanks for improving this hook! |
||
| }; | ||
| }; | ||
|
|
||
|
|
||
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.
We can remove it once this PR is merged