Runtime: support driving grafana app from the console (#42762)

This commit is contained in:
Ryan McKinley 2021-12-05 22:42:35 -08:00 committed by GitHub
parent e468fcf518
commit d41044bd8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 0 deletions

View File

@ -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();

View File

@ -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<number, PanelData | undefined>);
},
};
}