Skip to content

Commit

Permalink
[One Discover] Breakdown by log.level for logs sources (#200584)
Browse files Browse the repository at this point in the history
## 📓 Summary

Closes #183498 

This work sets the Breakdown selector for the Histogram to the
`log.level` field once a logs data source is resolved.
It also applies to ES|QL queries, the change is applied/removed on each
source change.

---------

Co-authored-by: Marco Antonio Ghiani <marcoantonio.ghiani@elastic.co>
Co-authored-by: Davis McPhee <davis.mcphee@elastic.co>
  • Loading branch information
3 people authored Nov 28, 2024
1 parent 93b67fb commit ac6025e
Show file tree
Hide file tree
Showing 33 changed files with 558 additions and 379 deletions.
2 changes: 2 additions & 0 deletions src/plugins/discover/public/__mocks__/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { uiActionsPluginMock } from '@kbn/ui-actions-plugin/public/mocks';
import { expressionsPluginMock } from '@kbn/expressions-plugin/public/mocks';
import { savedSearchPluginMock } from '@kbn/saved-search-plugin/public/mocks';
import {
analyticsServiceMock,
chromeServiceMock,
coreMock,
docLinksServiceMock,
Expand Down Expand Up @@ -149,6 +150,7 @@ export function createDiscoverServicesMock(): DiscoverServices {
corePluginMock.chrome.getActiveSolutionNavId$.mockReturnValue(new BehaviorSubject(null));

return {
analytics: analyticsServiceMock.createAnalyticsServiceStart(),
application: corePluginMock.application,
core: corePluginMock,
charts: chartPluginMock.createSetupContract(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ describe('useDiscoverHistogram', () => {
stateContainer.appState.update({
interval: 'auto',
hideChart: false,
breakdownField: 'extension',
});
const appState = stateContainer.appState;
const wrappedStateContainer = Object.create(appState);
Expand Down Expand Up @@ -166,7 +165,6 @@ describe('useDiscoverHistogram', () => {
expect(Object.keys(params?.initialState ?? {})).toEqual([
'chartHidden',
'timeInterval',
'breakdownField',
'totalHitsStatus',
'totalHitsResult',
]);
Expand Down Expand Up @@ -204,7 +202,6 @@ describe('useDiscoverHistogram', () => {
const state = {
timeInterval: '1m',
chartHidden: true,
breakdownField: 'test',
totalHitsStatus: UnifiedHistogramFetchStatus.loading,
totalHitsResult: undefined,
} as unknown as UnifiedHistogramState;
Expand All @@ -217,7 +214,6 @@ describe('useDiscoverHistogram', () => {
expect(stateContainer.appState.update).toHaveBeenCalledWith({
interval: state.timeInterval,
hideChart: state.chartHidden,
breakdownField: state.breakdownField,
});
});

Expand All @@ -228,7 +224,6 @@ describe('useDiscoverHistogram', () => {
const state = {
timeInterval: containerState.interval,
chartHidden: containerState.hideChart,
breakdownField: containerState.breakdownField,
totalHitsStatus: UnifiedHistogramFetchStatus.loading,
totalHitsResult: undefined,
} as unknown as UnifiedHistogramState;
Expand All @@ -254,18 +249,14 @@ describe('useDiscoverHistogram', () => {
api.setTimeInterval = jest.fn((timeInterval) => {
params = { ...params, timeInterval };
});
api.setBreakdownField = jest.fn((breakdownField) => {
params = { ...params, breakdownField };
});
act(() => {
hook.result.current.ref(api);
});
stateContainer.appState.update({ hideChart: true, interval: '1m', breakdownField: 'test' });
stateContainer.appState.update({ hideChart: true, interval: '1m' });
expect(api.setTotalHits).not.toHaveBeenCalled();
expect(api.setChartHidden).toHaveBeenCalled();
expect(api.setTimeInterval).toHaveBeenCalled();
expect(api.setBreakdownField).toHaveBeenCalled();
expect(Object.keys(params ?? {})).toEqual(['breakdownField', 'timeInterval', 'chartHidden']);
expect(Object.keys(params ?? {})).toEqual(['timeInterval', 'chartHidden']);
});

it('should exclude totalHitsStatus and totalHitsResult from Unified Histogram state updates', async () => {
Expand All @@ -275,7 +266,6 @@ describe('useDiscoverHistogram', () => {
const state = {
timeInterval: containerState.interval,
chartHidden: containerState.hideChart,
breakdownField: containerState.breakdownField,
totalHitsStatus: UnifiedHistogramFetchStatus.loading,
totalHitsResult: undefined,
} as unknown as UnifiedHistogramState;
Expand Down Expand Up @@ -310,7 +300,6 @@ describe('useDiscoverHistogram', () => {
const state = {
timeInterval: containerState.interval,
chartHidden: containerState.hideChart,
breakdownField: containerState.breakdownField,
totalHitsStatus: UnifiedHistogramFetchStatus.loading,
totalHitsResult: undefined,
} as unknown as UnifiedHistogramState;
Expand Down Expand Up @@ -355,7 +344,6 @@ describe('useDiscoverHistogram', () => {
const state = {
timeInterval: containerState.interval,
chartHidden: containerState.hideChart,
breakdownField: containerState.breakdownField,
totalHitsStatus: UnifiedHistogramFetchStatus.loading,
totalHitsResult: undefined,
} as unknown as UnifiedHistogramState;
Expand All @@ -381,63 +369,39 @@ describe('useDiscoverHistogram', () => {
});

it('should set isChartLoading to true for fetch start', async () => {
const fetch$ = new Subject<{
options: {
reset: boolean;
fetchMore: boolean;
};
searchSessionId: string;
}>();
const fetch$ = new Subject<void>();
const stateContainer = getStateContainer();
stateContainer.appState.update({ query: { esql: 'from *' } });
stateContainer.dataState.fetch$ = fetch$;
stateContainer.dataState.fetchChart$ = fetch$;
const { hook } = await renderUseDiscoverHistogram({ stateContainer });
act(() => {
fetch$.next({
options: { reset: false, fetchMore: false },
searchSessionId: '1234',
});
fetch$.next();
});
expect(hook.result.current.isChartLoading).toBe(true);
});
});

describe('refetching', () => {
it('should call refetch when savedSearchFetch$ is triggered', async () => {
const savedSearchFetch$ = new Subject<{
options: {
reset: boolean;
fetchMore: boolean;
};
searchSessionId: string;
}>();
const savedSearchFetch$ = new Subject<void>();
const stateContainer = getStateContainer();
stateContainer.dataState.fetch$ = savedSearchFetch$;
stateContainer.dataState.fetchChart$ = savedSearchFetch$;
const { hook } = await renderUseDiscoverHistogram({ stateContainer });
const api = createMockUnifiedHistogramApi();
act(() => {
hook.result.current.ref(api);
});
expect(api.refetch).toHaveBeenCalled();
act(() => {
savedSearchFetch$.next({
options: { reset: false, fetchMore: false },
searchSessionId: '1234',
});
savedSearchFetch$.next();
});
expect(api.refetch).toHaveBeenCalledTimes(2);
});

it('should skip the next refetch when hideChart changes from true to false', async () => {
const savedSearchFetch$ = new Subject<{
options: {
reset: boolean;
fetchMore: boolean;
};
searchSessionId: string;
}>();
const savedSearchFetch$ = new Subject<void>();
const stateContainer = getStateContainer();
stateContainer.dataState.fetch$ = savedSearchFetch$;
stateContainer.dataState.fetchChart$ = savedSearchFetch$;
const { hook, initialProps } = await renderUseDiscoverHistogram({ stateContainer });
const api = createMockUnifiedHistogramApi();
act(() => {
Expand All @@ -451,45 +415,9 @@ describe('useDiscoverHistogram', () => {
hook.rerender({ ...initialProps, hideChart: false });
});
act(() => {
savedSearchFetch$.next({
options: { reset: false, fetchMore: false },
searchSessionId: '1234',
});
});
expect(api.refetch).toHaveBeenCalledTimes(1);
});

it('should skip the next refetch when fetching more', async () => {
const savedSearchFetch$ = new Subject<{
options: {
reset: boolean;
fetchMore: boolean;
};
searchSessionId: string;
}>();
const stateContainer = getStateContainer();
stateContainer.dataState.fetch$ = savedSearchFetch$;
const { hook } = await renderUseDiscoverHistogram({ stateContainer });
const api = createMockUnifiedHistogramApi();
act(() => {
hook.result.current.ref(api);
});
expect(api.refetch).toHaveBeenCalledTimes(1);
act(() => {
savedSearchFetch$.next({
options: { reset: false, fetchMore: true },
searchSessionId: '1234',
});
savedSearchFetch$.next();
});
expect(api.refetch).toHaveBeenCalledTimes(1);

act(() => {
savedSearchFetch$.next({
options: { reset: false, fetchMore: false },
searchSessionId: '1234',
});
});
expect(api.refetch).toHaveBeenCalledTimes(2);
});
});

Expand Down
Loading

0 comments on commit ac6025e

Please sign in to comment.