Skip to content

Commit

Permalink
use updated dataset for index pattern
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Sebastian <[email protected]>
  • Loading branch information
paulstn committed Jul 25, 2024
1 parent 1ac301a commit 1d9de39
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 36 deletions.
26 changes: 12 additions & 14 deletions src/plugins/data/public/antlr/dql/code_completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ const findCursorIndex = (
return undefined;

Check warning on line 36 in src/plugins/data/public/antlr/dql/code_completion.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/dql/code_completion.ts#L36

Added line #L36 was not covered by tests
};

const findFieldSuggestions = (indexPattern: IndexPattern) => {
const findFieldSuggestions = (indexPattern: any) => {
const fieldNames: string[] = indexPattern.fields
.filter((idxField: IndexPatternField) => !idxField.subType) // filter removed .keyword fields
.map((idxField: { displayName: string }) => {
return idxField.displayName;
.filter((idxField: IndexPatternField) => !idxField?.subType) // filter removed .keyword fields

Check warning on line 41 in src/plugins/data/public/antlr/dql/code_completion.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/dql/code_completion.ts#L40-L41

Added lines #L40 - L41 were not covered by tests
.map((idxField: { name: string }) => {
return idxField.name;

Check warning on line 43 in src/plugins/data/public/antlr/dql/code_completion.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/dql/code_completion.ts#L43

Added line #L43 was not covered by tests
});

const fieldSuggestions: Array<{ text: string; type: string }> = fieldNames.map(

Check warning on line 46 in src/plugins/data/public/antlr/dql/code_completion.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/dql/code_completion.ts#L46

Added line #L46 was not covered by tests
Expand All @@ -65,22 +65,17 @@ const getFieldSuggestedValues = async (
});
};

const findValueSuggestions = async (
index: IndexPattern,
field: string,
value: string,
http?: HttpSetup
) => {
const findValueSuggestions = async (index: any, field: string, value: string, http?: HttpSetup) => {
// check to see if last field is within index and if it can suggest values, first check
// if .keyword appended field exists because that has values
const matchedField =
index.fields.find((idxField: IndexPatternField) => {
// check to see if the field matches another field with .keyword appended
if (idxField.displayName === `${field}.keyword`) return idxField;
if (idxField.name === `${field}.keyword`) return idxField;
}) ||
index.fields.find((idxField: IndexPatternField) => {
// if the display name matches, return
if (idxField.displayName === field) return idxField;
if (idxField.name === field) return idxField;
});

if (matchedField?.type === 'boolean') {
Expand All @@ -90,7 +85,7 @@ const findValueSuggestions = async (
if (!matchedField || !matchedField.aggregatable || matchedField.type !== 'string') return;

// ask api for suggestions
return await getFieldSuggestedValues(index.title, matchedField.displayName, value, http);
return await getFieldSuggestedValues(index.title, matchedField.name, value, http);

Check warning on line 88 in src/plugins/data/public/antlr/dql/code_completion.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/dql/code_completion.ts#L88

Added line #L88 was not covered by tests
};

// visitor for parsing the current query
Expand Down Expand Up @@ -122,8 +117,11 @@ export const getSuggestions = async ({
selectionEnd,
core: coreSetup,
}: QuerySuggestionGetFnArgs) => {
console.log('indexpa', indexPatterns);

Check failure on line 120 in src/plugins/data/public/antlr/dql/code_completion.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Unexpected console statement

Check failure on line 120 in src/plugins/data/public/antlr/dql/code_completion.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Unexpected console statement

Check warning on line 120 in src/plugins/data/public/antlr/dql/code_completion.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/dql/code_completion.ts#L120

Added line #L120 was not covered by tests

const http = coreSetup?.http;
const currentIndexPattern = indexPatterns[0] as IndexPattern;
const currentIndexPattern = indexPatterns[0];
console.log('curr', currentIndexPattern);

Check failure on line 124 in src/plugins/data/public/antlr/dql/code_completion.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Unexpected console statement

Check failure on line 124 in src/plugins/data/public/antlr/dql/code_completion.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Unexpected console statement

Check warning on line 124 in src/plugins/data/public/antlr/dql/code_completion.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/dql/code_completion.ts#L122-L124

Added lines #L122 - L124 were not covered by tests

const inputStream = CharStream.fromString(query);
const lexer = new DQLLexer(inputStream);
Expand Down
52 changes: 30 additions & 22 deletions src/plugins/data/public/ui/query_editor/query_editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import { QuerySuggestion } from '../../autocomplete';
import { fromUser, getQueryLog, PersistedLog, toUser } from '../../query';
import { SuggestionsListSize } from '../typeahead/suggestions_component';
import { DataSettings } from '../types';
import { fetchIndexPatterns } from './fetch_index_patterns';
import { QueryLanguageSelector } from './language_selector';
import { QueryEditorExtensions } from './query_editor_extensions';
import { QueryEditorBtnCollapse } from './query_editor_btn_collapse';
import { getQueryService } from '../../services';
import { SIMPLE_DATA_SET_TYPES } from '../../../common';

const LANGUAGE_ID_SQL = 'SQL';
monaco.languages.register({ id: LANGUAGE_ID_SQL });
Expand Down Expand Up @@ -107,6 +108,8 @@ export default class QueryEditorUI extends Component<Props, State> {

public inputRef: monaco.editor.IStandaloneCodeEditor | null = null;

private queryService = getQueryService();

private persistedLog: PersistedLog | undefined;
private abortController?: AbortController;
private services = this.props.opensearchDashboards.services;
Expand All @@ -121,26 +124,6 @@ export default class QueryEditorUI extends Component<Props, State> {
return toUser(this.props.query.query);
};

// TODO: MQL don't do this here? || Fetch data sources
private fetchIndexPatterns = async () => {
const stringPatterns = this.props.indexPatterns.filter(
(indexPattern) => typeof indexPattern === 'string'
) as string[];
const objectPatterns = this.props.indexPatterns.filter(
(indexPattern) => typeof indexPattern !== 'string'
) as IIndexPattern[];

const objectPatternsFromStrings = (await fetchIndexPatterns(
this.services.savedObjects!.client,
stringPatterns,
this.services.uiSettings!
)) as IIndexPattern[];

this.setState({
indexPatterns: [...objectPatterns, ...objectPatternsFromStrings],
});
};

private renderQueryEditorExtensions() {
if (
!(
Expand Down Expand Up @@ -305,18 +288,43 @@ export default class QueryEditorUI extends Component<Props, State> {
}
};

private fetchIndexPatterns = async () => {
const client = this.services.savedObjects.client;
const dataSet = this.queryService.dataSet.getDataSet();
const title = dataSet?.title;

const resp = await client.find({
type: 'index-pattern',
fields: ['title', 'timeFieldName', 'fields'],
search: `${title}*`,
searchFields: ['title'],
perPage: 100,
});

return resp.savedObjects.map((savedObject: any) => ({
id: savedObject.id,
title: savedObject.attributes?.title,
timeFieldName: savedObject.attributes?.timeFieldName,
fields: JSON.parse(savedObject.attributes?.fields),
type: SIMPLE_DATA_SET_TYPES.INDEX_PATTERN,
}));
};

provideCompletionItems = async (
model: monaco.editor.ITextModel,
position: monaco.Position
): Promise<monaco.languages.CompletionList> => {
const enhancements = this.props.settings.getQueryEnhancements(this.props.query.language);
const connectionService = enhancements?.connectionService;

const indexPatterns = await this.fetchIndexPatterns();

const suggestions = await this.services.data.autocomplete.getQuerySuggestions({
query: this.getQueryString(),
selectionStart: model.getOffsetAt(position),
selectionEnd: model.getOffsetAt(position),
language: this.props.query.language,
indexPatterns: this.state.indexPatterns,
indexPatterns,
position,
connectionService,
});
Expand Down

0 comments on commit 1d9de39

Please sign in to comment.