useSpanMetricSeries.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import keyBy from 'lodash/keyBy';
  2. import moment from 'moment';
  3. import {Series} from 'sentry/types/echarts';
  4. import {useQuery} from 'sentry/utils/queryClient';
  5. import usePageFilters from 'sentry/utils/usePageFilters';
  6. import type {Span} from 'sentry/views/starfish/queries/types';
  7. import {HOST} from 'sentry/views/starfish/utils/constants';
  8. import {getDateFilters} from 'sentry/views/starfish/utils/dates';
  9. import {getDateQueryFilter} from 'sentry/views/starfish/utils/getDateQueryFilter';
  10. import {zeroFillSeries} from 'sentry/views/starfish/utils/zeroFillSeries';
  11. const INTERVAL = 12;
  12. export type Metric = {
  13. count: number;
  14. interval: string;
  15. p50: number;
  16. p95: number;
  17. };
  18. export const useSpanMetricSeries = (span?: Span, referrer = 'span-metrics-series') => {
  19. const pageFilters = usePageFilters();
  20. const {startTime, endTime} = getDateFilters(pageFilters);
  21. const dateFilters = getDateQueryFilter(startTime, endTime);
  22. const query = span
  23. ? `
  24. SELECT
  25. toStartOfInterval(start_timestamp, INTERVAL ${INTERVAL} HOUR) as interval,
  26. count() as count,
  27. divide(count, multiply(${INTERVAL}, 60)) as spm,
  28. quantile(0.95)(exclusive_time) as p95,
  29. quantile(0.50)(exclusive_time) as p50,
  30. countIf(greaterOrEquals(status, 400) AND lessOrEquals(status, 599)) as "failure_count",
  31. "failure_count" / "count" as "failure_rate"
  32. FROM spans_experimental_starfish
  33. WHERE group_id = '${span.group_id}'
  34. ${dateFilters}
  35. GROUP BY interval
  36. ORDER BY interval
  37. `
  38. : '';
  39. const {isLoading, error, data} = useQuery<Metric[]>({
  40. queryKey: ['span-metrics-series', span?.group_id],
  41. queryFn: () =>
  42. fetch(`${HOST}/?query=${query}&referrer=${referrer}`).then(res => res.json()),
  43. retry: false,
  44. initialData: [],
  45. enabled: Boolean(span),
  46. });
  47. const parsedData = keyBy(
  48. ['spm', 'p50', 'p95', 'failure_rate'].map(seriesName => {
  49. const series: Series = {
  50. seriesName,
  51. data: data.map(datum => ({value: datum[seriesName], name: datum.interval})),
  52. };
  53. return zeroFillSeries(series, moment.duration(INTERVAL, 'hours'));
  54. }),
  55. 'seriesName'
  56. );
  57. return {isLoading, error, data: parsedData};
  58. };