useTraceTimelineEvents.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import {useMemo} from 'react';
  2. import type {Event} from 'sentry/types';
  3. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  4. import {getTraceTimeRangeFromEvent} from 'sentry/utils/performance/quickTrace/utils';
  5. import {useApiQuery} from 'sentry/utils/queryClient';
  6. import useOrganization from 'sentry/utils/useOrganization';
  7. export interface TimelineEvent {
  8. id: string;
  9. issue: string;
  10. 'issue.id': number;
  11. project: string;
  12. 'project.name': string;
  13. timestamp: string;
  14. title: string;
  15. }
  16. export interface TraceEventResponse {
  17. data: TimelineEvent[];
  18. meta: unknown;
  19. }
  20. interface UseTraceTimelineEventsOptions {
  21. event: Event;
  22. }
  23. export function useTraceTimelineEvents(
  24. {event}: UseTraceTimelineEventsOptions,
  25. isEnabled: boolean = true
  26. ) {
  27. const organization = useOrganization();
  28. const {start, end} = getTraceTimeRangeFromEvent(event);
  29. const traceId = event.contexts?.trace?.trace_id ?? '';
  30. const enabled = !!traceId && isEnabled;
  31. const {
  32. data: issuePlatformData,
  33. isLoading: isLoadingIssuePlatform,
  34. isError: isErrorIssuePlatform,
  35. } = useApiQuery<TraceEventResponse>(
  36. [
  37. `/organizations/${organization.slug}/events/`,
  38. {
  39. query: {
  40. // Get performance issues
  41. dataset: DiscoverDatasets.ISSUE_PLATFORM,
  42. field: ['title', 'project', 'timestamp', 'issue.id', 'issue'],
  43. per_page: 100,
  44. query: `trace:${traceId}`,
  45. referrer: 'api.issues.issue_events',
  46. sort: '-timestamp',
  47. start,
  48. end,
  49. },
  50. },
  51. ],
  52. {staleTime: Infinity, retry: false, enabled}
  53. );
  54. const {
  55. data: discoverData,
  56. isLoading: isLoadingDiscover,
  57. isError: isErrorDiscover,
  58. } = useApiQuery<{
  59. data: TimelineEvent[];
  60. meta: unknown;
  61. }>(
  62. [
  63. `/organizations/${organization.slug}/events/`,
  64. {
  65. query: {
  66. // Other events
  67. dataset: DiscoverDatasets.DISCOVER,
  68. field: ['title', 'project', 'timestamp', 'issue.id', 'issue'],
  69. per_page: 100,
  70. query: `trace:${traceId}`,
  71. referrer: 'api.issues.issue_events',
  72. sort: '-timestamp',
  73. start,
  74. end,
  75. },
  76. },
  77. ],
  78. {staleTime: Infinity, retry: false, enabled}
  79. );
  80. const eventData = useMemo(() => {
  81. if (
  82. isLoadingIssuePlatform ||
  83. isLoadingDiscover ||
  84. isErrorIssuePlatform ||
  85. isErrorDiscover
  86. ) {
  87. return {
  88. data: [],
  89. startTimestamp: 0,
  90. endTimestamp: 0,
  91. };
  92. }
  93. const events = [...issuePlatformData.data, ...discoverData.data];
  94. const timestamps = events.map(e => new Date(e.timestamp).getTime());
  95. const startTimestamp = Math.min(...timestamps);
  96. const endTimestamp = Math.max(...timestamps);
  97. return {
  98. data: events,
  99. startTimestamp,
  100. endTimestamp,
  101. };
  102. }, [
  103. issuePlatformData,
  104. discoverData,
  105. isLoadingIssuePlatform,
  106. isLoadingDiscover,
  107. isErrorIssuePlatform,
  108. isErrorDiscover,
  109. ]);
  110. return {
  111. data: eventData.data,
  112. startTimestamp: eventData.startTimestamp,
  113. endTimestamp: eventData.endTimestamp,
  114. isLoading: isLoadingIssuePlatform || isLoadingDiscover,
  115. isError: isErrorIssuePlatform || isErrorDiscover,
  116. };
  117. }