feat(loki-query-autocomplete): suggest unique history items (#60262)

This commit is contained in:
Matias Chomicki 2022-12-14 13:43:06 +01:00 committed by GitHub
parent 9a41277ecd
commit 2c1b65021d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 3 deletions

View File

@ -23,6 +23,13 @@ const history = [
expr: '{unit: test}',
},
},
{
ts: 87654321,
query: {
refId: 'test-1',
expr: '{unit: test}',
},
},
{
ts: 0,
query: {

View File

@ -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<HistoryItem<LokiQuery>> = []) {}
private history: string[] = [];
constructor(private languageProvider: LanguageProvider, history: Array<HistoryItem<LokiQuery>> = []) {
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<HistoryItem<LokiQuery>> = []) {
this.history = chain(history)
.map((history: HistoryItem<LokiQuery>) => 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[] = []) {

View File

@ -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 () => {