mirror of
https://github.com/grafana/grafana.git
synced 2025-01-09 23:53:25 -06:00
Runtime: support driving grafana app from the console (#42762)
This commit is contained in:
parent
e468fcf518
commit
d41044bd8e
@ -65,6 +65,7 @@ import { setPanelDataErrorView } from '@grafana/runtime/src/components/PanelData
|
|||||||
import { DatasourceSrv } from './features/plugins/datasource_srv';
|
import { DatasourceSrv } from './features/plugins/datasource_srv';
|
||||||
import { AngularApp } from './angular';
|
import { AngularApp } from './angular';
|
||||||
import { ModalManager } from './core/services/ModalManager';
|
import { ModalManager } from './core/services/ModalManager';
|
||||||
|
import { initWindowRuntime } from './features/runtime/init';
|
||||||
|
|
||||||
// add move to lodash for backward compatabilty with plugins
|
// add move to lodash for backward compatabilty with plugins
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
@ -125,6 +126,7 @@ export class GrafanaApp {
|
|||||||
const dataSourceSrv = new DatasourceSrv();
|
const dataSourceSrv = new DatasourceSrv();
|
||||||
dataSourceSrv.init(config.datasources, config.defaultDatasource);
|
dataSourceSrv.init(config.datasources, config.defaultDatasource);
|
||||||
setDataSourceSrv(dataSourceSrv);
|
setDataSourceSrv(dataSourceSrv);
|
||||||
|
initWindowRuntime();
|
||||||
|
|
||||||
// init modal manager
|
// init modal manager
|
||||||
const modalManager = new ModalManager();
|
const modalManager = new ModalManager();
|
||||||
|
59
public/app/features/runtime/init.ts
Normal file
59
public/app/features/runtime/init.ts
Normal 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>);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user