Dashboard Scene: Fix unnecessary diffing (#100158)

* Fix unnecessary diffing

* use a diferent approach; update legacyId type

* fix test
This commit is contained in:
Haris Rozajac 2025-02-07 10:47:07 -07:00 committed by GitHub
parent 7b48670104
commit f5d8f42635
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 22 additions and 8 deletions

View File

@ -90,7 +90,7 @@ type GrafanaClientAnnotations = {
// Labels // Labels
type GrafanaLabels = { type GrafanaLabels = {
[DeprecatedInternalId]?: number; [DeprecatedInternalId]?: string;
}; };
export interface Resource<T = object, S = object, K = string> extends TypeMeta<K> { export interface Resource<T = object, S = object, K = string> extends TypeMeta<K> {

View File

@ -64,7 +64,7 @@ export class V1DashboardSerializer implements DashboardSceneSerializerLike<Dashb
id: null, id: null,
uid: '', uid: '',
title: options.title || '', title: options.title || '',
description: options.description || '', description: options.description || undefined,
tags: options.isNew || options.copyTags ? saveModel.tags : [], tags: options.isNew || options.copyTags ? saveModel.tags : [],
}; };
} }

View File

@ -141,7 +141,11 @@ export function transformSaveModelSchemaV2ToScene(dto: DashboardWithAccessInfo<D
const isDashboardEditable = Boolean(dashboard.editable); const isDashboardEditable = Boolean(dashboard.editable);
const canSave = dto.access.canSave !== false; const canSave = dto.access.canSave !== false;
const dashboardId = metadata.labels?.[DeprecatedInternalId]; let dashboardId: number | undefined = undefined;
if (metadata.labels?.[DeprecatedInternalId]) {
dashboardId = parseInt(metadata.labels[DeprecatedInternalId], 10);
}
const meta: DashboardMeta = { const meta: DashboardMeta = {
canShare: dto.access.canShare !== false, canShare: dto.access.canShare !== false,

View File

@ -428,7 +428,7 @@ describe('ResponseTransformers', () => {
[AnnoKeySlug]: 'dashboard-slug', [AnnoKeySlug]: 'dashboard-slug',
}, },
labels: { labels: {
[DeprecatedInternalId]: 123, [DeprecatedInternalId]: '123',
}, },
}, },
}; };
@ -444,7 +444,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?.[AnnoKeyDashboardGnetId]).toBe('something-like-a-uid'); expect(transformed.metadata.annotations?.[AnnoKeyDashboardGnetId]).toBe('something-like-a-uid');
expect(transformed.metadata.labels?.[DeprecatedInternalId]).toBe(123); expect(transformed.metadata.labels?.[DeprecatedInternalId]).toBe('123');
// Spec // Spec
const spec = transformed.spec; const spec = transformed.spec;

View File

@ -134,7 +134,7 @@ export function ensureV2Response(
}; };
creationTimestamp = dto.meta.created; creationTimestamp = dto.meta.created;
labelsMeta = { labelsMeta = {
[DeprecatedInternalId]: dashboard.id ?? undefined, [DeprecatedInternalId]: dashboard.id?.toString() ?? undefined,
}; };
} }

View File

@ -10,6 +10,7 @@ import {
AnnoKeyMessage, AnnoKeyMessage,
AnnoKeyFolder, AnnoKeyFolder,
Resource, Resource,
DeprecatedInternalId,
} from 'app/features/apiserver/types'; } from 'app/features/apiserver/types';
import { getDashboardUrl } from 'app/features/dashboard-scene/utils/getDashboardUrl'; import { getDashboardUrl } from 'app/features/dashboard-scene/utils/getDashboardUrl';
import { DeleteDashboardResponse } from 'app/features/manage-dashboards/types'; import { DeleteDashboardResponse } from 'app/features/manage-dashboards/types';
@ -107,6 +108,10 @@ export class K8sDashboardAPI implements DashboardAPI<DashboardDTO, Dashboard> {
dashboard: dash.spec, dashboard: dash.spec,
}; };
if (dash.metadata.labels?.[DeprecatedInternalId]) {
result.dashboard.id = parseInt(dash.metadata.labels[DeprecatedInternalId], 10);
}
if (dash.metadata.annotations?.[AnnoKeyFolder]) { if (dash.metadata.annotations?.[AnnoKeyFolder]) {
try { try {
const folder = await backendSrv.getFolderByUid(dash.metadata.annotations[AnnoKeyFolder]); const folder = await backendSrv.getFolderByUid(dash.metadata.annotations[AnnoKeyFolder]);

View File

@ -113,7 +113,7 @@ describe('v2 dashboard API - Save', () => {
k8s: { k8s: {
name: 'test-dash', name: 'test-dash',
labels: { labels: {
[DeprecatedInternalId]: 123, [DeprecatedInternalId]: '123',
}, },
annotations: { annotations: {

View File

@ -143,10 +143,15 @@ export class K8sDashboardV2API
}) })
); );
let dashId = 0;
if (v.metadata.labels?.[DeprecatedInternalId]) {
dashId = parseInt(v.metadata.labels[DeprecatedInternalId], 10);
}
return { return {
uid: v.metadata.name, uid: v.metadata.name,
version: parseInt(v.metadata.resourceVersion, 10) ?? 0, version: parseInt(v.metadata.resourceVersion, 10) ?? 0,
id: v.metadata.labels?.[DeprecatedInternalId] ?? 0, id: dashId,
status: 'success', status: 'success',
url, url,
slug: '', slug: '',