dashboards.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import omit from 'lodash/omit';
  2. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  3. import {Client} from 'sentry/api';
  4. import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
  5. import {t} from 'sentry/locale';
  6. import {DashboardDetails, DashboardListItem, Widget} from 'sentry/views/dashboards/types';
  7. import {flattenErrors} from 'sentry/views/dashboards/utils';
  8. export function fetchDashboards(api: Client, orgSlug: string) {
  9. const promise: Promise<DashboardListItem[]> = api.requestPromise(
  10. `/organizations/${orgSlug}/dashboards/`,
  11. {
  12. method: 'GET',
  13. query: {sort: 'myDashboardsAndRecentlyViewed'},
  14. }
  15. );
  16. promise.catch(response => {
  17. const errorResponse = response?.responseJSON ?? null;
  18. if (errorResponse) {
  19. const errors = flattenErrors(errorResponse, {});
  20. addErrorMessage(errors[Object.keys(errors)[0]]);
  21. } else {
  22. addErrorMessage(t('Unable to fetch dashboards'));
  23. }
  24. });
  25. return promise;
  26. }
  27. export function createDashboard(
  28. api: Client,
  29. orgId: string,
  30. newDashboard: DashboardDetails,
  31. duplicate?: boolean
  32. ): Promise<DashboardDetails> {
  33. const {title, widgets, projects, environment, period, start, end, filters, utc} =
  34. newDashboard;
  35. const promise: Promise<DashboardDetails> = api.requestPromise(
  36. `/organizations/${orgId}/dashboards/`,
  37. {
  38. method: 'POST',
  39. data: {
  40. title,
  41. widgets: widgets.map(widget => omit(widget, ['tempId'])),
  42. duplicate,
  43. projects,
  44. environment,
  45. period,
  46. start,
  47. end,
  48. filters,
  49. utc,
  50. },
  51. query: {
  52. project: projects,
  53. },
  54. }
  55. );
  56. promise.catch(response => {
  57. const errorResponse = response?.responseJSON ?? null;
  58. if (errorResponse) {
  59. const errors = flattenErrors(errorResponse, {});
  60. addErrorMessage(errors[Object.keys(errors)[0]]);
  61. } else {
  62. addErrorMessage(t('Unable to create dashboard'));
  63. }
  64. });
  65. return promise;
  66. }
  67. export function updateDashboardVisit(
  68. api: Client,
  69. orgId: string,
  70. dashboardId: string | string[]
  71. ): Promise<void> {
  72. const promise = api.requestPromise(
  73. `/organizations/${orgId}/dashboards/${dashboardId}/visit/`,
  74. {
  75. method: 'POST',
  76. }
  77. );
  78. return promise;
  79. }
  80. export function fetchDashboard(
  81. api: Client,
  82. orgId: string,
  83. dashboardId: string
  84. ): Promise<DashboardDetails> {
  85. const promise: Promise<DashboardDetails> = api.requestPromise(
  86. `/organizations/${orgId}/dashboards/${dashboardId}/`,
  87. {
  88. method: 'GET',
  89. }
  90. );
  91. promise.catch(response => {
  92. const errorResponse = response?.responseJSON ?? null;
  93. if (errorResponse) {
  94. const errors = flattenErrors(errorResponse, {});
  95. addErrorMessage(errors[Object.keys(errors)[0]]);
  96. } else {
  97. addErrorMessage(t('Unable to load dashboard'));
  98. }
  99. });
  100. return promise;
  101. }
  102. export function updateDashboard(
  103. api: Client,
  104. orgId: string,
  105. dashboard: DashboardDetails
  106. ): Promise<DashboardDetails> {
  107. const {title, widgets, projects, environment, period, start, end, filters, utc} =
  108. dashboard;
  109. const data = {
  110. title,
  111. widgets: widgets.map(widget => omit(widget, ['tempId'])),
  112. projects,
  113. environment,
  114. period,
  115. start,
  116. end,
  117. filters,
  118. utc,
  119. };
  120. const promise: Promise<DashboardDetails> = api.requestPromise(
  121. `/organizations/${orgId}/dashboards/${dashboard.id}/`,
  122. {
  123. method: 'PUT',
  124. data,
  125. query: {
  126. project: projects,
  127. },
  128. }
  129. );
  130. promise.catch(response => {
  131. const errorResponse = response?.responseJSON ?? null;
  132. if (errorResponse) {
  133. const errors = flattenErrors(errorResponse, {});
  134. addErrorMessage(errors[Object.keys(errors)[0]]);
  135. } else {
  136. addErrorMessage(t('Unable to update dashboard'));
  137. }
  138. });
  139. return promise;
  140. }
  141. export function deleteDashboard(
  142. api: Client,
  143. orgId: string,
  144. dashboardId: string
  145. ): Promise<undefined> {
  146. const promise: Promise<undefined> = api.requestPromise(
  147. `/organizations/${orgId}/dashboards/${dashboardId}/`,
  148. {
  149. method: 'DELETE',
  150. }
  151. );
  152. promise.catch(response => {
  153. const errorResponse = response?.responseJSON ?? null;
  154. if (errorResponse) {
  155. const errors = flattenErrors(errorResponse, {});
  156. addErrorMessage(errors[Object.keys(errors)[0]]);
  157. } else {
  158. addErrorMessage(t('Unable to delete dashboard'));
  159. }
  160. });
  161. return promise;
  162. }
  163. export function validateWidget(
  164. api: Client,
  165. orgId: string,
  166. widget: Widget
  167. ): Promise<undefined> {
  168. const promise: Promise<undefined> = api.requestPromise(
  169. `/organizations/${orgId}/dashboards/widgets/`,
  170. {
  171. method: 'POST',
  172. data: widget,
  173. query: {
  174. // TODO: This should be replaced in the future with projects
  175. // when we save Dashboard page filters. This is being sent to
  176. // bypass validation when creating or updating dashboards
  177. project: [ALL_ACCESS_PROJECTS],
  178. },
  179. }
  180. );
  181. return promise;
  182. }