useSpanTransactions.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import {useQuery} from 'sentry/utils/queryClient';
  2. import usePageFilters from 'sentry/utils/usePageFilters';
  3. import type {IndexedSpan} 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 = (
  12. span?: Pick<IndexedSpan, 'group'>,
  13. referrer = 'span-transactions'
  14. ) => {
  15. const pageFilters = usePageFilters();
  16. const {startTime, endTime} = getDateFilters(pageFilters);
  17. const dateFilters = getDateQueryFilter(startTime, endTime);
  18. const query = span
  19. ? `
  20. SELECT
  21. transaction,
  22. count() AS count
  23. FROM spans_experimental_starfish
  24. WHERE
  25. group_id = '${span.group}'
  26. ${dateFilters}
  27. GROUP BY transaction
  28. ORDER BY -power(10, floor(log10(count()))), -quantile(0.75)(exclusive_time)
  29. LIMIT 5
  30. `
  31. : '';
  32. const {isLoading, error, data} = useQuery<Transaction[]>({
  33. queryKey: ['span-transactions', span?.group],
  34. queryFn: () =>
  35. fetch(`${HOST}/?query=${query}&referrer=${referrer}`).then(res => res.json()),
  36. retry: false,
  37. initialData: [],
  38. enabled: Boolean(span),
  39. });
  40. return {isLoading, error, data};
  41. };