explore.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { renderUrl } from 'app/core/utils/url';
  2. /**
  3. * Returns an Explore-URL that contains a panel's queries and the dashboard time range.
  4. *
  5. * @param panel Origin panel of the jump to Explore
  6. * @param panelTargets The origin panel's query targets
  7. * @param panelDatasource The origin panel's datasource
  8. * @param datasourceSrv Datasource service to query other datasources in case the panel datasource is mixed
  9. * @param timeSrv Time service to get the current dashboard range from
  10. */
  11. export async function getExploreUrl(
  12. panel: any,
  13. panelTargets: any[],
  14. panelDatasource: any,
  15. datasourceSrv: any,
  16. timeSrv: any
  17. ) {
  18. let exploreDatasource = panelDatasource;
  19. let exploreTargets = panelTargets;
  20. let url;
  21. // Mixed datasources need to choose only one datasource
  22. if (panelDatasource.meta.id === 'mixed' && panelTargets) {
  23. // Find first explore datasource among targets
  24. let mixedExploreDatasource;
  25. for (const t of panel.targets) {
  26. const datasource = await datasourceSrv.get(t.datasource);
  27. if (datasource && datasource.meta.explore) {
  28. mixedExploreDatasource = datasource;
  29. break;
  30. }
  31. }
  32. // Add all its targets
  33. if (mixedExploreDatasource) {
  34. exploreDatasource = mixedExploreDatasource;
  35. exploreTargets = panelTargets.filter(t => t.datasource === mixedExploreDatasource.name);
  36. }
  37. }
  38. if (exploreDatasource && exploreDatasource.meta.explore) {
  39. const range = timeSrv.timeRangeForUrl();
  40. const state = {
  41. ...exploreDatasource.getExploreState(exploreTargets),
  42. range,
  43. };
  44. const exploreState = JSON.stringify(state);
  45. url = renderUrl('/explore', { state: exploreState });
  46. }
  47. return url;
  48. }