useEventsStatsQuery.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import type {MultiSeriesEventsStats} from 'sentry/types';
  2. import type EventView from 'sentry/utils/discover/eventView';
  3. import {encodeSort} from 'sentry/utils/discover/eventView';
  4. import type {DiscoverQueryProps} from 'sentry/utils/discover/genericDiscoverQuery';
  5. import {useGenericDiscoverQuery} from 'sentry/utils/discover/genericDiscoverQuery';
  6. import {useLocation} from 'sentry/utils/useLocation';
  7. import useOrganization from 'sentry/utils/useOrganization';
  8. import {
  9. getRetryDelay,
  10. shouldRetryHandler,
  11. } from 'sentry/views/starfish/utils/retryHandlers';
  12. export function useEventsStatsQuery({
  13. eventView,
  14. enabled,
  15. referrer,
  16. initialData,
  17. excludeOther = false,
  18. }: {
  19. eventView: EventView;
  20. enabled?: boolean;
  21. excludeOther?: boolean;
  22. initialData?: MultiSeriesEventsStats;
  23. referrer?: string;
  24. }) {
  25. const location = useLocation();
  26. const organization = useOrganization();
  27. const result = useGenericDiscoverQuery<MultiSeriesEventsStats, DiscoverQueryProps>({
  28. route: 'events-stats',
  29. eventView,
  30. location,
  31. orgSlug: organization.slug,
  32. getRequestPayload: () => ({
  33. ...eventView.getEventsAPIPayload(location),
  34. yAxis: eventView.yAxis,
  35. topEvents: eventView.topEvents,
  36. excludeOther: excludeOther === true ? 1 : 0,
  37. partial: 1,
  38. orderby: eventView.sorts?.[0] ? encodeSort(eventView.sorts?.[0]) : undefined,
  39. interval: eventView.interval,
  40. }),
  41. options: {
  42. enabled,
  43. refetchOnWindowFocus: false,
  44. retry: shouldRetryHandler,
  45. retryDelay: getRetryDelay,
  46. },
  47. referrer,
  48. });
  49. return {...result, data: result.isLoading ? initialData : result.data};
  50. }