Skip to content
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] Fix redundant request when toggling histogram #205076

Draft
wants to merge 32 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
482e91a
Restore ESQL request count tests
kertal Dec 16, 2024
fd544c7
change tests
kertal Dec 16, 2024
501cb30
Initial state upgrade
kertal Dec 18, 2024
7a18cda
Fix tests and types
kertal Dec 18, 2024
bf52707
Enable functional tests
kertal Dec 18, 2024
488b939
Merge branch 'main' into discover-restore-esql-count-functions
kertal Dec 18, 2024
2bec13e
resolve redundant histogram requests
kertal Dec 13, 2024
0ff3474
Update functional tests
kertal Dec 18, 2024
82a8ab4
Fix functional test
kertal Dec 18, 2024
c641c2a
Fix functional test expect nr of searches
kertal Dec 18, 2024
f8d9052
Attempt to fix tests
kertal Dec 18, 2024
fb8824f
Remove only
kertal Dec 18, 2024
7c7f379
Merge branch 'main' into discover-restore-esql-count-functions
kertal Dec 18, 2024
50d3d23
Merge branch 'main' into discover-restore-esql-count-functions
kertal Dec 18, 2024
80f4579
Attempt to fix tests
kertal Dec 18, 2024
a0383fb
attempt to fix test
kertal Dec 18, 2024
fc38fe6
Merge remote-tracking branch 'upstream/main' into discover-restore-es…
kertal Dec 18, 2024
3101280
remove .only
kertal Dec 18, 2024
29251f8
Reduce complexity
kertal Dec 19, 2024
4081c46
Address flakiness
kertal Dec 19, 2024
4dcdc23
Undo functional test
kertal Dec 19, 2024
253e70a
Change unit test
kertal Dec 19, 2024
4f16e84
Merge remote-tracking branch 'upstream/main' into discover-restore-es…
kertal Dec 19, 2024
fa3b9ab
Fix functional test
kertal Dec 19, 2024
67cc653
remove .only (again)
kertal Dec 19, 2024
4d43fe8
Apply changes
kertal Dec 21, 2024
faae261
Merge branch
kertal Dec 21, 2024
f0b44e5
Merge main, fix conflict
kertal Dec 23, 2024
27947b7
Fix tests
kertal Dec 23, 2024
05eef5a
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Dec 23, 2024
2959723
Fix linting
kertal Dec 23, 2024
7183ef0
Fix i18n
kertal Dec 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ async function mountComponent(fetchStatus: FetchStatus, hits: EsHitRecord[]) {
stateContainer.appState.update({
dataSource: createDataViewDataSource({ dataViewId: dataViewMock.id! }),
});
stateContainer.internalState.transitions.setDataRequestParams({
timeRangeRelative: {
from: '2020-05-14T11:05:13.590',
to: '2020-05-14T11:20:13.590',
},
timeRangeAbsolute: {
from: '2020-05-14T11:05:13.590',
to: '2020-05-14T11:20:13.590',
},
});

stateContainer.dataState.data$.documents$ = documents$;

const props = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import useObservable from 'react-use/lib/useObservable';
import type { DocViewFilterFn } from '@kbn/unified-doc-viewer/types';
import { DiscoverGridSettings } from '@kbn/saved-search-plugin/common';
import { useQuerySubscriber } from '@kbn/unified-field-list';
import { map } from 'rxjs';
import { DiscoverGrid } from '../../../../components/discover_grid';
import { getDefaultRowsPerPage } from '../../../../../common/constants';
import { useInternalStateSelector } from '../../state_management/discover_internal_state_container';
Expand Down Expand Up @@ -112,6 +111,7 @@ function DiscoverDocumentsComponent({
const documents$ = stateContainer.dataState.data$.documents$;
const savedSearch = useSavedSearchInitial();
const { dataViews, capabilities, uiSettings, uiActions, ebtManager, fieldsMetadata } = services;
const requestParams = useInternalStateSelector((state) => state.dataRequestParams);
const [
dataSource,
query,
Expand Down Expand Up @@ -269,20 +269,14 @@ function DiscoverDocumentsComponent({
: undefined,
[documentState.esqlQueryColumns]
);

const { filters } = useQuerySubscriber({ data: services.data });

const timeRange = useObservable(
services.timefilter.getTimeUpdate$().pipe(map(() => services.timefilter.getTime())),
services.timefilter.getTime()
);

const cellActionsMetadata = useAdditionalCellActions({
dataSource,
dataView,
query,
filters,
timeRange,
timeRange: requestParams.timeRangeAbsolute,
});

const renderDocumentView = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,30 @@ import { createDataViewDataSource } from '../../../../../common/data_sources';
function getStateContainer(savedSearch?: SavedSearch) {
const stateContainer = getDiscoverStateMock({ isTimeBased: true, savedSearch });
const dataView = savedSearch?.searchSource?.getField('index') as DataView;

stateContainer.appState.update({
const appState = {
dataSource: createDataViewDataSource({ dataViewId: dataView?.id! }),
interval: 'auto',
hideChart: false,
});
};

stateContainer.appState.update(appState);

stateContainer.internalState.transitions.setDataView(dataView);
stateContainer.internalState.transitions.setDataRequestParams({
timeRangeAbsolute: {
from: '2020-05-14T11:05:13.590',
to: '2020-05-14T11:20:13.590',
},
timeRangeRelative: {
from: '2020-05-14T11:05:13.590',
to: '2020-05-14T11:20:13.590',
},
});

return stateContainer;
}

const mountComponent = async ({
isEsqlMode = false,
storage,
savedSearch = savedSearchMockWithTimeField,
searchSessionId = '123',
Expand All @@ -65,16 +75,7 @@ const mountComponent = async ({
const dataView = savedSearch?.searchSource?.getField('index') as DataView;

let 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.timefilter.timefilter.getTime = () => {
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 } } }))
);
Expand Down Expand Up @@ -176,8 +177,6 @@ describe('Discover histogram layout component', () => {
const { component } = await mountComponent();
expect(component.find(PanelsToggle).first().prop('isChartAvailable')).toBe(undefined);
expect(component.find(PanelsToggle).first().prop('renderedFor')).toBe('histogram');
expect(component.find(PanelsToggle).last().prop('isChartAvailable')).toBe(true);
expect(component.find(PanelsToggle).last().prop('renderedFor')).toBe('tabs');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ export const DiscoverHistogramLayout = ({
if (!searchSessionId && !isEsqlMode) {
return null;
}
if (hideChart) {
return (
<>
<DiscoverMainContent
{...mainContentProps}
stateContainer={stateContainer}
dataView={dataView}
panelsToggle={renderCustomChartToggleActions()}
/>
</>
);
}

return (
<UnifiedHistogramContainer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ async function mountComponent(
query,
});
stateContainer.internalState.transitions.setDataView(dataView);
stateContainer.internalState.transitions.setDataRequestParams({
timeRangeAbsolute: time,
timeRangeRelative: time,
});

const props = {
dataView,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,19 @@ describe('Discover main content component', () => {

it('should include PanelsToggle when chart is available', async () => {
const component = await mountComponent({ isChartAvailable: true });
expect(component.find(PanelsToggle).prop('isChartAvailable')).toBe(true);
expect(component.find(PanelsToggle).prop('renderedFor')).toBe('tabs');
expect(component.find(PanelsToggle).prop('renderedFor')).toBe('root');
expect(component.find(EuiHorizontalRule).exists()).toBe(true);
});

it('should include PanelsToggle when chart is available and hidden', async () => {
const component = await mountComponent({ isChartAvailable: true, hideChart: true });
expect(component.find(PanelsToggle).prop('isChartAvailable')).toBe(true);
expect(component.find(PanelsToggle).prop('renderedFor')).toBe('tabs');
expect(component.find(PanelsToggle).prop('renderedFor')).toBe('root');
expect(component.find(EuiHorizontalRule).exists()).toBe(false);
});

it('should include PanelsToggle when chart is not available', async () => {
const component = await mountComponent({ isChartAvailable: false });
expect(component.find(PanelsToggle).prop('isChartAvailable')).toBe(false);
expect(component.find(PanelsToggle).prop('renderedFor')).toBe('tabs');
expect(component.find(PanelsToggle).prop('renderedFor')).toBe('root');
expect(component.find(EuiHorizontalRule).exists()).toBe(false);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,11 @@ export const DiscoverMainContent = ({
setDiscoverViewMode={setDiscoverViewMode}
patternCount={patternCount}
dataView={dataView}
prepend={
React.isValidElement(panelsToggle)
? React.cloneElement(panelsToggle, { renderedFor: 'tabs', isChartAvailable })
: undefined
}
prepend={panelsToggle}
/>
);
},
[
viewMode,
isEsqlMode,
stateContainer,
setDiscoverViewMode,
dataView,
panelsToggle,
isChartAvailable,
]
[viewMode, isEsqlMode, stateContainer, setDiscoverViewMode, dataView, panelsToggle]
);

const viewModeToggle = useMemo(() => renderViewModeToggle(), [renderViewModeToggle]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,23 @@ describe('useDiscoverHistogram', () => {
});
expect(hook.result.current.isChartLoading).toBe(true);
});

it('should use timerange + timeRangeRelative + query given by the internalState container', async () => {
const fetch$ = new Subject<void>();
const stateContainer = getStateContainer();
const timeRangeAbs = { from: '2021-05-01T20:00:00Z', to: '2021-05-02T20:00:00Z' };
const timeRangeRel = { from: 'now-15m', to: 'now' };
stateContainer.internalState.transitions.setDataRequestParams({
timeRangeAbsolute: timeRangeAbs,
timeRangeRelative: timeRangeRel,
});
const { hook } = await renderUseDiscoverHistogram({ stateContainer });
act(() => {
fetch$.next();
});
expect(hook.result.current.timeRange).toBe(timeRangeAbs);
expect(hook.result.current.relativeTimeRange).toBe(timeRangeRel);
});
});

describe('refetching', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,9 @@ export const useDiscoverHistogram = ({
* Request params
*/
const { query, filters } = useQuerySubscriber({ data: services.data });
const requestParams = useInternalStateSelector((state) => state.dataRequestParams);
const customFilters = useInternalStateSelector((state) => state.customFilters);
const timefilter = services.data.query.timefilter.timefilter;
const timeRange = timefilter.getAbsoluteTime();
const relativeTimeRange = useObservable(
timefilter.getTimeUpdate$().pipe(map(() => timefilter.getTime())),
timefilter.getTime()
);

const { timeRangeRelative: relativeTimeRange, timeRangeAbsolute: timeRange } = requestParams;
// When in ES|QL mode, update the data view, query, and
// columns only when documents are done fetching so the Lens suggestions
// don't frequently change, such as when the user modifies the table
Expand Down Expand Up @@ -272,15 +267,15 @@ export const useDiscoverHistogram = ({
* Data fetching
*/

const skipRefetch = useRef<boolean>();
const skipRefetch = useRef<number | undefined>();

// Skip refetching when showing the chart since Lens will
// automatically fetch when the chart is shown
useEffect(() => {
if (skipRefetch.current === undefined) {
skipRefetch.current = false;
} else {
skipRefetch.current = !hideChart;
skipRefetch.current = 0;
} else if (!hideChart) {
skipRefetch.current = 3;
}
}, [hideChart]);

Expand Down Expand Up @@ -309,13 +304,14 @@ export const useDiscoverHistogram = ({
}

const subscription = fetchChart$.subscribe((source) => {
if (!skipRefetch.current) {
if (skipRefetch.current === 0) {
if (source === 'discover') addLog('Unified Histogram - Discover refetch');
if (source === 'lens') addLog('Unified Histogram - Lens suggestion refetch');
unifiedHistogram.refetch();
} else {
addLog('Unified Histogram - Skip refetch');
skipRefetch.current = skipRefetch.current! - 1;
}

skipRefetch.current = false;
});

// triggering the initial request for total hits hook
Expand Down Expand Up @@ -499,8 +495,25 @@ const createTotalHitsObservable = (state$?: Observable<UnifiedHistogramState>) =

const createCurrentSuggestionObservable = (state$: Observable<UnifiedHistogramState>) => {
return state$.pipe(
map((state) => state.currentSuggestionContext),
distinctUntilChanged(isEqual)
// Emit the previous and current state as a pair
pairwise(),
// Filter out the transition where chartHidden changes from false to true
filter(([prev, curr]) => {
const isTransition = prev.chartHidden && !curr.chartHidden;
if (isTransition) {
// console.log('Filtered out transition from chartHidden: false to true');
return false;
}
/**
const differences = getDifferences(
prev.currentSuggestionContext.suggestion,
curr.currentSuggestionContext.suggestion
);
console.log('Differences in currentSuggestionContext:', differences);
**/

return !isEqual(prev.currentSuggestionContext, curr.currentSuggestionContext);
})
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ describe('test fetchAll', () => {
rowHeight: false,
breakdownField: false,
},
dataRequestParams: {},
}),
searchSessionId: '123',
initialFetchStatus: FetchStatus.UNINITIALIZED,
Expand Down Expand Up @@ -273,6 +274,7 @@ describe('test fetchAll', () => {
rowHeight: false,
breakdownField: false,
},
dataRequestParams: {},
}),
};
fetchAll(subjects, false, deps);
Expand Down Expand Up @@ -396,6 +398,7 @@ describe('test fetchAll', () => {
rowHeight: false,
breakdownField: false,
},
dataRequestParams: {},
}),
};
fetchAll(subjects, false, deps);
Expand Down
Loading