useMonitorStats.tsx 1.4 KB

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