index.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 {getConfigFromTimeRange, getStartFromTimeWindow} 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 = useRef<HTMLDivElement>(null);
  29. const {width: timelineWidth} = useDimensions<HTMLDivElement>({elementRef});
  30. const timeWindowConfig = getConfigFromTimeRange(start, nowRef.current, timelineWidth);
  31. const rollup = Math.floor((timeWindowConfig.elapsedMinutes * 60) / timelineWidth);
  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. <BorderlessGridLineTimeLabels
  59. timeWindowConfig={timeWindowConfig}
  60. start={start}
  61. end={nowRef.current}
  62. width={timelineWidth}
  63. />
  64. </StickyGridLineTimeLabels>
  65. <GridLineOverlay
  66. stickyCursor
  67. showCursor={!isLoading}
  68. timeWindowConfig={timeWindowConfig}
  69. start={start}
  70. end={nowRef.current}
  71. width={timelineWidth}
  72. />
  73. {monitorList.map(monitor => (
  74. <TimelineTableRow
  75. key={monitor.id}
  76. monitor={monitor}
  77. timeWindowConfig={timeWindowConfig}
  78. start={start}
  79. bucketedData={monitorStats?.[monitor.slug]}
  80. end={nowRef.current}
  81. width={timelineWidth}
  82. />
  83. ))}
  84. </MonitorListPanel>
  85. );
  86. }
  87. const MonitorListPanel = styled(Panel)`
  88. display: grid;
  89. grid-template-columns: 350px 135px 1fr;
  90. `;
  91. const StickyResolutionSelector = styled(Sticky)`
  92. z-index: 1;
  93. padding: ${space(1.5)} ${space(2)};
  94. grid-column: 1/3;
  95. background: ${p => p.theme.background};
  96. border-top-left-radius: ${p => p.theme.panelBorderRadius};
  97. box-shadow: 0 1px ${p => p.theme.translucentBorder};
  98. &[data-stuck] {
  99. border-radius: 0;
  100. border-left: 1px solid ${p => p.theme.border};
  101. margin-left: -1px;
  102. }
  103. `;
  104. // We don't need border here because it is already accomplished via box-shadow below
  105. const BorderlessGridLineTimeLabels = styled(GridLineTimeLabels)`
  106. border: none;
  107. `;
  108. const StickyGridLineTimeLabels = styled(Sticky)`
  109. > * {
  110. height: 100%;
  111. }
  112. z-index: 1;
  113. background: ${p => p.theme.background};
  114. border-top-right-radius: ${p => p.theme.panelBorderRadius};
  115. box-shadow: 0 1px ${p => p.theme.translucentBorder};
  116. &[data-stuck] {
  117. border-radius: 0;
  118. border-right: 1px solid ${p => p.theme.border};
  119. margin-right: -1px;
  120. }
  121. `;
  122. const TimelineWidthTracker = styled('div')`
  123. position: absolute;
  124. width: 100%;
  125. grid-row: 1;
  126. grid-column: 3;
  127. `;