serverSideSampling.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import {Client} from 'sentry/api';
  2. import {t} from 'sentry/locale';
  3. import {ServerSideSamplingStore} from 'sentry/stores/serverSideSamplingStore';
  4. import {Organization, Project} 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 {distribution} = ServerSideSamplingStore.getState();
  17. const {startTimestamp, endTimestamp, project_breakdown} = distribution.data ?? {};
  18. ServerSideSamplingStore.fetchSdkVersions();
  19. if (!startTimestamp || !endTimestamp) {
  20. ServerSideSamplingStore.fetchSdkVersionsSuccess([]);
  21. return new Promise(resolve => {
  22. resolve([]);
  23. });
  24. }
  25. const projectIds = [
  26. projectID,
  27. ...(project_breakdown?.map(projectBreakdown => projectBreakdown.project_id) ?? []),
  28. ];
  29. const promise = api.requestPromise(
  30. `/organizations/${orgSlug}/dynamic-sampling/sdk-versions/`,
  31. {
  32. query: {
  33. project: projectIds,
  34. start: startTimestamp,
  35. end: endTimestamp,
  36. },
  37. }
  38. );
  39. promise.then(ServerSideSamplingStore.fetchSdkVersionsSuccess).catch(response => {
  40. const errorMessage = t('Unable to fetch sampling sdk versions');
  41. handleXhrErrorResponse(errorMessage)(response);
  42. ServerSideSamplingStore.fetchSdkVersionsError(errorMessage);
  43. });
  44. return promise;
  45. }
  46. export function fetchSamplingDistribution({
  47. api,
  48. orgSlug,
  49. projSlug,
  50. }: {
  51. api: Client;
  52. orgSlug: Organization['slug'];
  53. projSlug: Project['slug'];
  54. }): Promise<SamplingDistribution> {
  55. ServerSideSamplingStore.reset();
  56. ServerSideSamplingStore.fetchDistribution();
  57. const promise = api.requestPromise(
  58. `/projects/${orgSlug}/${projSlug}/dynamic-sampling/distribution/`
  59. );
  60. promise.then(ServerSideSamplingStore.fetchDistributionSuccess).catch(response => {
  61. const errorMessage = t('Unable to fetch sampling distribution');
  62. handleXhrErrorResponse(errorMessage)(response);
  63. ServerSideSamplingStore.fetchDistributionError(errorMessage);
  64. });
  65. return promise;
  66. }
  67. export function fetchProjectStats48h({
  68. api,
  69. orgSlug,
  70. projId,
  71. }: {
  72. api: Client;
  73. orgSlug: Organization['slug'];
  74. projId?: Project['id'];
  75. }) {
  76. ServerSideSamplingStore.fetchProjectStats48h();
  77. const promise = api.requestPromise(`/organizations/${orgSlug}/stats_v2/`, {
  78. query: {
  79. project: projId,
  80. category: 'transaction',
  81. field: 'sum(quantity)',
  82. interval: '1h',
  83. statsPeriod: '48h',
  84. groupBy: 'outcome',
  85. },
  86. });
  87. promise.then(ServerSideSamplingStore.fetchProjectStats48hSuccess).catch(response => {
  88. const errorMessage = t('Unable to fetch project stats from the last 48 hours');
  89. handleXhrErrorResponse(errorMessage)(response);
  90. ServerSideSamplingStore.fetchProjectStats48hError(errorMessage);
  91. });
  92. return promise;
  93. }
  94. export function fetchProjectStats30d({
  95. api,
  96. orgSlug,
  97. projId,
  98. }: {
  99. api: Client;
  100. orgSlug: Organization['slug'];
  101. projId?: Project['id'];
  102. }) {
  103. ServerSideSamplingStore.fetchProjectStats30d();
  104. const promise = api.requestPromise(`/organizations/${orgSlug}/stats_v2/`, {
  105. query: {
  106. project: projId,
  107. category: 'transaction',
  108. field: 'sum(quantity)',
  109. interval: '1d',
  110. statsPeriod: '30d',
  111. groupBy: ['outcome', 'reason'],
  112. },
  113. });
  114. promise.then(ServerSideSamplingStore.fetchProjectStats30dSuccess).catch(response => {
  115. const errorMessage = t('Unable to fetch project stats from the last 30 days');
  116. handleXhrErrorResponse(errorMessage)(response);
  117. ServerSideSamplingStore.fetchProjectStats30dError(errorMessage);
  118. });
  119. return promise;
  120. }