index.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import {useRef} from 'react';
  2. import styled from '@emotion/styled';
  3. import {deleteMonitorEnvironment} from 'sentry/actionCreators/monitors';
  4. import Panel from 'sentry/components/panels/panel';
  5. import {Sticky} from 'sentry/components/sticky';
  6. import {space} from 'sentry/styles/space';
  7. import {setApiQueryData, useApiQuery, useQueryClient} from 'sentry/utils/queryClient';
  8. import useApi from 'sentry/utils/useApi';
  9. import {useDimensions} from 'sentry/utils/useDimensions';
  10. import useOrganization from 'sentry/utils/useOrganization';
  11. import useRouter from 'sentry/utils/useRouter';
  12. import {
  13. GridLineOverlay,
  14. GridLineTimeLabels,
  15. } from 'sentry/views/monitors/components/overviewTimeline/gridLines';
  16. import {makeMonitorListQueryKey} from 'sentry/views/monitors/utils';
  17. import {Monitor} from '../../types';
  18. import {ResolutionSelector} from './resolutionSelector';
  19. import {TimelineTableRow} from './timelineTableRow';
  20. import {MonitorBucketData, TimeWindow} from './types';
  21. import {getConfigFromTimeRange, getStartFromTimeWindow} from './utils';
  22. interface Props {
  23. monitorList: Monitor[];
  24. }
  25. export function OverviewTimeline({monitorList}: Props) {
  26. const {location} = useRouter();
  27. const organization = useOrganization();
  28. const api = useApi();
  29. const queryClient = useQueryClient();
  30. const router = useRouter();
  31. const timeWindow: TimeWindow = location.query?.timeWindow ?? '24h';
  32. const nowRef = useRef<Date>(new Date());
  33. const start = getStartFromTimeWindow(nowRef.current, timeWindow);
  34. const elementRef = useRef<HTMLDivElement>(null);
  35. const {width: timelineWidth} = useDimensions<HTMLDivElement>({elementRef});
  36. const timeWindowConfig = getConfigFromTimeRange(start, nowRef.current, timelineWidth);
  37. const rollup = Math.floor((timeWindowConfig.elapsedMinutes * 60) / timelineWidth);
  38. const monitorStatsQueryKey = `/organizations/${organization.slug}/monitors-stats/`;
  39. const {data: monitorStats, isLoading} = useApiQuery<Record<string, MonitorBucketData>>(
  40. [
  41. monitorStatsQueryKey,
  42. {
  43. query: {
  44. until: Math.floor(nowRef.current.getTime() / 1000),
  45. since: Math.floor(start.getTime() / 1000),
  46. monitor: monitorList.map(m => m.slug),
  47. resolution: `${rollup}s`,
  48. ...location.query,
  49. },
  50. },
  51. ],
  52. {
  53. staleTime: 0,
  54. enabled: timelineWidth > 0,
  55. }
  56. );
  57. const handleDeleteEnvironment = async (monitor: Monitor, env: string) => {
  58. const success = await deleteMonitorEnvironment(
  59. api,
  60. organization.slug,
  61. monitor.slug,
  62. env
  63. );
  64. if (!success) {
  65. return;
  66. }
  67. const queryKey = makeMonitorListQueryKey(organization, router.location);
  68. setApiQueryData(queryClient, queryKey, (oldMonitorList: Monitor[]) => {
  69. const oldMonitorIdx = oldMonitorList.findIndex(m => m.slug === monitor.slug);
  70. if (oldMonitorIdx < 0) {
  71. return oldMonitorList;
  72. }
  73. const oldMonitor = oldMonitorList[oldMonitorIdx];
  74. const newEnvList = oldMonitor.environments.filter(e => e.name !== env);
  75. const newMonitor = {
  76. ...oldMonitor,
  77. environments: newEnvList,
  78. };
  79. return [
  80. ...oldMonitorList.slice(0, oldMonitorIdx),
  81. newMonitor,
  82. ...oldMonitorList.slice(oldMonitorIdx + 1),
  83. ];
  84. });
  85. };
  86. return (
  87. <MonitorListPanel>
  88. <TimelineWidthTracker ref={elementRef} />
  89. <StickyResolutionSelector>
  90. <ResolutionSelector />
  91. </StickyResolutionSelector>
  92. <StickyGridLineTimeLabels>
  93. <BorderlessGridLineTimeLabels
  94. timeWindowConfig={timeWindowConfig}
  95. start={start}
  96. end={nowRef.current}
  97. width={timelineWidth}
  98. />
  99. </StickyGridLineTimeLabels>
  100. <GridLineOverlay
  101. stickyCursor
  102. showCursor={!isLoading}
  103. timeWindowConfig={timeWindowConfig}
  104. start={start}
  105. end={nowRef.current}
  106. width={timelineWidth}
  107. />
  108. {monitorList.map(monitor => (
  109. <TimelineTableRow
  110. key={monitor.id}
  111. monitor={monitor}
  112. timeWindowConfig={timeWindowConfig}
  113. start={start}
  114. bucketedData={monitorStats?.[monitor.slug]}
  115. end={nowRef.current}
  116. width={timelineWidth}
  117. onDeleteEnvironment={env => handleDeleteEnvironment(monitor, env)}
  118. />
  119. ))}
  120. </MonitorListPanel>
  121. );
  122. }
  123. const MonitorListPanel = styled(Panel)`
  124. display: grid;
  125. grid-template-columns: 350px 135px 1fr;
  126. `;
  127. const StickyResolutionSelector = styled(Sticky)`
  128. z-index: 1;
  129. padding: ${space(1.5)} ${space(2)};
  130. grid-column: 1/3;
  131. background: ${p => p.theme.background};
  132. border-top-left-radius: ${p => p.theme.panelBorderRadius};
  133. box-shadow: 0 1px ${p => p.theme.translucentBorder};
  134. &[data-stuck] {
  135. border-radius: 0;
  136. border-left: 1px solid ${p => p.theme.border};
  137. margin-left: -1px;
  138. }
  139. `;
  140. // We don't need border here because it is already accomplished via box-shadow below
  141. const BorderlessGridLineTimeLabels = styled(GridLineTimeLabels)`
  142. border: none;
  143. `;
  144. const StickyGridLineTimeLabels = styled(Sticky)`
  145. > * {
  146. height: 100%;
  147. }
  148. z-index: 1;
  149. background: ${p => p.theme.background};
  150. border-top-right-radius: ${p => p.theme.panelBorderRadius};
  151. box-shadow: 0 1px ${p => p.theme.translucentBorder};
  152. &[data-stuck] {
  153. border-radius: 0;
  154. border-right: 1px solid ${p => p.theme.border};
  155. margin-right: -1px;
  156. }
  157. `;
  158. const TimelineWidthTracker = styled('div')`
  159. position: absolute;
  160. width: 100%;
  161. grid-row: 1;
  162. grid-column: 3;
  163. `;