Commit 02bbdca6 by kay delaney Committed by GitHub

Datasource/Loki: Simplifies autocompletion (#20840)

Unifies loki autocomplete so behavior isn't different
across explore modes.
Closes #20769
parent fd2b39a3
...@@ -9,6 +9,7 @@ import { beforeEach } from 'test/lib/common'; ...@@ -9,6 +9,7 @@ import { beforeEach } from 'test/lib/common';
import { makeMockLokiDatasource } from './mocks'; import { makeMockLokiDatasource } from './mocks';
import LokiDatasource from './datasource'; import LokiDatasource from './datasource';
import { FUNCTIONS } from './syntax';
jest.mock('app/store/store', () => ({ jest.mock('app/store/store', () => ({
store: { store: {
...@@ -31,16 +32,17 @@ describe('Language completion provider', () => { ...@@ -31,16 +32,17 @@ describe('Language completion provider', () => {
}; };
describe('empty query suggestions', () => { describe('empty query suggestions', () => {
it('returns no suggestions on empty context', async () => { it('returns function suggestions on empty context', async () => {
const instance = new LanguageProvider(datasource); const instance = new LanguageProvider(datasource);
const value = Plain.deserialize(''); const value = Plain.deserialize('');
const result = await instance.provideCompletionItems({ text: '', prefix: '', value, wrapperClasses: [] }); const result = await instance.provideCompletionItems({ text: '', prefix: '', value, wrapperClasses: [] });
expect(result.context).toBeUndefined(); expect(result.context).toBeUndefined();
expect(result.suggestions.length).toEqual(0); expect(result.suggestions.length).toEqual(1);
expect(result.suggestions[0].label).toEqual('Functions');
}); });
it('returns default suggestions with history on empty context when history was provided', async () => { it('returns function suggestions with history on empty context when history was provided', async () => {
const instance = new LanguageProvider(datasource); const instance = new LanguageProvider(datasource);
const value = Plain.deserialize(''); const value = Plain.deserialize('');
const history: LokiHistoryItem[] = [ const history: LokiHistoryItem[] = [
...@@ -64,10 +66,14 @@ describe('Language completion provider', () => { ...@@ -64,10 +66,14 @@ describe('Language completion provider', () => {
}, },
], ],
}, },
{
label: 'Functions',
items: FUNCTIONS,
},
]); ]);
}); });
it('returns no suggestions within regexp', async () => { it('returns function suggestions within regexp', async () => {
const instance = new LanguageProvider(datasource); const instance = new LanguageProvider(datasource);
const input = createTypeaheadInput('{} ()', '', undefined, 4, []); const input = createTypeaheadInput('{} ()', '', undefined, 4, []);
const history: LokiHistoryItem[] = [ const history: LokiHistoryItem[] = [
...@@ -78,8 +84,8 @@ describe('Language completion provider', () => { ...@@ -78,8 +84,8 @@ describe('Language completion provider', () => {
]; ];
const result = await instance.provideCompletionItems(input, { history }); const result = await instance.provideCompletionItems(input, { history });
expect(result.context).toBeUndefined(); expect(result.context).toBeUndefined();
expect(result.suggestions.length).toEqual(1);
expect(result.suggestions.length).toEqual(0); expect(result.suggestions[0].label).toEqual('Functions');
}); });
}); });
......
...@@ -3,7 +3,6 @@ import _ from 'lodash'; ...@@ -3,7 +3,6 @@ import _ from 'lodash';
// Services & Utils // Services & Utils
import { parseSelector, labelRegexp, selectorRegexp } from 'app/plugins/datasource/prometheus/language_utils'; import { parseSelector, labelRegexp, selectorRegexp } from 'app/plugins/datasource/prometheus/language_utils';
import { store } from 'app/store/store';
import syntax, { FUNCTIONS } from './syntax'; import syntax, { FUNCTIONS } from './syntax';
// Types // Types
...@@ -113,16 +112,6 @@ export default class LokiLanguageProvider extends LanguageProvider { ...@@ -113,16 +112,6 @@ export default class LokiLanguageProvider extends LanguageProvider {
* @param context.history Optional used only in getEmptyCompletionItems * @param context.history Optional used only in getEmptyCompletionItems
*/ */
async provideCompletionItems(input: TypeaheadInput, context?: TypeaheadContext): Promise<TypeaheadOutput> { async provideCompletionItems(input: TypeaheadInput, context?: TypeaheadContext): Promise<TypeaheadOutput> {
const exploreMode = store.getState().explore.left.mode;
if (exploreMode === ExploreMode.Logs) {
return this.provideLogCompletionItems(input, context);
}
return this.provideMetricsCompletionItems(input, context);
}
async provideMetricsCompletionItems(input: TypeaheadInput, context?: TypeaheadContext): Promise<TypeaheadOutput> {
const { wrapperClasses, value, prefix, text } = input; const { wrapperClasses, value, prefix, text } = input;
// Local text properties // Local text properties
...@@ -167,23 +156,6 @@ export default class LokiLanguageProvider extends LanguageProvider { ...@@ -167,23 +156,6 @@ export default class LokiLanguageProvider extends LanguageProvider {
}; };
} }
async provideLogCompletionItems(input: TypeaheadInput, context?: TypeaheadContext): Promise<TypeaheadOutput> {
const { wrapperClasses, value } = input;
// Local text properties
const empty = value.document.text.length === 0;
// Determine candidates by CSS context
if (wrapperClasses.includes('context-labels')) {
// Suggestions for {|} and {foo=|}
return await this.getLabelCompletionItems(input, context);
} else if (empty) {
return this.getEmptyCompletionItems(context || {}, ExploreMode.Logs);
}
return {
suggestions: [],
};
}
getEmptyCompletionItems(context: TypeaheadContext, mode?: ExploreMode): TypeaheadOutput { getEmptyCompletionItems(context: TypeaheadContext, mode?: ExploreMode): TypeaheadOutput {
const { history } = context; const { history } = context;
const suggestions = []; const suggestions = [];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment