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