index.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 type {Monitor} from 'sentry/views/monitors/types';
  22. import {makeMonitorListQueryKey} from 'sentry/views/monitors/utils';
  23. import {useMonitorTimes} from 'sentry/views/monitors/utils/useMonitorDates';
  24. import {TimelineTableRow} from './timelineTableRow';
  25. import type {MonitorBucketData} from './types';
  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 elementRef = useRef<HTMLDivElement>(null);
  36. const {width: timelineWidth} = useDimensions<HTMLDivElement>({elementRef});
  37. const {selectionQuery, timeWindowConfig} = useMonitorTimes({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. monitor: monitorList.map(m => m.id),
  45. ...selectionQuery,
  46. ...location.query,
  47. },
  48. },
  49. ],
  50. {
  51. staleTime: 0,
  52. enabled: timelineWidth > 0,
  53. }
  54. );
  55. const handleDeleteEnvironment = async (monitor: Monitor, env: string) => {
  56. const success = await deleteMonitorEnvironment(api, organization.slug, monitor, env);
  57. if (!success) {
  58. return;
  59. }
  60. const queryKey = makeMonitorListQueryKey(organization, location.query);
  61. setApiQueryData(queryClient, queryKey, (oldMonitorList: Monitor[]) => {
  62. const oldMonitorIdx = oldMonitorList.findIndex(m => m.slug === monitor.slug);
  63. if (oldMonitorIdx < 0) {
  64. return oldMonitorList;
  65. }
  66. const oldMonitor = oldMonitorList[oldMonitorIdx];
  67. const newEnvList = oldMonitor.environments.filter(e => e.name !== env);
  68. const updatedMonitor = {
  69. ...oldMonitor,
  70. environments: newEnvList,
  71. };
  72. const left = oldMonitorList.slice(0, oldMonitorIdx);
  73. const right = oldMonitorList.slice(oldMonitorIdx + 1);
  74. if (newEnvList.length === 0) {
  75. return [...left, ...right];
  76. }
  77. return [...left, updatedMonitor, ...right];
  78. });
  79. };
  80. const handleToggleMuteEnvironment = async (
  81. monitor: Monitor,
  82. env: string,
  83. isMuted: boolean
  84. ) => {
  85. const resp = await setEnvironmentIsMuted(
  86. api,
  87. organization.slug,
  88. monitor,
  89. env,
  90. isMuted
  91. );
  92. if (resp === null) {
  93. return;
  94. }
  95. const queryKey = makeMonitorListQueryKey(organization, location.query);
  96. setApiQueryData(queryClient, queryKey, (oldMonitorList: Monitor[]) => {
  97. const monitorIdx = oldMonitorList.findIndex(m => m.slug === monitor.slug);
  98. // TODO(davidenwang): in future only change the specifically modified environment for optimistic updates
  99. oldMonitorList[monitorIdx] = resp;
  100. return oldMonitorList;
  101. });
  102. };
  103. const handleToggleStatus = async (monitor: Monitor) => {
  104. const status = monitor.status === 'active' ? 'disabled' : 'active';
  105. const resp = await updateMonitor(api, organization.slug, monitor, {status});
  106. if (resp === null) {
  107. return;
  108. }
  109. const queryKey = makeMonitorListQueryKey(organization, location.query);
  110. setApiQueryData(queryClient, queryKey, (oldMonitorList: Monitor[]) => {
  111. const monitorIdx = oldMonitorList.findIndex(m => m.slug === monitor.slug);
  112. oldMonitorList[monitorIdx] = {...oldMonitorList[monitorIdx], status: resp.status};
  113. return oldMonitorList;
  114. });
  115. };
  116. return (
  117. <MonitorListPanel>
  118. <TimelineWidthTracker ref={elementRef} />
  119. <Header>
  120. <HeaderControls>
  121. <SortSelector size="xs" />
  122. </HeaderControls>
  123. <GridLineTimeLabels timeWindowConfig={timeWindowConfig} width={timelineWidth} />
  124. </Header>
  125. <GridLineOverlay
  126. stickyCursor
  127. allowZoom
  128. showCursor={!isLoading}
  129. timeWindowConfig={timeWindowConfig}
  130. width={timelineWidth}
  131. />
  132. {monitorList.map(monitor => (
  133. <TimelineTableRow
  134. key={monitor.id}
  135. monitor={monitor}
  136. timeWindowConfig={timeWindowConfig}
  137. bucketedData={monitorStats?.[monitor.id]}
  138. width={timelineWidth}
  139. onDeleteEnvironment={env => handleDeleteEnvironment(monitor, env)}
  140. onToggleMuteEnvironment={(env, isMuted) =>
  141. handleToggleMuteEnvironment(monitor, env, isMuted)
  142. }
  143. onToggleStatus={handleToggleStatus}
  144. />
  145. ))}
  146. </MonitorListPanel>
  147. );
  148. }
  149. const MonitorListPanel = styled(Panel)`
  150. display: grid;
  151. grid-template-columns: 350px 135px 1fr;
  152. `;
  153. const Header = styled(Sticky)`
  154. display: grid;
  155. grid-column: 1/-1;
  156. grid-template-columns: subgrid;
  157. z-index: 1;
  158. background: ${p => p.theme.background};
  159. border-top-left-radius: ${p => p.theme.panelBorderRadius};
  160. border-top-right-radius: ${p => p.theme.panelBorderRadius};
  161. box-shadow: 0 1px ${p => p.theme.translucentBorder};
  162. &[data-stuck] {
  163. border-radius: 0;
  164. border-left: 1px solid ${p => p.theme.border};
  165. border-right: 1px solid ${p => p.theme.border};
  166. margin: 0 -1px;
  167. }
  168. `;
  169. const HeaderControls = styled('div')`
  170. grid-column: 1/3;
  171. display: flex;
  172. gap: ${space(0.5)};
  173. padding: ${space(1.5)} ${space(2)};
  174. `;
  175. const TimelineWidthTracker = styled('div')`
  176. position: absolute;
  177. width: 100%;
  178. grid-row: 1;
  179. grid-column: 3;
  180. `;