useInteractionBreakdownTimeseriesQuery.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import {getInterval} from 'sentry/components/charts/utils';
  2. import type {Series} from 'sentry/types/echarts';
  3. import type {MetaType} from 'sentry/utils/discover/eventView';
  4. import EventView from 'sentry/utils/discover/eventView';
  5. import type {DiscoverQueryProps} from 'sentry/utils/discover/genericDiscoverQuery';
  6. import {useGenericDiscoverQuery} from 'sentry/utils/discover/genericDiscoverQuery';
  7. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  8. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  9. import {useLocation} from 'sentry/utils/useLocation';
  10. import useOrganization from 'sentry/utils/useOrganization';
  11. import usePageFilters from 'sentry/utils/usePageFilters';
  12. type Props = {
  13. element: string;
  14. operation: string;
  15. page: string;
  16. };
  17. export const useInteractionBreakdownTimeseriesQuery = ({
  18. operation,
  19. element,
  20. page,
  21. }: Props): {data: Series[]; isLoading: boolean} => {
  22. const pageFilters = usePageFilters();
  23. const location = useLocation();
  24. const organization = useOrganization();
  25. const search = new MutableSearch(
  26. `transaction.op:${operation} transaction:${page} interactionElement:${element}`
  27. );
  28. const projectTimeSeriesEventView = EventView.fromNewQueryWithPageFilters(
  29. {
  30. yAxis: [`p75(transaction.duration)`],
  31. name: 'Interaction Duration',
  32. query: search.formatString(),
  33. version: 2,
  34. fields: [],
  35. interval: getInterval(pageFilters.selection.datetime, 'low'),
  36. dataset: DiscoverDatasets.DISCOVER,
  37. },
  38. pageFilters.selection
  39. );
  40. const result = useGenericDiscoverQuery<
  41. {
  42. data: any[];
  43. meta: MetaType;
  44. },
  45. DiscoverQueryProps
  46. >({
  47. route: 'events-stats',
  48. eventView: projectTimeSeriesEventView,
  49. location,
  50. orgSlug: organization.slug,
  51. getRequestPayload: () => ({
  52. ...projectTimeSeriesEventView.getEventsAPIPayload(location),
  53. yAxis: projectTimeSeriesEventView.yAxis,
  54. topEvents: projectTimeSeriesEventView.topEvents,
  55. excludeOther: 0,
  56. partial: 1,
  57. orderby: undefined,
  58. interval: projectTimeSeriesEventView.interval,
  59. }),
  60. options: {
  61. enabled: pageFilters.isReady,
  62. refetchOnWindowFocus: false,
  63. },
  64. });
  65. const seriesData: Series = {
  66. seriesName: 'p75(duration)',
  67. data: [],
  68. };
  69. const transformedData = result?.data?.data.map(data => ({
  70. name: data[0] as string,
  71. value: data[1][0].count,
  72. }));
  73. seriesData.data = transformedData ?? [];
  74. return {data: [seriesData], isLoading: result.isLoading};
  75. };