Files
grafana/public/test/mocks/datasource_srv.ts
Matias Chomicki 2a734a9e3f Supplementary queries: allow plugin decoupling by allowing providers to return a request instance (#80281)
* Supplementary queries: add support for providers returning a request instance

* Formatting

* DataSourceWithSupplementaryQueriesSupport: update getDataProvider signature

* getLogLevelFromLabels: fix buggy implementation

* getLogLevelFromKey: fix key type

Why number??

* Revert "getLogLevelFromKey: fix key type"

This reverts commit 14a95298a6f803cc3270e0421b2e04dd0d65f131.

* getSupplementaryQueryProvider: remove observable support

* Datasources: remove unnecessary check

The switch is doing the same job

* Supplementary queries: update unit test

* datasource_srv: sync mock with real api

* Formatting

* Supplementary queries: pass targets from getSupplementaryQueryProvider

* LogsVolumeQueryOptions: remove range and make extract level optional

* logsModel: add missing range to test data

* query: sync tests with changes

* Formatting

* DataSourceWithSupplementaryQueriesSupport: update interface with deprecated and new methods

* DataSourceWithSupplementaryQueriesSupport: sync Loki and Elasticsearch

* queryLogsVolume: extractLevel no longer customizable

* Loki: update test

* Supplementary queries: add support for the new method

* hasSupplementaryQuerySupport: update signature

* Formatting

* Betterer

* Query: update test

* Supplementary queries: add test for the legacy API

* Update public/app/features/explore/utils/supplementaryQueries.ts

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>

---------

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>
2024-01-30 12:36:20 +02:00

110 lines
2.6 KiB
TypeScript

import { Observable } from 'rxjs';
import {
DataQueryRequest,
DataQueryResponse,
TestDataSourceResponse,
DataSourceApi,
DataSourceInstanceSettings,
DataSourcePluginMeta,
DataSourceRef,
getDataSourceUID,
} from '@grafana/data';
export class DatasourceSrvMock {
constructor(
private defaultDS: DataSourceApi,
private datasources: { [name: string]: DataSourceApi }
) {
//
}
get(ref?: DataSourceRef | string): Promise<DataSourceApi> {
if (!ref) {
return Promise.resolve(this.defaultDS);
}
const uid = getDataSourceUID(ref) ?? '';
const ds = this.datasources[uid];
if (ds) {
return Promise.resolve(ds);
}
return Promise.reject(`Unknown Datasource: ${JSON.stringify(ref)}`);
}
}
export class MockDataSourceApi extends DataSourceApi {
result: DataQueryResponse = { data: [] };
constructor(
name?: string,
result?: DataQueryResponse,
meta?: DataSourcePluginMeta,
public error: string | null = null
) {
super({ name: name ? name : 'MockDataSourceApi' } as DataSourceInstanceSettings);
if (result) {
this.result = result;
}
this.meta = meta || ({} as DataSourcePluginMeta);
}
query(request: DataQueryRequest): Promise<DataQueryResponse> | Observable<DataQueryResponse> {
if (this.error) {
return Promise.reject(this.error);
}
return new Promise((resolver) => {
setTimeout(() => {
resolver(this.result);
});
});
}
testDatasource(): Promise<TestDataSourceResponse> {
return Promise.resolve({ message: '', status: '' });
}
setupMixed(value: boolean) {
this.meta = this.meta || {};
this.meta.mixed = value;
return this;
}
}
export class MockObservableDataSourceApi extends DataSourceApi {
results: DataQueryResponse[] = [{ data: [] }];
constructor(
name?: string,
results?: DataQueryResponse[],
meta?: DataSourcePluginMeta,
private error: string | null = null
) {
super({ name: name ? name : 'MockDataSourceApi' } as DataSourceInstanceSettings);
if (results) {
this.results = results;
}
this.meta = meta || ({} as DataSourcePluginMeta);
}
query(request: DataQueryRequest): Observable<DataQueryResponse> {
return new Observable((observer) => {
if (this.error) {
observer.error(this.error);
}
if (this.results) {
this.results.forEach((response) => observer.next(response));
observer.complete();
}
});
}
testDatasource(): Promise<TestDataSourceResponse> {
return Promise.resolve({ message: '', status: '' });
}
}