useProfileEvents.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  2. import {t} from 'sentry/locale';
  3. import {PageFilters} from 'sentry/types';
  4. import {defined} from 'sentry/utils';
  5. import {useApiQuery} from 'sentry/utils/queryClient';
  6. import useOrganization from 'sentry/utils/useOrganization';
  7. import usePageFilters from 'sentry/utils/usePageFilters';
  8. import {ProfilingFieldType} from 'sentry/views/profiling/profileSummary/content';
  9. import type {EventsResults, Sort} from './types';
  10. export interface UseProfileEventsOptions<F extends string = ProfilingFieldType> {
  11. fields: readonly F[];
  12. referrer: string;
  13. sort: Sort<F>;
  14. cursor?: string;
  15. datetime?: PageFilters['datetime'];
  16. enabled?: boolean;
  17. limit?: number;
  18. projects?: (number | string)[];
  19. query?: string;
  20. refetchOnMount?: boolean;
  21. }
  22. export function useProfileEvents<F extends string>({
  23. fields,
  24. limit,
  25. referrer,
  26. query,
  27. sort,
  28. cursor,
  29. enabled = true,
  30. refetchOnMount = true,
  31. datetime,
  32. projects,
  33. }: UseProfileEventsOptions<F>) {
  34. const organization = useOrganization();
  35. const {selection} = usePageFilters();
  36. let dataset: 'profiles' | 'discover' = 'profiles';
  37. if (organization.features.includes('profiling-using-transactions')) {
  38. dataset = 'discover';
  39. query = `has:profile.id ${query ?? ''}`;
  40. }
  41. const path = `/organizations/${organization.slug}/events/`;
  42. const endpointOptions = {
  43. query: {
  44. dataset,
  45. referrer,
  46. project: projects || selection.projects,
  47. environment: selection.environments,
  48. ...normalizeDateTimeParams(datetime ?? selection.datetime),
  49. field: fields,
  50. per_page: limit,
  51. query,
  52. sort: sort.order === 'asc' ? sort.key : `-${sort.key}`,
  53. cursor,
  54. },
  55. };
  56. return useApiQuery<EventsResults<F>>([path, endpointOptions], {
  57. staleTime: 0,
  58. refetchOnWindowFocus: false,
  59. refetchOnMount,
  60. retry: false,
  61. enabled,
  62. });
  63. }
  64. export function formatError(error: any): string | null {
  65. if (!defined(error)) {
  66. return null;
  67. }
  68. const detail = error.responseJSON?.detail;
  69. if (typeof detail === 'string') {
  70. return detail;
  71. }
  72. const message = detail?.message;
  73. if (typeof message === 'string') {
  74. return message;
  75. }
  76. return t('An unknown error occurred.');
  77. }