mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Dashboard: replace datasource name with a reference object (#33817)
Co-authored-by: Torkel Ödegaard <torkel@grafana.com> Co-authored-by: Elfo404 <me@giordanoricci.com>
This commit is contained in:
@@ -6,17 +6,18 @@ import {
|
||||
DataQuery,
|
||||
DataQueryRequest,
|
||||
DataSourceApi,
|
||||
DataSourceRef,
|
||||
getDefaultTimeRange,
|
||||
LoadingState,
|
||||
PanelData,
|
||||
} from '@grafana/data';
|
||||
|
||||
export function isSharedDashboardQuery(datasource: string | DataSourceApi | null) {
|
||||
export function isSharedDashboardQuery(datasource: string | DataSourceRef | DataSourceApi | null) {
|
||||
if (!datasource) {
|
||||
// default datasource
|
||||
return false;
|
||||
}
|
||||
if (datasource === SHARED_DASHBODARD_QUERY) {
|
||||
if (datasource === SHARED_DASHBODARD_QUERY || (datasource as any)?.uid === SHARED_DASHBODARD_QUERY) {
|
||||
return true;
|
||||
}
|
||||
const ds = datasource as DataSourceApi;
|
||||
|
||||
@@ -385,7 +385,7 @@ export class ElasticDatasource
|
||||
const expandedQueries = queries.map(
|
||||
(query): ElasticsearchQuery => ({
|
||||
...query,
|
||||
datasource: this.name,
|
||||
datasource: this.getRef(),
|
||||
query: this.interpolateLuceneQuery(query.query || '', scopedVars),
|
||||
bucketAggs: query.bucketAggs?.map(interpolateBucketAgg),
|
||||
})
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
DataQueryRequest,
|
||||
DataQueryResponse,
|
||||
DataSourceInstanceSettings,
|
||||
DatasourceRef,
|
||||
DataSourceRef,
|
||||
isValidLiveChannelAddress,
|
||||
parseLiveChannelAddress,
|
||||
StreamingFrameOptions,
|
||||
@@ -18,6 +18,7 @@ import { GrafanaAnnotationQuery, GrafanaAnnotationType, GrafanaQuery, GrafanaQue
|
||||
import AnnotationQueryEditor from './components/AnnotationQueryEditor';
|
||||
import { getDashboardSrv } from '../../../features/dashboard/services/DashboardSrv';
|
||||
import { isString } from 'lodash';
|
||||
import { migrateDatasourceNameToRef } from 'app/features/dashboard/state/DashboardMigrator';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
let counter = 100;
|
||||
@@ -39,10 +40,16 @@ export class GrafanaDatasource extends DataSourceWithBackend<GrafanaQuery> {
|
||||
return json;
|
||||
},
|
||||
prepareQuery(anno: AnnotationQuery<GrafanaAnnotationQuery>): GrafanaQuery {
|
||||
let datasource: DatasourceRef | undefined | null = undefined;
|
||||
let datasource: DataSourceRef | undefined | null = undefined;
|
||||
if (isString(anno.datasource)) {
|
||||
datasource = anno.datasource as DatasourceRef;
|
||||
const ref = migrateDatasourceNameToRef(anno.datasource);
|
||||
if (ref) {
|
||||
datasource = ref;
|
||||
}
|
||||
} else {
|
||||
datasource = anno.datasource as DataSourceRef;
|
||||
}
|
||||
|
||||
return { ...anno, refId: anno.name, queryType: GrafanaQueryType.Annotations, datasource };
|
||||
},
|
||||
};
|
||||
|
||||
@@ -231,7 +231,7 @@ export class GraphiteDatasource extends DataSourceApi<
|
||||
expandedQueries = queries.map((query) => {
|
||||
const expandedQuery = {
|
||||
...query,
|
||||
datasource: this.name,
|
||||
datasource: this.getRef(),
|
||||
target: this.templateSrv.replace(query.target ?? '', scopedVars),
|
||||
};
|
||||
return expandedQuery;
|
||||
|
||||
@@ -342,7 +342,7 @@ export default class InfluxDatasource extends DataSourceWithBackend<InfluxQuery,
|
||||
expandedQueries = queries.map((query) => {
|
||||
const expandedQuery = {
|
||||
...query,
|
||||
datasource: this.name,
|
||||
datasource: this.getRef(),
|
||||
measurement: this.templateSrv.replace(query.measurement ?? '', scopedVars, 'regex'),
|
||||
policy: this.templateSrv.replace(query.policy ?? '', scopedVars, 'regex'),
|
||||
};
|
||||
|
||||
@@ -311,7 +311,7 @@ export class LokiDatasource
|
||||
if (queries && queries.length) {
|
||||
expandedQueries = queries.map((query) => ({
|
||||
...query,
|
||||
datasource: this.name,
|
||||
datasource: this.getRef(),
|
||||
expr: this.templateSrv.replace(query.expr, scopedVars, this.interpolateQueryExpr),
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -30,9 +30,9 @@ describe('MixedDatasource', () => {
|
||||
const ds = new MixedDatasource({} as any);
|
||||
const requestMixed = getQueryOptions({
|
||||
targets: [
|
||||
{ refId: 'QA', datasource: 'A' }, // 1
|
||||
{ refId: 'QB', datasource: 'B' }, // 2
|
||||
{ refId: 'QC', datasource: 'C' }, // 3
|
||||
{ refId: 'QA', datasource: { uid: 'A' } }, // 1
|
||||
{ refId: 'QB', datasource: { uid: 'B' } }, // 2
|
||||
{ refId: 'QC', datasource: { uid: 'C' } }, // 3
|
||||
],
|
||||
});
|
||||
|
||||
@@ -52,11 +52,11 @@ describe('MixedDatasource', () => {
|
||||
const ds = new MixedDatasource({} as any);
|
||||
const requestMixed = getQueryOptions({
|
||||
targets: [
|
||||
{ refId: 'QA', datasource: 'A' }, // 1
|
||||
{ refId: 'QD', datasource: 'D' }, // 2
|
||||
{ refId: 'QB', datasource: 'B' }, // 3
|
||||
{ refId: 'QE', datasource: 'E' }, // 4
|
||||
{ refId: 'QC', datasource: 'C' }, // 5
|
||||
{ refId: 'QA', datasource: { uid: 'A' } }, // 1
|
||||
{ refId: 'QD', datasource: { uid: 'D' } }, // 2
|
||||
{ refId: 'QB', datasource: { uid: 'B' } }, // 3
|
||||
{ refId: 'QE', datasource: { uid: 'E' } }, // 4
|
||||
{ refId: 'QC', datasource: { uid: 'C' } }, // 5
|
||||
],
|
||||
});
|
||||
|
||||
@@ -84,9 +84,9 @@ describe('MixedDatasource', () => {
|
||||
const ds = new MixedDatasource({} as any);
|
||||
const request: any = {
|
||||
targets: [
|
||||
{ refId: 'A', datasource: 'Loki' },
|
||||
{ refId: 'B', datasource: 'Loki' },
|
||||
{ refId: 'C', datasource: 'A' },
|
||||
{ refId: 'A', datasource: { uid: 'Loki' } },
|
||||
{ refId: 'B', datasource: { uid: 'Loki' } },
|
||||
{ refId: 'C', datasource: { uid: 'A' } },
|
||||
],
|
||||
};
|
||||
|
||||
@@ -115,8 +115,8 @@ describe('MixedDatasource', () => {
|
||||
await expect(
|
||||
ds.query({
|
||||
targets: [
|
||||
{ refId: 'QA', datasource: 'A' },
|
||||
{ refId: 'QB', datasource: 'B' },
|
||||
{ refId: 'QA', datasource: { uid: 'A' } },
|
||||
{ refId: 'QB', datasource: { uid: 'B' } },
|
||||
],
|
||||
} as any)
|
||||
).toEmitValuesWith((results) => {
|
||||
|
||||
@@ -26,7 +26,7 @@ export class MixedDatasource extends DataSourceApi<DataQuery> {
|
||||
query(request: DataQueryRequest<DataQuery>): Observable<DataQueryResponse> {
|
||||
// Remove any invalid queries
|
||||
const queries = request.targets.filter((t) => {
|
||||
return t.datasource !== MIXED_DATASOURCE_NAME;
|
||||
return t.datasource?.type !== MIXED_DATASOURCE_NAME;
|
||||
});
|
||||
|
||||
if (!queries.length) {
|
||||
@@ -34,19 +34,23 @@ export class MixedDatasource extends DataSourceApi<DataQuery> {
|
||||
}
|
||||
|
||||
// Build groups of queries to run in parallel
|
||||
const sets: { [key: string]: DataQuery[] } = groupBy(queries, 'datasource');
|
||||
const sets: { [key: string]: DataQuery[] } = groupBy(queries, 'datasource.uid');
|
||||
const mixed: BatchedQueries[] = [];
|
||||
|
||||
for (const key in sets) {
|
||||
const targets = sets[key];
|
||||
const dsName = targets[0].datasource;
|
||||
|
||||
mixed.push({
|
||||
datasource: getDataSourceSrv().get(dsName, request.scopedVars),
|
||||
datasource: getDataSourceSrv().get(targets[0].datasource, request.scopedVars),
|
||||
targets,
|
||||
});
|
||||
}
|
||||
|
||||
// Missing UIDs?
|
||||
if (!mixed.length) {
|
||||
return of({ data: [] } as DataQueryResponse); // nothing
|
||||
}
|
||||
|
||||
return this.batchQueries(mixed, request);
|
||||
}
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ export class MssqlDatasource extends DataSourceWithBackend<MssqlQuery, MssqlOpti
|
||||
expandedQueries = queries.map((query) => {
|
||||
const expandedQuery = {
|
||||
...query,
|
||||
datasource: this.name,
|
||||
datasource: this.getRef(),
|
||||
rawSql: this.templateSrv.replace(query.rawSql, scopedVars, this.interpolateVariable),
|
||||
rawQuery: true,
|
||||
};
|
||||
|
||||
@@ -62,7 +62,7 @@ export class MysqlDatasource extends DataSourceWithBackend<MySQLQuery, MySQLOpti
|
||||
expandedQueries = queries.map((query) => {
|
||||
const expandedQuery = {
|
||||
...query,
|
||||
datasource: this.name,
|
||||
datasource: this.getRef(),
|
||||
rawSql: this.templateSrv.replace(query.rawSql, scopedVars, this.interpolateVariable),
|
||||
rawQuery: true,
|
||||
};
|
||||
|
||||
@@ -64,7 +64,7 @@ export class PostgresDatasource extends DataSourceWithBackend<PostgresQuery, Pos
|
||||
expandedQueries = queries.map((query) => {
|
||||
const expandedQuery = {
|
||||
...query,
|
||||
datasource: this.name,
|
||||
datasource: this.getRef(),
|
||||
rawSql: this.templateSrv.replace(query.rawSql, scopedVars, this.interpolateVariable),
|
||||
rawQuery: true,
|
||||
};
|
||||
|
||||
@@ -780,7 +780,7 @@ export class PrometheusDatasource extends DataSourceWithBackend<PromQuery, PromO
|
||||
expandedQueries = queries.map((query) => {
|
||||
const expandedQuery = {
|
||||
...query,
|
||||
datasource: this.name,
|
||||
datasource: this.getRef(),
|
||||
expr: this.templateSrv.replace(query.expr, scopedVars, this.interpolateQueryExpr),
|
||||
interval: this.templateSrv.replace(query.interval, scopedVars),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user