From d41044bd8e8cf89fc5075f5d15ef205c9580e639 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Sun, 5 Dec 2021 22:42:35 -0800 Subject: [PATCH] Runtime: support driving grafana app from the console (#42762) --- public/app/app.ts | 2 + public/app/features/runtime/init.ts | 59 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 public/app/features/runtime/init.ts diff --git a/public/app/app.ts b/public/app/app.ts index 473d122d973..5a04fe47580 100644 --- a/public/app/app.ts +++ b/public/app/app.ts @@ -65,6 +65,7 @@ import { setPanelDataErrorView } from '@grafana/runtime/src/components/PanelData import { DatasourceSrv } from './features/plugins/datasource_srv'; import { AngularApp } from './angular'; import { ModalManager } from './core/services/ModalManager'; +import { initWindowRuntime } from './features/runtime/init'; // add move to lodash for backward compatabilty with plugins // @ts-ignore @@ -125,6 +126,7 @@ export class GrafanaApp { const dataSourceSrv = new DatasourceSrv(); dataSourceSrv.init(config.datasources, config.defaultDatasource); setDataSourceSrv(dataSourceSrv); + initWindowRuntime(); // init modal manager const modalManager = new ModalManager(); diff --git a/public/app/features/runtime/init.ts b/public/app/features/runtime/init.ts new file mode 100644 index 00000000000..593f99f454a --- /dev/null +++ b/public/app/features/runtime/init.ts @@ -0,0 +1,59 @@ +import { UrlQueryMap, PanelData } from '@grafana/data'; +import { getLocationSrv } from '@grafana/runtime'; +import { getDashboardSrv } from '../dashboard/services/DashboardSrv'; +import { getTimeSrv } from '../dashboard/services/TimeSrv'; + +/** + * This will setup features that are accessible through the root window location + * + * This is useful for manipulating the application from external drivers like puppetter/cypress + * + * @internal and subject to change + */ +export function initWindowRuntime() { + (window as any).grafanaRuntime = { + /** Navigate the page within the currently loaded application */ + updateLocation: (path: string, query?: UrlQueryMap) => { + if (query?.theme) { + throw new Error(`chaning theme requires full page refresh`); + } + getLocationSrv().update({ + path, + query, + replace: true, + partial: false, + }); + }, + + /** Get info for the current dashboard. This will include the migrated dashboard JSON */ + getDashboardSaveModel: () => { + const d = getDashboardSrv().getCurrent(); + if (!d) { + return undefined; + } + return d.getSaveModelClone(); + }, + + /** The selected time range */ + getDashboardTimeRange: () => { + const tr = getTimeSrv().timeRange(); + return { + from: tr.from.valueOf(), + to: tr.to.valueOf(), + raw: tr.raw, + }; + }, + + /** Get the query results for the last loaded data */ + getPanelData: () => { + const d = getDashboardSrv().getCurrent(); + if (!d) { + return undefined; + } + return d.panels.reduce((acc, panel) => { + acc[panel.id] = panel.getQueryRunner().getLastResult(); + return acc; + }, {} as Record); + }, + }; +}