Skip to content

Commit

Permalink
Initial changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal committed May 26, 2023
1 parent 42e380f commit ee4a3c7
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ function DiscoverDocumentsComponent({
// 5. this is propagated to Discover's URL and causes an unwanted change of state to an unsorted state
// This solution switches to the loading state in this component when the URL index doesn't match the dataView.id
const isDataViewLoading = index && dataView.id && index !== dataView.id;
console.log({index, id: dataView.id, isDataViewLoading});
const isEmptyDataResult = !documentState.result || documentState.result.length === 0;
const isPlainRecord = useMemo(() => getRawRecordType(query) === RecordRawType.PLAIN, [query]);
const rows = useMemo(() => documentState.result || [], [documentState.result]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const renderHookWithContext = (
appState?: DiscoverAppState
) => {
const props = getHookProps(query, useDataViewsService ? getDataViewsService() : undefined);
props.stateContainer.actions.setDataView(dataViewMock);
if (appState) {
props.stateContainer.appState.getState = jest.fn(() => {
return appState;
Expand Down Expand Up @@ -284,7 +285,7 @@ describe('useTextBasedQueryLanguage', () => {
fetchStatus: FetchStatus.LOADING,
query: { sql: 'SELECT * from the-data-view-title WHERE field1=2' },
});
await waitFor(() => expect(replaceUrlState).toHaveBeenCalledTimes(0));
await waitFor(() => expect(replaceUrlState).toHaveBeenCalledTimes(1));
documents$.next({
recordRawType: RecordRawType.PLAIN,
fetchStatus: FetchStatus.COMPLETE,
Expand All @@ -297,7 +298,7 @@ describe('useTextBasedQueryLanguage', () => {
],
query: { sql: 'SELECT * from the-data-view-title WHERE field1=2' },
});
await waitFor(() => expect(replaceUrlState).toHaveBeenCalledTimes(1));
await waitFor(() => expect(replaceUrlState).toHaveBeenCalledTimes(2));
stateContainer.appState.getState = jest.fn(() => {
return { columns: ['field1', 'field2'], index: 'the-data-view-id' };
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,14 @@ export function useTextBasedQueryLanguage({
}
const indexPatternFromQuery = getIndexPatternFromSQLQuery(query.sql);

const dataViewObj = await dataViews.create({
title: indexPatternFromQuery,
});
stateContainer.internalState.transitions.setAdHocDataViews([dataViewObj]);

if (dataViewObj.fields.getByName('@timestamp')?.type === 'date') {
dataViewObj.timeFieldName = '@timestamp';
}
const dataViewObj = stateContainer.internalState.getState().dataView!;

// don't set the columns on initial fetch, to prevent overwriting existing state
const addColumnsToState = Boolean(
nextColumns.length && (!initialFetch || !stateColumns?.length)
);
// no need to reset index to state if it hasn't changed
const addDataViewToState = Boolean(dataViewObj.id !== index);
const addDataViewToState = Boolean(dataViewObj?.id !== index) || initialFetch;
const queryChanged = indexPatternFromQuery !== indexTitle.current;
if (!addColumnsToState && !queryChanged) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import { AutoRefreshDoneFn } from '@kbn/data-plugin/public';
import type { DatatableColumn } from '@kbn/expressions-plugin/common';
import { RequestAdapter } from '@kbn/inspector-plugin/common';
import { SavedSearch } from '@kbn/saved-search-plugin/public';
import { AggregateQuery, Query } from '@kbn/es-query';
import { AggregateQuery, getIndexPatternFromSQLQuery, Query } from '@kbn/es-query';
import type { SearchResponse } from '@elastic/elasticsearch/lib/api/types';
import { DataView } from '@kbn/data-views-plugin/common';
import { isTextBasedQuery } from '../utils/is_text_based_query';
import { getRawRecordType } from '../utils/get_raw_record_type';
import { DiscoverAppState } from './discover_app_state_container';
import { DiscoverServices } from '../../../build_services';
Expand Down Expand Up @@ -129,11 +131,13 @@ export function getDataStateContainer({
searchSessionManager,
getAppState,
getSavedSearch,
setDataView,
}: {
services: DiscoverServices;
searchSessionManager: DiscoverSearchSessionManager;
getAppState: () => DiscoverAppState;
getSavedSearch: () => SavedSearch;
setDataView: (dataView: DataView) => void;
}): DiscoverDataStateContainer {
const { data, uiSettings, toastNotifications } = services;
const { timefilter } = data.query.timefilter;
Expand Down Expand Up @@ -226,7 +230,24 @@ export function getDataStateContainer({
};
}

const fetchQuery = (resetQuery?: boolean) => {
const fetchQuery = async (resetQuery?: boolean) => {
const query = getAppState().query;
const currentDataView = getSavedSearch().searchSource.getField('index');

if (query && isTextBasedQuery(query)) {
const indexPatternFromQuery = getIndexPatternFromSQLQuery(query.sql);
if (indexPatternFromQuery !== currentDataView?.getIndexPattern()) {
const dataViewObj = await services.dataViews.create({
title: indexPatternFromQuery,
});

if (dataViewObj.fields.getByName('@timestamp')?.type === 'date') {
dataViewObj.timeFieldName = '@timestamp';
}
setDataView(dataViewObj);
}
}

if (resetQuery) {
refetch$.next('reset');
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,6 @@ export function getDiscoverStateContainer({
*/
const internalStateContainer = getInternalStateContainer();

const dataStateContainer = getDataStateContainer({
services,
searchSessionManager,
getAppState: appStateContainer.getState,
getSavedSearch: savedSearchContainer.getState,
});

const pauseAutoRefreshInterval = async (dataView: DataView) => {
if (dataView && (!dataView.isTimeBased() || dataView.type === DataViewType.ROLLUP)) {
const state = stateStorage.get<QueryState>(GLOBAL_STATE_URL_KEY);
Expand All @@ -247,13 +240,20 @@ export function getDiscoverStateContainer({
}
}
};

const setDataView = (dataView: DataView) => {
internalStateContainer.transitions.setDataView(dataView);
pauseAutoRefreshInterval(dataView);
savedSearchContainer.getState().searchSource.setField('index', dataView);
};

const dataStateContainer = getDataStateContainer({
services,
searchSessionManager,
getAppState: appStateContainer.getState,
getSavedSearch: savedSearchContainer.getState,
setDataView,
});

const loadDataViewList = async () => {
const dataViewList = await services.dataViews.getIdsWithTitle(true);
internalStateContainer.transitions.setSavedDataViews(dataViewList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
import type { SavedSearch } from '@kbn/saved-search-plugin/public';
import { cloneDeep, isEqual } from 'lodash';
import { getIndexPatternFromSQLQuery } from '@kbn/es-query';
import { isTextBasedQuery } from '../utils/is_text_based_query';
import { loadAndResolveDataView } from '../utils/resolve_data_view';
import { DiscoverInternalStateContainer } from './discover_internal_state_container';
Expand Down Expand Up @@ -151,6 +152,22 @@ const getStateDataView = async (
if (dataView) {
return dataView;
}
const query = appState?.query;

if (query && isTextBasedQuery(query)) {
const indexPatternFromQuery = getIndexPatternFromSQLQuery(query.sql);
if (!dataView || indexPatternFromQuery !== dataView.title) {
const dataViewObj = await services.dataViews.create({
id: 'textBasedIdxPattern',
title: indexPatternFromQuery,
});

if (dataViewObj.fields.getByName('@timestamp')?.type === 'date') {
dataViewObj.timeFieldName = '@timestamp';
}
return dataViewObj;
}
}

const result = await loadAndResolveDataView(
{
Expand Down

0 comments on commit ee4a3c7

Please sign in to comment.