init.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { PanelData } from '@grafana/data';
  2. import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv';
  3. import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
  4. /**
  5. * This will setup features that are accessible through the root window location
  6. *
  7. * This is useful for manipulating the application from external drivers like puppetter/cypress
  8. *
  9. * @internal and subject to change
  10. */
  11. export function initWindowRuntime() {
  12. (window as any).grafanaRuntime = {
  13. /** Get info for the current dashboard. This will include the migrated dashboard JSON */
  14. getDashboardSaveModel: () => {
  15. const d = getDashboardSrv().getCurrent();
  16. if (!d) {
  17. return undefined;
  18. }
  19. return d.getSaveModelClone();
  20. },
  21. /** The selected time range */
  22. getDashboardTimeRange: () => {
  23. const tr = getTimeSrv().timeRange();
  24. return {
  25. from: tr.from.valueOf(),
  26. to: tr.to.valueOf(),
  27. raw: tr.raw,
  28. };
  29. },
  30. /** Get the query results for the last loaded data */
  31. getPanelData: () => {
  32. const d = getDashboardSrv().getCurrent();
  33. if (!d) {
  34. return undefined;
  35. }
  36. return d.panels.reduce((acc, panel) => {
  37. acc[panel.id] = panel.getQueryRunner().getLastResult();
  38. return acc;
  39. }, {} as Record<number, PanelData | undefined>);
  40. },
  41. };
  42. }