diff --git a/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/CompletionDataProvider.test.ts b/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/CompletionDataProvider.test.ts index c55ad148165..7c076ec3101 100644 --- a/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/CompletionDataProvider.test.ts +++ b/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/CompletionDataProvider.test.ts @@ -23,6 +23,13 @@ const history = [ expr: '{unit: test}', }, }, + { + ts: 87654321, + query: { + refId: 'test-1', + expr: '{unit: test}', + }, + }, { ts: 0, query: { diff --git a/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/CompletionDataProvider.ts b/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/CompletionDataProvider.ts index b2df73aeffc..9f15bd09cdf 100644 --- a/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/CompletionDataProvider.ts +++ b/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/CompletionDataProvider.ts @@ -1,3 +1,5 @@ +import { chain } from 'lodash'; + import { HistoryItem } from '@grafana/data'; import { escapeLabelValueInExactSelector } from 'app/plugins/datasource/prometheus/language_utils'; @@ -7,7 +9,10 @@ import { LokiQuery } from '../../../types'; import { Label } from './situation'; export class CompletionDataProvider { - constructor(private languageProvider: LanguageProvider, private history: Array> = []) {} + private history: string[] = []; + constructor(private languageProvider: LanguageProvider, history: Array> = []) { + this.setHistory(history); + } private buildSelector(labels: Label[]): string { const allLabelTexts = labels.map( @@ -17,8 +22,16 @@ export class CompletionDataProvider { return `{${allLabelTexts.join(',')}}`; } + setHistory(history: Array> = []) { + this.history = chain(history) + .map((history: HistoryItem) => history.query.expr) + .filter() + .uniq() + .value(); + } + getHistory() { - return this.history.map((entry) => entry.query.expr).filter((expr) => expr !== undefined); + return this.history; } async getLabelNames(otherLabels: Label[] = []) { diff --git a/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/completions.test.ts b/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/completions.test.ts index caab25519ad..b1a9e8e7974 100644 --- a/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/completions.test.ts +++ b/public/app/plugins/datasource/loki/components/monaco-query-field/monaco-completion-provider/completions.test.ts @@ -197,7 +197,7 @@ describe('getCompletions', () => { const situation = { type } as Situation; const completions = await getCompletions(situation, completionProvider); - expect(completions).toHaveLength(25); + expect(completions).toHaveLength(24); }); test('Returns completion options when the situation is IN_DURATION', async () => {