serverSideSampling.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. projects,
  12. statsPeriod,
  13. }: {
  14. api: Client;
  15. orgSlug: Organization['slug'];
  16. projectID?: Project['id']; // TODO: Remove conditional on this when projects is removed.
  17. projects?: number[]; // TODO: Remove this later since we do not need this for source check in the long term.
  18. statsPeriod?: string;
  19. }): Promise<SamplingSdkVersion[]> {
  20. const {samplingDistribution} = ServerSideSamplingStore.getState();
  21. const projectIds = projects ?? [
  22. projectID,
  23. ...(samplingDistribution.project_breakdown?.map(
  24. projectBreakdown => projectBreakdown.project_id
  25. ) ?? []),
  26. ];
  27. const promise = api.requestPromise(
  28. `/organizations/${orgSlug}/dynamic-sampling/sdk-versions/`,
  29. {
  30. query: {
  31. project: projectIds,
  32. statsPeriod: statsPeriod ?? '24h',
  33. },
  34. }
  35. );
  36. ServerSideSamplingStore.setFetching(true);
  37. promise
  38. .then(ServerSideSamplingStore.loadSamplingSdkVersionsSuccess)
  39. .catch(response => {
  40. const errorMessage = t('Unable to fetch sampling sdk versions');
  41. handleXhrErrorResponse(errorMessage)(response);
  42. })
  43. .finally(() => {
  44. ServerSideSamplingStore.setFetching(false);
  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. ServerSideSamplingStore.reset();
  58. ServerSideSamplingStore.setFetching(true);
  59. const promise = api.requestPromise(
  60. `/projects/${orgSlug}/${projSlug}/dynamic-sampling/distribution/`,
  61. {
  62. query: {
  63. statsPeriod: '24h',
  64. },
  65. }
  66. );
  67. promise
  68. .then(ServerSideSamplingStore.loadSamplingDistributionSuccess)
  69. .catch(response => {
  70. const errorMessage = t('Unable to fetch sampling distribution');
  71. handleXhrErrorResponse(errorMessage)(response);
  72. })
  73. .finally(() => {
  74. ServerSideSamplingStore.setFetching(false);
  75. });
  76. return promise;
  77. }