Commit eaba5028 by Ryan McKinley Committed by Torkel Ödegaard

MixedQuery: refactor so other components could also batch queries (#20219)

* query mixed

* query mixed

* batch queries

* check that the values only have 3 rows at the *end* as well
parent dbc68ef6
......@@ -47,5 +47,6 @@ describe('MixedDatasource', () => {
expect(results[1].data).toEqual(['BBBB']);
expect(results[2].data).toEqual(['CCCC']);
expect(results[2].state).toEqual(LoadingState.Done);
expect(results.length).toBe(3);
});
});
......@@ -16,6 +16,11 @@ import { mergeMap, map } from 'rxjs/operators';
export const MIXED_DATASOURCE_NAME = '-- Mixed --';
export interface BatchedQueries {
datasource: Promise<DataSourceApi>;
targets: DataQuery[];
}
export class MixedDatasource extends DataSourceApi<DataQuery> {
constructor(instanceSettings: DataSourceInstanceSettings) {
super(instanceSettings);
......@@ -31,24 +36,35 @@ export class MixedDatasource extends DataSourceApi<DataQuery> {
return of({ data: [] } as DataQueryResponse); // nothing
}
// Build groups of queries to run in parallel
const sets: { [key: string]: DataQuery[] } = groupBy(queries, 'datasource');
const observables: Array<Observable<DataQueryResponse>> = [];
let runningSubRequests = 0;
const mixed: BatchedQueries[] = [];
for (const key in sets) {
const targets = sets[key];
const dsName = targets[0].datasource;
mixed.push({
datasource: getDataSourceSrv().get(dsName),
targets,
});
}
return this.batchQueries(mixed, request);
}
const observable = from(getDataSourceSrv().get(dsName)).pipe(
batchQueries(mixed: BatchedQueries[], request: DataQueryRequest<DataQuery>): Observable<DataQueryResponse> {
const observables: Array<Observable<DataQueryResponse>> = [];
let runningSubRequests = 0;
for (let i = 0; i < mixed.length; i++) {
const query = mixed[i];
if (!query.targets || !query.targets.length) {
continue;
}
const observable = from(query.datasource).pipe(
mergeMap((dataSourceApi: DataSourceApi) => {
const datasourceRequest = cloneDeep(request);
datasourceRequest.requestId = `${dsName}${datasourceRequest.requestId || ''}`;
// all queries hidden return empty result for for this requestId
if (datasourceRequest.targets.length === 0) {
return of({ data: [], key: datasourceRequest.requestId });
}
datasourceRequest.requestId = `mixed-${i}-${datasourceRequest.requestId || ''}`;
datasourceRequest.targets = query.targets;
runningSubRequests++;
let hasCountedAsDone = false;
......@@ -79,7 +95,7 @@ export class MixedDatasource extends DataSourceApi<DataQuery> {
...response,
data: response.data || [],
state: runningSubRequests === 0 ? LoadingState.Done : LoadingState.Loading,
key: `${dsName}${response.key || ''}`,
key: `mixed-${i}-${response.key || ''}`,
} as DataQueryResponse;
})
);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment