serverSideSampling.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 {samplingDistribution} = ServerSideSamplingStore.getState();
  17. const {startTimestamp, endTimestamp, project_breakdown} = samplingDistribution;
  18. if (!startTimestamp || !endTimestamp) {
  19. ServerSideSamplingStore.setFetching(false);
  20. ServerSideSamplingStore.loadSamplingSdkVersionsSuccess([]);
  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. ServerSideSamplingStore.setFetching(true);
  40. promise
  41. .then(ServerSideSamplingStore.loadSamplingSdkVersionsSuccess)
  42. .catch(response => {
  43. const errorMessage = t('Unable to fetch sampling sdk versions');
  44. handleXhrErrorResponse(errorMessage)(response);
  45. })
  46. .finally(() => {
  47. ServerSideSamplingStore.setFetching(false);
  48. });
  49. return promise;
  50. }
  51. export function fetchSamplingDistribution({
  52. api,
  53. orgSlug,
  54. projSlug,
  55. }: {
  56. api: Client;
  57. orgSlug: Organization['slug'];
  58. projSlug: Project['slug'];
  59. }): Promise<SamplingDistribution> {
  60. ServerSideSamplingStore.reset();
  61. ServerSideSamplingStore.setFetching(true);
  62. const promise = api.requestPromise(
  63. `/projects/${orgSlug}/${projSlug}/dynamic-sampling/distribution/`
  64. );
  65. promise
  66. .then(ServerSideSamplingStore.loadSamplingDistributionSuccess)
  67. .catch(response => {
  68. const errorMessage = t('Unable to fetch sampling distribution');
  69. handleXhrErrorResponse(errorMessage)(response);
  70. })
  71. .finally(() => {
  72. ServerSideSamplingStore.setFetching(false);
  73. });
  74. return promise;
  75. }