grafana/public/app/features/runtime/init.ts
Torkel Ödegaard b01cbc7aef
Dashboard: Fixes save drawer always comparing changes against first loaded version (#76506)
* Dashboard: Fixes save changes diff after first save

* Lots of type issues

* better fix

* Update some more places to use new function

* Fix

* Update

* Update

* remove console.log

* Update
2023-10-13 16:23:23 +02:00

58 lines
1.7 KiB
TypeScript

import { PanelData, RawTimeRange } from '@grafana/data';
import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv';
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { DashboardModel } from '../dashboard/state';
declare global {
interface Window {
grafanaRuntime?: {
getDashboardSaveModel: () => DashboardModel | undefined;
getDashboardTimeRange: () => { from: number; to: number; raw: RawTimeRange };
getPanelData: () => Record<number, PanelData | undefined> | undefined;
};
}
}
/**
* 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.grafanaRuntime = {
/** Get info for the current dashboard. This will include the migrated dashboard JSON */
getDashboardSaveModel: () => {
const d = getDashboardSrv().getCurrent();
if (!d) {
return undefined;
}
return d.getSaveModelCloneOld();
},
/** 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<Record<number, PanelData | undefined>>((acc, panel) => {
acc[panel.id] = panel.getQueryRunner().getLastResult();
return acc;
}, {});
},
};
}