useSpanTransactionMetrics.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import keyBy from 'lodash/keyBy';
  2. import {useQuery} from 'sentry/utils/queryClient';
  3. import usePageFilters from 'sentry/utils/usePageFilters';
  4. import type {Span} from 'sentry/views/starfish/queries/types';
  5. import {HOST} from 'sentry/views/starfish/utils/constants';
  6. import {getDateFilters} from 'sentry/views/starfish/utils/dates';
  7. import {getDateQueryFilter} from 'sentry/views/starfish/utils/getDateQueryFilter';
  8. const INTERVAL = 12;
  9. export type SpanTransactionMetrics = {
  10. p50: number;
  11. p95: number;
  12. spm: number;
  13. 'sum(span.self_time)': number;
  14. total_time: number;
  15. transaction: string;
  16. };
  17. export const useSpanTransactionMetrics = (
  18. span?: Pick<Span, 'group_id'>,
  19. transactions?: string[],
  20. referrer = 'span-transaction-metrics'
  21. ) => {
  22. const pageFilters = usePageFilters();
  23. const {startTime, endTime} = getDateFilters(pageFilters);
  24. const dateFilters = getDateQueryFilter(startTime, endTime);
  25. const query =
  26. span && transactions && transactions.length > 0
  27. ? `
  28. SELECT
  29. transaction,
  30. quantile(0.5)(exclusive_time) as p50,
  31. quantile(0.5)(exclusive_time) as p95,
  32. sum(exclusive_time) as "sum(span.self_time)",
  33. sum(exclusive_time) as total_time,
  34. divide(count(), multiply(${INTERVAL}, 60)) as spm
  35. FROM spans_experimental_starfish
  36. WHERE group_id = '${span.group_id}'
  37. ${dateFilters}
  38. AND transaction IN ('${transactions.join("','")}')
  39. GROUP BY transaction
  40. `
  41. : '';
  42. const {isLoading, error, data} = useQuery<SpanTransactionMetrics[]>({
  43. queryKey: [
  44. 'span-transactions-metrics',
  45. span?.group_id,
  46. transactions?.join(',') || '',
  47. dateFilters,
  48. ],
  49. queryFn: () =>
  50. fetch(`${HOST}/?query=${query}&referrer=${referrer}`).then(res => res.json()),
  51. retry: false,
  52. initialData: [],
  53. enabled: Boolean(query),
  54. });
  55. const parsedData = keyBy(data, 'transaction');
  56. return {isLoading, error, data: parsedData};
  57. };