Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datasources: Propagate TestDataDB default selection to PanelModel #57703

Closed
wants to merge 10 commits into from
16 changes: 15 additions & 1 deletion e2e/various-suite/explore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ e2e.scenario({

// Both queries above should have been run and be shown in the query history
e2e.components.QueryTab.queryHistoryButton().should('be.visible').click();
e2e.components.QueryHistory.queryText().should('have.length', 2).should('contain', 'csv_metric_values');
e2e.components.QueryHistory.queryText()
.should('have.length.gte', 3)
.should('contain', 'csv_metric_values')
.should('contain', 'random_walk');
// Latest three queries should be [csv_metric_values, csv_metric_values, random_walk]
e2e.components.QueryHistory.queryText().each(($el, i) => {
if (i === 0 || i === 1) {
expect($el.text()).to.equal('csv_metric_values');
} else if (i === 2) {
expect($el.text()).to.equal('random_walk');
} else {
return false;
}
return true;
});
},
});
62 changes: 46 additions & 16 deletions public/app/features/dashboard/state/PanelModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { cloneDeep, defaultsDeep, isArray, isEqual, keys } from 'lodash';
import { v4 as uuidv4 } from 'uuid';

import {
CoreApp,
DataConfigSource,
DataFrameDTO,
DataLink,
Expand All @@ -17,7 +18,7 @@ import {
PanelModel as IPanelModel,
DataSourceRef,
} from '@grafana/data';
import { getTemplateSrv, RefreshEvent } from '@grafana/runtime';
import { getTemplateSrv, getDataSourceSrv, RefreshEvent } from '@grafana/runtime';
import config from 'app/core/config';
import { safeStringifyValue } from 'app/core/utils/explore';
import { getNextRefIdChar } from 'app/core/utils/query';
Expand Down Expand Up @@ -269,6 +270,33 @@ export class PanelModel implements DataConfigSource, IPanelModel {
}
}

// Attempts to populate each panel target with a well-formed query definition
async ensureWellFormedQueries() {
if (this.targets && isArray(this.targets)) {
// If we modified any targets, publish one update event
let shouldPublish = false;
for (let i = 0; i < this.targets.length; i++) {
const query = this.targets[i];
if (!query.datasource && this.datasource) {
query.datasource = this.datasource;
}
if (Object.keys(query).length === 2) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is based on the combination of the above if statement, plus the ensureQueryIds() function. If the only two properties on the query are the ones we are hard-coding in, the query is not well-formed. If there is a better way of inferring this, let me know.

// If query has only refId and datasource, try to set the default query for its datasource
const datasourceInstance = await getDataSourceSrv().get(query.datasource);
const defaultQuery = datasourceInstance.getDefaultQuery?.(CoreApp.PanelEditor);
mmandrus marked this conversation as resolved.
Show resolved Hide resolved
if (defaultQuery) {
this.targets[i] = { ...defaultQuery, ...query };
shouldPublish = true;
}
}
}
if (shouldPublish) {
this.configRev++;
this.events.publish(new PanelQueriesChangedEvent());
}
mmandrus marked this conversation as resolved.
Show resolved Hide resolved
}
}

getOptions() {
return this.options;
}
Expand Down Expand Up @@ -342,21 +370,23 @@ export class PanelModel implements DataConfigSource, IPanelModel {
width,
publicDashboardAccessToken,
}: RunPanelQueryOptions) {
this.getQueryRunner().run({
datasource: this.datasource,
queries: this.targets,
panelId: this.id,
dashboardId: dashboardId,
dashboardUID: dashboardUID,
publicDashboardAccessToken,
timezone: dashboardTimezone,
timeRange: timeData.timeRange,
timeInfo: timeData.timeInfo,
maxDataPoints: this.maxDataPoints || Math.floor(width),
minInterval: this.interval,
scopedVars: this.scopedVars,
cacheTimeout: this.cacheTimeout,
transformations: this.transformations,
this.ensureWellFormedQueries().then(() => {
this.getQueryRunner().run({
datasource: this.datasource,
queries: this.targets,
panelId: this.id,
dashboardId: dashboardId,
dashboardUID: dashboardUID,
publicDashboardAccessToken,
timezone: dashboardTimezone,
timeRange: timeData.timeRange,
timeInfo: timeData.timeInfo,
maxDataPoints: this.maxDataPoints || Math.floor(width),
minInterval: this.interval,
scopedVars: this.scopedVars,
cacheTimeout: this.cacheTimeout,
transformations: this.transformations,
});
});
}

Expand Down
17 changes: 17 additions & 0 deletions public/app/plugins/datasource/testdata/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { delay } from 'rxjs/operators';
import {
AnnotationEvent,
ArrayDataFrame,
CoreApp,
DataFrame,
DataQueryRequest,
DataQueryResponse,
Expand All @@ -18,6 +19,7 @@ import {
import { DataSourceWithBackend, getBackendSrv, getGrafanaLiveSrv, getTemplateSrv, TemplateSrv } from '@grafana/runtime';
import { getSearchFilterScopedVar } from 'app/features/variables/utils';

import { defaultQuery } from './constants';
import { queryMetricTree } from './metricTree';
import { generateRandomNodes, savedNodesResponse } from './nodeGraphUtils';
import { runStream } from './runStreams';
Expand Down Expand Up @@ -258,6 +260,21 @@ export class TestDataDataSource extends DataSourceWithBackend<TestDataQuery> {

return null;
}

getDefaultQuery(app: CoreApp): TestDataQuery {
switch (app) {
// Ignore the app input for now, always use the same default query
case CoreApp.CloudAlerting:
case CoreApp.UnifiedAlerting:
case CoreApp.Dashboard:
case CoreApp.Explore:
case CoreApp.PanelEditor:
case CoreApp.PanelViewer:
case CoreApp.Unknown:
default:
return defaultQuery;
}
}
Comment on lines +264 to +277
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
getDefaultQuery(app: CoreApp): TestDataQuery {
switch (app) {
// Ignore the app input for now, always use the same default query
case CoreApp.CloudAlerting:
case CoreApp.UnifiedAlerting:
case CoreApp.Dashboard:
case CoreApp.Explore:
case CoreApp.PanelEditor:
case CoreApp.PanelViewer:
case CoreApp.Unknown:
default:
return defaultQuery;
}
}
getDefaultQuery(app: CoreApp): TestDataQuery {
return defaultQuery;
}

}

function runGrafanaAPI(target: TestDataQuery, req: DataQueryRequest<TestDataQuery>): Observable<DataQueryResponse> {
Expand Down