From de1df2f4e95b6434e9b0370e694c0b3382d106fd Mon Sep 17 00:00:00 2001 From: Haris Rozajac <58232930+harisrozajac@users.noreply.github.com> Date: Mon, 27 Jan 2025 06:28:51 -0700 Subject: [PATCH] Dashboard: Add ScopeMeta to DashboardModel (#99475) --- .../features/dashboard-scene/scene/DashboardScene.tsx | 4 +++- .../serialization/transformSaveModelToScene.ts | 9 +++++++++ .../serialization/transformSceneToSaveModel.ts | 2 ++ public/app/features/dashboard/state/DashboardModel.ts | 9 +++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/public/app/features/dashboard-scene/scene/DashboardScene.tsx b/public/app/features/dashboard-scene/scene/DashboardScene.tsx index 1accc0eeff2..e609e489632 100644 --- a/public/app/features/dashboard-scene/scene/DashboardScene.tsx +++ b/public/app/features/dashboard-scene/scene/DashboardScene.tsx @@ -34,7 +34,7 @@ import store from 'app/core/store'; import { DashboardWithAccessInfo } from 'app/features/dashboard/api/types'; import { SaveDashboardAsOptions } from 'app/features/dashboard/components/SaveDashboard/types'; import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv'; -import { DashboardModel } from 'app/features/dashboard/state/DashboardModel'; +import { DashboardModel, ScopeMeta } from 'app/features/dashboard/state/DashboardModel'; import { PanelModel } from 'app/features/dashboard/state/PanelModel'; import { dashboardWatcher } from 'app/features/live/dashboard/dashboardWatcher'; import { getClosestScopesFacade, ScopesFacade } from 'app/features/scopes'; @@ -99,6 +99,8 @@ export interface DashboardSceneState extends SceneObjectState { preload?: boolean; /** A uid when saved */ uid?: string; + /** @experimental */ + scopeMeta?: ScopeMeta; /** @deprecated */ id?: number | null; /** Layout of panels */ diff --git a/public/app/features/dashboard-scene/serialization/transformSaveModelToScene.ts b/public/app/features/dashboard-scene/serialization/transformSaveModelToScene.ts index 0a7108cdcf3..c3b82b69b66 100644 --- a/public/app/features/dashboard-scene/serialization/transformSaveModelToScene.ts +++ b/public/app/features/dashboard-scene/serialization/transformSaveModelToScene.ts @@ -217,6 +217,14 @@ export function createDashboardSceneFromDashboardModel(oldModel: DashboardModel, }); } + const scopeMeta = + config.featureToggles.scopeFilters && oldModel.scopeMeta + ? { + trait: oldModel.scopeMeta.trait, + groups: oldModel.scopeMeta.groups, + } + : undefined; + const behaviorList: SceneObjectState['$behaviors'] = [ new behaviors.CursorSync({ sync: oldModel.graphTooltip, @@ -249,6 +257,7 @@ export function createDashboardSceneFromDashboardModel(oldModel: DashboardModel, tags: oldModel.tags || [], title: oldModel.title, version: oldModel.version, + scopeMeta, body: new DefaultGridLayoutManager({ grid: new SceneGridLayout({ isLazy: !(dto.preload || contextSrv.user.authenticatedBy === 'render'), diff --git a/public/app/features/dashboard-scene/serialization/transformSceneToSaveModel.ts b/public/app/features/dashboard-scene/serialization/transformSceneToSaveModel.ts index 235f5038dc3..c1b6c96ec07 100644 --- a/public/app/features/dashboard-scene/serialization/transformSceneToSaveModel.ts +++ b/public/app/features/dashboard-scene/serialization/transformSceneToSaveModel.ts @@ -140,6 +140,8 @@ export function transformSceneToSaveModel(scene: DashboardScene, isSnapshot = fa liveNow, schemaVersion: DASHBOARD_SCHEMA_VERSION, refresh: refreshPicker?.state.refresh, + // @ts-expect-error not in dashboard schema because it's experimental + scopeMeta: state.scopeMeta, }; return sortedDeepCloneWithoutNulls(dashboard); diff --git a/public/app/features/dashboard/state/DashboardModel.ts b/public/app/features/dashboard/state/DashboardModel.ts index 590149f994b..f640cc8cbb6 100644 --- a/public/app/features/dashboard/state/DashboardModel.ts +++ b/public/app/features/dashboard/state/DashboardModel.ts @@ -56,6 +56,12 @@ export interface CloneOptions { export type DashboardLinkType = 'link' | 'dashboards'; +/** @experimental */ +export interface ScopeMeta { + trait: string; + groups: string[]; +} + export class DashboardModel implements TimeModel { /** @deprecated use UID */ id: any; @@ -87,6 +93,7 @@ export class DashboardModel implements TimeModel { panelInEdit?: PanelModel; panelInView?: PanelModel; fiscalYearStartMonth?: number; + scopeMeta?: ScopeMeta; private panelsAffectedByVariableChange: number[] | null; private appEventsSubscription: Subscription; private lastRefresh: number; @@ -155,6 +162,8 @@ export class DashboardModel implements TimeModel { this.links = data.links ?? []; this.gnetId = data.gnetId || null; this.panels = map(data.panels ?? [], (panelData) => new PanelModel(panelData)); + // @ts-expect-error - experimental and it's not included in the schema + this.scopeMeta = data.scopeMeta; // Deep clone original dashboard to avoid mutations by object reference this.originalDashboard = cloneDeep(data); this.originalTemplating = cloneDeep(this.templating);