DataSource: Adding possibility to hide queries from the inspector (#54892)

* adding the possibility to hide queries when they are executed via the DataSourceWithBackend.query
This commit is contained in:
Marcus Andersson 2022-09-13 13:27:16 +02:00 committed by GitHub
parent 6bc0d97c49
commit c95b530502
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 80 additions and 19 deletions

View File

@ -501,6 +501,9 @@ export interface DataQueryRequest<TQuery extends DataQuery = DataQuery> {
// Explore state used by various datasources
liveStreaming?: boolean;
// Make it possible to hide support queries from the inspector
hideFromInspector?: boolean;
}
export interface DataQueryTimings {

View File

@ -1,5 +1,5 @@
import { of } from 'rxjs';
import { BackendSrv, BackendSrvRequest } from 'src/services';
import { BackendSrv, BackendSrvRequest, FetchResponse } from 'src/services';
import {
DataSourceJsonData,
@ -19,7 +19,7 @@ class MyDataSource extends DataSourceWithBackend<DataQuery, DataSourceJsonData>
}
}
const mockDatasourceRequest = jest.fn();
const mockDatasourceRequest = jest.fn<Promise<FetchResponse>, BackendSrvRequest[]>();
const backendSrv = {
fetch: (options: BackendSrvRequest) => {
@ -39,28 +39,15 @@ jest.mock('../services', () => ({
describe('DataSourceWithBackend', () => {
test('check the executed queries', () => {
const settings = {
name: 'test',
id: 1234,
uid: 'abc',
type: 'dummy',
jsonData: {},
} as DataSourceInstanceSettings<DataSourceJsonData>;
mockDatasourceRequest.mockReset();
mockDatasourceRequest.mockReturnValue(Promise.resolve({}));
const ds = new MyDataSource(settings);
ds.query({
const mock = runQueryAndReturnFetchMock({
maxDataPoints: 10,
intervalMs: 5000,
targets: [{ refId: 'A' }, { refId: 'B', datasource: { type: 'sample' } }],
} as DataQueryRequest);
const mock = mockDatasourceRequest.mock;
expect(mock.calls.length).toBe(1);
const args = mock.calls[0][0];
expect(mock.calls.length).toBe(1);
expect(args).toMatchInlineSnapshot(`
Object {
"data": Object {
@ -87,6 +74,52 @@ describe('DataSourceWithBackend', () => {
},
],
},
"hideFromInspector": false,
"method": "POST",
"requestId": undefined,
"url": "/api/ds/query",
}
`);
});
test('check that the executed queries is hidden from inspector', () => {
const mock = runQueryAndReturnFetchMock({
maxDataPoints: 10,
intervalMs: 5000,
targets: [{ refId: 'A' }, { refId: 'B', datasource: { type: 'sample' } }],
hideFromInspector: true,
} as DataQueryRequest);
const args = mock.calls[0][0];
expect(mock.calls.length).toBe(1);
expect(args).toMatchInlineSnapshot(`
Object {
"data": Object {
"queries": Array [
Object {
"datasource": Object {
"type": "dummy",
"uid": "abc",
},
"datasourceId": 1234,
"intervalMs": 5000,
"maxDataPoints": 10,
"refId": "A",
},
Object {
"datasource": Object {
"type": "sample",
"uid": "?",
},
"datasourceId": undefined,
"intervalMs": 5000,
"maxDataPoints": 10,
"refId": "B",
},
],
},
"hideFromInspector": true,
"method": "POST",
"requestId": undefined,
"url": "/api/ds/query",
@ -116,3 +149,23 @@ describe('DataSourceWithBackend', () => {
expect(obs).toBeDefined();
});
});
function runQueryAndReturnFetchMock(
request: DataQueryRequest
): jest.MockContext<Promise<FetchResponse>, BackendSrvRequest[]> {
const settings = {
name: 'test',
id: 1234,
uid: 'abc',
type: 'dummy',
jsonData: {},
} as DataSourceInstanceSettings<DataSourceJsonData>;
mockDatasourceRequest.mockReset();
mockDatasourceRequest.mockReturnValue(Promise.resolve({} as FetchResponse));
const ds = new MyDataSource(settings);
ds.query(request);
return mockDatasourceRequest.mock;
}

View File

@ -110,7 +110,7 @@ class DataSourceWithBackend<
* Ideally final -- any other implementation may not work as expected
*/
query(request: DataQueryRequest<TQuery>): Observable<DataQueryResponse> {
const { intervalMs, maxDataPoints, range, requestId } = request;
const { intervalMs, maxDataPoints, range, requestId, hideFromInspector = false } = request;
let targets = request.targets;
if (this.filterQuery) {
@ -174,6 +174,7 @@ class DataSourceWithBackend<
method: 'POST',
data: body,
requestId,
hideFromInspector,
})
.pipe(
switchMap((raw) => {

View File

@ -678,13 +678,17 @@ export function queryLogsVolume<TQuery extends DataQuery, TOptions extends DataS
): Observable<DataQueryResponse> {
const timespan = options.range.to.valueOf() - options.range.from.valueOf();
const intervalInfo = getIntervalInfo(logsVolumeRequest.scopedVars, timespan);
logsVolumeRequest.interval = intervalInfo.interval;
logsVolumeRequest.scopedVars.__interval = { value: intervalInfo.interval, text: intervalInfo.interval };
if (intervalInfo.intervalMs !== undefined) {
logsVolumeRequest.intervalMs = intervalInfo.intervalMs;
logsVolumeRequest.scopedVars.__interval_ms = { value: intervalInfo.intervalMs, text: intervalInfo.intervalMs };
}
logsVolumeRequest.hideFromInspector = true;
return new Observable((observer) => {
let rawLogsVolume: DataFrame[] = [];
observer.next({