useInteractionBreakdownTimeseriesQuery.tsx 2.4 KB

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