Skip to content

Commit

Permalink
Datasource/CloudWatch: Fixes various autocomplete issues (#24583)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaydelaney committed May 12, 2020
1 parent a50cb6a commit c191994
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/grafana-ui/src/slate-plugins/suggestions.tsx
Expand Up @@ -295,8 +295,8 @@ const handleTypeahead = async (
}
}

// Filter out the already typed value (prefix) unless it inserts custom text
newGroup.items = newGroup.items.filter(c => c.insertText || (c.filterText || c.label) !== prefix);
// Filter out the already typed value (prefix) unless it inserts custom text not matching the prefix
newGroup.items = newGroup.items.filter(c => !(c.insertText === prefix || (c.filterText ?? c.label) === prefix));
}

if (!group.skipSort) {
Expand Down
Expand Up @@ -8,11 +8,11 @@ import {
AGGREGATION_FUNCTIONS_STATS,
BOOLEAN_FUNCTIONS,
DATETIME_FUNCTIONS,
FUNCTIONS,
IP_FUNCTIONS,
NUMERIC_OPERATORS,
QUERY_COMMANDS,
STRING_FUNCTIONS,
FIELD_AND_FILTER_FUNCTIONS,
} from './syntax';

const fields = ['field1', '@message'];
Expand All @@ -33,19 +33,19 @@ describe('CloudWatchLanguageProvider', () => {
});

it('should suggest fields and functions after field command', async () => {
await runSuggestionTest('fields \\', [fields, FUNCTIONS.map(v => v.label)]);
await runSuggestionTest('fields \\', [fields, FIELD_AND_FILTER_FUNCTIONS.map(v => v.label)]);
});

it('should suggest fields and functions after comma', async () => {
await runSuggestionTest('fields field1, \\', [fields, FUNCTIONS.map(v => v.label)]);
await runSuggestionTest('fields field1, \\', [fields, FIELD_AND_FILTER_FUNCTIONS.map(v => v.label)]);
});

it('should suggest fields and functions after comma with prefix', async () => {
await runSuggestionTest('fields field1, @mess\\', [fields, FUNCTIONS.map(v => v.label)]);
await runSuggestionTest('fields field1, @mess\\', [fields, FIELD_AND_FILTER_FUNCTIONS.map(v => v.label)]);
});

it('should suggest fields and functions after display command', async () => {
await runSuggestionTest('display \\', [fields, FUNCTIONS.map(v => v.label)]);
await runSuggestionTest('display \\', [fields, FIELD_AND_FILTER_FUNCTIONS.map(v => v.label)]);
});

it('should suggest functions after stats command', async () => {
Expand All @@ -62,8 +62,7 @@ describe('CloudWatchLanguageProvider', () => {
it('should suggest fields and some functions after comparison operator', async () => {
await runSuggestionTest('filter field1 >= \\', [
fields,
BOOLEAN_FUNCTIONS.map(v => v.label),
NUMERIC_OPERATORS.map(v => v.label),
[...NUMERIC_OPERATORS.map(v => v.label), ...BOOLEAN_FUNCTIONS.map(v => v.label)],
]);
});

Expand Down
18 changes: 8 additions & 10 deletions public/app/plugins/datasource/cloudwatch/language_provider.ts
Expand Up @@ -4,13 +4,13 @@ import _ from 'lodash';
// Services & Utils
import syntax, {
QUERY_COMMANDS,
FUNCTIONS,
AGGREGATION_FUNCTIONS_STATS,
STRING_FUNCTIONS,
DATETIME_FUNCTIONS,
IP_FUNCTIONS,
BOOLEAN_FUNCTIONS,
NUMERIC_OPERATORS,
FIELD_AND_FILTER_FUNCTIONS,
} from './syntax';

// Types
Expand Down Expand Up @@ -210,7 +210,7 @@ export class CloudWatchLanguageProvider extends LanguageProvider {

if (['display', 'fields'].includes(queryCommand)) {
const typeaheadOutput = await this.getFieldCompletionItems(context.logGroupNames ?? []);
typeaheadOutput.suggestions.push(...this.getFunctionCompletionItems().suggestions);
typeaheadOutput.suggestions.push(...this.getFieldAndFilterFunctionCompletionItems().suggestions);

return typeaheadOutput;
}
Expand Down Expand Up @@ -264,19 +264,17 @@ export class CloudWatchLanguageProvider extends LanguageProvider {

private handleComparison = async (context?: TypeaheadContext) => {
const fieldsSuggestions = await this.getFieldCompletionItems(context?.logGroupNames ?? []);
const boolFuncSuggestions = this.getBoolFuncCompletionItems();
const numFuncSuggestions = this.getNumericFuncCompletionItems();

fieldsSuggestions.suggestions.push(...boolFuncSuggestions.suggestions, ...numFuncSuggestions.suggestions);
const comparisonSuggestions = this.getComparisonCompletionItems();
fieldsSuggestions.suggestions.push(...comparisonSuggestions.suggestions);
return fieldsSuggestions;
};

private getCommandCompletionItems = (): TypeaheadOutput => {
return { suggestions: [{ prefixMatch: true, label: 'Commands', items: QUERY_COMMANDS }] };
};

private getFunctionCompletionItems = (): TypeaheadOutput => {
return { suggestions: [{ prefixMatch: true, label: 'Functions', items: FUNCTIONS }] };
private getFieldAndFilterFunctionCompletionItems = (): TypeaheadOutput => {
return { suggestions: [{ prefixMatch: true, label: 'Functions', items: FIELD_AND_FILTER_FUNCTIONS }] };
};

private getStatsAggCompletionItems = (): TypeaheadOutput => {
Expand All @@ -295,13 +293,13 @@ export class CloudWatchLanguageProvider extends LanguageProvider {
};
};

private getNumericFuncCompletionItems = (): TypeaheadOutput => {
private getComparisonCompletionItems = (): TypeaheadOutput => {
return {
suggestions: [
{
prefixMatch: true,
label: 'Functions',
items: NUMERIC_OPERATORS,
items: NUMERIC_OPERATORS.concat(BOOLEAN_FUNCTIONS),
},
],
};
Expand Down
8 changes: 3 additions & 5 deletions public/app/plugins/datasource/cloudwatch/syntax.ts
Expand Up @@ -9,12 +9,10 @@ export const QUERY_COMMANDS: CompletionItem[] = [
{ label: 'display', documentation: 'Specifies which fields to display in the query results' },
{
label: 'filter',
insertText: 'filter',
documentation: 'Filters the results of a query based on one or more conditions',
},
{
label: 'stats',
insertText: 'stats',
documentation: 'Calculates aggregate statistics based on the values of log fields',
},
{ label: 'sort', documentation: 'Sorts the retrieved log events' },
Expand Down Expand Up @@ -205,7 +203,6 @@ export const IP_FUNCTIONS = [
},
{
label: 'isIpv6InSubnet',
insertText: 'isIpv6InSubnet',
detail: 'isIpv6InSubnet(fieldname, string)',
documentation: 'Returns true if the field is a valid v6 IP address within the specified v6 subnet.',
},
Expand Down Expand Up @@ -306,15 +303,16 @@ export const NON_AGGREGATION_FUNCS_STATS = [
export const STATS_FUNCS = [...AGGREGATION_FUNCTIONS_STATS, ...NON_AGGREGATION_FUNCS_STATS];

export const KEYWORDS = ['as', 'like', 'by', 'in', 'desc', 'asc'];
export const FUNCTIONS = [
export const FIELD_AND_FILTER_FUNCTIONS = [
...NUMERIC_OPERATORS,
...GENERAL_FUNCTIONS,
...STRING_FUNCTIONS,
...DATETIME_FUNCTIONS,
...IP_FUNCTIONS,
...STATS_FUNCS,
];

export const FUNCTIONS = [...FIELD_AND_FILTER_FUNCTIONS, ...STATS_FUNCS];

const tokenizer: Grammar = {
comment: {
pattern: /^#.*/,
Expand Down

0 comments on commit c191994

Please sign in to comment.