mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Loki: Respect pre-selected filters in adhoc filter queries (#89022)
* Loki: In adhoc filters, respect pre-selected filters * improve test,fix quotes * Refactor
This commit is contained in:
parent
558aaf22bd
commit
09e74acc94
@ -19,6 +19,7 @@ import {
|
|||||||
ToggleFilterAction,
|
ToggleFilterAction,
|
||||||
DataQueryRequest,
|
DataQueryRequest,
|
||||||
ScopedVars,
|
ScopedVars,
|
||||||
|
AdHocVariableFilter,
|
||||||
} from '@grafana/data';
|
} from '@grafana/data';
|
||||||
import {
|
import {
|
||||||
BackendSrv,
|
BackendSrv,
|
||||||
@ -1701,6 +1702,67 @@ describe('LokiDatasource', () => {
|
|||||||
expect(ds.applyTemplateVariables).toHaveBeenCalledWith(expect.objectContaining(query), scopedVars, filters);
|
expect(ds.applyTemplateVariables).toHaveBeenCalledWith(expect.objectContaining(query), scopedVars, filters);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('getTagKeys', () => {
|
||||||
|
it('should pass timeRange and filters to the request', async () => {
|
||||||
|
const ds = createLokiDatasource();
|
||||||
|
const filters = [
|
||||||
|
{ key: 'foo', operator: '=', value: 'bar' },
|
||||||
|
{ key: 'foo2', operator: '=', value: 'bar2' },
|
||||||
|
];
|
||||||
|
const spy = jest.spyOn(ds.languageProvider, 'fetchLabels').mockResolvedValue([]);
|
||||||
|
|
||||||
|
await ds.getTagKeys({ filters, timeRange: mockTimeRange });
|
||||||
|
expect(spy).toHaveBeenCalledWith({ streamSelector: '{foo="bar", foo2="bar2"}', timeRange: mockTimeRange });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should pass regex filters', async () => {
|
||||||
|
const ds = createLokiDatasource();
|
||||||
|
const filters = [
|
||||||
|
{ key: 'foo', operator: '=~', value: 'abc|def' },
|
||||||
|
{ key: 'foo2', operator: '=', value: 'bar2' },
|
||||||
|
];
|
||||||
|
const spy = jest.spyOn(ds.languageProvider, 'fetchLabels').mockResolvedValue([]);
|
||||||
|
|
||||||
|
await ds.getTagKeys({ filters, timeRange: mockTimeRange });
|
||||||
|
expect(spy).toHaveBeenCalledWith({ streamSelector: '{foo=~"abc|def", foo2="bar2"}', timeRange: mockTimeRange });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should pass empty stream selector when no filters', async () => {
|
||||||
|
const ds = createLokiDatasource();
|
||||||
|
const filters: AdHocVariableFilter[] = [];
|
||||||
|
const spy = jest.spyOn(ds.languageProvider, 'fetchLabels').mockResolvedValue([]);
|
||||||
|
|
||||||
|
await ds.getTagKeys({ filters, timeRange: mockTimeRange });
|
||||||
|
expect(spy).toHaveBeenCalledWith({ streamSelector: '{}', timeRange: mockTimeRange });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getTagValues', () => {
|
||||||
|
it('should pass timeRange and filters to the request', async () => {
|
||||||
|
const ds = createLokiDatasource();
|
||||||
|
const filters = [
|
||||||
|
{ key: 'foo', operator: '=', value: 'bar' },
|
||||||
|
{ key: 'foo2', operator: '=', value: 'bar2' },
|
||||||
|
];
|
||||||
|
const spy = jest.spyOn(ds.languageProvider, 'fetchLabelValues').mockResolvedValue([]);
|
||||||
|
|
||||||
|
await ds.getTagValues({ key: 'label1', filters, timeRange: mockTimeRange });
|
||||||
|
expect(spy).toHaveBeenCalledWith('label1', {
|
||||||
|
streamSelector: '{foo="bar", foo2="bar2"}',
|
||||||
|
timeRange: mockTimeRange,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should pass empty stream selector when no filters', async () => {
|
||||||
|
const ds = createLokiDatasource();
|
||||||
|
const filters: AdHocVariableFilter[] = [];
|
||||||
|
const spy = jest.spyOn(ds.languageProvider, 'fetchLabelValues').mockResolvedValue([]);
|
||||||
|
|
||||||
|
await ds.getTagValues({ key: 'label1', filters, timeRange: mockTimeRange });
|
||||||
|
expect(spy).toHaveBeenCalledWith('label1', { streamSelector: '{}', timeRange: mockTimeRange });
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('applyTemplateVariables', () => {
|
describe('applyTemplateVariables', () => {
|
||||||
|
@ -743,7 +743,11 @@ export class LokiDatasource
|
|||||||
* @returns A Promise that resolves to an array of label names represented as MetricFindValue objects.
|
* @returns A Promise that resolves to an array of label names represented as MetricFindValue objects.
|
||||||
*/
|
*/
|
||||||
async getTagKeys(options?: DataSourceGetTagKeysOptions<LokiQuery>): Promise<MetricFindValue[]> {
|
async getTagKeys(options?: DataSourceGetTagKeysOptions<LokiQuery>): Promise<MetricFindValue[]> {
|
||||||
const result = await this.languageProvider.fetchLabels({ timeRange: options?.timeRange });
|
let streamSelector = '{}';
|
||||||
|
for (const filter of options?.filters ?? []) {
|
||||||
|
streamSelector = addLabelToQuery(streamSelector, filter.key, filter.operator, filter.value);
|
||||||
|
}
|
||||||
|
const result = await this.languageProvider.fetchLabels({ timeRange: options?.timeRange, streamSelector });
|
||||||
return result.map((value: string) => ({ text: value }));
|
return result.map((value: string) => ({ text: value }));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -752,7 +756,14 @@ export class LokiDatasource
|
|||||||
* @returns A Promise that resolves to an array of label values represented as MetricFindValue objects
|
* @returns A Promise that resolves to an array of label values represented as MetricFindValue objects
|
||||||
*/
|
*/
|
||||||
async getTagValues(options: DataSourceGetTagValuesOptions<LokiQuery>): Promise<MetricFindValue[]> {
|
async getTagValues(options: DataSourceGetTagValuesOptions<LokiQuery>): Promise<MetricFindValue[]> {
|
||||||
const result = await this.languageProvider.fetchLabelValues(options.key, { timeRange: options.timeRange });
|
let streamSelector = '{}';
|
||||||
|
for (const filter of options?.filters ?? []) {
|
||||||
|
streamSelector = addLabelToQuery(streamSelector, filter.key, filter.operator, filter.value);
|
||||||
|
}
|
||||||
|
const result = await this.languageProvider.fetchLabelValues(options.key, {
|
||||||
|
timeRange: options.timeRange,
|
||||||
|
streamSelector,
|
||||||
|
});
|
||||||
return result.map((value: string) => ({ text: value }));
|
return result.map((value: string) => ({ text: value }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user