DashboardSrv.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { lastValueFrom } from 'rxjs';
  2. import { BackendSrvRequest } from '@grafana/runtime';
  3. import { appEvents } from 'app/core/app_events';
  4. import { getBackendSrv } from 'app/core/services/backend_srv';
  5. import { saveDashboard } from 'app/features/manage-dashboards/state/actions';
  6. import { DashboardMeta } from 'app/types';
  7. import { RemovePanelEvent } from '../../../types/events';
  8. import { DashboardModel } from '../state/DashboardModel';
  9. import { removePanel } from '../utils/panel';
  10. export interface SaveDashboardOptions {
  11. /** The complete dashboard model. If `dashboard.id` is not set a new dashboard will be created. */
  12. dashboard: DashboardModel;
  13. /** Set a commit message for the version history. */
  14. message?: string;
  15. /** The id of the folder to save the dashboard in. */
  16. folderId?: number;
  17. /** The UID of the folder to save the dashboard in. Overrides `folderId`. */
  18. folderUid?: string;
  19. /** Set to `true` if you want to overwrite existing dashboard with newer version,
  20. * same dashboard title in folder or same dashboard uid. */
  21. overwrite?: boolean;
  22. /** Set the dashboard refresh interval.
  23. * If this is lower than the minimum refresh interval, Grafana will ignore it and will enforce the minimum refresh interval. */
  24. refresh?: string;
  25. }
  26. interface SaveDashboardResponse {
  27. id: number;
  28. slug: string;
  29. status: string;
  30. uid: string;
  31. url: string;
  32. version: number;
  33. }
  34. export class DashboardSrv {
  35. dashboard?: DashboardModel;
  36. constructor() {
  37. appEvents.subscribe(RemovePanelEvent, (e) => this.onRemovePanel(e.payload));
  38. }
  39. create(dashboard: any, meta: DashboardMeta) {
  40. return new DashboardModel(dashboard, meta);
  41. }
  42. setCurrent(dashboard: DashboardModel) {
  43. this.dashboard = dashboard;
  44. }
  45. getCurrent(): DashboardModel | undefined {
  46. if (!this.dashboard) {
  47. console.warn('Calling getDashboardSrv().getCurrent() without calling getDashboardSrv().setCurrent() first.');
  48. }
  49. return this.dashboard;
  50. }
  51. onRemovePanel = (panelId: number) => {
  52. const dashboard = this.getCurrent();
  53. if (dashboard) {
  54. removePanel(dashboard, dashboard.getPanelById(panelId)!, true);
  55. }
  56. };
  57. saveJSONDashboard(json: string) {
  58. const parsedJson = JSON.parse(json);
  59. return saveDashboard({
  60. dashboard: parsedJson,
  61. folderId: this.dashboard?.meta.folderId || parsedJson.folderId,
  62. });
  63. }
  64. saveDashboard(
  65. data: SaveDashboardOptions,
  66. requestOptions?: Pick<BackendSrvRequest, 'showErrorAlert' | 'showSuccessAlert'>
  67. ) {
  68. return lastValueFrom(
  69. getBackendSrv().fetch<SaveDashboardResponse>({
  70. url: '/api/dashboards/db/',
  71. method: 'POST',
  72. data: {
  73. ...data,
  74. dashboard: data.dashboard.getSaveModelClone(),
  75. },
  76. ...requestOptions,
  77. })
  78. );
  79. }
  80. starDashboard(dashboardId: string, isStarred: any) {
  81. const backendSrv = getBackendSrv();
  82. let promise;
  83. if (isStarred) {
  84. promise = backendSrv.delete('/api/user/stars/dashboard/' + dashboardId).then(() => {
  85. return false;
  86. });
  87. } else {
  88. promise = backendSrv.post('/api/user/stars/dashboard/' + dashboardId).then(() => {
  89. return true;
  90. });
  91. }
  92. return promise.then((res: boolean) => {
  93. if (this.dashboard && this.dashboard.id === dashboardId) {
  94. this.dashboard.meta.isStarred = res;
  95. }
  96. return res;
  97. });
  98. }
  99. }
  100. //
  101. // Code below is to export the service to React components
  102. //
  103. let singletonInstance: DashboardSrv;
  104. export function setDashboardSrv(instance: DashboardSrv) {
  105. singletonInstance = instance;
  106. }
  107. export function getDashboardSrv(): DashboardSrv {
  108. if (!singletonInstance) {
  109. singletonInstance = new DashboardSrv();
  110. }
  111. return singletonInstance;
  112. }