Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocomplete for data values #245

Merged
merged 3 commits into from
Nov 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ import {
import { EuiTextArea } from '@elastic/eui';
import { IQueryBarProps } from './search';
import { getDataValueQuery } from './queries/data_queries';
import { isEmpty } from 'lodash';
import DSLService from 'public/services/requests/dsl';
import { uiSettingsService } from '../../../../common/utils';

let currIndex: string = '';
let currField: string = '';
let currFieldType: string | undefined = '';
let currFieldType: string = '';

let inFieldsCommaLoop: boolean = false;
let inMatch: boolean = false;
Expand All @@ -34,6 +33,7 @@ const indexList: string[] = [];
const fieldList: string[] = [];
const fieldsFromBackend: fieldItem[] = [];
const indicesFromBackend: indexItem[] = [];
const dataValuesFromBackend: dataItem[] = [];

const firstCommand = [{ label: 'source' }];

Expand Down Expand Up @@ -119,6 +119,7 @@ const getSuggestions = async (str: string, dslService: DSLService) => {
nextWhere = Number.MAX_SAFE_INTEGER;
nextStats = Number.MAX_SAFE_INTEGER;
currField = '';
currFieldType = '';
return fillSuggestions(str, prefix, pipeCommands);
} else if (splittedModel[splittedModel.length - 2].includes(',')) {
if (inFieldsCommaLoop) {
Expand All @@ -129,7 +130,7 @@ const getSuggestions = async (str: string, dslService: DSLService) => {
return fillSuggestions(
str,
prefix,
await getDataValues(currIndex, currField, currFieldType, dslService)
dataValuesFromBackend
);
}
return fullSuggestions;
Expand All @@ -152,7 +153,8 @@ const getSuggestions = async (str: string, dslService: DSLService) => {
} else if (inMatch && fieldList.includes(splittedModel[splittedModel.length - 2])) {
inMatch = true;
currField = splittedModel[splittedModel.length - 2];
currFieldType = fieldsFromBackend.find((field) => field.label === currField)?.type;
currFieldType = fieldsFromBackend.find((field) => field.label === currField)?.type || '';
await getDataValues(currIndex, currField, currFieldType, dslService)
return [{ label: str + ',', input: str, suggestion: ',', itemName: ','}].filter(
({ suggestion }) => suggestion.startsWith(prefix) && prefix !== suggestion
);
Expand Down Expand Up @@ -213,21 +215,15 @@ const getSuggestions = async (str: string, dslService: DSLService) => {
itemName: '=',
});
currField = splittedModel[splittedModel.length - 2];
currFieldType = fieldsFromBackend.find((field: {label: string, type: string}) => field.label === currField)?.type;
currFieldType = fieldsFromBackend.find((field: {label: string, type: string}) => field.label === currField)?.type || '';
await getDataValues(currIndex, currField, currFieldType, dslService);
return fullSuggestions.filter((suggestion: { label: string }) => suggestion.label.toLowerCase().startsWith(lowerPrefix) && lowerPrefix.localeCompare(suggestion.label.toLowerCase()));
} else if (nextWhere === splittedModel.length - 2) {
if (isEmpty(prefix)) {
if (!currFieldType) {
console.error('Current field type is undefined')
return [];
}
return fillSuggestions(
str,
prefix,
await getDataValues(currIndex, currField, currFieldType, dslService)
dataValuesFromBackend
);
}
return [];
} else if (nextWhere === splittedModel.length - 3 || nextStats === splittedModel.length - 5 || nextWhere === splittedModel.length - 5) {
return [{ label: str + '|', input: str, suggestion: '|', itemName: '|' }].filter(
({ label }) => label.toLowerCase().startsWith(lowerPrefix) && lowerPrefix.localeCompare(label.toLowerCase())
Expand Down Expand Up @@ -289,7 +285,7 @@ const getDataValues = async (
dslService: DSLService
) => {
const res = (await dslService.fetch(getDataValueQuery(index, field)))?.aggregations?.top_tags?.buckets || [];
const dataValuesFromBackend: dataItem[] = [];
dataValuesFromBackend.length = 0;
res.forEach((e: any) => {
if (fieldType === 'string') {
dataValuesFromBackend.push({ label: '"' + e.key + '"', doc_count: e.doc_count });
Expand Down