Skip to content

Commit

Permalink
ppl working
Browse files Browse the repository at this point in the history
Signed-off-by: Kawika Avilla <kavilla414@gmail.com>
  • Loading branch information
kavilla committed Jul 22, 2024
1 parent 369953b commit e7c4c55
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 82 deletions.
10 changes: 9 additions & 1 deletion src/plugins/data/common/search/search_source/search_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,12 @@ export class SearchSource {
const dataFrame = createDataFrame({
name: searchRequest.index.title || searchRequest.index,
fields: [],
...(rawQueryString && { meta: { queryConfig: parseRawQueryString(rawQueryString) } }),
...(rawQueryString && {
meta: {
queryConfig: parseRawQueryString(rawQueryString),
...(searchRequest.dataSourceId && { dataSource: searchRequest.dataSourceId }),
},
}),
});
await this.setDataFrame(dataFrame);
return this.getDataFrame();
Expand Down Expand Up @@ -353,6 +358,9 @@ export class SearchSource {
if (getConfig(UI_SETTINGS.COURIER_BATCH_SEARCHES)) {
response = await this.legacyFetch(searchRequest, options);
} else if (this.isUnsupportedRequest(searchRequest)) {
const dataSet = this.getField('index');
searchRequest.dataSourceId = dataSet?.dataSourceRef?.id;

response = await this.fetchExternalSearch(searchRequest, options);
} else {
const indexPattern = this.getField('index');
Expand Down
52 changes: 36 additions & 16 deletions src/plugins/data/public/ui/dataset_navigator/dataset_navigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import _ from 'lodash';
import { i18n } from '@osd/i18n';
import { fetchClusters } from './fetch_clusters';
import { fetchIndices } from './fetch_indices';
import { getIndexPatterns, getSearchService } from '../../services';
import { getIndexPatterns, getQueryService, getSearchService, getUiService } from '../../services';
import { fetchIndexPatterns } from './fetch_index_patterns';

export interface DataSetOption {
Expand All @@ -43,6 +43,7 @@ interface DataSetNavigatorState {
searchValue: string | undefined;
isOpen: boolean;
selectedDataSet: any;
selectedDataSetTitle: any;
selectedIndex?: any;
selectedDataSource?: any;
}
Expand All @@ -52,6 +53,8 @@ export default class DataSetNavigator extends Component<DataSetNavigatorProps> {
private isMounted: boolean = false;
state: DataSetNavigatorState;
private search = getSearchService();
private queryService = getQueryService();
private settings = getUiService().Settings;
private indexPatternsService = getIndexPatterns();

constructor(props: DataSetNavigatorProps) {
Expand All @@ -66,6 +69,7 @@ export default class DataSetNavigator extends Component<DataSetNavigatorProps> {
selectedDataSource: undefined,
selectedIndex: undefined,
selectedDataSet: undefined,
selectedDataSetTitle: undefined,
};

this.fetchIndexPatterns();
Expand Down Expand Up @@ -130,15 +134,27 @@ export default class DataSetNavigator extends Component<DataSetNavigatorProps> {
}
};

getQueryStringInitialValue(selectedDataSet: any) {
const language = this.settings.getUserQueryLanguage();
const input = this.settings.getQueryEnhancements(language)?.searchBar?.queryStringInput
?.initialValue;

if (!this.state.selectedDataSet || !input)
return {
query: '',
language,
};

return { query: input.replace('<data_source>', selectedDataSet), language };
}

handleDataSetChange = async (dataSet: any) => {
let selectedDataSet = dataSet;
const dataSetTitle =
typeof dataSet === 'string' ? dataSet : dataSet.name ?? dataSet.attributes.title;
const selectedDataSet = dataSet.dataSourceRef
? `${dataSet.dataSourceRefName}::${dataSetTitle}`
: dataSetTitle;

if (!dataSet.attributes?.title) {
const tempDataSet = await this.indexPatternsService.create(
if (!selectedDataSet.attributes?.title) {
selectedDataSet = await this.indexPatternsService.create(
{
id: dataSet.name,
title: dataSet.name,
Expand All @@ -151,10 +167,15 @@ export default class DataSetNavigator extends Component<DataSetNavigatorProps> {
},
true
);
await this.indexPatternsService.saveToCache(tempDataSet.title, tempDataSet);
await this.indexPatternsService.saveToCache(dataSet.name, selectedDataSet);
}
this.setState({ selectedDataSet });
this.props.onSelectDataSet(dataSet);
const selectedDataSetTitle = dataSet.dataSourceRef
? `${dataSet.dataSourceRefName}::${dataSetTitle}`
: dataSetTitle;
this.setState({ selectedDataSetTitle });
this.search.df.clear();
this.queryService.queryString.setQuery(this.getQueryStringInitialValue(dataSetTitle));
await this.props.onSelectDataSet(selectedDataSet);
this.closePopover();
};

Expand All @@ -171,6 +192,7 @@ export default class DataSetNavigator extends Component<DataSetNavigatorProps> {
isOpen,
indexPatterns,
selectedDataSet,
selectedDataSetTitle,
selectedDataSource,
dataSources,
externalDataSources,
Expand All @@ -193,9 +215,7 @@ export default class DataSetNavigator extends Component<DataSetNavigatorProps> {
iconSide="right"
onClick={this.onButtonClick}
>
{typeof selectedDataSet === 'string'
? selectedDataSet
: selectedDataSet?.attributes?.title ?? 'Select a DataSet'}
{selectedDataSetTitle ?? selectedDataSet?.attributes?.title}
</EuiButtonEmpty>
}
isOpen={isOpen}
Expand Down Expand Up @@ -230,8 +250,8 @@ export default class DataSetNavigator extends Component<DataSetNavigatorProps> {
items: indexPatterns!.flatMap((indexPattern: any, index: number, arr: any[]) => [
{
name: indexPattern.attributes.title,
onClick: () => {
this.handleDataSetChange(indexPattern);
onClick: async () => {
await this.handleDataSetChange(indexPattern);
},
},
...(index < arr.length - 1 ? [{ isSeparator: true }] : []),
Expand Down Expand Up @@ -329,8 +349,8 @@ export default class DataSetNavigator extends Component<DataSetNavigatorProps> {
<EuiButton
size="s"
fullWidth
onClick={() => {
this.handleDataSetChange(selectedIndex);
onClick={async () => {
await this.handleDataSetChange(selectedIndex);
}}
>
Select
Expand Down
15 changes: 9 additions & 6 deletions src/plugins/data/public/ui/query_editor/query_editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ export interface QueryEditorProps {
dataSource?: DataSource;
query: Query;
container?: HTMLDivElement;
dataSourceContainerRef?: React.RefCallback<HTMLDivElement>;
containerRef?: React.RefCallback<HTMLDivElement>;
languageSelectorContainerRef?: React.RefCallback<HTMLDivElement>;
dataSetContainerRef?: React.RefCallback<HTMLDivElement>;
setOnDataSetSelect?: (_: () => void) => void;
settings: Settings;
disableAutoFocus?: boolean;
screenTitle?: string;
Expand Down Expand Up @@ -78,6 +77,7 @@ interface State {
isSuggestionsVisible: boolean;
index: number | null;
suggestions: QuerySuggestion[];
selectedDataSet: any;
indexPatterns: IIndexPattern[];
isCollapsed: boolean;
timeStamp: IFieldType | null;
Expand Down Expand Up @@ -107,6 +107,7 @@ export default class QueryEditorUI extends Component<Props, State> {
index: null,
suggestions: [],
indexPatterns: [],
selectedDataSet: null,
isCollapsed: false, // default to expand mode
timeStamp: null,
lineCount: undefined,
Expand Down Expand Up @@ -144,8 +145,10 @@ export default class QueryEditorUI extends Component<Props, State> {
this.services.uiSettings!
)) as IIndexPattern[];

const indexPatterns = [...objectPatterns, ...objectPatternsFromStrings];
this.setState({
indexPatterns: [...objectPatterns, ...objectPatternsFromStrings],
indexPatterns,
selectedDataSet: indexPatterns[0],
});
};

Expand Down Expand Up @@ -214,7 +217,7 @@ export default class QueryEditorUI extends Component<Props, State> {
}
};

private onSelectDataSet = (dataSet: any) => {
private onSelectDataSet = () => {
const newQuery = {
query: this.props.getQueryStringInitialValue?.(this.props.query.language) ?? '',
language: this.props.query.language,
Expand Down Expand Up @@ -399,7 +402,7 @@ export default class QueryEditorUI extends Component<Props, State> {
/>
</EuiFlexItem>
<EuiFlexItem grow={2} className={`${className}__dataSetWrapper`}>
<div ref={this.props.containerRef} />
<div ref={this.props.dataSetContainerRef} />
</EuiFlexItem>
<EuiFlexItem grow={10}>
<EuiFlexGroup gutterSize="none">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const QueryEditor = withOpenSearchDashboards(QueryEditorUI);
// @internal
export interface QueryEditorTopRowProps {
query?: Query;
dataSourceContainerRef?: React.RefCallback<HTMLDivElement>;
containerRef?: React.RefCallback<HTMLDivElement>;
dataSetContainerRef?: React.RefCallback<HTMLDivElement>;
setOnDataSetSelect?: (_: () => void) => void;
settings?: Settings;
onSubmit: (payload: { dateRange: TimeRange; query?: Query }) => void;
onChange: (payload: { dateRange: TimeRange; query?: Query }) => void;
Expand Down Expand Up @@ -223,8 +223,8 @@ export default function QueryEditorTopRow(props: QueryEditorTopRowProps) {
dataSource={props.dataSource}
prepend={props.prepend}
query={parsedQuery}
dataSourceContainerRef={props.dataSourceContainerRef}
containerRef={props.containerRef}
dataSetContainerRef={props.dataSetContainerRef}
setOnDataSetSelect={props.setOnDataSetSelect}
settings={props.settings!}
screenTitle={props.screenTitle}
onChange={onQueryChange}
Expand Down
22 changes: 8 additions & 14 deletions src/plugins/data/public/ui/search_bar/create_search_bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ interface StatefulSearchBarDeps {
data: Omit<DataPublicPluginStart, 'ui'>;
storage: IStorageWrapper;
settings: Settings;
setDataSourceContainerRef: (ref: HTMLDivElement | null) => void;
setContainerRef: (ref: HTMLDivElement | null) => void;
setDataSetContainerRef: (ref: HTMLDivElement | null) => void;
setOnDataSetSelect?: (_: () => void) => void;
}

export type StatefulSearchBarProps = SearchBarOwnProps & {
Expand Down Expand Up @@ -139,8 +139,8 @@ export function createSearchBar({
storage,
data,
settings,
setDataSourceContainerRef,
setContainerRef,
setDataSetContainerRef,
setOnDataSetSelect,
}: StatefulSearchBarDeps) {
// App name should come from the core application service.
// Until it's available, we'll ask the user to provide it for the pre-wired component.
Expand Down Expand Up @@ -176,15 +176,9 @@ export function createSearchBar({
notifications: core.notifications,
});

const dataSourceContainerRef = useCallback((node) => {
const dataSetContainerRef = useCallback((node) => {
if (node) {
setDataSourceContainerRef(node);
}
}, []);

const containerRef = useCallback((node) => {
if (node) {
setContainerRef(node);
setDataSetContainerRef(node);
}
}, []);

Expand Down Expand Up @@ -228,8 +222,8 @@ export function createSearchBar({
filters={filters}
query={query}
settings={settings}
dataSourceContainerRef={dataSourceContainerRef}
containerRef={containerRef}
dataSetContainerRef={dataSetContainerRef}
setOnDataSetSelect={setOnDataSetSelect}
onFiltersUpdated={defaultFiltersUpdated(data.query)}
onRefreshChange={defaultOnRefreshChange(data.query)}
savedQuery={savedQuery}
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/data/public/ui/search_bar/search_bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export interface SearchBarOwnProps {
// Query bar - should be in SearchBarInjectedDeps
query?: Query;
settings?: Settings;
dataSourceContainerRef?: React.RefCallback<HTMLDivElement>;
containerRef?: React.RefCallback<HTMLDivElement>;
dataSetContainerRef?: React.RefCallback<HTMLDivElement>;
setOnDataSetSelect?: (_: () => void) => void;
// Show when user has privileges to save
showSaveQuery?: boolean;
savedQuery?: SavedQuery;
Expand Down Expand Up @@ -491,8 +491,8 @@ class SearchBarUI extends Component<SearchBarProps, State> {
queryEditor = (
<QueryEditorTopRow
timeHistory={this.props.timeHistory}
dataSourceContainerRef={this.props.dataSourceContainerRef}
containerRef={this.props.containerRef}
dataSetContainerRef={this.props.dataSetContainerRef}
setOnDataSetSelect={this.props.setOnDataSetSelect}
settings={this.props.settings}
query={this.state.query}
screenTitle={this.props.screenTitle}
Expand Down
9 changes: 9 additions & 0 deletions src/plugins/data/public/ui/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ export class Settings {
return true;
}

getUserQueryDataSet() {
return this.storage.get('opensearchDashboards.userQueryDataSet');
}

setUserQueryDataSet(dataSet: any) {
this.storage.set('opensearchDashboards.userQueryDataSet', dataSet);
return true;
}

getUiOverrides() {
return this.storage.get('opensearchDashboards.uiOverrides') || {};
}
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/data/public/ui/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@ export interface IUiStart {
*/
Settings: Settings;
DataSetNavigator: React.ComponentType<DataSetNavigatorProps>;
container$: Observable<HTMLDivElement | null>;
dataSetContainer$: Observable<HTMLDivElement | null>;
onSelectDataSet?: () => void | undefined;
}
20 changes: 10 additions & 10 deletions src/plugins/data/public/ui/ui_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ export class UiService implements Plugin<IUiSetup, IUiStart> {
enhancementsConfig: ConfigSchema['enhancements'];
private queryEnhancements: Map<string, QueryEnhancement> = new Map();
private queryEditorExtensionMap: Record<string, QueryEditorExtensionConfig> = {};
private dataSourceContainer$ = new BehaviorSubject<HTMLDivElement | null>(null);
private container$ = new BehaviorSubject<HTMLDivElement | null>(null);

private dataSetContainer$ = new BehaviorSubject<HTMLDivElement | null>(null);
private onSelectDataSet?: () => void;
constructor(initializerContext: PluginInitializerContext<ConfigSchema>) {
const { enhancements } = initializerContext.config.get<ConfigSchema>();

Expand Down Expand Up @@ -63,21 +62,21 @@ export class UiService implements Plugin<IUiSetup, IUiStart> {
queryEditorExtensionMap: this.queryEditorExtensionMap,
});

const setDataSourceContainerRef = (ref: HTMLDivElement | null) => {
this.dataSourceContainer$.next(ref);
const setDataSetContainerRef = (ref: HTMLDivElement | null) => {
this.dataSetContainer$.next(ref);
};

const setContainerRef = (ref: HTMLDivElement | null) => {
this.container$.next(ref);
const setOnDataSetSelect = (onSelectDataSet: () => void) => {
this.onSelectDataSet = onSelectDataSet;
};

const SearchBar = createSearchBar({
core,
data: dataServices,
storage,
settings: Settings,
setDataSourceContainerRef,
setContainerRef,
setDataSetContainerRef,
setOnDataSetSelect,
});

return {
Expand All @@ -86,7 +85,8 @@ export class UiService implements Plugin<IUiSetup, IUiStart> {
SearchBar,
SuggestionsComponent,
Settings,
container$: this.container$,
dataSetContainer$: this.dataSetContainer$,
onSelectDataSet: this.onSelectDataSet,
};
}

Expand Down
Loading

0 comments on commit e7c4c55

Please sign in to comment.