useSpanSeenEvent.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import {useQuery} from 'sentry/utils/queryClient';
  2. import {useQueryGetEvent} from 'sentry/views/starfish/modules/databaseModule/queries';
  3. import type {Span} from 'sentry/views/starfish/queries/types';
  4. import {HOST} from 'sentry/views/starfish/utils/constants';
  5. type HasTransaction = {
  6. transaction_id: string;
  7. };
  8. function useFirstSeenSpan(span?: Pick<Span, 'group_id'>, referrer = 'first-seen-span') {
  9. const query = span
  10. ? `
  11. SELECT transaction_id
  12. FROM spans_experimental_starfish
  13. WHERE group_id = '${span.group_id}'
  14. ORDER BY start_timestamp ASC
  15. LIMIT 1
  16. `
  17. : null;
  18. const result = useQuery<(Span & HasTransaction)[]>({
  19. queryKey: ['span', 'first', span?.group_id],
  20. queryFn: () =>
  21. fetch(`${HOST}/?query=${query}&referrer=${referrer}`).then(res => res.json()),
  22. retry: false,
  23. initialData: [],
  24. enabled: Boolean(span),
  25. });
  26. return {...result, data: result.data[0]};
  27. }
  28. export const useSpanFirstSeenEvent = (
  29. span?: Pick<Span, 'group_id'>,
  30. referrer = 'span-first-seen-event'
  31. ) => {
  32. const {data} = useFirstSeenSpan(span, referrer);
  33. return useQueryGetEvent(data?.transaction_id);
  34. };
  35. function useLastSeenSpan(span?: Pick<Span, 'group_id'>, referrer = 'last-seen-span') {
  36. const query = span
  37. ? `
  38. SELECT transaction_id
  39. FROM spans_experimental_starfish
  40. WHERE group_id = '${span.group_id}'
  41. ORDER BY start_timestamp DESC
  42. LIMIT 1
  43. `
  44. : null;
  45. const result = useQuery<(Span & HasTransaction)[]>({
  46. queryKey: ['span', 'last', span?.group_id],
  47. queryFn: () =>
  48. fetch(`${HOST}/?query=${query}&referrer=${referrer}`).then(res => res.json()),
  49. retry: false,
  50. initialData: [],
  51. enabled: Boolean(span),
  52. });
  53. return {...result, data: result.data[0]};
  54. }
  55. export const useSpanLastSeenEvent = (
  56. span?: Pick<Span, 'group_id'>,
  57. referrer = 'span-last-seen-event'
  58. ) => {
  59. const {data} = useLastSeenSpan(span, referrer);
  60. return useQueryGetEvent(data?.transaction_id);
  61. };