useSpanMetrics.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. const INTERVAL = 12;
  8. export type SpanMetrics = {
  9. count: number;
  10. first_seen: string;
  11. last_seen: string;
  12. p50: number;
  13. p95: number;
  14. spm: number;
  15. total_time: number;
  16. };
  17. export const useSpanMetrics = (
  18. span?: Pick<Span, 'group_id'>,
  19. queryFilters: {transactionName?: string} = {},
  20. referrer = 'span-metrics'
  21. ) => {
  22. const pageFilters = usePageFilters();
  23. const {startTime, endTime} = getDateFilters(pageFilters);
  24. const dateFilters = getDateQueryFilter(startTime, endTime);
  25. const filters: string[] = [];
  26. if (queryFilters.transactionName) {
  27. filters.push(`transaction = ${queryFilters.transactionName}`);
  28. }
  29. const query = span
  30. ? `
  31. SELECT
  32. count() as count,
  33. min(timestamp) as first_seen,
  34. max(timestamp) as last_seen,
  35. sum(exclusive_time) as total_time,
  36. quantile(0.5)(exclusive_time) as p50,
  37. quantile(0.5)(exclusive_time) as p95,
  38. divide(count, multiply(${INTERVAL}, 60)) as spm
  39. FROM spans_experimental_starfish
  40. WHERE group_id = '${span.group_id}'
  41. ${dateFilters}
  42. ${filters.join(' AND ')}`
  43. : '';
  44. const {isLoading, error, data} = useQuery<SpanMetrics[]>({
  45. queryKey: ['span-metrics', span?.group_id, dateFilters],
  46. queryFn: () =>
  47. fetch(`${HOST}/?query=${query}&referrer=${referrer}`).then(res => res.json()),
  48. retry: false,
  49. initialData: [],
  50. enabled: Boolean(span),
  51. });
  52. return {isLoading, error, data: data[0] ?? {}};
  53. };