useMonitorStats.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import {useApiQuery} from 'sentry/utils/queryClient';
  2. import useOrganization from 'sentry/utils/useOrganization';
  3. import useRouter from 'sentry/utils/useRouter';
  4. import type {MonitorBucket, TimeWindowConfig} from '../types';
  5. interface Options {
  6. /**
  7. * The list of monitor IDs to fetch stats for
  8. */
  9. monitors: string[];
  10. /**
  11. * The window configuration object
  12. */
  13. timeWindowConfig: TimeWindowConfig;
  14. }
  15. /**
  16. * Fetches Monitor stats
  17. */
  18. export function useMonitorStats({monitors, timeWindowConfig}: Options) {
  19. const {start, end, elapsedMinutes, timelineWidth} = timeWindowConfig;
  20. const rollup = Math.floor((elapsedMinutes * 60) / timelineWidth);
  21. const selectionQuery = {
  22. since: Math.floor(start.getTime() / 1000),
  23. until: Math.floor(end.getTime() / 1000),
  24. resolution: `${rollup}s`,
  25. };
  26. const organization = useOrganization();
  27. const router = useRouter();
  28. const location = router.location;
  29. const monitorStatsQueryKey = `/organizations/${organization.slug}/monitors-stats/`;
  30. return useApiQuery<Record<string, MonitorBucket[]>>(
  31. [
  32. monitorStatsQueryKey,
  33. {
  34. query: {
  35. monitor: monitors,
  36. ...selectionQuery,
  37. ...location.query,
  38. },
  39. },
  40. ],
  41. {
  42. staleTime: 0,
  43. enabled: timelineWidth > 0,
  44. }
  45. );
  46. }