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] Histogram debugging v3 #203786

Closed
wants to merge 14 commits into from
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 @@ -110,6 +109,7 @@ function DiscoverDocumentsComponent({
}) {
const services = useDiscoverServices();
const documents$ = stateContainer.dataState.data$.documents$;
const main$ = stateContainer.dataState.data$.main$;
const savedSearch = useSavedSearchInitial();
const { dataViews, capabilities, uiSettings, uiActions, ebtManager, fieldsMetadata } = services;
const [
Expand Down Expand Up @@ -272,17 +272,12 @@ function DiscoverDocumentsComponent({

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: main$.getValue().timeRange,
});

const renderDocumentView = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@ 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: '',
Expand All @@ -86,6 +80,8 @@ const mountComponent = async ({
const main$ = new BehaviorSubject({
fetchStatus: FetchStatus.COMPLETE,
foundDocuments: true,
timeRange: { 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' },
}) as DataMain$;

const documents$ = new BehaviorSubject({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ async function mountComponent(
main$: DataMain$ = new BehaviorSubject({
fetchStatus: FetchStatus.COMPLETE,
foundDocuments: true,
timeRange: { 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' },
}) as DataMain$
) {
const searchSourceMock = createSearchSourceMock({ index: dataView });
const services = createDiscoverServicesMock();
const time = { from: '2020-05-14T11:05:13.590', to: '2020-05-14T11:20:13.590' };
services.data.query.timefilter.timefilter.getTime = () => time;

(services.data.query.queryString.getDefaultQuery as jest.Mock).mockReturnValue({
language: 'kuery',
query: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import {
UnifiedHistogramState,
} from '@kbn/unified-histogram-plugin/public';
import { createMockUnifiedHistogramApi } from '@kbn/unified-histogram-plugin/public/mocks';
import { checkHitCount, sendErrorTo } from '../../hooks/use_saved_search_messages';
import {
checkHitCount,
sendErrorTo,
sendFetchStartMsg,
} from '../../hooks/use_saved_search_messages';
import type { InspectorAdapters } from '../../hooks/use_inspector';
import { UnifiedHistogramCustomization } from '../../../../customizations/customization_types/histogram_customization';
import { useDiscoverCustomization } from '../../../../customizations';
Expand Down Expand Up @@ -379,6 +383,23 @@ describe('useDiscoverHistogram', () => {
});
expect(hook.result.current.isChartLoading).toBe(true);
});

it('should use timerange + timeRangeRelative + query given by the data main$ observable', async () => {
const fetch$ = new Subject<void>();
const stateContainer = getStateContainer();
const timeRange = { from: '2021-05-01T20:00:00Z', to: '2021-05-02T20:00:00Z' };
const timeRangeRelative = { from: 'now-15m', to: 'now' };
const query = { esql: 'test' };
sendFetchStartMsg(stateContainer.dataState.data$.main$, timeRange, timeRangeRelative, query);

const { hook } = await renderUseDiscoverHistogram({ stateContainer });
act(() => {
fetch$.next();
});
expect(hook.result.current.timeRange).toBe(timeRange);
expect(hook.result.current.relativeTimeRange).toBe(timeRangeRelative);
expect(hook.result.current.query).toBe(query);
});
});

describe('refetching', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,9 @@ export const useDiscoverHistogram = ({
/**
* Request params
*/
const { query, filters } = useQuerySubscriber({ data: services.data });
const { filters } = useQuerySubscriber({ data: services.data });
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, timeRange, query } = useObservable(main$, main$.getValue());

// When in ES|QL mode, update the data view, query, and
// columns only when documents are done fetching so the Lens suggestions
Expand Down Expand Up @@ -337,9 +332,6 @@ export const useDiscoverHistogram = ({
return allFilters.length ? allFilters : EMPTY_FILTERS;
}, [filters, customFilters]);

// eslint-disable-next-line react-hooks/exhaustive-deps
const timeRangeMemoized = useMemo(() => timeRange, [timeRange?.from, timeRange?.to]);

const onVisContextChanged = useCallback(
(
nextVisContext: UnifiedHistogramVisContext | undefined,
Expand Down Expand Up @@ -399,8 +391,8 @@ export const useDiscoverHistogram = ({
dataView: isEsqlMode ? esqlDataView : dataView,
query: isEsqlMode ? esqlQuery : query,
filters: filtersMemoized,
timeRange: timeRangeMemoized,
relativeTimeRange,
timeRange,
relativeTimeRange: timeRangeRelative,
columns: isEsqlMode ? esqlColumns : undefined,
onFilter: histogramCustomization?.onFilter,
onBrushEnd: histogramCustomization?.onBrushEnd,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export function fetchAll(
services,
sort: getAppState().sort as SortOrder[],
customFilters: getInternalState().customFilters,
inputTimeRange: dataSubjects.main$.getValue().timeRange,
});
}

Expand All @@ -117,6 +118,7 @@ export function fetchAll(
data,
expressions,
profilesManager,
inputTimeRange: dataSubjects.main$.getValue().timeRange,
})
: fetchDocuments(searchSource, fetchDeps);
const fetchType = isEsqlQuery ? 'fetchTextBased' : 'fetchDocuments';
Expand Down Expand Up @@ -245,6 +247,7 @@ export async function fetchMoreDocuments(
services,
sort: getAppState().sort as SortOrder[],
customFilters: getInternalState().customFilters,
inputTimeRange: dataSubjects.main$.getValue().timeRange,
});

// Fetch more documents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function fetchEsql({
expressions: ExpressionsStart;
profilesManager: ProfilesManager;
}): Promise<RecordsFetchResponse> {
const timeRange = inputTimeRange ?? data.query.timefilter.timefilter.getTime();
const timeRange = inputTimeRange ?? data.query.timefilter.timefilter.getAbsoluteTime();
return textBasedQueryStateToAstWithValidation({
filters,
query,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { ISearchSource } from '@kbn/data-plugin/public';
import { DataViewType, DataView } from '@kbn/data-views-plugin/public';
import { Filter } from '@kbn/es-query';
import type { ISearchSource } from '@kbn/data-plugin/public';
import { DataViewType, type DataView } from '@kbn/data-views-plugin/public';
import type { Filter, TimeRange } from '@kbn/es-query';
import type { SortOrder } from '@kbn/saved-search-plugin/public';
import { SORT_DEFAULT_ORDER_SETTING } from '@kbn/discover-utils';
import { DiscoverServices } from '../../../build_services';
Expand All @@ -25,11 +25,13 @@ export function updateVolatileSearchSource(
services,
sort,
customFilters,
inputTimeRange,
}: {
dataView: DataView;
services: DiscoverServices;
sort?: SortOrder[];
customFilters: Filter[];
inputTimeRange?: TimeRange;
}
) {
const { uiSettings, data } = services;
Expand All @@ -48,7 +50,7 @@ export function updateVolatileSearchSource(

if (dataView.type !== DataViewType.ROLLUP) {
// Set the date range filter fields from timeFilter using the absolute format. Search sessions requires that it be converted from a relative range
const timeFilter = data.query.timefilter.timefilter.createFilter(dataView);
const timeFilter = data.query.timefilter.timefilter.createFilter(dataView, inputTimeRange);
filters = timeFilter ? [...filters, timeFilter] : filters;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
sendLoadingMoreFinishedMsg,
sendNoResultsFoundMsg,
sendPartialMsg,
sendFetchStartMsg,
} from './use_saved_search_messages';
import { FetchStatus } from '../../types';
import { BehaviorSubject } from 'rxjs';
Expand Down Expand Up @@ -188,4 +189,23 @@ describe('test useSavedSearch message generators', () => {
});
checkHitCount(main$, 0);
});

test('sendFetchStartMsg', (done) => {
const main$ = new BehaviorSubject<DataMainMsg>({
fetchStatus: FetchStatus.COMPLETE,
});
const timeRange = { from: 'fromAbs', to: 'toAbs' };
const timeRangeRelative = { from: 'fromRel', to: 'toRel' };
const query = { query: 'query', language: 'test' };
main$.subscribe((value) => {
if (value.fetchStatus === FetchStatus.LOADING) {
expect(value.fetchStatus).toBe(FetchStatus.LOADING);
expect(value.timeRange).toBe(timeRange);
expect(value.timeRangeRelative).toBe(timeRangeRelative);
expect(value.query).toBe(query);
done();
}
});
sendFetchStartMsg(main$, timeRange, timeRangeRelative, query);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import type { BehaviorSubject } from 'rxjs';
import type { DataTableRecord } from '@kbn/discover-utils/src/types';
import type { SearchResponseWarning } from '@kbn/search-response-warnings';
import { AggregateQuery, Query, TimeRange } from '@kbn/es-query';
import { FetchStatus } from '../../types';
import type {
DataDocuments$,
Expand Down Expand Up @@ -37,15 +38,37 @@ export function sendCompleteMsg(main$: DataMain$, foundDocuments = true) {
if (main$.getValue().fetchStatus === FetchStatus.COMPLETE) {
return;
}
main$.next({ fetchStatus: FetchStatus.COMPLETE, foundDocuments, error: undefined });
main$.next({
...main$.getValue(),
fetchStatus: FetchStatus.COMPLETE,
foundDocuments,
error: undefined,
});
}

/**
* Send message when data fetching starts via main observable
*/
export function sendFetchStartMsg(
main$: DataMain$,
timeRange: TimeRange,
timeRangeRelative: TimeRange,
query: AggregateQuery | Query | undefined
) {
main$.next({
...main$.getValue(),
timeRange,
timeRangeRelative,
query,
});
}

/**
* Send PARTIAL message via main observable when first result is returned
*/
export function sendPartialMsg(main$: DataMain$) {
if (main$.getValue().fetchStatus === FetchStatus.LOADING) {
main$.next({ fetchStatus: FetchStatus.PARTIAL });
main$.next({ ...main$.getValue(), fetchStatus: FetchStatus.PARTIAL });
}
}

Expand All @@ -57,7 +80,7 @@ export function sendLoadingMsg<T extends DataMsg>(
props?: Omit<T, 'fetchStatus'>
) {
if (data$.getValue().fetchStatus !== FetchStatus.LOADING) {
data$.next({ ...props, fetchStatus: FetchStatus.LOADING } as T);
data$.next({ ...data$.getValue(), ...props, fetchStatus: FetchStatus.LOADING } as T);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { AutoRefreshDoneFn } from '@kbn/data-plugin/public';
import type { DatatableColumn } from '@kbn/expressions-plugin/common';
import { RequestAdapter } from '@kbn/inspector-plugin/common';
import type { SavedSearch } from '@kbn/saved-search-plugin/public';
import { AggregateQuery, isOfAggregateQueryType, Query } from '@kbn/es-query';
import { AggregateQuery, isOfAggregateQueryType, Query, TimeRange } from '@kbn/es-query';
import type { SearchResponse } from '@elastic/elasticsearch/lib/api/types';
import type { DataView } from '@kbn/data-views-plugin/common';
import { reportPerformanceMetricEvent } from '@kbn/ebt-tools';
Expand All @@ -26,7 +26,7 @@ import type { DiscoverSearchSessionManager } from './discover_search_session';
import { FetchStatus } from '../../types';
import { validateTimeRange } from './utils/validate_time_range';
import { fetchAll, fetchMoreDocuments } from '../data_fetching/fetch_all';
import { sendResetMsg } from '../hooks/use_saved_search_messages';
import { sendResetMsg, sendFetchStartMsg } from '../hooks/use_saved_search_messages';
import { getFetch$ } from '../data_fetching/get_fetch_observable';
import type { DiscoverInternalStateContainer } from './discover_internal_state_container';
import { getDefaultProfileState } from './utils/get_default_profile_state';
Expand All @@ -53,6 +53,8 @@ export interface DataMsg {

export interface DataMainMsg extends DataMsg {
foundDocuments?: boolean;
timeRange?: TimeRange;
timeRangeRelative?: TimeRange;
}

export interface DataDocumentsMsg extends DataMsg {
Expand Down Expand Up @@ -235,6 +237,12 @@ export function getDataStateContainer({

abortController?.abort();
abortControllerFetchMore?.abort();
sendFetchStartMsg(
dataSubjects.main$,
timefilter.getAbsoluteTime(),
timefilter.getTime(),
appStateContainer.getState().query
);

if (options.fetchMore) {
abortControllerFetchMore = new AbortController();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ export const useRequestParams = ({
query: originalQuery,
filters: originalFilters,
timeRange: originalTimeRange,
relativeTimeRange: originalRelativeTimeRange,
}: {
services: UnifiedHistogramServices;
query?: Query | AggregateQuery;
filters?: Filter[];
timeRange?: TimeRange;
relativeTimeRange?: TimeRange;
}): UseRequestParamsResult => {
const { data } = services;

Expand All @@ -42,14 +44,15 @@ export const useRequestParams = ({
);

const relativeTimeRange = useMemo(
() => originalTimeRange ?? data.query.timefilter.timefilter.getTimeDefaults(),
[data.query.timefilter.timefilter, originalTimeRange]
() => originalRelativeTimeRange ?? data.query.timefilter.timefilter.getTimeDefaults(),
[data.query.timefilter.timefilter, originalRelativeTimeRange]
);

const timeRange = useRef(getAbsoluteTimeRange(relativeTimeRange));
const timeRange = useRef(originalTimeRange ?? getAbsoluteTimeRange(relativeTimeRange));

const getTimeRange = useCallback(() => timeRange.current, []);
const updateTimeRange = useStableCallback(() => {
timeRange.current = getAbsoluteTimeRange(relativeTimeRange);
timeRange.current = originalTimeRange ?? getAbsoluteTimeRange(relativeTimeRange);
});

return { filters, query, getTimeRange, updateTimeRange, relativeTimeRange };
Expand Down
1 change: 1 addition & 0 deletions src/plugins/unified_histogram/public/layout/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ export const UnifiedHistogramLayout = ({
query: originalQuery,
filters: originalFilters,
timeRange: originalTimeRange,
relativeTimeRange,
});

const [lensVisService] = useState(() => new LensVisService({ services, lensSuggestionsApi }));
Expand Down