api.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { lastValueFrom } from 'rxjs';
  2. import { getBackendSrv } from '../../../core/services/backend_srv';
  3. import { DashboardSearchHit } from '../../search/types';
  4. import {
  5. LibraryElementConnectionDTO,
  6. LibraryElementDTO,
  7. LibraryElementKind,
  8. LibraryElementsSearchResult,
  9. PanelModelWithLibraryPanel,
  10. } from '../types';
  11. export interface GetLibraryPanelsOptions {
  12. searchString?: string;
  13. perPage?: number;
  14. page?: number;
  15. excludeUid?: string;
  16. sortDirection?: string;
  17. typeFilter?: string[];
  18. folderFilter?: string[];
  19. }
  20. export async function getLibraryPanels({
  21. searchString = '',
  22. perPage = 100,
  23. page = 1,
  24. excludeUid = '',
  25. sortDirection = '',
  26. typeFilter = [],
  27. folderFilter = [],
  28. }: GetLibraryPanelsOptions = {}): Promise<LibraryElementsSearchResult> {
  29. const params = new URLSearchParams();
  30. params.append('searchString', searchString);
  31. params.append('sortDirection', sortDirection);
  32. params.append('typeFilter', typeFilter.join(','));
  33. params.append('folderFilter', folderFilter.join(','));
  34. params.append('excludeUid', excludeUid);
  35. params.append('perPage', perPage.toString(10));
  36. params.append('page', page.toString(10));
  37. params.append('kind', LibraryElementKind.Panel.toString(10));
  38. const { result } = await getBackendSrv().get<{ result: LibraryElementsSearchResult }>(
  39. `/api/library-elements?${params.toString()}`
  40. );
  41. return result;
  42. }
  43. export async function getLibraryPanel(uid: string, isHandled = false): Promise<LibraryElementDTO> {
  44. const response = await lastValueFrom(
  45. getBackendSrv().fetch<{ result: LibraryElementDTO }>({
  46. method: 'GET',
  47. url: `/api/library-elements/${uid}`,
  48. showSuccessAlert: !isHandled,
  49. showErrorAlert: !isHandled,
  50. })
  51. );
  52. return response.data.result;
  53. }
  54. export async function getLibraryPanelByName(name: string): Promise<LibraryElementDTO[]> {
  55. const { result } = await getBackendSrv().get<{ result: LibraryElementDTO[] }>(`/api/library-elements/name/${name}`);
  56. return result;
  57. }
  58. export async function addLibraryPanel(
  59. panelSaveModel: PanelModelWithLibraryPanel,
  60. folderId: number
  61. ): Promise<LibraryElementDTO> {
  62. const { result } = await getBackendSrv().post(`/api/library-elements`, {
  63. folderId,
  64. name: panelSaveModel.libraryPanel.name,
  65. model: panelSaveModel,
  66. kind: LibraryElementKind.Panel,
  67. });
  68. return result;
  69. }
  70. export async function updateLibraryPanel(panelSaveModel: PanelModelWithLibraryPanel): Promise<LibraryElementDTO> {
  71. const { uid, name, version } = panelSaveModel.libraryPanel;
  72. const kind = LibraryElementKind.Panel;
  73. const model = panelSaveModel;
  74. const { result } = await getBackendSrv().patch(`/api/library-elements/${uid}`, {
  75. name,
  76. model,
  77. version,
  78. kind,
  79. });
  80. return result;
  81. }
  82. export function deleteLibraryPanel(uid: string): Promise<{ message: string }> {
  83. return getBackendSrv().delete(`/api/library-elements/${uid}`);
  84. }
  85. export async function getLibraryPanelConnectedDashboards(
  86. libraryPanelUid: string
  87. ): Promise<LibraryElementConnectionDTO[]> {
  88. const { result } = await getBackendSrv().get<{ result: LibraryElementConnectionDTO[] }>(
  89. `/api/library-elements/${libraryPanelUid}/connections`
  90. );
  91. return result;
  92. }
  93. export async function getConnectedDashboards(uid: string): Promise<DashboardSearchHit[]> {
  94. const connections = await getLibraryPanelConnectedDashboards(uid);
  95. if (connections.length === 0) {
  96. return [];
  97. }
  98. const searchHits = await getBackendSrv().search({ dashboardIds: connections.map((c) => c.connectionId) });
  99. return searchHits;
  100. }