mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Datasource/Loki: Simplifies autocompletion (#20840)
Unifies loki autocomplete so behavior isn't different across explore modes. Closes #20769
This commit is contained in:
parent
fd2b39a3e9
commit
02bbdca604
@ -9,6 +9,7 @@ import { beforeEach } from 'test/lib/common';
|
||||
|
||||
import { makeMockLokiDatasource } from './mocks';
|
||||
import LokiDatasource from './datasource';
|
||||
import { FUNCTIONS } from './syntax';
|
||||
|
||||
jest.mock('app/store/store', () => ({
|
||||
store: {
|
||||
@ -31,16 +32,17 @@ describe('Language completion provider', () => {
|
||||
};
|
||||
|
||||
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 value = Plain.deserialize('');
|
||||
const result = await instance.provideCompletionItems({ text: '', prefix: '', value, wrapperClasses: [] });
|
||||
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 value = Plain.deserialize('');
|
||||
const history: LokiHistoryItem[] = [
|
||||
@ -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 input = createTypeaheadInput('{} ()', '', undefined, 4, []);
|
||||
const history: LokiHistoryItem[] = [
|
||||
@ -78,8 +84,8 @@ describe('Language completion provider', () => {
|
||||
];
|
||||
const result = await instance.provideCompletionItems(input, { history });
|
||||
expect(result.context).toBeUndefined();
|
||||
|
||||
expect(result.suggestions.length).toEqual(0);
|
||||
expect(result.suggestions.length).toEqual(1);
|
||||
expect(result.suggestions[0].label).toEqual('Functions');
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -3,7 +3,6 @@ import _ from 'lodash';
|
||||
|
||||
// Services & Utils
|
||||
import { parseSelector, labelRegexp, selectorRegexp } from 'app/plugins/datasource/prometheus/language_utils';
|
||||
import { store } from 'app/store/store';
|
||||
import syntax, { FUNCTIONS } from './syntax';
|
||||
|
||||
// Types
|
||||
@ -113,16 +112,6 @@ export default class LokiLanguageProvider extends LanguageProvider {
|
||||
* @param context.history Optional used only in getEmptyCompletionItems
|
||||
*/
|
||||
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;
|
||||
|
||||
// Local text properties
|
||||
@ -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 {
|
||||
const { history } = context;
|
||||
const suggestions = [];
|
||||
|
Loading…
Reference in New Issue
Block a user