actions.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { getBackendSrv } from '@grafana/runtime';
  2. import { backendSrv } from 'app/core/services/backend_srv';
  3. import { DashboardModel } from 'app/features/dashboard/state';
  4. import { initVariablesTransaction } from 'app/features/variables/state/actions';
  5. import { ThunkResult } from 'app/types';
  6. import { Report, ReportDTO, SchedulingOptions } from '../../types';
  7. import { applyDefaultVariables } from '../utils/variables';
  8. import {
  9. reportLoaded,
  10. reportLoadingBegin,
  11. reportLoadingEnd,
  12. reportsLoaded,
  13. setLastUid,
  14. testEmailSendBegin,
  15. testEmailSendEnd,
  16. } from './reducers';
  17. const baseUrl = 'api/reports';
  18. export function getReports(): ThunkResult<void> {
  19. return async (dispatch) => {
  20. const reports = await getBackendSrv().get(baseUrl);
  21. dispatch(reportsLoaded(reports));
  22. };
  23. }
  24. export function initVariables(dashboardUid: string, templateVars?: Report['templateVars']): ThunkResult<void> {
  25. return async (dispatch) => {
  26. const resp = await backendSrv.getDashboardByUid(dashboardUid);
  27. const dashboard = new DashboardModel(resp.dashboard, resp.meta);
  28. const list = applyDefaultVariables(dashboard.templating.list, templateVars);
  29. dispatch(initVariablesTransaction(resp.dashboard.uid, { ...dashboard, templating: { list } } as DashboardModel));
  30. dispatch(setLastUid(dashboardUid));
  31. };
  32. }
  33. export function loadReport(id: number): ThunkResult<void> {
  34. return async (dispatch) => {
  35. dispatch(reportLoadingBegin());
  36. try {
  37. const report = await getBackendSrv().get(`${baseUrl}/${id}`);
  38. if (!report.dashboards) {
  39. report.dashboards = [
  40. {
  41. dashboard: {
  42. uid: report.dashboardUid,
  43. id: report.dashboardId,
  44. name: report.dashboardName,
  45. },
  46. reportVariables: report.templateVars,
  47. timeRange: report.options.timeRange,
  48. },
  49. ];
  50. }
  51. for (const db of report.dashboards) {
  52. if (db.dashboard.uid) {
  53. dispatch(initVariables(db.dashboard.uid, db.reportVariables));
  54. }
  55. }
  56. dispatch(reportLoaded(report));
  57. } catch (e) {
  58. dispatch(reportLoadingEnd());
  59. }
  60. };
  61. }
  62. export function sendTestEmail(report: ReportDTO): ThunkResult<void> {
  63. return (dispatch) => {
  64. dispatch(testEmailSendBegin());
  65. return getBackendSrv()
  66. .post(`${baseUrl}/test-email/`, report)
  67. .finally(() => dispatch(testEmailSendEnd()));
  68. };
  69. }
  70. export function deleteReport(id: number): ThunkResult<void> {
  71. return async (dispatch) => {
  72. await getBackendSrv().delete(`${baseUrl}/${id}`);
  73. dispatch(getReports());
  74. };
  75. }
  76. export function createReport(report: ReportDTO): ThunkResult<void> {
  77. return async (dispatch) => {
  78. try {
  79. await getBackendSrv().post(baseUrl, report);
  80. } catch (error) {
  81. throw error;
  82. }
  83. dispatch(getReports());
  84. };
  85. }
  86. export function updateReport(report: ReportDTO): ThunkResult<void> {
  87. return async (dispatch) => {
  88. const deprecatedFields = ['hour', 'minute', 'day'];
  89. report = {
  90. ...report,
  91. schedule: Object.fromEntries(
  92. Object.entries(report.schedule).filter(([key, _]: [string, any]) => !deprecatedFields.includes(key) as unknown)
  93. ) as SchedulingOptions,
  94. };
  95. await getBackendSrv().put(`${baseUrl}/${report.id}`, report);
  96. dispatch(getReports());
  97. };
  98. }