2023-07-12 13:37:26 +02:00
|
|
|
import * as H from 'history';
|
2023-08-29 14:17:55 +02:00
|
|
|
import { Unsubscribable } from 'rxjs';
|
2023-07-12 13:37:26 +02:00
|
|
|
|
2023-11-20 18:19:30 +01:00
|
|
|
import { CoreApp, DataQueryRequest, NavIndex, NavModelItem } from '@grafana/data';
|
2023-11-02 20:02:25 +01:00
|
|
|
import { config, locationService } from '@grafana/runtime';
|
2023-07-06 11:21:03 +02:00
|
|
|
import {
|
|
|
|
|
getUrlSyncManager,
|
2023-09-14 12:17:04 +02:00
|
|
|
SceneFlexLayout,
|
2023-07-06 11:21:03 +02:00
|
|
|
SceneGridItem,
|
2023-08-24 07:26:23 +02:00
|
|
|
SceneGridLayout,
|
2023-07-06 11:21:03 +02:00
|
|
|
SceneObject,
|
|
|
|
|
SceneObjectBase,
|
|
|
|
|
SceneObjectState,
|
|
|
|
|
SceneObjectStateChangedEvent,
|
2023-08-29 14:17:55 +02:00
|
|
|
sceneUtils,
|
2023-11-08 14:08:59 +01:00
|
|
|
SceneVariable,
|
|
|
|
|
SceneVariableDependencyConfigLike,
|
2023-07-06 11:21:03 +02:00
|
|
|
} from '@grafana/scenes';
|
2023-11-29 15:01:40 +01:00
|
|
|
import { DashboardLink } from '@grafana/schema';
|
2023-11-08 14:08:59 +01:00
|
|
|
import appEvents from 'app/core/app_events';
|
2023-11-02 20:02:25 +01:00
|
|
|
import { getNavModel } from 'app/core/selectors/navModel';
|
2023-10-13 16:24:04 +02:00
|
|
|
import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv';
|
2023-11-08 14:08:59 +01:00
|
|
|
import { VariablesChanged } from 'app/features/variables/types';
|
2023-09-14 12:17:04 +02:00
|
|
|
import { DashboardMeta } from 'app/types';
|
2023-07-06 11:21:03 +02:00
|
|
|
|
2023-08-25 14:11:47 +02:00
|
|
|
import { DashboardSceneRenderer } from '../scene/DashboardSceneRenderer';
|
2023-08-29 14:17:55 +02:00
|
|
|
import { SaveDashboardDrawer } from '../serialization/SaveDashboardDrawer';
|
2023-11-20 18:19:30 +01:00
|
|
|
import { DashboardEditView } from '../settings/utils';
|
2023-10-13 16:24:04 +02:00
|
|
|
import { DashboardModelCompatibilityWrapper } from '../utils/DashboardModelCompatibilityWrapper';
|
2023-10-20 15:22:56 +02:00
|
|
|
import { getDashboardUrl } from '../utils/urlBuilders';
|
2023-11-30 11:20:15 +01:00
|
|
|
import { forceRenderChildren, getClosestVizPanel, getPanelIdForVizPanel } from '../utils/utils';
|
2023-07-06 11:21:03 +02:00
|
|
|
|
2023-08-29 14:17:55 +02:00
|
|
|
import { DashboardSceneUrlSync } from './DashboardSceneUrlSync';
|
2023-11-30 11:20:15 +01:00
|
|
|
import { ViewPanelScene } from './ViewPanelScene';
|
2023-10-20 15:22:56 +02:00
|
|
|
import { setupKeyboardShortcuts } from './keyboardShortcuts';
|
2023-08-29 14:17:55 +02:00
|
|
|
|
2023-07-06 11:21:03 +02:00
|
|
|
export interface DashboardSceneState extends SceneObjectState {
|
2023-10-13 16:24:04 +02:00
|
|
|
/** The title */
|
2022-11-17 16:15:51 +01:00
|
|
|
title: string;
|
2023-11-28 12:26:09 +01:00
|
|
|
/** Tags */
|
|
|
|
|
tags?: string[];
|
2023-11-29 15:01:40 +01:00
|
|
|
/** Links */
|
|
|
|
|
links?: DashboardLink[];
|
2023-10-13 16:24:04 +02:00
|
|
|
/** A uid when saved */
|
2023-01-17 18:02:46 +01:00
|
|
|
uid?: string;
|
2023-10-13 16:24:04 +02:00
|
|
|
/** @deprecated */
|
|
|
|
|
id?: number | null;
|
|
|
|
|
/** Layout of panels */
|
2023-01-17 18:02:46 +01:00
|
|
|
body: SceneObject;
|
2023-10-13 16:24:04 +02:00
|
|
|
/** NavToolbar actions */
|
2022-11-17 16:15:51 +01:00
|
|
|
actions?: SceneObject[];
|
2023-10-13 16:24:04 +02:00
|
|
|
/** Fixed row at the top of the canvas with for example variables and time range controls */
|
2023-01-17 18:02:46 +01:00
|
|
|
controls?: SceneObject[];
|
2023-10-13 16:24:04 +02:00
|
|
|
/** True when editing */
|
2023-07-06 11:21:03 +02:00
|
|
|
isEditing?: boolean;
|
2023-10-13 16:24:04 +02:00
|
|
|
/** True when user made a change */
|
2023-07-06 11:21:03 +02:00
|
|
|
isDirty?: boolean;
|
2023-09-14 12:17:04 +02:00
|
|
|
/** meta flags */
|
|
|
|
|
meta: DashboardMeta;
|
2023-08-30 10:09:47 +02:00
|
|
|
/** Panel to inspect */
|
2023-09-05 13:51:46 +02:00
|
|
|
inspectPanelKey?: string;
|
2023-11-30 11:20:15 +01:00
|
|
|
/** Panel to view in fullscreen */
|
|
|
|
|
viewPanelScene?: ViewPanelScene;
|
2023-11-20 18:19:30 +01:00
|
|
|
/** Edit view */
|
|
|
|
|
editview?: DashboardEditView;
|
2023-09-19 16:02:21 +02:00
|
|
|
/** Scene object that handles the current drawer or modal */
|
|
|
|
|
overlay?: SceneObject;
|
2022-11-17 16:15:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class DashboardScene extends SceneObjectBase<DashboardSceneState> {
|
2023-07-06 09:22:02 +02:00
|
|
|
static Component = DashboardSceneRenderer;
|
2022-11-17 16:15:51 +01:00
|
|
|
|
2023-08-29 14:17:55 +02:00
|
|
|
/**
|
|
|
|
|
* Handles url sync
|
|
|
|
|
*/
|
2023-07-12 13:37:26 +02:00
|
|
|
protected _urlSync = new DashboardSceneUrlSync(this);
|
2023-11-08 14:08:59 +01:00
|
|
|
/**
|
|
|
|
|
* Get notified when variables change
|
|
|
|
|
*/
|
|
|
|
|
protected _variableDependency = new DashboardVariableDependency();
|
|
|
|
|
|
2023-08-29 14:17:55 +02:00
|
|
|
/**
|
|
|
|
|
* State before editing started
|
|
|
|
|
*/
|
|
|
|
|
private _initialState?: DashboardSceneState;
|
|
|
|
|
/**
|
|
|
|
|
* Url state before editing started
|
|
|
|
|
*/
|
2023-11-20 18:19:30 +01:00
|
|
|
private _initialUrlState?: H.Location;
|
2023-08-29 14:17:55 +02:00
|
|
|
/**
|
|
|
|
|
* change tracking subscription
|
|
|
|
|
*/
|
|
|
|
|
private _changeTrackerSub?: Unsubscribable;
|
2023-07-12 13:37:26 +02:00
|
|
|
|
2023-09-14 12:17:04 +02:00
|
|
|
public constructor(state: Partial<DashboardSceneState>) {
|
|
|
|
|
super({
|
|
|
|
|
title: 'Dashboard',
|
|
|
|
|
meta: {},
|
|
|
|
|
body: state.body ?? new SceneFlexLayout({ children: [] }),
|
|
|
|
|
...state,
|
|
|
|
|
});
|
2022-11-17 16:15:51 +01:00
|
|
|
|
2023-09-11 13:51:05 +02:00
|
|
|
this.addActivationHandler(() => this._activationHandler());
|
2023-03-17 06:50:37 -07:00
|
|
|
}
|
2022-11-17 16:15:51 +01:00
|
|
|
|
2023-09-11 13:51:05 +02:00
|
|
|
private _activationHandler() {
|
2023-10-04 13:21:01 +02:00
|
|
|
window.__grafanaSceneContext = this;
|
|
|
|
|
|
2023-08-29 14:17:55 +02:00
|
|
|
if (this.state.isEditing) {
|
|
|
|
|
this.startTrackingChanges();
|
2023-07-06 11:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-20 15:22:56 +02:00
|
|
|
const clearKeyBindings = setupKeyboardShortcuts(this);
|
2023-10-13 16:24:04 +02:00
|
|
|
const oldDashboardWrapper = new DashboardModelCompatibilityWrapper(this);
|
|
|
|
|
|
|
|
|
|
// @ts-expect-error
|
|
|
|
|
getDashboardSrv().setCurrent(oldDashboardWrapper);
|
|
|
|
|
|
2023-08-29 14:17:55 +02:00
|
|
|
// Deactivation logic
|
|
|
|
|
return () => {
|
2023-10-04 13:21:01 +02:00
|
|
|
window.__grafanaSceneContext = undefined;
|
2023-10-20 15:22:56 +02:00
|
|
|
clearKeyBindings();
|
2023-08-29 14:17:55 +02:00
|
|
|
this.stopTrackingChanges();
|
|
|
|
|
this.stopUrlSync();
|
2023-10-13 16:24:04 +02:00
|
|
|
oldDashboardWrapper.destroy();
|
2023-08-29 14:17:55 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public startUrlSync() {
|
2023-07-06 11:21:03 +02:00
|
|
|
getUrlSyncManager().initSync(this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 14:17:55 +02:00
|
|
|
public stopUrlSync() {
|
|
|
|
|
getUrlSyncManager().cleanUp(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public onEnterEditMode = () => {
|
|
|
|
|
// Save this state
|
|
|
|
|
this._initialState = sceneUtils.cloneSceneObjectState(this.state);
|
2023-11-20 18:19:30 +01:00
|
|
|
this._initialUrlState = locationService.getLocation();
|
2023-08-29 14:17:55 +02:00
|
|
|
|
|
|
|
|
// Switch to edit mode
|
2023-07-06 11:21:03 +02:00
|
|
|
this.setState({ isEditing: true });
|
2023-08-24 07:26:23 +02:00
|
|
|
|
2023-08-29 14:17:55 +02:00
|
|
|
// Propagate change edit mode change to children
|
2023-08-24 07:26:23 +02:00
|
|
|
if (this.state.body instanceof SceneGridLayout) {
|
|
|
|
|
this.state.body.setState({ isDraggable: true, isResizable: true });
|
|
|
|
|
forceRenderChildren(this.state.body, true);
|
|
|
|
|
}
|
2023-08-29 14:17:55 +02:00
|
|
|
|
|
|
|
|
this.startTrackingChanges();
|
2023-07-06 11:21:03 +02:00
|
|
|
};
|
2023-01-17 18:02:46 +01:00
|
|
|
|
2023-08-29 14:17:55 +02:00
|
|
|
public onDiscard = () => {
|
|
|
|
|
// No need to listen to changes anymore
|
|
|
|
|
this.stopTrackingChanges();
|
|
|
|
|
// Stop url sync before updating url
|
|
|
|
|
this.stopUrlSync();
|
|
|
|
|
// Now we can update url
|
2023-11-20 18:19:30 +01:00
|
|
|
locationService.replace({ pathname: this._initialUrlState?.pathname, search: this._initialUrlState?.search });
|
2023-08-29 14:17:55 +02:00
|
|
|
// Update state and disable editing
|
|
|
|
|
this.setState({ ...this._initialState, isEditing: false });
|
|
|
|
|
// and start url sync again
|
|
|
|
|
this.startUrlSync();
|
2023-08-24 07:26:23 +02:00
|
|
|
|
|
|
|
|
// Disable grid dragging
|
|
|
|
|
if (this.state.body instanceof SceneGridLayout) {
|
|
|
|
|
this.state.body.setState({ isDraggable: false, isResizable: false });
|
|
|
|
|
forceRenderChildren(this.state.body, true);
|
|
|
|
|
}
|
2023-01-17 18:02:46 +01:00
|
|
|
};
|
2023-07-12 13:37:26 +02:00
|
|
|
|
2023-08-29 14:17:55 +02:00
|
|
|
public onSave = () => {
|
2023-09-22 13:04:17 +02:00
|
|
|
this.setState({ overlay: new SaveDashboardDrawer({ dashboardRef: this.getRef() }) });
|
2023-08-29 14:17:55 +02:00
|
|
|
};
|
|
|
|
|
|
2023-11-02 20:02:25 +01:00
|
|
|
public getPageNav(location: H.Location, navIndex: NavIndex) {
|
2023-11-30 11:20:15 +01:00
|
|
|
const { meta, viewPanelScene } = this.state;
|
2023-11-02 20:02:25 +01:00
|
|
|
|
2023-07-12 13:37:26 +02:00
|
|
|
let pageNav: NavModelItem = {
|
|
|
|
|
text: this.state.title,
|
2023-09-11 13:51:05 +02:00
|
|
|
url: getDashboardUrl({
|
|
|
|
|
uid: this.state.uid,
|
|
|
|
|
currentQueryParams: location.search,
|
2023-11-20 18:19:30 +01:00
|
|
|
updateQuery: { viewPanel: null, inspect: null, editview: null },
|
2023-11-02 20:02:25 +01:00
|
|
|
useExperimentalURL: Boolean(config.featureToggles.dashboardSceneForViewers && meta.canEdit),
|
2023-09-11 13:51:05 +02:00
|
|
|
}),
|
2023-07-12 13:37:26 +02:00
|
|
|
};
|
|
|
|
|
|
2023-11-22 15:22:00 +00:00
|
|
|
const { folderUid } = meta;
|
2023-11-02 20:02:25 +01:00
|
|
|
|
|
|
|
|
if (folderUid) {
|
2023-11-22 15:22:00 +00:00
|
|
|
const folderNavModel = getNavModel(navIndex, `folder-dashboards-${folderUid}`).main;
|
|
|
|
|
// If the folder hasn't loaded (maybe user doesn't have permission on it?) then
|
|
|
|
|
// don't show the "page not found" breadcrumb
|
|
|
|
|
if (folderNavModel.id !== 'not-found') {
|
|
|
|
|
pageNav = {
|
|
|
|
|
...pageNav,
|
|
|
|
|
parentItem: folderNavModel,
|
|
|
|
|
};
|
2023-11-02 20:02:25 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-30 11:20:15 +01:00
|
|
|
if (viewPanelScene) {
|
2023-07-12 13:37:26 +02:00
|
|
|
pageNav = {
|
|
|
|
|
text: 'View panel',
|
|
|
|
|
parentItem: pageNav,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pageNav;
|
|
|
|
|
}
|
2023-08-25 14:11:47 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the body (layout) or the full view panel
|
|
|
|
|
*/
|
2023-11-30 11:20:15 +01:00
|
|
|
public getBodyToRender(): SceneObject {
|
|
|
|
|
return this.state.viewPanelScene ?? this.state.body;
|
2023-08-25 14:11:47 +02:00
|
|
|
}
|
2023-07-12 13:37:26 +02:00
|
|
|
|
2023-08-29 14:17:55 +02:00
|
|
|
private startTrackingChanges() {
|
|
|
|
|
this._changeTrackerSub = this.subscribeToEvent(
|
|
|
|
|
SceneObjectStateChangedEvent,
|
|
|
|
|
(event: SceneObjectStateChangedEvent) => {
|
|
|
|
|
if (event.payload.changedObject instanceof SceneGridItem) {
|
2023-09-14 12:17:04 +02:00
|
|
|
this.setIsDirty();
|
2023-08-29 14:17:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
2023-07-12 13:37:26 +02:00
|
|
|
}
|
|
|
|
|
|
2023-09-14 12:17:04 +02:00
|
|
|
private setIsDirty() {
|
|
|
|
|
if (!this.state.isDirty) {
|
|
|
|
|
this.setState({ isDirty: true });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 14:17:55 +02:00
|
|
|
private stopTrackingChanges() {
|
|
|
|
|
this._changeTrackerSub?.unsubscribe();
|
2023-07-12 13:37:26 +02:00
|
|
|
}
|
|
|
|
|
|
2023-08-29 14:17:55 +02:00
|
|
|
public getInitialState(): DashboardSceneState | undefined {
|
|
|
|
|
return this._initialState;
|
2023-07-12 13:37:26 +02:00
|
|
|
}
|
2023-09-19 16:02:21 +02:00
|
|
|
|
|
|
|
|
public showModal(modal: SceneObject) {
|
|
|
|
|
this.setState({ overlay: modal });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public closeModal() {
|
|
|
|
|
this.setState({ overlay: undefined });
|
|
|
|
|
}
|
2023-09-29 13:19:03 +02:00
|
|
|
|
2023-11-02 20:02:25 +01:00
|
|
|
public async onStarDashboard() {
|
|
|
|
|
const { meta, uid } = this.state;
|
|
|
|
|
if (!uid) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const result = await getDashboardSrv().starDashboard(uid, Boolean(meta.isStarred));
|
|
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
|
meta: {
|
|
|
|
|
...meta,
|
|
|
|
|
isStarred: result,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Failed to star dashboard', err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-20 18:19:30 +01:00
|
|
|
public onOpenSettings = () => {
|
|
|
|
|
locationService.partial({ editview: 'settings' });
|
|
|
|
|
};
|
|
|
|
|
|
2023-09-29 13:19:03 +02:00
|
|
|
/**
|
|
|
|
|
* Called by the SceneQueryRunner to privide contextural parameters (tracking) props for the request
|
|
|
|
|
*/
|
|
|
|
|
public enrichDataRequest(sceneObject: SceneObject): Partial<DataQueryRequest> {
|
|
|
|
|
const panel = getClosestVizPanel(sceneObject);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
app: CoreApp.Dashboard,
|
|
|
|
|
dashboardUID: this.state.uid,
|
|
|
|
|
panelId: (panel && getPanelIdForVizPanel(panel)) ?? 0,
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-10-13 11:42:42 +02:00
|
|
|
|
|
|
|
|
canEditDashboard() {
|
2023-11-02 20:02:25 +01:00
|
|
|
const { meta } = this.state;
|
|
|
|
|
|
|
|
|
|
return Boolean(meta.canEdit || meta.canMakeEditable);
|
2023-10-13 11:42:42 +02:00
|
|
|
}
|
2023-01-17 18:02:46 +01:00
|
|
|
}
|
2023-11-08 14:08:59 +01:00
|
|
|
|
|
|
|
|
export class DashboardVariableDependency implements SceneVariableDependencyConfigLike {
|
|
|
|
|
private _emptySet = new Set<string>();
|
|
|
|
|
|
|
|
|
|
getNames(): Set<string> {
|
|
|
|
|
return this._emptySet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public hasDependencyOn(): boolean {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public variableUpdatesCompleted(changedVars: Set<SceneVariable>) {
|
|
|
|
|
if (changedVars.size > 0) {
|
|
|
|
|
// Temp solution for some core panels (like dashlist) to know that variables have changed
|
|
|
|
|
appEvents.publish(new VariablesChanged({ refreshAll: true, panelIds: [] }));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|