2025-01-27 21:14:19 +08:00
|
|
|
import { Dashboard } from '@grafana/schema';
|
2025-04-02 22:22:24 +08:00
|
|
|
import { Spec as DashboardV2Spec } from '@grafana/schema/dist/esm/schema/dashboard/v2alpha1/types.spec.gen';
|
2024-12-18 05:17:09 +08:00
|
|
|
import { DashboardDTO } from 'app/types';
|
2024-06-05 22:34:23 +08:00
|
|
|
|
2025-03-13 01:43:32 +08:00
|
|
|
import { UnifiedDashboardAPI } from './UnifiedDashboardAPI';
|
2024-12-18 05:17:09 +08:00
|
|
|
import { LegacyDashboardAPI } from './legacy';
|
|
|
|
import { DashboardAPI, DashboardWithAccessInfo } from './types';
|
|
|
|
import { getDashboardsApiVersion } from './utils';
|
2025-02-20 19:42:59 +08:00
|
|
|
import { K8sDashboardAPI } from './v1';
|
2025-01-02 19:23:58 +08:00
|
|
|
import { K8sDashboardV2API } from './v2';
|
2024-06-05 22:34:23 +08:00
|
|
|
|
2024-12-18 05:17:09 +08:00
|
|
|
type DashboardAPIClients = {
|
2025-01-27 21:14:19 +08:00
|
|
|
legacy: DashboardAPI<DashboardDTO, Dashboard>;
|
2025-02-20 19:42:59 +08:00
|
|
|
v1: DashboardAPI<DashboardDTO, Dashboard>;
|
2025-01-27 21:14:19 +08:00
|
|
|
v2: DashboardAPI<DashboardDTO | DashboardWithAccessInfo<DashboardV2Spec>, DashboardV2Spec>;
|
2025-03-13 01:43:32 +08:00
|
|
|
unified: DashboardAPI<DashboardDTO | DashboardWithAccessInfo<DashboardV2Spec>, Dashboard | DashboardV2Spec>;
|
2024-12-18 05:17:09 +08:00
|
|
|
};
|
2024-06-05 22:34:23 +08:00
|
|
|
|
2024-12-18 05:17:09 +08:00
|
|
|
let clients: Partial<DashboardAPIClients> | undefined;
|
2024-06-05 22:34:23 +08:00
|
|
|
|
2024-12-18 05:17:09 +08:00
|
|
|
export function setDashboardAPI(override: Partial<DashboardAPIClients> | undefined) {
|
|
|
|
if (process.env.NODE_ENV !== 'test') {
|
|
|
|
throw new Error('dashboardAPI can be only overridden in test environment');
|
2024-06-05 22:34:23 +08:00
|
|
|
}
|
2024-12-18 05:17:09 +08:00
|
|
|
clients = override;
|
2024-06-05 22:34:23 +08:00
|
|
|
}
|
|
|
|
|
2024-12-18 05:17:09 +08:00
|
|
|
// Overloads
|
2025-03-13 01:43:32 +08:00
|
|
|
export function getDashboardAPI(): DashboardAPI<
|
|
|
|
DashboardDTO | DashboardWithAccessInfo<DashboardV2Spec>,
|
|
|
|
Dashboard | DashboardV2Spec
|
|
|
|
>;
|
|
|
|
export function getDashboardAPI(responseFormat: 'v1'): DashboardAPI<DashboardDTO, Dashboard>;
|
2025-01-27 21:14:19 +08:00
|
|
|
export function getDashboardAPI(
|
2025-03-13 01:43:32 +08:00
|
|
|
responseFormat: 'v2'
|
2025-01-27 21:14:19 +08:00
|
|
|
): DashboardAPI<DashboardWithAccessInfo<DashboardV2Spec>, DashboardV2Spec>;
|
|
|
|
export function getDashboardAPI(
|
2025-03-13 01:43:32 +08:00
|
|
|
responseFormat?: 'v1' | 'v2'
|
|
|
|
): DashboardAPI<DashboardDTO | DashboardWithAccessInfo<DashboardV2Spec>, Dashboard | DashboardV2Spec> {
|
|
|
|
const v = getDashboardsApiVersion(responseFormat);
|
2024-12-18 05:17:09 +08:00
|
|
|
|
|
|
|
if (!clients) {
|
|
|
|
clients = {
|
|
|
|
legacy: new LegacyDashboardAPI(),
|
2025-02-20 19:42:59 +08:00
|
|
|
v1: new K8sDashboardAPI(),
|
2025-03-13 01:43:32 +08:00
|
|
|
v2: new K8sDashboardV2API(),
|
|
|
|
unified: new UnifiedDashboardAPI(),
|
2024-06-26 18:35:04 +08:00
|
|
|
};
|
2024-06-05 22:34:23 +08:00
|
|
|
}
|
|
|
|
|
2024-12-18 05:17:09 +08:00
|
|
|
if (!clients[v]) {
|
|
|
|
throw new Error(`Unknown Dashboard API version: ${v}`);
|
2024-06-05 22:34:23 +08:00
|
|
|
}
|
|
|
|
|
2024-12-18 05:17:09 +08:00
|
|
|
return clients[v];
|
2024-06-20 23:49:19 +08:00
|
|
|
}
|