mirror of
https://github.com/grafana/grafana.git
synced 2025-01-17 12:03:26 -06:00
Explore: simplify URL generation logic (#40640)
* Explore: simplify URL generation logic * Fix test
This commit is contained in:
parent
2c28aea654
commit
0e63ad44aa
@ -236,11 +236,8 @@ export class KeybindingSrv {
|
||||
if (contextSrv.hasAccessToExplore()) {
|
||||
this.bindWithPanelId('x', async (panelId) => {
|
||||
const panel = dashboard.getPanelById(panelId)!;
|
||||
const datasource = await getDatasourceSrv().get(panel.datasource);
|
||||
const url = await getExploreUrl({
|
||||
panel,
|
||||
panelTargets: panel.targets,
|
||||
panelDatasource: datasource,
|
||||
datasourceSrv: getDatasourceSrv(),
|
||||
timeSrv: getTimeSrv(),
|
||||
});
|
||||
|
@ -194,16 +194,12 @@ describe('getExploreUrl', () => {
|
||||
const args = ({
|
||||
panel: {
|
||||
getSavedId: () => 1,
|
||||
},
|
||||
panelTargets: [{ refId: 'A', expr: 'query1', legendFormat: 'legendFormat1' }],
|
||||
panelDatasource: {
|
||||
name: 'testDataSource',
|
||||
meta: {
|
||||
id: '1',
|
||||
},
|
||||
targets: [{ refId: 'A', expr: 'query1', legendFormat: 'legendFormat1' }],
|
||||
},
|
||||
datasourceSrv: {
|
||||
get: jest.fn(),
|
||||
get() {
|
||||
return {};
|
||||
},
|
||||
getDataSourceById: jest.fn(),
|
||||
},
|
||||
timeSrv: {
|
||||
|
@ -49,40 +49,35 @@ const MAX_HISTORY_ITEMS = 100;
|
||||
export const LAST_USED_DATASOURCE_KEY = 'grafana.explore.datasource';
|
||||
export const lastUsedDatasourceKeyForOrgId = (orgId: number) => `${LAST_USED_DATASOURCE_KEY}.${orgId}`;
|
||||
|
||||
/**
|
||||
* Returns an Explore-URL that contains a panel's queries and the dashboard time range.
|
||||
*
|
||||
* @param panelTargets The origin panel's query targets
|
||||
* @param panelDatasource The origin panel's datasource
|
||||
* @param datasourceSrv Datasource service to query other datasources in case the panel datasource is mixed
|
||||
* @param timeSrv Time service to get the current dashboard range from
|
||||
*/
|
||||
export interface GetExploreUrlArguments {
|
||||
panel: PanelModel;
|
||||
panelTargets: DataQuery[];
|
||||
panelDatasource: DataSourceApi;
|
||||
/** Datasource service to query other datasources in case the panel datasource is mixed */
|
||||
datasourceSrv: DataSourceSrv;
|
||||
/** Time service to get the current dashboard range from */
|
||||
timeSrv: TimeSrv;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an Explore-URL that contains a panel's queries and the dashboard time range.
|
||||
*/
|
||||
export async function getExploreUrl(args: GetExploreUrlArguments): Promise<string | undefined> {
|
||||
const { panel, panelTargets, panelDatasource, datasourceSrv, timeSrv } = args;
|
||||
let exploreDatasource = panelDatasource;
|
||||
const { panel, datasourceSrv, timeSrv } = args;
|
||||
let exploreDatasource = await datasourceSrv.get(panel.datasource);
|
||||
|
||||
/** In Explore, we don't have legend formatter and we don't want to keep
|
||||
* legend formatting as we can't change it
|
||||
*/
|
||||
let exploreTargets: DataQuery[] = panelTargets.map((t) => omit(t, 'legendFormat'));
|
||||
let exploreTargets: DataQuery[] = panel.targets.map((t) => omit(t, 'legendFormat'));
|
||||
let url: string | undefined;
|
||||
|
||||
// Mixed datasources need to choose only one datasource
|
||||
if (panelDatasource.meta?.id === 'mixed' && exploreTargets) {
|
||||
if (exploreDatasource.meta?.id === 'mixed' && exploreTargets) {
|
||||
// Find first explore datasource among targets
|
||||
for (const t of exploreTargets) {
|
||||
const datasource = await datasourceSrv.get(t.datasource || undefined);
|
||||
if (datasource) {
|
||||
exploreDatasource = datasource;
|
||||
exploreTargets = panelTargets.filter((t) => t.datasource === datasource.name);
|
||||
exploreTargets = panel.targets.filter((t) => t.datasource === datasource.name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +27,6 @@ const getNavigateToExploreContext = async (openInNewWindow?: (url: string) => vo
|
||||
return {
|
||||
url,
|
||||
panel,
|
||||
datasource,
|
||||
get,
|
||||
getDataSourceSrv,
|
||||
getTimeSrv,
|
||||
@ -51,13 +50,6 @@ describe('navigateToExplore', () => {
|
||||
expect(getDataSourceSrv).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('then getDataSourceSrv.get should have been called with correct arguments', async () => {
|
||||
const { get, panel } = await getNavigateToExploreContext(openInNewWindow);
|
||||
|
||||
expect(get).toHaveBeenCalledTimes(1);
|
||||
expect(get).toHaveBeenCalledWith(panel.datasource);
|
||||
});
|
||||
|
||||
it('then getTimeSrv should have been called once', async () => {
|
||||
const { getTimeSrv } = await getNavigateToExploreContext(openInNewWindow);
|
||||
|
||||
@ -65,15 +57,13 @@ describe('navigateToExplore', () => {
|
||||
});
|
||||
|
||||
it('then getExploreUrl should have been called with correct arguments', async () => {
|
||||
const { getExploreUrl, panel, datasource, getDataSourceSrv, getTimeSrv } = await getNavigateToExploreContext(
|
||||
const { getExploreUrl, panel, getDataSourceSrv, getTimeSrv } = await getNavigateToExploreContext(
|
||||
openInNewWindow
|
||||
);
|
||||
|
||||
expect(getExploreUrl).toHaveBeenCalledTimes(1);
|
||||
expect(getExploreUrl).toHaveBeenCalledWith({
|
||||
panel,
|
||||
panelTargets: panel.targets,
|
||||
panelDatasource: datasource,
|
||||
datasourceSrv: getDataSourceSrv(),
|
||||
timeSrv: getTimeSrv(),
|
||||
});
|
||||
@ -94,13 +84,6 @@ describe('navigateToExplore', () => {
|
||||
expect(getDataSourceSrv).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('then getDataSourceSrv.get should have been called with correct arguments', async () => {
|
||||
const { get, panel } = await getNavigateToExploreContext(openInNewWindow);
|
||||
|
||||
expect(get).toHaveBeenCalledTimes(1);
|
||||
expect(get).toHaveBeenCalledWith(panel.datasource);
|
||||
});
|
||||
|
||||
it('then getTimeSrv should have been called once', async () => {
|
||||
const { getTimeSrv } = await getNavigateToExploreContext(openInNewWindow);
|
||||
|
||||
@ -108,15 +91,13 @@ describe('navigateToExplore', () => {
|
||||
});
|
||||
|
||||
it('then getExploreUrl should have been called with correct arguments', async () => {
|
||||
const { getExploreUrl, panel, datasource, getDataSourceSrv, getTimeSrv } = await getNavigateToExploreContext(
|
||||
const { getExploreUrl, panel, getDataSourceSrv, getTimeSrv } = await getNavigateToExploreContext(
|
||||
openInNewWindow
|
||||
);
|
||||
|
||||
expect(getExploreUrl).toHaveBeenCalledTimes(1);
|
||||
expect(getExploreUrl).toHaveBeenCalledWith({
|
||||
panel,
|
||||
panelTargets: panel.targets,
|
||||
panelDatasource: datasource,
|
||||
datasourceSrv: getDataSourceSrv(),
|
||||
timeSrv: getTimeSrv(),
|
||||
});
|
||||
|
@ -138,11 +138,8 @@ export const navigateToExplore = (
|
||||
return async (dispatch) => {
|
||||
const { getDataSourceSrv, getTimeSrv, getExploreUrl, openInNewWindow } = dependencies;
|
||||
const datasourceSrv = getDataSourceSrv();
|
||||
const datasource = await datasourceSrv.get(panel.datasource);
|
||||
const path = await getExploreUrl({
|
||||
panel,
|
||||
panelTargets: panel.targets,
|
||||
panelDatasource: datasource,
|
||||
datasourceSrv,
|
||||
timeSrv: getTimeSrv(),
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user