index.tsx 7.0 KB

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