mirror of https://github.com/grafana/grafana.git
				
				
				
			DashboardV2: Add support for revision and gnetId in schema and response transformers (#98477)
This commit is contained in:
		
							parent
							
								
									deab83a958
								
							
						
					
					
						commit
						51d9b577d5
					
				|  | @ -37,10 +37,10 @@ export interface DashboardV2Spec { | ||||||
| 	layout: GridLayoutKind; | 	layout: GridLayoutKind; | ||||||
| 	// Version of the JSON schema, incremented each time a Grafana update brings
 | 	// Version of the JSON schema, incremented each time a Grafana update brings
 | ||||||
| 	// changes to said schema.
 | 	// changes to said schema.
 | ||||||
| 	// version: will rely on k8s resource versioning, via metadata.resorceVersion
 |  | ||||||
| 	// revision?: int // for plugins only
 |  | ||||||
| 	// gnetId?: string // ??? Wat is this used for?
 |  | ||||||
| 	schemaVersion: number; | 	schemaVersion: number; | ||||||
|  | 	// Plugins only. The version of the dashboard installed together with the plugin.
 | ||||||
|  | 	// This is used to determine if the dashboard should be updated when the plugin is updated.
 | ||||||
|  | 	revision?: number; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export const defaultDashboardV2Spec = (): DashboardV2Spec => ({ | export const defaultDashboardV2Spec = (): DashboardV2Spec => ({ | ||||||
|  | @ -486,11 +486,11 @@ export interface AnnotationQuerySpec { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export const defaultAnnotationQuerySpec = (): AnnotationQuerySpec => ({ | export const defaultAnnotationQuerySpec = (): AnnotationQuerySpec => ({ | ||||||
| 	builtIn: false, |  | ||||||
| 	enable: false, | 	enable: false, | ||||||
| 	hide: false, | 	hide: false, | ||||||
| 	iconColor: "", | 	iconColor: "", | ||||||
| 	name: "", | 	name: "", | ||||||
|  | 	builtIn: false, | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| export interface AnnotationQueryKind { | export interface AnnotationQueryKind { | ||||||
|  |  | ||||||
|  | @ -53,10 +53,9 @@ DashboardV2Spec: { | ||||||
|   // changes to said schema. |   // changes to said schema. | ||||||
|   schemaVersion: uint16 | *39 |   schemaVersion: uint16 | *39 | ||||||
| 
 | 
 | ||||||
| 
 |   // Plugins only. The version of the dashboard installed together with the plugin. | ||||||
|   // version: will rely on k8s resource versioning, via metadata.resorceVersion |   // This is used to determine if the dashboard should be updated when the plugin is updated. | ||||||
|   // revision?: int // for plugins only |   revision?: uint16 | ||||||
|   // gnetId?: string // ??? Wat is this used for? |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -50,6 +50,7 @@ export const AnnoKeySavedFromUI = 'grafana.app/saved-from-ui'; | ||||||
| export const AnnoKeyDashboardNotFound = 'grafana.app/dashboard-not-found'; | export const AnnoKeyDashboardNotFound = 'grafana.app/dashboard-not-found'; | ||||||
| export const AnnoKeyDashboardIsSnapshot = 'grafana.app/dashboard-is-snapshot'; | export const AnnoKeyDashboardIsSnapshot = 'grafana.app/dashboard-is-snapshot'; | ||||||
| export const AnnoKeyDashboardIsNew = 'grafana.app/dashboard-is-new'; | export const AnnoKeyDashboardIsNew = 'grafana.app/dashboard-is-new'; | ||||||
|  | export const AnnoKeyDashboardGnetId = 'grafana.app/dashboard-gnet-id'; | ||||||
| 
 | 
 | ||||||
| // Annotations provided by the API
 | // Annotations provided by the API
 | ||||||
| type GrafanaAnnotations = { | type GrafanaAnnotations = { | ||||||
|  | @ -77,6 +78,10 @@ type GrafanaClientAnnotations = { | ||||||
|   [AnnoKeyDashboardNotFound]?: boolean; |   [AnnoKeyDashboardNotFound]?: boolean; | ||||||
|   [AnnoKeyDashboardIsSnapshot]?: boolean; |   [AnnoKeyDashboardIsSnapshot]?: boolean; | ||||||
|   [AnnoKeyDashboardIsNew]?: boolean; |   [AnnoKeyDashboardIsNew]?: boolean; | ||||||
|  | 
 | ||||||
|  |   // TODO: This should be provided by the API
 | ||||||
|  |   // This is the dashboard ID for the Gcom API. This set when a dashboard is created through importing a dashboard from Grafana.com.
 | ||||||
|  |   [AnnoKeyDashboardGnetId]?: string; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| export interface Resource<T = object, K = string> extends TypeMeta<K> { | export interface Resource<T = object, K = string> extends TypeMeta<K> { | ||||||
|  |  | ||||||
|  | @ -2,6 +2,7 @@ import { DataQuery } from '@grafana/schema'; | ||||||
| import { DashboardV2Spec } from '@grafana/schema/dist/esm/schema/dashboard/v2alpha0/dashboard.gen'; | import { DashboardV2Spec } from '@grafana/schema/dist/esm/schema/dashboard/v2alpha0/dashboard.gen'; | ||||||
| import { | import { | ||||||
|   AnnoKeyCreatedBy, |   AnnoKeyCreatedBy, | ||||||
|  |   AnnoKeyDashboardGnetId, | ||||||
|   AnnoKeyDashboardId, |   AnnoKeyDashboardId, | ||||||
|   AnnoKeyFolder, |   AnnoKeyFolder, | ||||||
|   AnnoKeySlug, |   AnnoKeySlug, | ||||||
|  | @ -39,6 +40,8 @@ describe('ResponseTransformers', () => { | ||||||
|         fiscalYearStartMonth: 1, |         fiscalYearStartMonth: 1, | ||||||
|         weekStart: 'monday', |         weekStart: 'monday', | ||||||
|         version: 1, |         version: 1, | ||||||
|  |         gnetId: 'something-like-a-uid', | ||||||
|  |         revision: 225, | ||||||
|         links: [ |         links: [ | ||||||
|           { |           { | ||||||
|             title: 'Link 1', |             title: 'Link 1', | ||||||
|  | @ -101,6 +104,7 @@ describe('ResponseTransformers', () => { | ||||||
|       expect(transformed.metadata.annotations?.[AnnoKeyFolder]).toEqual('folder1'); |       expect(transformed.metadata.annotations?.[AnnoKeyFolder]).toEqual('folder1'); | ||||||
|       expect(transformed.metadata.annotations?.[AnnoKeySlug]).toEqual('dashboard-slug'); |       expect(transformed.metadata.annotations?.[AnnoKeySlug]).toEqual('dashboard-slug'); | ||||||
|       expect(transformed.metadata.annotations?.[AnnoKeyDashboardId]).toBe(123); |       expect(transformed.metadata.annotations?.[AnnoKeyDashboardId]).toBe(123); | ||||||
|  |       expect(transformed.metadata.annotations?.[AnnoKeyDashboardGnetId]).toBe('something-like-a-uid'); | ||||||
| 
 | 
 | ||||||
|       const spec = transformed.spec; |       const spec = transformed.spec; | ||||||
|       expect(spec.title).toBe(dashboardV1.title); |       expect(spec.title).toBe(dashboardV1.title); | ||||||
|  | @ -111,6 +115,7 @@ describe('ResponseTransformers', () => { | ||||||
|       expect(spec.preload).toBe(dashboardV1.preload); |       expect(spec.preload).toBe(dashboardV1.preload); | ||||||
|       expect(spec.liveNow).toBe(dashboardV1.liveNow); |       expect(spec.liveNow).toBe(dashboardV1.liveNow); | ||||||
|       expect(spec.editable).toBe(dashboardV1.editable); |       expect(spec.editable).toBe(dashboardV1.editable); | ||||||
|  |       expect(spec.revision).toBe(dashboardV1.revision); | ||||||
|       expect(spec.timeSettings.from).toBe(dashboardV1.time?.from); |       expect(spec.timeSettings.from).toBe(dashboardV1.time?.from); | ||||||
|       expect(spec.timeSettings.to).toBe(dashboardV1.time?.to); |       expect(spec.timeSettings.to).toBe(dashboardV1.time?.to); | ||||||
|       expect(spec.timeSettings.timezone).toBe(dashboardV1.timezone); |       expect(spec.timeSettings.timezone).toBe(dashboardV1.timezone); | ||||||
|  | @ -155,6 +160,7 @@ describe('ResponseTransformers', () => { | ||||||
|             'grafana.app/updatedTimestamp': '2023-01-02T00:00:00Z', |             'grafana.app/updatedTimestamp': '2023-01-02T00:00:00Z', | ||||||
|             'grafana.app/folder': 'folder1', |             'grafana.app/folder': 'folder1', | ||||||
|             'grafana.app/slug': 'dashboard-slug', |             'grafana.app/slug': 'dashboard-slug', | ||||||
|  |             'grafana.app/dashboard-gnet-id': 'something-like-a-uid', | ||||||
|           }, |           }, | ||||||
|         }, |         }, | ||||||
|         spec: { |         spec: { | ||||||
|  | @ -166,6 +172,7 @@ describe('ResponseTransformers', () => { | ||||||
|           preload: true, |           preload: true, | ||||||
|           liveNow: false, |           liveNow: false, | ||||||
|           editable: true, |           editable: true, | ||||||
|  |           revision: 225, | ||||||
|           timeSettings: { |           timeSettings: { | ||||||
|             from: 'now-6h', |             from: 'now-6h', | ||||||
|             to: 'now', |             to: 'now', | ||||||
|  | @ -245,6 +252,8 @@ describe('ResponseTransformers', () => { | ||||||
|       expect(dashboard.preload).toBe(dashboardV2.spec.preload); |       expect(dashboard.preload).toBe(dashboardV2.spec.preload); | ||||||
|       expect(dashboard.liveNow).toBe(dashboardV2.spec.liveNow); |       expect(dashboard.liveNow).toBe(dashboardV2.spec.liveNow); | ||||||
|       expect(dashboard.editable).toBe(dashboardV2.spec.editable); |       expect(dashboard.editable).toBe(dashboardV2.spec.editable); | ||||||
|  |       expect(dashboard.revision).toBe(225); | ||||||
|  |       expect(dashboard.gnetId).toBe(dashboardV2.metadata.annotations?.['grafana.app/dashboard-gnet-id']); | ||||||
|       expect(dashboard.time?.from).toBe(dashboardV2.spec.timeSettings.from); |       expect(dashboard.time?.from).toBe(dashboardV2.spec.timeSettings.from); | ||||||
|       expect(dashboard.time?.to).toBe(dashboardV2.spec.timeSettings.to); |       expect(dashboard.time?.to).toBe(dashboardV2.spec.timeSettings.to); | ||||||
|       expect(dashboard.timezone).toBe(dashboardV2.spec.timeSettings.timezone); |       expect(dashboard.timezone).toBe(dashboardV2.spec.timeSettings.timezone); | ||||||
|  |  | ||||||
|  | @ -15,6 +15,7 @@ import { | ||||||
| import { DataTransformerConfig } from '@grafana/schema/src/raw/dashboard/x/dashboard_types.gen'; | import { DataTransformerConfig } from '@grafana/schema/src/raw/dashboard/x/dashboard_types.gen'; | ||||||
| import { | import { | ||||||
|   AnnoKeyCreatedBy, |   AnnoKeyCreatedBy, | ||||||
|  |   AnnoKeyDashboardGnetId, | ||||||
|   AnnoKeyDashboardId, |   AnnoKeyDashboardId, | ||||||
|   AnnoKeyFolder, |   AnnoKeyFolder, | ||||||
|   AnnoKeySlug, |   AnnoKeySlug, | ||||||
|  | @ -75,6 +76,7 @@ export function ensureV2Response( | ||||||
|     preload: dashboard.preload || dashboardDefaults.preload, |     preload: dashboard.preload || dashboardDefaults.preload, | ||||||
|     liveNow: dashboard.liveNow, |     liveNow: dashboard.liveNow, | ||||||
|     editable: dashboard.editable, |     editable: dashboard.editable, | ||||||
|  |     revision: dashboard.revision, | ||||||
|     timeSettings: { |     timeSettings: { | ||||||
|       from: dashboard.time?.from || timeSettingsDefaults.from, |       from: dashboard.time?.from || timeSettingsDefaults.from, | ||||||
|       to: dashboard.time?.to || timeSettingsDefaults.to, |       to: dashboard.time?.to || timeSettingsDefaults.to, | ||||||
|  | @ -108,6 +110,7 @@ export function ensureV2Response( | ||||||
|         [AnnoKeyFolder]: accessAndMeta.folderUid, |         [AnnoKeyFolder]: accessAndMeta.folderUid, | ||||||
|         [AnnoKeySlug]: accessAndMeta.slug, |         [AnnoKeySlug]: accessAndMeta.slug, | ||||||
|         [AnnoKeyDashboardId]: dashboard.id ?? undefined, |         [AnnoKeyDashboardId]: dashboard.id ?? undefined, | ||||||
|  |         [AnnoKeyDashboardGnetId]: dashboard.gnetId ?? undefined, | ||||||
|       }, |       }, | ||||||
|     }, |     }, | ||||||
|     spec, |     spec, | ||||||
|  | @ -176,6 +179,8 @@ export function ensureV1Response( | ||||||
|         preload: spec.preload, |         preload: spec.preload, | ||||||
|         liveNow: spec.liveNow, |         liveNow: spec.liveNow, | ||||||
|         editable: spec.editable, |         editable: spec.editable, | ||||||
|  |         gnetId: dashboard.metadata.annotations?.[AnnoKeyDashboardGnetId], | ||||||
|  |         revision: spec.revision, | ||||||
|         time: { |         time: { | ||||||
|           from: spec.timeSettings.from, |           from: spec.timeSettings.from, | ||||||
|           to: spec.timeSettings.to, |           to: spec.timeSettings.to, | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue