groupCheckIns.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import {Fragment} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import moment from 'moment-timezone';
  5. import Duration from 'sentry/components/duration';
  6. import GridEditable, {type GridColumnOrder} from 'sentry/components/gridEditable';
  7. import LoadingError from 'sentry/components/loadingError';
  8. import LoadingIndicator from 'sentry/components/loadingIndicator';
  9. import {Tooltip} from 'sentry/components/tooltip';
  10. import {IconInfo} from 'sentry/icons';
  11. import {t} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import type {User} from 'sentry/types/user';
  14. import {defined} from 'sentry/utils';
  15. import {FIELD_FORMATTERS} from 'sentry/utils/discover/fieldRenderers';
  16. import parseLinkHeader from 'sentry/utils/parseLinkHeader';
  17. import {decodeScalar} from 'sentry/utils/queryString';
  18. import {useLocation} from 'sentry/utils/useLocation';
  19. import useOrganization from 'sentry/utils/useOrganization';
  20. import {useParams} from 'sentry/utils/useParams';
  21. import {useUser} from 'sentry/utils/useUser';
  22. import {EventListTable} from 'sentry/views/issueDetails/streamline/eventListTable';
  23. import {useCronIssueAlertId} from 'sentry/views/issueDetails/streamline/issueCronCheckTimeline';
  24. import {useGroup} from 'sentry/views/issueDetails/useGroup';
  25. import {type CheckIn, CheckInStatus} from 'sentry/views/monitors/types';
  26. import {statusToText, tickStyle} from 'sentry/views/monitors/utils';
  27. import {scheduleAsText} from 'sentry/views/monitors/utils/scheduleAsText';
  28. import {useMonitorCheckIns} from 'sentry/views/monitors/utils/useMonitorCheckIns';
  29. export default function GroupCheckIns() {
  30. const organization = useOrganization();
  31. const {groupId} = useParams<{groupId: string}>();
  32. const location = useLocation();
  33. const user = useUser();
  34. const cronAlertId = useCronIssueAlertId({groupId});
  35. const {
  36. data: group,
  37. isPending: isGroupPending,
  38. isError: isGroupError,
  39. refetch: refetchGroup,
  40. } = useGroup({groupId});
  41. const canFetchMonitorCheckIns =
  42. Boolean(organization.slug) && Boolean(group?.project.slug) && Boolean(cronAlertId);
  43. const {cursor, ...locationQuery} = location.query;
  44. const {
  45. data: cronData = [],
  46. isPending: isDataPending,
  47. getResponseHeader,
  48. } = useMonitorCheckIns(
  49. {
  50. orgSlug: organization.slug,
  51. projectSlug: group?.project.slug ?? '',
  52. monitorIdOrSlug: cronAlertId ?? '',
  53. limit: 50,
  54. cursor: decodeScalar(cursor),
  55. queryParams: locationQuery,
  56. },
  57. {enabled: canFetchMonitorCheckIns}
  58. );
  59. if (isGroupError) {
  60. return <LoadingError onRetry={refetchGroup} />;
  61. }
  62. if (isGroupPending) {
  63. return <LoadingIndicator />;
  64. }
  65. const links = parseLinkHeader(getResponseHeader?.('Link') ?? '');
  66. const previousDisabled = links?.previous?.results === false;
  67. const nextDisabled = links?.next?.results === false;
  68. const pageCount = cronData.length;
  69. return (
  70. <EventListTable
  71. title={t('All Check-Ins')}
  72. pagination={{
  73. tableUnits: t('check-ins'),
  74. links,
  75. pageCount,
  76. nextDisabled,
  77. previousDisabled,
  78. }}
  79. >
  80. <GridEditable
  81. isLoading={isDataPending}
  82. emptyMessage={t('No matching check-ins found')}
  83. data={cronData}
  84. columnOrder={[
  85. {key: 'dateCreated', width: 225, name: t('Timestamp')},
  86. {key: 'status', width: 100, name: t('Status')},
  87. {key: 'duration', width: 130, name: t('Duration')},
  88. {key: 'environment', width: 120, name: t('Environment')},
  89. {key: 'monitorConfig', width: 145, name: t('Monitor Config')},
  90. {key: 'id', width: 100, name: t('ID')},
  91. ]}
  92. columnSortBy={[]}
  93. grid={{
  94. renderHeadCell: (column: GridColumnOrder) => <CheckInHeader column={column} />,
  95. renderBodyCell: (column, dataRow) => (
  96. <CheckInCell column={column} dataRow={dataRow} userOptions={user.options} />
  97. ),
  98. }}
  99. />
  100. </EventListTable>
  101. );
  102. }
  103. function CheckInHeader({column}: {column: GridColumnOrder}) {
  104. if (column.key === 'monitorConfig') {
  105. return (
  106. <Cell>
  107. {t('Monitor Config')}
  108. <Tooltip
  109. title={t(
  110. 'These are snapshots of the monitor configuration at the time of the check-in. They may differ from the current monitor config.'
  111. )}
  112. style={{lineHeight: 0}}
  113. >
  114. <IconInfo size="xs" />
  115. </Tooltip>
  116. </Cell>
  117. );
  118. }
  119. return <Cell>{column.name}</Cell>;
  120. }
  121. function CheckInCell({
  122. dataRow,
  123. column,
  124. userOptions,
  125. }: {
  126. column: GridColumnOrder<string>;
  127. dataRow: CheckIn;
  128. userOptions: User['options'];
  129. }) {
  130. const theme = useTheme();
  131. const columnKey = column.key as keyof CheckIn;
  132. if (!dataRow[columnKey]) {
  133. return <Cell />;
  134. }
  135. switch (columnKey) {
  136. case 'dateCreated': {
  137. const format = userOptions.clock24Hours
  138. ? 'MMM D, YYYY HH:mm:ss z'
  139. : 'MMM D, YYYY h:mm:ss A z';
  140. return (
  141. <HoverableCell>
  142. <Tooltip
  143. maxWidth={300}
  144. isHoverable
  145. title={
  146. <LabelledTooltip>
  147. {dataRow.expectedTime && (
  148. <Fragment>
  149. <dt>{t('Expected at')}</dt>
  150. <dd>
  151. {moment
  152. .tz(dataRow.expectedTime, userOptions?.timezone ?? '')
  153. .format(format)}
  154. </dd>
  155. </Fragment>
  156. )}
  157. <dt>{t('Received at')}</dt>
  158. <dd>
  159. {moment
  160. .tz(dataRow[columnKey], userOptions?.timezone ?? '')
  161. .format(format)}
  162. </dd>
  163. </LabelledTooltip>
  164. }
  165. >
  166. {FIELD_FORMATTERS.date.renderFunc('dateCreated', dataRow)}
  167. </Tooltip>
  168. </HoverableCell>
  169. );
  170. }
  171. case 'duration': {
  172. const cellData = dataRow[columnKey];
  173. if (typeof cellData === 'number') {
  174. return (
  175. <Cell>
  176. <Duration seconds={cellData / 1000} abbreviation exact />
  177. </Cell>
  178. );
  179. }
  180. return <Cell>{cellData}</Cell>;
  181. }
  182. case 'status': {
  183. const status = dataRow[columnKey];
  184. let checkResult = <Cell>{status}</Cell>;
  185. if (Object.values(CheckInStatus).includes(status)) {
  186. const colorKey = tickStyle[status].labelColor ?? 'textColor';
  187. checkResult = (
  188. <Cell style={{color: theme[colorKey] as string}}>{statusToText[status]}</Cell>
  189. );
  190. }
  191. return checkResult;
  192. }
  193. case 'monitorConfig': {
  194. const config = dataRow[columnKey];
  195. return (
  196. <HoverableCell>
  197. <Tooltip
  198. maxWidth={400}
  199. isHoverable
  200. title={
  201. <LabelledTooltip>
  202. <dt>{t('Schedule')}</dt>
  203. <dd>{scheduleAsText(config)}</dd>
  204. {defined(config.schedule_type) && (
  205. <Fragment>
  206. <dt>{t('Schedule Type')}</dt>
  207. <dd>{config.schedule_type}</dd>
  208. </Fragment>
  209. )}
  210. {defined(config.checkin_margin) && (
  211. <Fragment>
  212. <dt>{t('Check-in Margin')}</dt>
  213. <dd>{config.checkin_margin}</dd>
  214. </Fragment>
  215. )}
  216. {defined(config.max_runtime) && (
  217. <Fragment>
  218. <dt>{t('Max Runtime')}</dt>
  219. <dd>{config.max_runtime}</dd>
  220. </Fragment>
  221. )}
  222. {defined(config.timezone) && (
  223. <Fragment>
  224. <dt>{t('Timezone')}</dt>
  225. <dd>{config.timezone}</dd>
  226. </Fragment>
  227. )}
  228. {defined(config.failure_issue_threshold) && (
  229. <Fragment>
  230. <dt>{t('Failure Threshold')}</dt>
  231. <dd>{config.failure_issue_threshold}</dd>
  232. </Fragment>
  233. )}
  234. {defined(config.recovery_threshold) && (
  235. <Fragment>
  236. <dt>{t('Recovery Threshold')}</dt>
  237. <dd>{config.recovery_threshold}</dd>
  238. </Fragment>
  239. )}
  240. </LabelledTooltip>
  241. }
  242. >
  243. {t('View Config')}
  244. </Tooltip>
  245. </HoverableCell>
  246. );
  247. }
  248. // We don't query groups for this table yet
  249. case 'groups':
  250. return <Cell />;
  251. default:
  252. return <Cell>{dataRow[columnKey]}</Cell>;
  253. }
  254. }
  255. const Cell = styled('div')`
  256. display: flex;
  257. align-items: center;
  258. text-align: left;
  259. gap: ${space(1)};
  260. `;
  261. const HoverableCell = styled(Cell)`
  262. color: ${p => p.theme.subText};
  263. text-decoration: underline;
  264. text-decoration-style: dotted;
  265. `;
  266. const LabelledTooltip = styled('div')`
  267. display: grid;
  268. grid-template-columns: max-content 1fr;
  269. gap: ${space(0.5)} ${space(1)};
  270. text-align: left;
  271. margin: 0;
  272. `;