DashboardV2: Add support for revision and gnetId in schema and response transformers (#98477)

This commit is contained in:
Ivan Ortega Alba 2025-01-03 13:17:27 +01:00 committed by GitHub
parent deab83a958
commit 51d9b577d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 26 additions and 8 deletions

View File

@ -37,10 +37,10 @@ export interface DashboardV2Spec {
layout: GridLayoutKind;
// Version of the JSON schema, incremented each time a Grafana update brings
// 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;
// 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 => ({
@ -486,11 +486,11 @@ export interface AnnotationQuerySpec {
}
export const defaultAnnotationQuerySpec = (): AnnotationQuerySpec => ({
builtIn: false,
enable: false,
hide: false,
iconColor: "",
name: "",
builtIn: false,
});
export interface AnnotationQueryKind {

View File

@ -53,10 +53,9 @@ DashboardV2Spec: {
// changes to said schema.
schemaVersion: uint16 | *39
// version: will rely on k8s resource versioning, via metadata.resorceVersion
// revision?: int // for plugins only
// gnetId?: string // ??? Wat is this used for?
// 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?: uint16
}

View File

@ -50,6 +50,7 @@ export const AnnoKeySavedFromUI = 'grafana.app/saved-from-ui';
export const AnnoKeyDashboardNotFound = 'grafana.app/dashboard-not-found';
export const AnnoKeyDashboardIsSnapshot = 'grafana.app/dashboard-is-snapshot';
export const AnnoKeyDashboardIsNew = 'grafana.app/dashboard-is-new';
export const AnnoKeyDashboardGnetId = 'grafana.app/dashboard-gnet-id';
// Annotations provided by the API
type GrafanaAnnotations = {
@ -77,6 +78,10 @@ type GrafanaClientAnnotations = {
[AnnoKeyDashboardNotFound]?: boolean;
[AnnoKeyDashboardIsSnapshot]?: 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> {

View File

@ -2,6 +2,7 @@ import { DataQuery } from '@grafana/schema';
import { DashboardV2Spec } from '@grafana/schema/dist/esm/schema/dashboard/v2alpha0/dashboard.gen';
import {
AnnoKeyCreatedBy,
AnnoKeyDashboardGnetId,
AnnoKeyDashboardId,
AnnoKeyFolder,
AnnoKeySlug,
@ -39,6 +40,8 @@ describe('ResponseTransformers', () => {
fiscalYearStartMonth: 1,
weekStart: 'monday',
version: 1,
gnetId: 'something-like-a-uid',
revision: 225,
links: [
{
title: 'Link 1',
@ -101,6 +104,7 @@ describe('ResponseTransformers', () => {
expect(transformed.metadata.annotations?.[AnnoKeyFolder]).toEqual('folder1');
expect(transformed.metadata.annotations?.[AnnoKeySlug]).toEqual('dashboard-slug');
expect(transformed.metadata.annotations?.[AnnoKeyDashboardId]).toBe(123);
expect(transformed.metadata.annotations?.[AnnoKeyDashboardGnetId]).toBe('something-like-a-uid');
const spec = transformed.spec;
expect(spec.title).toBe(dashboardV1.title);
@ -111,6 +115,7 @@ describe('ResponseTransformers', () => {
expect(spec.preload).toBe(dashboardV1.preload);
expect(spec.liveNow).toBe(dashboardV1.liveNow);
expect(spec.editable).toBe(dashboardV1.editable);
expect(spec.revision).toBe(dashboardV1.revision);
expect(spec.timeSettings.from).toBe(dashboardV1.time?.from);
expect(spec.timeSettings.to).toBe(dashboardV1.time?.to);
expect(spec.timeSettings.timezone).toBe(dashboardV1.timezone);
@ -155,6 +160,7 @@ describe('ResponseTransformers', () => {
'grafana.app/updatedTimestamp': '2023-01-02T00:00:00Z',
'grafana.app/folder': 'folder1',
'grafana.app/slug': 'dashboard-slug',
'grafana.app/dashboard-gnet-id': 'something-like-a-uid',
},
},
spec: {
@ -166,6 +172,7 @@ describe('ResponseTransformers', () => {
preload: true,
liveNow: false,
editable: true,
revision: 225,
timeSettings: {
from: 'now-6h',
to: 'now',
@ -245,6 +252,8 @@ describe('ResponseTransformers', () => {
expect(dashboard.preload).toBe(dashboardV2.spec.preload);
expect(dashboard.liveNow).toBe(dashboardV2.spec.liveNow);
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?.to).toBe(dashboardV2.spec.timeSettings.to);
expect(dashboard.timezone).toBe(dashboardV2.spec.timeSettings.timezone);

View File

@ -15,6 +15,7 @@ import {
import { DataTransformerConfig } from '@grafana/schema/src/raw/dashboard/x/dashboard_types.gen';
import {
AnnoKeyCreatedBy,
AnnoKeyDashboardGnetId,
AnnoKeyDashboardId,
AnnoKeyFolder,
AnnoKeySlug,
@ -75,6 +76,7 @@ export function ensureV2Response(
preload: dashboard.preload || dashboardDefaults.preload,
liveNow: dashboard.liveNow,
editable: dashboard.editable,
revision: dashboard.revision,
timeSettings: {
from: dashboard.time?.from || timeSettingsDefaults.from,
to: dashboard.time?.to || timeSettingsDefaults.to,
@ -108,6 +110,7 @@ export function ensureV2Response(
[AnnoKeyFolder]: accessAndMeta.folderUid,
[AnnoKeySlug]: accessAndMeta.slug,
[AnnoKeyDashboardId]: dashboard.id ?? undefined,
[AnnoKeyDashboardGnetId]: dashboard.gnetId ?? undefined,
},
},
spec,
@ -176,6 +179,8 @@ export function ensureV1Response(
preload: spec.preload,
liveNow: spec.liveNow,
editable: spec.editable,
gnetId: dashboard.metadata.annotations?.[AnnoKeyDashboardGnetId],
revision: spec.revision,
time: {
from: spec.timeSettings.from,
to: spec.timeSettings.to,