useTraceEventView.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import {useMemo} from 'react';
  2. import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
  3. import EventView from 'sentry/utils/discover/eventView';
  4. import type {TraceViewQueryParams} from './useTraceQueryParams';
  5. export function useTraceEventView(
  6. traceSlug: string,
  7. params: TraceViewQueryParams
  8. ): EventView {
  9. return useMemo(() => {
  10. let startTimeStamp = params.start;
  11. let endTimeStamp = params.end;
  12. // If timestamp exists in the query params, we want to use it to set the start and end time
  13. // with a buffer of 1.5 days, for retrieving events belonging to the trace.
  14. if (typeof params.timestamp === 'number') {
  15. const buffer = 36 * 60 * 60 * 1000; // 1.5 days in milliseconds
  16. const dateFromTimestamp = new Date(params.timestamp * 1000);
  17. startTimeStamp = new Date(dateFromTimestamp.getTime() - buffer).toISOString();
  18. endTimeStamp = new Date(dateFromTimestamp.getTime() + buffer).toISOString();
  19. }
  20. return EventView.fromSavedQuery({
  21. id: undefined,
  22. name: `Events with Trace ID ${traceSlug}`,
  23. fields: ['title', 'event.type', 'project', 'timestamp'],
  24. orderby: '-timestamp',
  25. query: `trace:${traceSlug}`,
  26. projects: [ALL_ACCESS_PROJECTS],
  27. version: 2,
  28. start: startTimeStamp,
  29. end: endTimeStamp,
  30. range: !(startTimeStamp || endTimeStamp) ? params.statsPeriod : undefined,
  31. });
  32. }, [params, traceSlug]);
  33. }