serverSideSampling.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import {Client} from 'sentry/api';
  2. import {t} from 'sentry/locale';
  3. import {ServerSideSamplingStore} from 'sentry/stores/serverSideSamplingStore';
  4. import {Organization, Project, SeriesApi} from 'sentry/types';
  5. import {SamplingDistribution, SamplingSdkVersion} from 'sentry/types/sampling';
  6. import handleXhrErrorResponse from 'sentry/utils/handleXhrErrorResponse';
  7. export function fetchSamplingSdkVersions({
  8. api,
  9. orgSlug,
  10. projectID,
  11. }: {
  12. api: Client;
  13. orgSlug: Organization['slug'];
  14. projectID: Project['id'];
  15. }): Promise<SamplingSdkVersion[]> {
  16. const sdkVersions = ServerSideSamplingStore.getState().sdkVersions.data;
  17. if (sdkVersions !== undefined) {
  18. return new Promise(resolve => resolve(sdkVersions));
  19. }
  20. const distribution = ServerSideSamplingStore.getState().distribution.data;
  21. const {startTimestamp, endTimestamp, project_breakdown} = distribution ?? {};
  22. ServerSideSamplingStore.sdkVersionsRequestLoading();
  23. if (!startTimestamp || !endTimestamp) {
  24. ServerSideSamplingStore.sdkVersionsRequestSuccess([]);
  25. return new Promise(resolve => resolve([]));
  26. }
  27. const projectIds = [
  28. projectID,
  29. ...(project_breakdown?.map(projectBreakdown => projectBreakdown.project_id) ?? []),
  30. ];
  31. const promise = api.requestPromise(
  32. `/organizations/${orgSlug}/dynamic-sampling/sdk-versions/`,
  33. {
  34. query: {
  35. project: projectIds,
  36. start: startTimestamp,
  37. end: endTimestamp,
  38. },
  39. }
  40. );
  41. promise.then(ServerSideSamplingStore.sdkVersionsRequestSuccess).catch(response => {
  42. const errorMessage = t('Unable to fetch sampling sdk versions');
  43. handleXhrErrorResponse(errorMessage)(response);
  44. ServerSideSamplingStore.sdkVersionsRequestError(errorMessage);
  45. });
  46. return promise;
  47. }
  48. export function fetchSamplingDistribution({
  49. api,
  50. orgSlug,
  51. projSlug,
  52. }: {
  53. api: Client;
  54. orgSlug: Organization['slug'];
  55. projSlug: Project['slug'];
  56. }): Promise<SamplingDistribution> {
  57. const distribution = ServerSideSamplingStore.getState().distribution.data;
  58. if (distribution !== undefined) {
  59. return new Promise(resolve => resolve(distribution));
  60. }
  61. ServerSideSamplingStore.distributionRequestLoading();
  62. const promise = api.requestPromise(
  63. `/projects/${orgSlug}/${projSlug}/dynamic-sampling/distribution/`
  64. );
  65. promise.then(ServerSideSamplingStore.distributionRequestSuccess).catch(response => {
  66. const errorMessage = t('Unable to fetch sampling distribution');
  67. handleXhrErrorResponse(errorMessage)(response);
  68. ServerSideSamplingStore.distributionRequestError(errorMessage);
  69. });
  70. return promise;
  71. }
  72. function fetchProjectStats48h({
  73. api,
  74. orgSlug,
  75. projId,
  76. }: {
  77. api: Client;
  78. orgSlug: Organization['slug'];
  79. projId?: Project['id'];
  80. }) {
  81. ServerSideSamplingStore.projectStats48hRequestLoading();
  82. const promise = api.requestPromise(`/organizations/${orgSlug}/stats_v2/`, {
  83. query: {
  84. project: projId,
  85. category: 'transaction',
  86. field: 'sum(quantity)',
  87. interval: '1h',
  88. statsPeriod: '48h',
  89. groupBy: 'outcome',
  90. },
  91. });
  92. promise.then(ServerSideSamplingStore.projectStats48hRequestSuccess).catch(response => {
  93. const errorMessage = t('Unable to fetch project stats from the last 48 hours');
  94. handleXhrErrorResponse(errorMessage)(response);
  95. ServerSideSamplingStore.projectStats48hRequestError(errorMessage);
  96. });
  97. return promise;
  98. }
  99. function fetchProjectStats30d({
  100. api,
  101. orgSlug,
  102. projId,
  103. }: {
  104. api: Client;
  105. orgSlug: Organization['slug'];
  106. projId?: Project['id'];
  107. }) {
  108. ServerSideSamplingStore.projectStats30dRequestLoading();
  109. const promise = api.requestPromise(`/organizations/${orgSlug}/stats_v2/`, {
  110. query: {
  111. project: projId,
  112. category: 'transaction',
  113. field: 'sum(quantity)',
  114. interval: '1d',
  115. statsPeriod: '30d',
  116. groupBy: ['outcome', 'reason'],
  117. },
  118. });
  119. promise.then(ServerSideSamplingStore.projectStats30dRequestSuccess).catch(response => {
  120. const errorMessage = t('Unable to fetch project stats from the last 30 days');
  121. handleXhrErrorResponse(errorMessage)(response);
  122. ServerSideSamplingStore.projectStats30dRequestError(errorMessage);
  123. });
  124. return promise;
  125. }
  126. export function fetchProjectStats(props: {
  127. api: Client;
  128. orgSlug: Organization['slug'];
  129. projId?: Project['id'];
  130. }): Promise<[SeriesApi, SeriesApi]> {
  131. const projectStats48h = ServerSideSamplingStore.getState().projectStats48h.data;
  132. const projectStats30d = ServerSideSamplingStore.getState().projectStats30d.data;
  133. if (projectStats48h !== undefined && projectStats30d !== undefined) {
  134. return new Promise(resolve => resolve([projectStats48h, projectStats30d]));
  135. }
  136. ServerSideSamplingStore.reset();
  137. return Promise.all([fetchProjectStats48h(props), fetchProjectStats30d(props)]);
  138. }