useMonitorStats.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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, timelineWidth, rollupConfig} = timeWindowConfig;
  21. // Add the underscan to our selection time
  22. const additionalInterval =
  23. (rollupConfig.timelineUnderscanWidth / rollupConfig.bucketPixels) *
  24. rollupConfig.interval;
  25. // XXX(epurkhiser): We are dropping 1 bucket worth of data on the right side
  26. // to account for the fact that this bucket is actually over-scan becauase
  27. // the query on the backend is inclusive.
  28. const until =
  29. Math.floor(end.getTime() / 1000) + additionalInterval - rollupConfig.interval;
  30. const selectionQuery = {
  31. since: Math.floor(start.getTime() / 1000),
  32. until,
  33. resolution: `${rollupConfig.interval}s`,
  34. };
  35. const organization = useOrganization();
  36. const location = useLocation();
  37. const monitorStatsQueryKey = `/organizations/${organization.slug}/monitors-stats/`;
  38. return useApiQuery<Record<string, MonitorBucket[]>>(
  39. [
  40. monitorStatsQueryKey,
  41. {
  42. query: {
  43. monitor: monitors,
  44. project: location.query.project,
  45. environment: location.query.environment,
  46. ...selectionQuery,
  47. },
  48. },
  49. ],
  50. {
  51. staleTime: 0,
  52. enabled: timelineWidth > 0,
  53. }
  54. );
  55. }