mirror of https://github.com/grafana/grafana
Datasource: Change query filtering (#84656)
* call filterQuery from queryrunner * test query hide filtering * fix more broken tests * lint errrors * remove redundant filterQuery call * skip filter in variable queries * fix broken cypress test * change tooltip text * fix translations * fix comments * do not execute query is targets are empty * add more tests * remove unsued import * update translations * revert id change * change header text * update comment for hide prop * rename hide query prop * change tooltip and introduce different toggle state text * update tests * update comment and regenerate types * run extract again * fix broken e2e test * track event * fix build issues * revert changes in wire filepull/84913/head
parent
410f5e3e3a
commit
29d4f6a217
@ -0,0 +1,69 @@ |
||||
import { Observable } from 'rxjs'; |
||||
|
||||
import { |
||||
DataQuery, |
||||
DataQueryRequest, |
||||
DataQueryResponse, |
||||
DataSourceApi, |
||||
DataSourceInstanceSettings, |
||||
DataSourceJsonData, |
||||
DataSourcePluginMeta, |
||||
PluginMetaInfo, |
||||
PluginType, |
||||
TestDataSourceResponse, |
||||
} from '../../types'; |
||||
|
||||
export interface TestQuery extends DataQuery { |
||||
query: string; |
||||
} |
||||
|
||||
export interface TestJsonData extends DataSourceJsonData { |
||||
url?: string; |
||||
} |
||||
|
||||
const info: PluginMetaInfo = { |
||||
author: { |
||||
name: '', |
||||
}, |
||||
description: '', |
||||
links: [], |
||||
logos: { |
||||
large: '', |
||||
small: '', |
||||
}, |
||||
screenshots: [], |
||||
updated: '', |
||||
version: '', |
||||
}; |
||||
|
||||
export const meta: DataSourcePluginMeta<DataSourceJsonData> = { |
||||
id: '', |
||||
name: '', |
||||
type: PluginType.datasource, |
||||
info, |
||||
module: '', |
||||
baseUrl: '', |
||||
}; |
||||
|
||||
export const TestDataSettings: DataSourceInstanceSettings<TestJsonData> = { |
||||
jsonData: { url: 'http://localhost:3000' }, |
||||
id: 0, |
||||
uid: '', |
||||
type: '', |
||||
name: 'Test Datasource', |
||||
meta, |
||||
readOnly: false, |
||||
access: 'direct', |
||||
}; |
||||
|
||||
export class TestDataSource extends DataSourceApi<TestQuery, DataSourceJsonData> { |
||||
query(request: DataQueryRequest<TestQuery>): Promise<DataQueryResponse> | Observable<DataQueryResponse> { |
||||
throw new Error('Method not implemented.'); |
||||
} |
||||
testDatasource(): Promise<TestDataSourceResponse> { |
||||
throw new Error('Method not implemented.'); |
||||
} |
||||
constructor(instanceSettings: DataSourceInstanceSettings<TestJsonData> = TestDataSettings) { |
||||
super(instanceSettings); |
||||
} |
||||
} |
@ -0,0 +1,59 @@ |
||||
import { DataSourceApi, dateTime, DataQuery } from '@grafana/data'; |
||||
|
||||
import { PanelModel } from '../dashboard/state'; |
||||
import { createDashboardModelFixture } from '../dashboard/state/__fixtures__/dashboardFixtures'; |
||||
import { TestQuery, getMockDataSource } from '../query/state/__mocks__/mockDataSource'; |
||||
|
||||
import { executeAnnotationQuery } from './executeAnnotationQuery'; |
||||
import { AnnotationQueryOptions } from './types'; |
||||
|
||||
describe('executeAnnotationQuery', () => { |
||||
let filterQuerySpy: jest.SpyInstance; |
||||
let querySpy: jest.SpyInstance; |
||||
let ds: DataSourceApi; |
||||
|
||||
const setup = ({ query, filterQuery }: { query: TestQuery; filterQuery?: typeof ds.filterQuery }) => { |
||||
const options: AnnotationQueryOptions = { |
||||
range: { from: dateTime(), to: dateTime(), raw: { from: '1h', to: 'now' } }, |
||||
dashboard: createDashboardModelFixture({ |
||||
panels: [{ id: 1, type: 'graph' }], |
||||
}), |
||||
panel: {} as PanelModel, |
||||
}; |
||||
|
||||
const ds = getMockDataSource(); |
||||
if (filterQuery) { |
||||
ds.filterQuery = filterQuery; |
||||
filterQuerySpy = jest.spyOn(ds, 'filterQuery'); |
||||
} |
||||
querySpy = jest.spyOn(ds, 'query'); |
||||
executeAnnotationQuery(options, ds, { |
||||
name: '', |
||||
enable: false, |
||||
iconColor: '', |
||||
target: query, |
||||
}); |
||||
}; |
||||
|
||||
beforeEach(() => { |
||||
jest.clearAllMocks(); |
||||
}); |
||||
|
||||
it('Should not call query method in case query is filtered out', async () => { |
||||
setup({ |
||||
query: { q: 'SUM(foo)', refId: 'A' }, |
||||
filterQuery: (query: TestQuery) => query.q !== 'SUM(foo)', |
||||
}); |
||||
expect(filterQuerySpy).toHaveBeenCalledTimes(1); |
||||
expect(querySpy).not.toHaveBeenCalled(); |
||||
}); |
||||
|
||||
it('Should call backend in case query is not filtered out', async () => { |
||||
setup({ |
||||
filterQuery: (_: DataQuery) => true, |
||||
query: { q: 'SUM(foo)', refId: 'A' }, |
||||
}); |
||||
expect(filterQuerySpy).toHaveBeenCalledTimes(1); |
||||
expect(querySpy).toHaveBeenCalledTimes(1); |
||||
}); |
||||
}); |
@ -0,0 +1,77 @@ |
||||
import { Observable } from 'rxjs'; |
||||
|
||||
import { |
||||
DataQuery, |
||||
DataSourceJsonData, |
||||
PluginMetaInfo, |
||||
DataSourcePluginMeta, |
||||
PluginType, |
||||
DataSourceInstanceSettings, |
||||
DataSourceApi, |
||||
DataQueryRequest, |
||||
DataQueryResponse, |
||||
TestDataSourceResponse, |
||||
} from '@grafana/data'; |
||||
|
||||
export interface TestQuery extends DataQuery { |
||||
q?: string; |
||||
} |
||||
|
||||
export interface TestJsonData extends DataSourceJsonData { |
||||
url?: string; |
||||
} |
||||
|
||||
const info: PluginMetaInfo = { |
||||
author: { |
||||
name: '', |
||||
}, |
||||
description: '', |
||||
links: [], |
||||
logos: { |
||||
large: '', |
||||
small: '', |
||||
}, |
||||
screenshots: [], |
||||
updated: '', |
||||
version: '', |
||||
}; |
||||
|
||||
export const meta: DataSourcePluginMeta<DataSourceJsonData> = { |
||||
id: '', |
||||
name: '', |
||||
type: PluginType.datasource, |
||||
info, |
||||
module: '', |
||||
baseUrl: '', |
||||
}; |
||||
|
||||
export const TestDataSettings: DataSourceInstanceSettings<TestJsonData> = { |
||||
jsonData: { url: 'http://localhost:3000' }, |
||||
id: 0, |
||||
uid: '', |
||||
type: '', |
||||
name: 'Test Datasource', |
||||
meta, |
||||
readOnly: false, |
||||
access: 'direct', |
||||
}; |
||||
|
||||
export class TestDataSource extends DataSourceApi<TestQuery, DataSourceJsonData, {}> { |
||||
constructor(instanceSettings: DataSourceInstanceSettings<TestJsonData> = TestDataSettings) { |
||||
super(instanceSettings); |
||||
} |
||||
|
||||
query(request: DataQueryRequest<TestQuery>): Promise<DataQueryResponse> | Observable<DataQueryResponse> { |
||||
return Promise.resolve({ |
||||
data: [], |
||||
}); |
||||
} |
||||
|
||||
testDatasource(): Promise<TestDataSourceResponse> { |
||||
throw new Error('Method not implemented.'); |
||||
} |
||||
} |
||||
|
||||
export const getMockDataSource = () => { |
||||
return new TestDataSource(); |
||||
}; |
Loading…
Reference in new issue