2025-01-27 21:14:19 +08:00
|
|
|
import { Dashboard } from '@grafana/schema';
|
2025-01-16 20:18:47 +08:00
|
|
|
import { DashboardV2Spec } from '@grafana/schema/dist/esm/schema/dashboard/v2alpha0';
|
2024-12-18 05:17:09 +08:00
|
|
|
import { DashboardDTO } from 'app/types';
|
2024-06-05 22:34:23 +08:00
|
|
|
|
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>;
|
2024-12-18 05:17:09 +08:00
|
|
|
};
|
2024-06-05 22:34:23 +08:00
|
|
|
|
2024-12-18 05:17:09 +08:00
|
|
|
type DashboardReturnTypes = DashboardDTO | DashboardWithAccessInfo<DashboardV2Spec>;
|
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-01-27 21:14:19 +08:00
|
|
|
export function getDashboardAPI(): DashboardAPI<DashboardDTO, Dashboard>;
|
|
|
|
export function getDashboardAPI(
|
|
|
|
requestV2Response: 'v2'
|
|
|
|
): DashboardAPI<DashboardWithAccessInfo<DashboardV2Spec>, DashboardV2Spec>;
|
|
|
|
export function getDashboardAPI(
|
|
|
|
requestV2Response?: 'v2'
|
|
|
|
): DashboardAPI<DashboardReturnTypes, Dashboard | DashboardV2Spec> {
|
2024-12-18 05:17:09 +08:00
|
|
|
const v = getDashboardsApiVersion();
|
|
|
|
const isConvertingToV1 = !requestV2Response;
|
|
|
|
|
|
|
|
if (!clients) {
|
|
|
|
clients = {
|
|
|
|
legacy: new LegacyDashboardAPI(),
|
2025-02-20 19:42:59 +08:00
|
|
|
v1: new K8sDashboardAPI(),
|
2025-01-02 19:23:58 +08:00
|
|
|
v2: new K8sDashboardV2API(isConvertingToV1),
|
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 (v === 'v2' && requestV2Response === 'v2') {
|
2025-01-02 19:23:58 +08:00
|
|
|
return new K8sDashboardV2API(false);
|
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
|
|
|
}
|