serverSideSampling.tsx 4.7 KB

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