This commit is contained in:
Ryan McKinley
2025-12-02 15:00:14 +03:00
parent 36a7c10f67
commit 18c587a460
4 changed files with 25 additions and 5 deletions
@@ -1,7 +1,7 @@
import { Dashboard } from '@grafana/schema';
import { Spec as DashboardV2Spec } from '@grafana/schema/dist/esm/schema/dashboard/v2';
import { isResource } from 'app/features/apiserver/guards';
import { Resource, ResourceList } from 'app/features/apiserver/types';
import { ListOptions, Resource, ResourceList } from 'app/features/apiserver/types';
import { DashboardDataDTO, DashboardDTO } from 'app/types/dashboard';
import { SaveDashboardCommand } from '../components/SaveDashboard/types';
@@ -56,6 +56,13 @@ export class UnifiedDashboardAPI
return await this.v1Client.deleteDashboard(uid, showSuccessAlert);
}
async listDashboardHistory(uid: string) {
return this.listAcrossVersions({
labelSelector: 'grafana.app/get-history=true',
fieldSelector: `metadata.name=${uid}`,
});
}
/**
* List deleted dashboards handling mixed v1/v2 versions or pure v2 dashboards.
*
@@ -68,14 +75,25 @@ export class UnifiedDashboardAPI
async listDeletedDashboards(
options: ListDeletedDashboardsOptions
): Promise<ResourceList<Dashboard | DashboardV2Spec>> {
const v1Response = await this.v1Client.listDeletedDashboards(options);
return this.listAcrossVersions(options);
}
/**
* Steps:
* 1. Call v1 client to get all deleted dashboards
* 2. Check if any items have failed conversion from v2 versions
* 3. If v2 dashboards are detected, call v2 client
* 4. Filter and combine v1 and v2 dashboards into one response
*/
private async listAcrossVersions(options: ListOptions): Promise<ResourceList<Dashboard | DashboardV2Spec>> {
const v1Response = await this.v1Client.client.list(options);
const filteredV1Items = v1Response.items.filter((item) => !failedFromVersion(item, ['v2']));
if (filteredV1Items.length === v1Response.items.length) {
return v1Response;
}
const v2Response = await this.v2Client.listDeletedDashboards(options);
const v2Response = await this.v2Client.client.list(options);
const filteredV2Items = v2Response.items.filter((item) => !failedFromVersion(item, ['v0', 'v1']));
return {
+1 -1
View File
@@ -36,7 +36,7 @@ export const K8S_V1_DASHBOARD_API_CONFIG = {
};
export class K8sDashboardAPI implements DashboardAPI<DashboardDTO, Dashboard> {
private client: ResourceClient<DashboardDataDTO, Status>;
client: ResourceClient<DashboardDataDTO, Status>;
constructor() {
this.client = new ScopedResourceClient<DashboardDataDTO>(K8S_V1_DASHBOARD_API_CONFIG);
+1 -1
View File
@@ -35,7 +35,7 @@ export const K8S_V2_DASHBOARD_API_CONFIG = {
export class K8sDashboardV2API
implements DashboardAPI<DashboardWithAccessInfo<DashboardV2Spec> | DashboardDTO, DashboardV2Spec>
{
private client: ResourceClient<DashboardV2Spec, Status>;
client: ResourceClient<DashboardV2Spec, Status>;
constructor() {
this.client = new ScopedResourceClient<DashboardV2Spec>(K8S_V2_DASHBOARD_API_CONFIG);
@@ -49,6 +49,7 @@ describe('validateUid', () => {
saveDashboard: jest.fn(),
listDeletedDashboards: jest.fn(),
restoreDashboard: jest.fn(),
listDashboardHistory: jest.fn(),
},
v2: {
getDashboardDTO: jest.fn().mockResolvedValue(v2Dashboard),
@@ -56,6 +57,7 @@ describe('validateUid', () => {
saveDashboard: jest.fn(),
listDeletedDashboards: jest.fn(),
restoreDashboard: jest.fn(),
listDashboardHistory: jest.fn(),
},
});
});