From c014f8b806468f5943c30af82b60ac0b70e04e10 Mon Sep 17 00:00:00 2001 From: Giordano Ricci Date: Mon, 28 Feb 2022 11:30:11 +0000 Subject: [PATCH] DashboardSrv: add saveDashboard method (#45627) * DashboardSrv: add saveDashboard method * revert external changes * use DashboardModel instead of DashboardDataDTO * use fetch instead of request * use getSaveModelClone in saveDashboard --- .../dashboard/services/DashboardSrv.ts | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/public/app/features/dashboard/services/DashboardSrv.ts b/public/app/features/dashboard/services/DashboardSrv.ts index 2341977103f..89e5e1ce937 100644 --- a/public/app/features/dashboard/services/DashboardSrv.ts +++ b/public/app/features/dashboard/services/DashboardSrv.ts @@ -5,6 +5,25 @@ import { DashboardMeta } from 'app/types'; import { getBackendSrv } from 'app/core/services/backend_srv'; import { saveDashboard } from 'app/features/manage-dashboards/state/actions'; import { RemovePanelEvent } from '../../../types/events'; +import { BackendSrvRequest } from '@grafana/runtime'; +import { lastValueFrom } from 'rxjs'; + +export interface SaveDashboardOptions { + /** The complete dashboard model. If `dashboard.id` is not set a new dashboard will be created. */ + dashboard: DashboardModel; + /** Set a commit message for the version history. */ + message?: string; + /** The id of the folder to save the dashboard in. */ + folderId?: number; + /** The UID of the folder to save the dashboard in. Overrides `folderId`. */ + folderUid?: string; + /** Set to `true` if you want to overwrite existing dashboard with newer version, + * same dashboard title in folder or same dashboard uid. */ + overwrite?: boolean; + /** Set the dashboard refresh interval. + * If this is lower than the minimum refresh interval, Grafana will ignore it and will enforce the minimum refresh interval. */ + refresh?: string; +} export class DashboardSrv { dashboard?: DashboardModel; @@ -43,6 +62,23 @@ export class DashboardSrv { }); } + saveDashboard( + data: SaveDashboardOptions, + requestOptions?: Pick + ) { + return lastValueFrom( + getBackendSrv().fetch({ + url: '/api/dashboards/db/', + method: 'POST', + data: { + ...data, + dashboard: data.dashboard.getSaveModelClone(), + }, + ...requestOptions, + }) + ); + } + starDashboard(dashboardId: string, isStarred: any) { const backendSrv = getBackendSrv(); let promise;