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

Minor fixes to editor autocomplete #90

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 11 additions & 5 deletions src/components/LuceneQueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,23 @@ export function LuceneQueryEditor(props: LuceneQueryEditorProps){

const {autocompleter} = props;
const datasourceCompletions = useCallback(async (context: CompletionContext)=>{
let suggestions;
let word = context.matchBefore(/\S*/);
if (!word){ return null }
const suggestions = await autocompleter(word?.text);
return {
from: word.from + suggestions.from,
options: suggestions.options
suggestions = await autocompleter(word?.text);
if (suggestions && suggestions.options.length > 0 ) {
return {
from: word.from + suggestions.from,
options: suggestions.options
}
}
return null
}, [autocompleter])


const autocomplete = autocompletion({ override: [datasourceCompletions] })
const autocomplete = autocompletion({
override: [datasourceCompletions]
})

return (<CodeMirror
ref={editorRef}
Expand Down
25 changes: 14 additions & 11 deletions src/datasource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,15 @@ export class QuickwitDataSource
getTerms(queryDef: TermsQuery, range = getDefaultTimeRange()): Observable<MetricFindValue[]> {
const dataquery = this.getDataQueryRequest(queryDef, range)
return super.query(dataquery).pipe(
mergeMap(res=> res.data.map((df: DataFrame)=>{

return df.fields[0]!.values.map((bucket)=>({
text: bucket,
value: bucket,
}))
mergeMap(res=> {
return res.data.map((df: DataFrame)=>{
if (df.fields.length === 0) { return [] }
return df.fields[0].values.map((bucket)=>({
text: bucket,
value: bucket,
}))
})
})
)
)
}

Expand Down Expand Up @@ -335,15 +336,17 @@ export class QuickwitDataSource
* Get tag keys for adhoc filters
*/
getTagKeys(spec?: FieldCapsSpec) {
return lastValueFrom(this.getFields(spec));
const fields = this.getFields(spec)
return lastValueFrom(fields, {defaultValue:[]});
}

/**
* Get tag values for adhoc filters
*/
getTagValues(options: any) {
const range = getDefaultTimeRange();
return lastValueFrom(this.getTerms({ field: options.key }, range));
const terms = this.getTerms({ field: options.key }, range)
return lastValueFrom(terms, {defaultValue:[]});
}

/**
Expand Down Expand Up @@ -436,12 +439,12 @@ export class QuickwitDataSource
if (query) {
if (parsedQuery.find === 'fields') {
parsedQuery.type = this.interpolateLuceneQuery(parsedQuery.type);
return lastValueFrom(this.getFields({aggregatable:true, type:parsedQuery.type, _range:range}));
return lastValueFrom(this.getFields({aggregatable:true, type:parsedQuery.type, _range:range}), {defaultValue:[]});
}
if (parsedQuery.find === 'terms') {
parsedQuery.field = this.interpolateLuceneQuery(parsedQuery.field);
parsedQuery.query = this.interpolateLuceneQuery(parsedQuery.query);
return lastValueFrom(this.getTerms(parsedQuery, range));
return lastValueFrom(this.getTerms(parsedQuery, range), {defaultValue:[]});
}
}
return Promise.resolve([]);
Expand Down
2 changes: 1 addition & 1 deletion src/datasource/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function useDatasourceFields(datasource: QuickwitDataSource) {
const getSuggestions = useCallback(async (word: string): Promise<Suggestion> => {
let suggestions: Suggestion = { from: 0, options: [] };

const wordIsField = word.match(/([\w\.]+):"?(\S*)/);
const wordIsField = word.match(/([^:\s]+):"?([^"\s]*)"?/);
if (wordIsField?.length) {
const [_match, fieldName, _fieldValue] = wordIsField;
const candidateValues = await datasource.getTagValues({ key: fieldName });
Expand Down
Loading