Skip to content

Commit

Permalink
need to address time range still
Browse files Browse the repository at this point in the history
Signed-off-by: Kawika Avilla <[email protected]>
  • Loading branch information
kavilla committed Apr 25, 2024
1 parent 96b721b commit 575fcd8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/plugins/data/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const UI_SETTINGS = {
DOC_HIGHLIGHT: 'doc_table:highlight',
QUERY_STRING_OPTIONS: 'query:queryString:options',
QUERY_ALLOW_LEADING_WILDCARDS: 'query:allowLeadingWildcards',
QUERY_DATA_SOURCE_READONLY: 'query:dataSourceReadOnly',
SEARCH_QUERY_LANGUAGE: 'search:queryLanguage',
SORT_OPTIONS: 'sort:options',
COURIER_IGNORE_FILTER_IF_FIELD_NOT_IN_INDEX: 'courier:ignoreFilterIfFieldNotInIndex',
Expand Down
63 changes: 53 additions & 10 deletions src/plugins/data/public/ui/query_string_input/query_bar_top_row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
import { EuiSuperUpdateButton, OnRefreshProps } from '@elastic/eui';
import { FormattedMessage } from '@osd/i18n/react';
import { Toast } from 'src/core/public';
import { isEqual, compact } from 'lodash';
import { IDataPluginServices, IIndexPattern, TimeRange, TimeHistoryContract, Query } from '../..';
import {
useOpenSearchDashboards,
Expand All @@ -54,7 +55,7 @@ import {
} from '../../../../opensearch_dashboards_react/public';
import QueryStringInputUI from './query_string_input';
import { doesKueryExpressionHaveLuceneSyntaxError, UI_SETTINGS } from '../../../common';
import { PersistedLog, getQueryLog } from '../../query';
import { PersistedLog, fromUser, getQueryLog } from '../../query';
import { NoDataPopover } from './no_data_popover';
import { QueryEnhancement, Settings } from '../types';

Expand Down Expand Up @@ -98,13 +99,21 @@ export default function QueryBarTopRow(props: QueryBarTopRowProps) {
const { uiSettings, notifications, storage, appName, docLinks } = opensearchDashboards.services;

const osdDQLDocs: string = docLinks!.links.opensearchDashboards.dql.base;
const isDataSourceReadOnly = uiSettings.get('query:dataSourceReadOnly');

const queryLanguage = props.query && props.query.language;
const queryUiEnhancement =
(queryLanguage &&
props.queryEnhancements &&
props.queryEnhancements.get(queryLanguage)?.searchBar) ||
null;
const parsedQuery = isValidQuery(props.query)
? props.query!
: { query: getQueryStringInitialValue(queryLanguage!), language: queryLanguage! };
if (!isEqual(parsedQuery.query, props.query?.query)) {
onQueryChange(parsedQuery);
onSubmit({ query: parsedQuery, dateRange: getDateRange() });

Check warning on line 115 in src/plugins/data/public/ui/query_string_input/query_bar_top_row.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/query_string_input/query_bar_top_row.tsx#L114-L115

Added lines #L114 - L115 were not covered by tests
}
const persistedLog: PersistedLog | undefined = React.useMemo(
() =>
queryLanguage && uiSettings && storage && appName
Expand All @@ -125,17 +134,18 @@ export default function QueryBarTopRow(props: QueryBarTopRowProps) {
const defaultTimeSetting = uiSettings!.get(UI_SETTINGS.TIMEPICKER_TIME_DEFAULTS);
return {
from:
queryUiEnhancement?.dateRange?.initialFrom ||
props.dateRangeFrom ||
queryUiEnhancement?.dateRange?.initialFrom ||
defaultTimeSetting.from,
to: queryUiEnhancement?.dateRange?.initialTo || props.dateRangeTo || defaultTimeSetting.to,
to: props.dateRangeTo || queryUiEnhancement?.dateRange?.initialTo || defaultTimeSetting.to,
};
}

function onQueryChange(query: Query) {
function onQueryChange(query: Query, dateRange?: TimeRange) {
if (!isValidQuery(query)) return;
props.onChange({
query,
dateRange: getDateRange(),
dateRange: dateRange ?? getDateRange(),
});
}

Expand Down Expand Up @@ -192,10 +202,10 @@ export default function QueryBarTopRow(props: QueryBarTopRowProps) {
props.onSubmit({ query, dateRange });
}

function onInputSubmit(query: Query) {
function onInputSubmit(query: Query, dateRange?: TimeRange) {
onSubmit({
query,
dateRange: getDateRange(),
dateRange: dateRange ?? getDateRange(),
});
}

Expand All @@ -207,6 +217,38 @@ export default function QueryBarTopRow(props: QueryBarTopRowProps) {
return valueAsMoment.toISOString();
}

function isValidQuery(query: Query | undefined) {
if (!query || !query.query) return false;
return (

Check warning on line 222 in src/plugins/data/public/ui/query_string_input/query_bar_top_row.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/query_string_input/query_bar_top_row.tsx#L222

Added line #L222 was not covered by tests
!Array.isArray(props.indexPatterns!) ||
compact(props.indexPatterns!).length === 0 ||
!isDataSourceReadOnly ||
fromUser(query!.query).includes(
typeof props.indexPatterns[0] === 'string'
? props.indexPatterns[0]
: props.indexPatterns[0].title
)
);
}

function getQueryStringInitialValue(language: string) {
const { indexPatterns, queryEnhancements } = props;
const input = queryEnhancements?.get(language)?.searchBar?.queryStringInput?.initialValue;

Check warning on line 236 in src/plugins/data/public/ui/query_string_input/query_bar_top_row.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/query_string_input/query_bar_top_row.tsx#L235-L236

Added lines #L235 - L236 were not covered by tests

if (
!indexPatterns ||
(!Array.isArray(indexPatterns) && compact(indexPatterns).length > 0) ||
!input
)
return '';

Check warning on line 243 in src/plugins/data/public/ui/query_string_input/query_bar_top_row.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/query_string_input/query_bar_top_row.tsx#L243

Added line #L243 was not covered by tests

const defaultDataSource = indexPatterns[0];

Check warning on line 245 in src/plugins/data/public/ui/query_string_input/query_bar_top_row.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/query_string_input/query_bar_top_row.tsx#L245

Added line #L245 was not covered by tests
const dataSource =
typeof defaultDataSource === 'string' ? defaultDataSource : defaultDataSource.title;

return input.replace('<data_source>', dataSource);

Check warning on line 249 in src/plugins/data/public/ui/query_string_input/query_bar_top_row.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/query_string_input/query_bar_top_row.tsx#L249

Added line #L249 was not covered by tests
}

function renderQueryInput() {
if (!shouldRenderQueryInput()) return;
return (
Expand All @@ -215,13 +257,14 @@ export default function QueryBarTopRow(props: QueryBarTopRowProps) {
disableAutoFocus={props.disableAutoFocus}
indexPatterns={props.indexPatterns!}
prepend={props.prepend}
query={props.query!}
query={parsedQuery}
queryEnhancements={props.queryEnhancements}
settings={props.settings}
screenTitle={props.screenTitle}
onChange={onQueryChange}
onChangeQueryInputFocus={onChangeQueryInputFocus}
onSubmit={onInputSubmit}
getQueryStringInitialValue={getQueryStringInitialValue}
persistedLog={persistedLog}
dataTestSubj={props.dataTestSubj}
/>
Expand Down Expand Up @@ -320,8 +363,8 @@ export default function QueryBarTopRow(props: QueryBarTopRowProps) {
return (
<EuiFlexItem className={wrapperClasses}>
<EuiSuperDatePicker
start={queryUiEnhancement?.dateRange?.initialFrom ?? props.dateRangeFrom}
end={queryUiEnhancement?.dateRange?.initialTo ?? props.dateRangeTo}
start={props.dateRangeFrom}
end={props.dateRangeTo}
isPaused={props.isRefreshPaused}
refreshInterval={props.refreshInterval}
onTimeChange={onTimeChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {
import { FormattedMessage } from '@osd/i18n/react';
import { debounce, compact, isEqual, isFunction } from 'lodash';
import { Toast } from 'src/core/public';
import { IDataPluginServices, IIndexPattern, Query } from '../..';
import { IDataPluginServices, IIndexPattern, Query, TimeRange } from '../..';
import { QuerySuggestion, QuerySuggestionTypes } from '../../autocomplete';

import {
Expand All @@ -74,9 +74,10 @@ export interface QueryStringInputProps {
placeholder?: string;
languageSwitcherPopoverAnchorPosition?: PopoverAnchorPosition;
onBlur?: () => void;
onChange?: (query: Query) => void;
onChange?: (query: Query, dateRange?: TimeRange) => void;
onChangeQueryInputFocus?: (isFocused: boolean) => void;
onSubmit?: (query: Query) => void;
onSubmit?: (query: Query, dateRange?: TimeRange) => void;
getQueryStringInitialValue?: (language: string) => string;
dataTestSubj?: string;
size?: SuggestionsListSize;
className?: string;
Expand Down Expand Up @@ -133,20 +134,9 @@ export default class QueryStringInputUI extends Component<Props, State> {
private componentIsUnmounting = false;
private queryBarInputDivRefInstance: RefObject<HTMLDivElement> = createRef();

private getQueryStringInitialValue = (language: string) => {
const input = this.props.queryEnhancements?.get(language)?.searchBar?.queryStringInput;
// TODO: SQL replace with data_source length
if (!input || !input.initialValue || this.state.indexPatterns.length === 0) return '';
const defaultDataSource = this.state.indexPatterns[0];
return input.initialValue.replace(
'<data_source>',
typeof defaultDataSource === 'string' ? defaultDataSource : defaultDataSource.title
);
};

private getQueryString = () => {
if (!this.props.query.query) {
return this.getQueryStringInitialValue(this.props.query.language);
return this.props.getQueryStringInitialValue?.(this.props.query.language) ?? '';
}
return toUser(this.props.query.query);
};
Expand Down Expand Up @@ -243,7 +233,7 @@ export default class QueryStringInputUI extends Component<Props, State> {
}
}, 100);

private onSubmit = (query: Query) => {
private onSubmit = (query: Query, dateRange?: TimeRange) => {
if (this.props.onSubmit) {
if (this.persistedLog) {
this.persistedLog.add(query.query);
Expand All @@ -253,11 +243,11 @@ export default class QueryStringInputUI extends Component<Props, State> {
}
};

private onChange = (query: Query) => {
private onChange = (query: Query, dateRange?: TimeRange) => {
this.updateSuggestions();

if (this.props.onChange) {
this.props.onChange({ query: fromUser(query.query), language: query.language });
this.props.onChange({ query: fromUser(query.query), language: query.language }, dateRange);

Check warning on line 250 in src/plugins/data/public/ui/query_string_input/query_string_input.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/query_string_input/query_string_input.tsx#L250

Added line #L250 was not covered by tests
}
};

Expand Down Expand Up @@ -486,7 +476,7 @@ export default class QueryStringInputUI extends Component<Props, State> {
});

const newQuery = {

Check warning on line 478 in src/plugins/data/public/ui/query_string_input/query_string_input.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/query_string_input/query_string_input.tsx#L478

Added line #L478 was not covered by tests
query: this.getQueryStringInitialValue(language),
query: this.props.getQueryStringInitialValue?.(language) ?? '',
language,
};
this.props.settings?.updateSettings({

Check warning on line 482 in src/plugins/data/public/ui/query_string_input/query_string_input.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/query_string_input/query_string_input.tsx#L482

Added line #L482 was not covered by tests
Expand All @@ -496,8 +486,15 @@ export default class QueryStringInputUI extends Component<Props, State> {
fields: this.props.queryEnhancements?.get(language)?.fields,
},
});
this.onChange(newQuery);
this.onSubmit(newQuery);
const dateRangeEnhancement = this.props.queryEnhancements?.get(language)?.searchBar?.dateRange;

Check warning on line 489 in src/plugins/data/public/ui/query_string_input/query_string_input.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/query_string_input/query_string_input.tsx#L489

Added line #L489 was not covered by tests
const dateRange = dateRangeEnhancement
? {
from: dateRangeEnhancement.initialFrom!,
to: dateRangeEnhancement.initialTo!,
}
: undefined;
this.onChange(newQuery, dateRange);
this.onSubmit(newQuery, dateRange);

Check warning on line 497 in src/plugins/data/public/ui/query_string_input/query_string_input.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/query_string_input/query_string_input.tsx#L496-L497

Added lines #L496 - L497 were not covered by tests
};

private onOutsideClick = () => {
Expand Down
13 changes: 13 additions & 0 deletions src/plugins/data/server/ui_settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,5 +736,18 @@ export function getUiSettings(): Record<string, UiSettingsParams<unknown>> {
category: ['search'],
schema: schema.string(),
},
[UI_SETTINGS.QUERY_DATA_SOURCE_READONLY]: {
name: i18n.translate('data.advancedSettings.query.dataSourceReadOnlyTitle', {
defaultMessage: 'Data source is read-only in query',
}),
value: true,
description: i18n.translate('data.advancedSettings.query.dataSourceReadOnlyText', {
defaultMessage:
'When set, modifying the data source is not allowed in a query clause. ' +
'This will throw an error on data source changed, and reset the data source on submit. ' +
'<br><strong>Experimental</strong>: Setting to false enables data source changes.',
}),
schema: schema.boolean(),
},
};
}

0 comments on commit 575fcd8

Please sign in to comment.