diff --git a/common/utils/index.ts b/common/utils/index.ts index 91caccf22d..524118c18b 100644 --- a/common/utils/index.ts +++ b/common/utils/index.ts @@ -7,6 +7,7 @@ export { getIndexPatternFromRawQuery, preprocessQuery, buildQuery, + buildRawQuery, composeFinalQuery, removeBacktick, } from './query_utils'; diff --git a/common/utils/query_utils.ts b/common/utils/query_utils.ts index 81853112cc..414cf21d56 100644 --- a/common/utils/query_utils.ts +++ b/common/utils/query_utils.ts @@ -123,6 +123,13 @@ export const buildQuery = (baseQuery: string, currQuery: string) => { return fullQuery; }; +export const buildRawQuery = (query: any, appBaseQuery: string) => { + const rawQueryStr = (query.rawQuery as string).includes(appBaseQuery) + ? query.rawQuery + : buildQuery(appBaseQuery, query.rawQuery); + return rawQueryStr; +}; + export const composeFinalQuery = ( curQuery: string, startingTime: string, diff --git a/public/components/event_analytics/explorer/explorer.tsx b/public/components/event_analytics/explorer/explorer.tsx index e75652f600..e29c8c47be 100644 --- a/public/components/event_analytics/explorer/explorer.tsx +++ b/public/components/event_analytics/explorer/explorer.tsx @@ -260,9 +260,6 @@ export const Explorer = ({ const fetchData = async (startingTime?: string, endingTime?: string) => { const curQuery: IQuery = queryRef.current!; - const rawQueryStr = (curQuery![RAW_QUERY] as string).includes(appBaseQuery) - ? curQuery![RAW_QUERY] - : buildQuery(appBasedRef.current, curQuery![RAW_QUERY]); new PPLDataFetcher( { ...curQuery }, { batch, dispatch, changeQuery, changeVizConfig }, diff --git a/public/services/data_fetchers/ppl/ppl_data_fetcher.ts b/public/services/data_fetchers/ppl/ppl_data_fetcher.ts index e621910c5d..592d8ab2d0 100644 --- a/public/services/data_fetchers/ppl/ppl_data_fetcher.ts +++ b/public/services/data_fetchers/ppl/ppl_data_fetcher.ts @@ -7,7 +7,11 @@ import { isEmpty } from 'lodash'; import { IDefaultTimestampState, IQuery } from '../../../../common/types/explorer'; import { IDataFetcher } from '../fetch_interface'; import { DataFetcherBase } from '../fetcher_base'; -import { composeFinalQuery, getIndexPatternFromRawQuery } from '../../../../common/utils'; +import { + buildRawQuery, + composeFinalQuery, + getIndexPatternFromRawQuery, +} from '../../../../common/utils'; import { FILTERED_PATTERN, PATTERNS_REGEX, @@ -31,8 +35,6 @@ export class PPLDataFetcher extends DataFetcherBase implements IDataFetcher { protected readonly notifications ) { super(); - // index/index patterns for this search - this.queryIndex = this.getIndex(this.searchParams.query.rawQuery); } async setTimestamp(index: string) { @@ -65,6 +67,10 @@ export class PPLDataFetcher extends DataFetcherBase implements IDataFetcher { if (isEmpty(query)) return; + this.queryIndex = this.getIndex(buildRawQuery(query, appBaseQuery)); + + if (this.queryIndex === '') return; // Returns if page is refreshed + const { tabId, findAutoInterval, @@ -77,7 +83,7 @@ export class PPLDataFetcher extends DataFetcherBase implements IDataFetcher { } = this.searchContext; const { dispatch, changeQuery } = this.storeContext; - await this.processTimestamp(query); + await this.processTimestamp(query, appBaseQuery); if (isEmpty(this.timestamp)) return; const curStartTime = startingTime || this.query[SELECTED_DATE_RANGE][0]; @@ -97,7 +103,7 @@ export class PPLDataFetcher extends DataFetcherBase implements IDataFetcher { ); // update UI with new query state - await this.updateQueryState(this.query[RAW_QUERY], finalQuery, this.timestamp); + await this.updateQueryState(this.query[RAW_QUERY], finalQuery, this.timestamp, appBaseQuery); // calculate proper time interval for count distribution if (!selectedInterval.current || selectedInterval.current.text === 'Auto') { findAutoInterval(curStartTime, curEndTime); @@ -152,8 +158,8 @@ export class PPLDataFetcher extends DataFetcherBase implements IDataFetcher { } } - async processTimestamp(query: IQuery) { - if (query[SELECTED_TIMESTAMP]) { + async processTimestamp(query: IQuery, appBaseQuery: string) { + if (query[SELECTED_TIMESTAMP] && appBaseQuery === '') { this.timestamp = query[SELECTED_TIMESTAMP]; } else { await this.setTimestamp(this.queryIndex); @@ -169,7 +175,12 @@ export class PPLDataFetcher extends DataFetcherBase implements IDataFetcher { return await timestampUtils.getTimestamp(indexPattern); } - async updateQueryState(rawQuery: string, finalQuery: string, curTimestamp: string) { + async updateQueryState( + rawQuery: string, + finalQuery: string, + curTimestamp: string, + appBaseQuery: string + ) { const { batch, dispatch, changeQuery, changeVizConfig } = this.storeContext; const { query } = this.searchParams; const { @@ -186,7 +197,7 @@ export class PPLDataFetcher extends DataFetcherBase implements IDataFetcher { tabId, query: { finalQuery, - [RAW_QUERY]: query.rawQuery, + [RAW_QUERY]: buildRawQuery(query, appBaseQuery), [SELECTED_TIMESTAMP]: curTimestamp, }, })