index.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import {useRef} from 'react';
  2. import styled from '@emotion/styled';
  3. import Panel from 'sentry/components/panels/panel';
  4. import {Sticky} from 'sentry/components/sticky';
  5. import {space} from 'sentry/styles/space';
  6. import {useApiQuery} from 'sentry/utils/queryClient';
  7. import {useDimensions} from 'sentry/utils/useDimensions';
  8. import useOrganization from 'sentry/utils/useOrganization';
  9. import useRouter from 'sentry/utils/useRouter';
  10. import {
  11. GridLineOverlay,
  12. GridLineTimeLabels,
  13. } from 'sentry/views/monitors/components/overviewTimeline/gridLines';
  14. import {Monitor} from '../../types';
  15. import {ResolutionSelector} from './resolutionSelector';
  16. import {TimelineTableRow} from './timelineTableRow';
  17. import {MonitorBucketData, TimeWindow} from './types';
  18. import {getStartFromTimeWindow, timeWindowConfig} from './utils';
  19. interface Props {
  20. monitorList: Monitor[];
  21. }
  22. export function OverviewTimeline({monitorList}: Props) {
  23. const {location} = useRouter();
  24. const organization = useOrganization();
  25. const timeWindow: TimeWindow = location.query?.timeWindow ?? '24h';
  26. const nowRef = useRef<Date>(new Date());
  27. const start = getStartFromTimeWindow(nowRef.current, timeWindow);
  28. const {elementRef, width: timelineWidth} = useDimensions<HTMLDivElement>();
  29. const rollup = Math.floor(
  30. (timeWindowConfig[timeWindow].elapsedMinutes * 60) / timelineWidth
  31. );
  32. const monitorStatsQueryKey = `/organizations/${organization.slug}/monitors-stats/`;
  33. const {data: monitorStats, isLoading} = useApiQuery<Record<string, MonitorBucketData>>(
  34. [
  35. monitorStatsQueryKey,
  36. {
  37. query: {
  38. until: Math.floor(nowRef.current.getTime() / 1000),
  39. since: Math.floor(start.getTime() / 1000),
  40. monitor: monitorList.map(m => m.slug),
  41. resolution: `${rollup}s`,
  42. ...location.query,
  43. },
  44. },
  45. ],
  46. {
  47. staleTime: 0,
  48. enabled: timelineWidth > 0,
  49. }
  50. );
  51. return (
  52. <MonitorListPanel>
  53. <TimelineWidthTracker ref={elementRef} />
  54. <StickyResolutionSelector>
  55. <ResolutionSelector />
  56. </StickyResolutionSelector>
  57. <StickyGridLineTimeLabels>
  58. <GridLineTimeLabels
  59. timeWindow={timeWindow}
  60. end={nowRef.current}
  61. width={timelineWidth}
  62. />
  63. </StickyGridLineTimeLabels>
  64. <GridLineOverlay
  65. stickyCursor
  66. showCursor={!isLoading}
  67. timeWindow={timeWindow}
  68. end={nowRef.current}
  69. width={timelineWidth}
  70. />
  71. {monitorList.map(monitor => (
  72. <TimelineTableRow
  73. key={monitor.id}
  74. monitor={monitor}
  75. timeWindow={timeWindow}
  76. bucketedData={monitorStats?.[monitor.slug]}
  77. end={nowRef.current}
  78. start={start}
  79. width={timelineWidth}
  80. />
  81. ))}
  82. </MonitorListPanel>
  83. );
  84. }
  85. const MonitorListPanel = styled(Panel)`
  86. display: grid;
  87. grid-template-columns: 350px 135px 1fr;
  88. `;
  89. const StickyResolutionSelector = styled(Sticky)`
  90. z-index: 1;
  91. padding: ${space(1.5)} ${space(2)};
  92. grid-column: 1/3;
  93. background: ${p => p.theme.background};
  94. border-top-left-radius: ${p => p.theme.panelBorderRadius};
  95. box-shadow: 0 1px ${p => p.theme.translucentBorder};
  96. &[data-stuck] {
  97. border-radius: 0;
  98. border-left: 1px solid ${p => p.theme.border};
  99. margin-left: -1px;
  100. }
  101. `;
  102. const StickyGridLineTimeLabels = styled(Sticky)`
  103. > * {
  104. height: 100%;
  105. }
  106. z-index: 1;
  107. background: ${p => p.theme.background};
  108. border-top-right-radius: ${p => p.theme.panelBorderRadius};
  109. box-shadow: 0 1px ${p => p.theme.translucentBorder};
  110. &[data-stuck] {
  111. border-radius: 0;
  112. border-right: 1px solid ${p => p.theme.border};
  113. margin-right: -1px;
  114. }
  115. `;
  116. const TimelineWidthTracker = styled('div')`
  117. position: absolute;
  118. width: 100%;
  119. grid-row: 1;
  120. grid-column: 3;
  121. `;