useReplayTraceMeta.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import {useMemo} from 'react';
  2. import type {Location} from 'history';
  3. import {getUtcDateString} from 'sentry/utils/dates';
  4. import type {TableDataRow} from 'sentry/utils/discover/discoverQuery';
  5. import EventView from 'sentry/utils/discover/eventView';
  6. import {useApiQuery} from 'sentry/utils/queryClient';
  7. import useOrganization from 'sentry/utils/useOrganization';
  8. import type {ReplayRecord} from 'sentry/views/replays/types';
  9. import {type TraceMetaQueryResults, useTraceMeta} from './useTraceMeta';
  10. // Fetches the meta data for all the traces in a replay and combines the results.
  11. export function useReplayTraceMeta(
  12. replayRecord: ReplayRecord | undefined
  13. ): TraceMetaQueryResults {
  14. const organization = useOrganization();
  15. // EventView that is used to fetch the list of events for the replay
  16. const eventView = useMemo(() => {
  17. if (!replayRecord) {
  18. return null;
  19. }
  20. const replayId = replayRecord?.id;
  21. const projectId = replayRecord?.project_id;
  22. const start = getUtcDateString(replayRecord?.started_at.getTime());
  23. const end = getUtcDateString(replayRecord?.finished_at.getTime());
  24. return EventView.fromSavedQuery({
  25. id: undefined,
  26. name: `Traces in replay ${replayId}`,
  27. fields: ['trace', 'count(trace)', 'min(timestamp)'],
  28. orderby: 'min_timestamp',
  29. query: `replayId:${replayId}`,
  30. projects: [Number(projectId)],
  31. version: 2,
  32. start,
  33. end,
  34. });
  35. }, [replayRecord]);
  36. const start = getUtcDateString(replayRecord?.started_at.getTime());
  37. const end = getUtcDateString(replayRecord?.finished_at.getTime());
  38. const {
  39. data: eventsData,
  40. isLoading: eventsIsLoading,
  41. isRefetching: eventsIsRefetching,
  42. refetch: eventsRefetch,
  43. } = useApiQuery<{data: TableDataRow[]}>(
  44. [
  45. `/organizations/${organization.slug}/events/`,
  46. {
  47. query: eventView
  48. ? {
  49. ...eventView.getEventsAPIPayload({
  50. start,
  51. end,
  52. limit: 10,
  53. } as unknown as Location),
  54. sort: ['min_timestamp', 'trace'],
  55. }
  56. : {},
  57. },
  58. ],
  59. {
  60. staleTime: Infinity,
  61. enabled: !!eventView && !!replayRecord,
  62. }
  63. );
  64. const traceIds = useMemo(() => {
  65. return (eventsData?.data ?? []).map(({trace}) => String(trace)).filter(Boolean);
  66. }, [eventsData]);
  67. const meta = useTraceMeta(traceIds);
  68. const metaResults = useMemo(() => {
  69. return {
  70. data: meta.data,
  71. isLoading: eventsIsLoading || meta.isLoading,
  72. isRefetching: meta.isRefetching || eventsIsRefetching,
  73. refetch: () => {
  74. meta.refetch();
  75. eventsRefetch();
  76. },
  77. };
  78. }, [meta, eventsIsLoading, eventsIsRefetching, eventsRefetch]);
  79. return metaResults;
  80. }