index.tsx 7.1 KB

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