| 
									
										
										
										
											2025-01-02 19:23:58 +08:00
										 |  |  | import { config, locationService } from '@grafana/runtime'; | 
					
						
							| 
									
										
										
										
											2025-01-27 21:14:19 +08:00
										 |  |  | import { Dashboard } from '@grafana/schema/dist/esm/index.gen'; | 
					
						
							| 
									
										
										
										
											2025-07-30 21:01:27 +08:00
										 |  |  | import { Spec as DashboardV2Spec } from '@grafana/schema/dist/esm/schema/dashboard/v2'; | 
					
						
							|  |  |  | import { Status } from '@grafana/schema/src/schema/dashboard/v2'; | 
					
						
							| 
									
										
										
										
											2025-06-12 20:49:55 +08:00
										 |  |  | import { Resource } from 'app/features/apiserver/types'; | 
					
						
							| 
									
										
										
										
											2025-07-09 12:15:33 +08:00
										 |  |  | import { DashboardDataDTO, DashboardDTO } from 'app/types/dashboard'; | 
					
						
							| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-27 21:14:19 +08:00
										 |  |  | import { SaveDashboardCommand } from '../components/SaveDashboard/types'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  | import { DashboardWithAccessInfo } from './types'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-25 18:47:42 +08:00
										 |  |  | export function isV2StoredVersion(version: string | undefined): boolean { | 
					
						
							| 
									
										
										
										
											2025-07-30 21:01:27 +08:00
										 |  |  |   return version === 'v2alpha1' || version === 'v2beta1'; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-13 01:43:32 +08:00
										 |  |  | export function getDashboardsApiVersion(responseFormat?: 'v1' | 'v2') { | 
					
						
							|  |  |  |   const isDashboardSceneEnabled = config.featureToggles.dashboardScene; | 
					
						
							|  |  |  |   const isKubernetesDashboardsEnabled = config.featureToggles.kubernetesDashboards; | 
					
						
							| 
									
										
										
										
											2025-01-02 19:23:58 +08:00
										 |  |  |   const forcingOldDashboardArch = locationService.getSearch().get('scenes') === 'false'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-13 01:43:32 +08:00
										 |  |  |   // Force legacy API when dashboard scene is disabled or explicitly forced
 | 
					
						
							|  |  |  |   if (!isDashboardSceneEnabled || forcingOldDashboardArch) { | 
					
						
							|  |  |  |     if (responseFormat === 'v2') { | 
					
						
							|  |  |  |       throw new Error('v2 is not supported for legacy architecture'); | 
					
						
							| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-13 01:43:32 +08:00
										 |  |  |     return isKubernetesDashboardsEnabled ? 'v1' : 'legacy'; | 
					
						
							| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-13 01:43:32 +08:00
										 |  |  |   // Unified manages redirection between v1 and v2, but when responseFormat is undefined we get the unified API
 | 
					
						
							|  |  |  |   if (isKubernetesDashboardsEnabled) { | 
					
						
							|  |  |  |     if (responseFormat === 'v1') { | 
					
						
							|  |  |  |       return 'v1'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (responseFormat === 'v2') { | 
					
						
							|  |  |  |       return 'v2'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 'unified'; | 
					
						
							| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-13 01:43:32 +08:00
										 |  |  |   // Handle non-kubernetes case
 | 
					
						
							|  |  |  |   if (responseFormat === 'v2') { | 
					
						
							|  |  |  |     throw new Error('v2 is not supported if kubernetes dashboards are disabled'); | 
					
						
							| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 'legacy'; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-20 19:42:59 +08:00
										 |  |  | // This function is used to determine if the dashboard is in v2 format or also v1 format
 | 
					
						
							| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  | export function isDashboardResource( | 
					
						
							|  |  |  |   obj?: DashboardDTO | DashboardWithAccessInfo<DashboardV2Spec> | DashboardWithAccessInfo<DashboardDataDTO> | null | 
					
						
							|  |  |  | ): obj is DashboardWithAccessInfo<DashboardV2Spec> | DashboardWithAccessInfo<DashboardDataDTO> { | 
					
						
							|  |  |  |   if (!obj) { | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2025-02-20 19:42:59 +08:00
										 |  |  |   // is v1 or v2 format?
 | 
					
						
							| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  |   const isK8sDashboard = 'kind' in obj && obj.kind === 'DashboardWithAccessInfo'; | 
					
						
							|  |  |  |   return isK8sDashboard; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-27 21:14:19 +08:00
										 |  |  | export function isDashboardV2Spec(obj: Dashboard | DashboardDataDTO | DashboardV2Spec): obj is DashboardV2Spec { | 
					
						
							| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  |   return 'elements' in obj; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-02 19:23:58 +08:00
										 |  |  | export function isDashboardV0Spec(obj: DashboardDataDTO | DashboardV2Spec): obj is DashboardDataDTO { | 
					
						
							| 
									
										
										
										
											2025-02-20 19:42:59 +08:00
										 |  |  |   return !isDashboardV2Spec(obj); // not v2 spec means it's v1 spec
 | 
					
						
							| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2025-01-02 19:23:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function isDashboardV2Resource( | 
					
						
							|  |  |  |   obj: DashboardDTO | DashboardWithAccessInfo<DashboardDataDTO> | DashboardWithAccessInfo<DashboardV2Spec> | 
					
						
							|  |  |  | ): obj is DashboardWithAccessInfo<DashboardV2Spec> { | 
					
						
							|  |  |  |   return isDashboardResource(obj) && isDashboardV2Spec(obj.spec); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-01-27 21:14:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function isV1DashboardCommand( | 
					
						
							|  |  |  |   cmd: SaveDashboardCommand<Dashboard | DashboardV2Spec> | 
					
						
							|  |  |  | ): cmd is SaveDashboardCommand<Dashboard> { | 
					
						
							|  |  |  |   return !isDashboardV2Spec(cmd.dashboard); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-16 17:53:44 +08:00
										 |  |  | export function isV1ClassicDashboard(obj: Dashboard | DashboardV2Spec): obj is Dashboard { | 
					
						
							|  |  |  |   return !isDashboardV2Spec(obj); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-27 21:14:19 +08:00
										 |  |  | export function isV2DashboardCommand( | 
					
						
							|  |  |  |   cmd: SaveDashboardCommand<Dashboard | DashboardV2Spec> | 
					
						
							|  |  |  | ): cmd is SaveDashboardCommand<DashboardV2Spec> { | 
					
						
							|  |  |  |   return isDashboardV2Spec(cmd.dashboard); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-06-12 20:49:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Helper function to extract the stored version from a dashboard resource if conversion failed | 
					
						
							|  |  |  |  * @param item - Dashboard resource item | 
					
						
							|  |  |  |  * @returns The stored version string if conversion failed, undefined otherwise | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export function getFailedVersion( | 
					
						
							|  |  |  |   item: Resource<Dashboard | DashboardV2Spec | DashboardDataDTO, Status> | 
					
						
							|  |  |  | ): string | undefined { | 
					
						
							|  |  |  |   return item.status?.conversion?.failed ? item.status.conversion.storedVersion : undefined; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Helper function to check if a dashboard resource has a failed conversion from a specific version family | 
					
						
							|  |  |  |  * @param item - Dashboard resource item | 
					
						
							|  |  |  |  * @param versionPrefix - Version prefix to check (e.g., 'v1', 'v2') | 
					
						
							|  |  |  |  * @returns True if conversion failed and stored version starts with the specified prefix | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export function failedFromVersion( | 
					
						
							|  |  |  |   item: Resource<Dashboard | DashboardV2Spec | DashboardDataDTO, Status>, | 
					
						
							|  |  |  |   versionPrefix: string | 
					
						
							|  |  |  | ): boolean { | 
					
						
							|  |  |  |   const storedVersion = getFailedVersion(item); | 
					
						
							|  |  |  |   return !!storedVersion && storedVersion.startsWith(versionPrefix); | 
					
						
							|  |  |  | } |