The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/features/library-panels/utils.ts

60 lines
2.0 KiB

import { LibraryPanelDTO, PanelModelLibraryPanel } from './types';
import { PanelModel } from '../dashboard/state';
import { addLibraryPanel, updateLibraryPanel } from './state/api';
import { createErrorNotification, createSuccessNotification } from '../../core/copy/appNotification';
import { AppNotification } from '../../types';
export function createPanelLibraryErrorNotification(message: string): AppNotification {
return createErrorNotification(message);
}
export function createPanelLibrarySuccessNotification(message: string): AppNotification {
return createSuccessNotification(message);
}
export function toPanelModelLibraryPanel(libraryPanelDto: LibraryPanelDTO): PanelModelLibraryPanel {
const { uid, name, meta, version } = libraryPanelDto;
return { uid, name, meta, version };
}
export async function saveAndRefreshLibraryPanel(panel: PanelModel, folderId: number): Promise<LibraryPanelDTO> {
const panelSaveModel = toPanelSaveModel(panel);
const savedPanel = await saveOrUpdateLibraryPanel(panelSaveModel, folderId);
updatePanelModelWithUpdate(panel, savedPanel);
return savedPanel;
}
function toPanelSaveModel(panel: PanelModel): any {
let panelSaveModel = panel.getSaveModel();
panelSaveModel = {
libraryPanel: {
name: panel.title,
uid: undefined,
},
...panelSaveModel,
};
return panelSaveModel;
}
function updatePanelModelWithUpdate(panel: PanelModel, updated: LibraryPanelDTO): void {
panel.restoreModel({
...updated.model,
hasChanged: false, // reset dirty flag, since changes have been saved
libraryPanel: toPanelModelLibraryPanel(updated),
});
panel.refresh();
}
function saveOrUpdateLibraryPanel(panel: any, folderId: number): Promise<LibraryPanelDTO> {
if (!panel.libraryPanel) {
return Promise.reject();
}
if (panel.libraryPanel && panel.libraryPanel.uid === undefined) {
panel.libraryPanel.name = panel.title;
return addLibraryPanel(panel, folderId!);
}
return updateLibraryPanel(panel, folderId!);
}