index.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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(api, organization.slug, monitor, env);
  64. if (!success) {
  65. return;
  66. }
  67. const queryKey = makeMonitorListQueryKey(organization, location.query);
  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 updatedMonitor = {
  76. ...oldMonitor,
  77. environments: newEnvList,
  78. };
  79. const left = oldMonitorList.slice(0, oldMonitorIdx);
  80. const right = oldMonitorList.slice(oldMonitorIdx + 1);
  81. if (newEnvList.length === 0) {
  82. return [...left, ...right];
  83. }
  84. return [...left, updatedMonitor, ...right];
  85. });
  86. };
  87. const handleToggleMuteEnvironment = async (
  88. monitor: Monitor,
  89. env: string,
  90. isMuted: boolean
  91. ) => {
  92. const resp = await setEnvironmentIsMuted(
  93. api,
  94. organization.slug,
  95. monitor,
  96. env,
  97. isMuted
  98. );
  99. if (resp === null) {
  100. return;
  101. }
  102. const queryKey = makeMonitorListQueryKey(organization, location.query);
  103. setApiQueryData(queryClient, queryKey, (oldMonitorList: Monitor[]) => {
  104. const monitorIdx = oldMonitorList.findIndex(m => m.slug === monitor.slug);
  105. // TODO(davidenwang): in future only change the specifically modified environment for optimistic updates
  106. oldMonitorList[monitorIdx] = resp;
  107. return oldMonitorList;
  108. });
  109. };
  110. const handleToggleStatus = async (monitor: Monitor) => {
  111. const status = monitor.status === 'active' ? 'disabled' : 'active';
  112. const resp = await updateMonitor(api, organization.slug, monitor, {status});
  113. if (resp === null) {
  114. return;
  115. }
  116. const queryKey = makeMonitorListQueryKey(organization, location.query);
  117. setApiQueryData(queryClient, queryKey, (oldMonitorList: Monitor[]) => {
  118. const monitorIdx = oldMonitorList.findIndex(m => m.slug === monitor.slug);
  119. oldMonitorList[monitorIdx] = {...oldMonitorList[monitorIdx], status: resp.status};
  120. return oldMonitorList;
  121. });
  122. };
  123. return (
  124. <MonitorListPanel>
  125. <TimelineWidthTracker ref={elementRef} />
  126. <Header>
  127. <HeaderControls>
  128. <ResolutionSelector />
  129. <SortSelector />
  130. </HeaderControls>
  131. <GridLineTimeLabels
  132. timeWindowConfig={timeWindowConfig}
  133. start={start}
  134. end={nowRef.current}
  135. width={timelineWidth}
  136. />
  137. </Header>
  138. <GridLineOverlay
  139. stickyCursor
  140. showCursor={!isLoading}
  141. timeWindowConfig={timeWindowConfig}
  142. start={start}
  143. end={nowRef.current}
  144. width={timelineWidth}
  145. />
  146. {monitorList.map(monitor => (
  147. <TimelineTableRow
  148. key={monitor.id}
  149. monitor={monitor}
  150. timeWindowConfig={timeWindowConfig}
  151. start={start}
  152. bucketedData={monitorStats?.[monitor.slug]}
  153. end={nowRef.current}
  154. width={timelineWidth}
  155. onDeleteEnvironment={env => handleDeleteEnvironment(monitor, env)}
  156. onToggleMuteEnvironment={(env, isMuted) =>
  157. handleToggleMuteEnvironment(monitor, env, isMuted)
  158. }
  159. onToggleStatus={handleToggleStatus}
  160. />
  161. ))}
  162. </MonitorListPanel>
  163. );
  164. }
  165. const MonitorListPanel = styled(Panel)`
  166. display: grid;
  167. grid-template-columns: 350px 135px 1fr;
  168. `;
  169. const Header = styled(Sticky)`
  170. display: grid;
  171. grid-column: 1/-1;
  172. grid-template-columns: subgrid;
  173. z-index: 1;
  174. background: ${p => p.theme.background};
  175. border-top-left-radius: ${p => p.theme.panelBorderRadius};
  176. border-top-right-radius: ${p => p.theme.panelBorderRadius};
  177. box-shadow: 0 1px ${p => p.theme.translucentBorder};
  178. &[data-stuck] {
  179. border-radius: 0;
  180. border-left: 1px solid ${p => p.theme.border};
  181. border-right: 1px solid ${p => p.theme.border};
  182. margin: 0 -1px;
  183. }
  184. `;
  185. const HeaderControls = styled('div')`
  186. grid-column: 1/3;
  187. display: flex;
  188. gap: ${space(0.5)};
  189. padding: ${space(1.5)} ${space(2)};
  190. `;
  191. const TimelineWidthTracker = styled('div')`
  192. position: absolute;
  193. width: 100%;
  194. grid-row: 1;
  195. grid-column: 3;
  196. `;