forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
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
[Discover] Enhance breakdown by functionality #4
Merged
davismcphee
merged 11 commits into
davismcphee:enhancement-unified-histogram-lens
from
dimaanj:fix-some-tests
Nov 16, 2022
+671
−387
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ff69195
[Discover] fix some tests
dimaanj b94bcc7
Merge remote-tracking branch 'davis-remote/enhancement-unified-histog…
dimaanj 6bce46e
[Discover] fix include typings
dimaanj b582bc7
[Discover] try to fix type check failure
dimaanj 26b2feb
[Discover] fix discover layout test
dimaanj b59058d
[Discover] fix ts config
dimaanj 4a992b1
[Discover] fix tests
dimaanj 6121d61
[Discover] fix ci
dimaanj b0bff11
[Discover] add navigation to Lens using breakdown by field
dimaanj 76d8e35
[Discover] fix tests
dimaanj 274f0df
[Discover] fix remaining tests
dimaanj 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 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
137 changes: 137 additions & 0 deletions
137
...ns/discover/public/application/main/components/layout/discover_histogram_content.test.tsx
This file contains 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,137 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { Subject, BehaviorSubject, of } from 'rxjs'; | ||
import { mountWithIntl } from '@kbn/test-jest-helpers'; | ||
import { esHits } from '../../../../__mocks__/es_hits'; | ||
import { dataViewMock } from '../../../../__mocks__/data_view'; | ||
import { GetStateReturn } from '../../services/discover_state'; | ||
import { savedSearchMock } from '../../../../__mocks__/saved_search'; | ||
import { | ||
AvailableFields$, | ||
DataDocuments$, | ||
DataMain$, | ||
DataTotalHits$, | ||
RecordRawType, | ||
} from '../../hooks/use_saved_search'; | ||
import { discoverServiceMock } from '../../../../__mocks__/services'; | ||
import { FetchStatus } from '../../../types'; | ||
import { KibanaContextProvider, KibanaThemeProvider } from '@kbn/kibana-react-plugin/public'; | ||
import { buildDataTableRecord } from '../../../../utils/build_data_record'; | ||
import { | ||
DiscoverHistogramContent, | ||
DiscoverHistogramContentProps, | ||
} from './discover_histogram_content'; | ||
import { VIEW_MODE } from '@kbn/saved-search-plugin/public'; | ||
import { CoreTheme } from '@kbn/core/public'; | ||
import { act } from 'react-dom/test-utils'; | ||
import { setTimeout } from 'timers/promises'; | ||
import { DocumentViewModeToggle } from '../../../../components/view_mode_toggle'; | ||
import { searchSourceInstanceMock } from '@kbn/data-plugin/common/search/search_source/mocks'; | ||
|
||
const mountComponent = async ({ | ||
isPlainRecord = false, | ||
}: { | ||
isPlainRecord?: boolean; | ||
} = {}) => { | ||
const services = discoverServiceMock; | ||
services.data.query.timefilter.timefilter.getAbsoluteTime = () => { | ||
return { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' }; | ||
}; | ||
|
||
(services.data.query.queryString.getDefaultQuery as jest.Mock).mockReturnValue({ | ||
language: 'kuery', | ||
query: '', | ||
}); | ||
(searchSourceInstanceMock.fetch$ as jest.Mock).mockImplementation( | ||
jest.fn().mockReturnValue(of({ rawResponse: { hits: { total: 2 } } })) | ||
); | ||
|
||
const main$ = new BehaviorSubject({ | ||
fetchStatus: FetchStatus.COMPLETE, | ||
recordRawType: isPlainRecord ? RecordRawType.PLAIN : RecordRawType.DOCUMENT, | ||
foundDocuments: true, | ||
}) as DataMain$; | ||
|
||
const documents$ = new BehaviorSubject({ | ||
fetchStatus: FetchStatus.COMPLETE, | ||
result: esHits.map((esHit) => buildDataTableRecord(esHit, dataViewMock)), | ||
}) as DataDocuments$; | ||
|
||
const availableFields$ = new BehaviorSubject({ | ||
fetchStatus: FetchStatus.COMPLETE, | ||
fields: [] as string[], | ||
}) as AvailableFields$; | ||
|
||
const totalHits$ = new BehaviorSubject({ | ||
fetchStatus: FetchStatus.COMPLETE, | ||
result: Number(esHits.length), | ||
}) as DataTotalHits$; | ||
|
||
const savedSearchData$ = { | ||
main$, | ||
documents$, | ||
totalHits$, | ||
availableFields$, | ||
}; | ||
|
||
const props: DiscoverHistogramContentProps = { | ||
isPlainRecord, | ||
dataView: dataViewMock, | ||
navigateTo: jest.fn(), | ||
setExpandedDoc: jest.fn(), | ||
savedSearch: savedSearchMock, | ||
savedSearchData$, | ||
savedSearchRefetch$: new Subject(), | ||
state: { columns: [], hideChart: false }, | ||
stateContainer: { | ||
setAppState: () => {}, | ||
appStateContainer: { | ||
getState: () => ({ | ||
interval: 'auto', | ||
}), | ||
}, | ||
} as unknown as GetStateReturn, | ||
onFieldEdited: jest.fn(), | ||
columns: [], | ||
viewMode: VIEW_MODE.DOCUMENT_LEVEL, | ||
onAddFilter: jest.fn(), | ||
}; | ||
|
||
const coreTheme$ = new BehaviorSubject<CoreTheme>({ darkMode: false }); | ||
|
||
const component = mountWithIntl( | ||
<KibanaContextProvider services={services}> | ||
<KibanaThemeProvider theme$={coreTheme$}> | ||
<DiscoverHistogramContent {...props} /> | ||
</KibanaThemeProvider> | ||
</KibanaContextProvider> | ||
); | ||
|
||
// DiscoverMainContent uses UnifiedHistogramLayout which | ||
// is lazy loaded, so we need to wait for it to be loaded | ||
await act(() => setTimeout(0)); | ||
component.update(); | ||
|
||
return component; | ||
}; | ||
|
||
describe('Discover histogram content component', () => { | ||
describe('DocumentViewModeToggle', () => { | ||
it('should show DocumentViewModeToggle when isPlainRecord is false', async () => { | ||
const component = await mountComponent(); | ||
expect(component.find(DocumentViewModeToggle).exists()).toBe(true); | ||
}); | ||
|
||
it('should not show DocumentViewModeToggle when isPlainRecord is true', async () => { | ||
const component = await mountComponent({ isPlainRecord: true }); | ||
expect(component.find(DocumentViewModeToggle).exists()).toBe(false); | ||
}); | ||
}); | ||
}); |
127 changes: 127 additions & 0 deletions
127
...plugins/discover/public/application/main/components/layout/discover_histogram_content.tsx
This file contains 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,127 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { EuiFlexGroup, EuiFlexItem, EuiHorizontalRule } from '@elastic/eui'; | ||
import { SavedSearch } from '@kbn/saved-search-plugin/public'; | ||
import React, { useCallback } from 'react'; | ||
import { DataView } from '@kbn/data-views-plugin/common'; | ||
import { METRIC_TYPE } from '@kbn/analytics'; | ||
import { useDiscoverServices } from '../../../../hooks/use_discover_services'; | ||
import { DataTableRecord } from '../../../../types'; | ||
import { DocumentViewModeToggle, VIEW_MODE } from '../../../../components/view_mode_toggle'; | ||
import { DocViewFilterFn } from '../../../../services/doc_views/doc_views_types'; | ||
import { DataRefetch$, SavedSearchData } from '../../hooks/use_saved_search'; | ||
import { AppState, GetStateReturn } from '../../services/discover_state'; | ||
import { FieldStatisticsTable } from '../field_stats_table'; | ||
import { DiscoverDocuments } from './discover_documents'; | ||
import { DOCUMENTS_VIEW_CLICK, FIELD_STATISTICS_VIEW_CLICK } from '../field_stats_table/constants'; | ||
|
||
const FieldStatisticsTableMemoized = React.memo(FieldStatisticsTable); | ||
|
||
export interface CommonDiscoverHistogramProps { | ||
dataView: DataView; | ||
savedSearch: SavedSearch; | ||
isPlainRecord: boolean; | ||
navigateTo: (url: string) => void; | ||
savedSearchData$: SavedSearchData; | ||
savedSearchRefetch$: DataRefetch$; | ||
expandedDoc?: DataTableRecord; | ||
setExpandedDoc: (doc?: DataTableRecord) => void; | ||
viewMode: VIEW_MODE; | ||
onAddFilter: DocViewFilterFn | undefined; | ||
onFieldEdited: () => Promise<void>; | ||
columns: string[]; | ||
state: AppState; | ||
stateContainer: GetStateReturn; | ||
} | ||
|
||
export interface DiscoverHistogramContentProps extends CommonDiscoverHistogramProps { | ||
chartHidden?: boolean; | ||
} | ||
|
||
export const DiscoverHistogramContent = ({ | ||
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. I may rename this back to |
||
dataView, | ||
isPlainRecord, | ||
navigateTo, | ||
savedSearchData$, | ||
savedSearchRefetch$, | ||
expandedDoc, | ||
setExpandedDoc, | ||
viewMode, | ||
onAddFilter, | ||
onFieldEdited, | ||
columns, | ||
state, | ||
stateContainer, | ||
savedSearch, | ||
chartHidden, | ||
}: DiscoverHistogramContentProps) => { | ||
const { trackUiMetric } = useDiscoverServices(); | ||
|
||
const setDiscoverViewMode = useCallback( | ||
(mode: VIEW_MODE) => { | ||
stateContainer.setAppState({ viewMode: mode }); | ||
|
||
if (trackUiMetric) { | ||
if (mode === VIEW_MODE.AGGREGATED_LEVEL) { | ||
trackUiMetric(METRIC_TYPE.CLICK, FIELD_STATISTICS_VIEW_CLICK); | ||
} else { | ||
trackUiMetric(METRIC_TYPE.CLICK, DOCUMENTS_VIEW_CLICK); | ||
} | ||
} | ||
}, | ||
[trackUiMetric, stateContainer] | ||
); | ||
|
||
return ( | ||
<EuiFlexGroup | ||
className="eui-fullHeight" | ||
direction="column" | ||
gutterSize="none" | ||
responsive={false} | ||
> | ||
{!isPlainRecord && ( | ||
<EuiFlexItem grow={false}> | ||
<EuiHorizontalRule margin="none" /> | ||
<DocumentViewModeToggle viewMode={viewMode} setDiscoverViewMode={setDiscoverViewMode} /> | ||
</EuiFlexItem> | ||
)} | ||
{viewMode === VIEW_MODE.DOCUMENT_LEVEL ? ( | ||
<DiscoverDocuments | ||
// The documents grid doesn't rerender when the chart visibility changes | ||
// which causes it to render blank space, so we need to force a rerender | ||
key={`docKey${chartHidden}`} | ||
documents$={savedSearchData$.documents$} | ||
expandedDoc={expandedDoc} | ||
dataView={dataView} | ||
navigateTo={navigateTo} | ||
onAddFilter={!isPlainRecord ? onAddFilter : undefined} | ||
savedSearch={savedSearch} | ||
setExpandedDoc={setExpandedDoc} | ||
state={state} | ||
stateContainer={stateContainer} | ||
onFieldEdited={!isPlainRecord ? onFieldEdited : undefined} | ||
/> | ||
) : ( | ||
<FieldStatisticsTableMemoized | ||
availableFields$={savedSearchData$.availableFields$} | ||
savedSearch={savedSearch} | ||
dataView={dataView} | ||
query={state.query} | ||
filters={state.filters} | ||
columns={columns} | ||
stateContainer={stateContainer} | ||
onAddFilter={!isPlainRecord ? onAddFilter : undefined} | ||
trackUiMetric={trackUiMetric} | ||
savedSearchRefetch$={savedSearchRefetch$} | ||
savedSearchDataTotalHits$={savedSearchData$.totalHits$} | ||
/> | ||
)} | ||
</EuiFlexGroup> | ||
); | ||
}; |
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 might be able to drop this prop and just use the
key
on this component instead, then we can just use the common props.