useEventsStatsQuery.tsx 1.5 KB

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