useReplayTraceMeta.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 {data: eventsData, isLoading: eventsIsLoading} = useApiQuery<{
  39. data: TableDataRow[];
  40. }>(
  41. [
  42. `/organizations/${organization.slug}/events/`,
  43. {
  44. query: eventView
  45. ? {
  46. ...eventView.getEventsAPIPayload({
  47. start,
  48. end,
  49. limit: 10,
  50. } as unknown as Location),
  51. sort: ['min_timestamp', 'trace'],
  52. }
  53. : {},
  54. },
  55. ],
  56. {
  57. staleTime: Infinity,
  58. enabled: !!eventView && !!replayRecord,
  59. }
  60. );
  61. const traceIds = useMemo(() => {
  62. return (eventsData?.data ?? []).map(({trace}) => String(trace)).filter(Boolean);
  63. }, [eventsData]);
  64. const meta = useTraceMeta(traceIds);
  65. const metaResults = useMemo(() => {
  66. return {
  67. data: meta.data,
  68. isLoading: eventsIsLoading || meta.isLoading,
  69. errors: meta.errors,
  70. };
  71. }, [meta, eventsIsLoading]);
  72. return metaResults;
  73. }