useSpanTransactions.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import {useQuery} from 'sentry/utils/queryClient';
  2. import usePageFilters from 'sentry/utils/usePageFilters';
  3. import type {Span} from 'sentry/views/starfish/queries/types';
  4. import {HOST} from 'sentry/views/starfish/utils/constants';
  5. import {getDateFilters} from 'sentry/views/starfish/utils/dates';
  6. import {getDateQueryFilter} from 'sentry/views/starfish/utils/getDateQueryFilter';
  7. type Transaction = {
  8. count: number;
  9. transaction: string;
  10. };
  11. export const useSpanTransactions = (span?: Span, referrer = 'span-transactions') => {
  12. const pageFilters = usePageFilters();
  13. const {startTime, endTime} = getDateFilters(pageFilters);
  14. const dateFilters = getDateQueryFilter(startTime, endTime);
  15. const query = span
  16. ? `
  17. SELECT
  18. transaction,
  19. count() AS count
  20. FROM spans_experimental_starfish
  21. WHERE
  22. group_id = '${span.group_id}'
  23. ${dateFilters}
  24. GROUP BY transaction
  25. ORDER BY -power(10, floor(log10(count()))), -quantile(0.75)(exclusive_time)
  26. LIMIT 5
  27. `
  28. : '';
  29. const {isLoading, error, data} = useQuery<Transaction[]>({
  30. queryKey: ['span-transactions', span?.group_id],
  31. queryFn: () =>
  32. fetch(`${HOST}/?query=${query}&referrer=${referrer}`).then(res => res.json()),
  33. retry: false,
  34. initialData: [],
  35. enabled: Boolean(span),
  36. });
  37. return {isLoading, error, data};
  38. };