monitorCheckIns.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {LinkButton} from 'sentry/components/button';
  4. import {SectionHeading} from 'sentry/components/charts/styles';
  5. import {DateTime} from 'sentry/components/dateTime';
  6. import Duration from 'sentry/components/duration';
  7. import ProjectBadge from 'sentry/components/idBadge/projectBadge';
  8. import LoadingError from 'sentry/components/loadingError';
  9. import Pagination from 'sentry/components/pagination';
  10. import {PanelTable} from 'sentry/components/panels/panelTable';
  11. import Placeholder from 'sentry/components/placeholder';
  12. import ShortId from 'sentry/components/shortId';
  13. import {
  14. StatusIndicator,
  15. type StatusIndicatorProps,
  16. } from 'sentry/components/statusIndicator';
  17. import Text from 'sentry/components/text';
  18. import {Tooltip} from 'sentry/components/tooltip';
  19. import {IconDownload} from 'sentry/icons';
  20. import {t, tct} from 'sentry/locale';
  21. import {space} from 'sentry/styles/space';
  22. import {defined} from 'sentry/utils';
  23. import {useApiQuery} from 'sentry/utils/queryClient';
  24. import {useLocation} from 'sentry/utils/useLocation';
  25. import useOrganization from 'sentry/utils/useOrganization';
  26. import {useUser} from 'sentry/utils/useUser';
  27. import {QuickContextHovercard} from 'sentry/views/discover/table/quickContext/quickContextHovercard';
  28. import {ContextType} from 'sentry/views/discover/table/quickContext/utils';
  29. import type {CheckIn, Monitor, MonitorEnvironment} from 'sentry/views/monitors/types';
  30. import {CheckInStatus} from 'sentry/views/monitors/types';
  31. import {statusToText} from 'sentry/views/monitors/utils';
  32. type Props = {
  33. monitor: Monitor;
  34. monitorEnvs: MonitorEnvironment[];
  35. orgSlug: string;
  36. };
  37. export const checkStatusToIndicatorStatus: Record<
  38. CheckInStatus,
  39. StatusIndicatorProps['status']
  40. > = {
  41. [CheckInStatus.OK]: 'success',
  42. [CheckInStatus.ERROR]: 'error',
  43. [CheckInStatus.IN_PROGRESS]: 'muted',
  44. [CheckInStatus.MISSED]: 'warning',
  45. [CheckInStatus.TIMEOUT]: 'error',
  46. [CheckInStatus.UNKNOWN]: 'muted',
  47. };
  48. function MonitorCheckIns({monitor, monitorEnvs, orgSlug}: Props) {
  49. const user = useUser();
  50. const location = useLocation();
  51. const organization = useOrganization();
  52. const queryKey = [
  53. `/projects/${orgSlug}/${monitor.project.slug}/monitors/${monitor.slug}/checkins/`,
  54. {
  55. query: {
  56. per_page: '10',
  57. environment: monitorEnvs.map(e => e.name),
  58. expand: 'groups',
  59. ...location.query,
  60. },
  61. },
  62. ] as const;
  63. const {
  64. data: checkInList,
  65. getResponseHeader,
  66. isPending,
  67. isError,
  68. } = useApiQuery<CheckIn[]>(queryKey, {staleTime: 0});
  69. if (isError) {
  70. return <LoadingError />;
  71. }
  72. const generateDownloadUrl = (checkin: CheckIn) =>
  73. `/api/0/organizations/${orgSlug}/monitors/${monitor.slug}/checkins/${checkin.id}/attachment/`;
  74. const emptyCell = <Text>{'\u2014'}</Text>;
  75. // XXX(epurkhiser): Attachmnets are still experimental and may not exist in
  76. // the future. For now hide these if they're not being used.
  77. const hasAttachments = checkInList?.some(checkin => checkin.attachmentId !== null);
  78. const hasMultiEnv = monitorEnvs.length > 1;
  79. const headers = [
  80. t('Status'),
  81. t('Started'),
  82. t('Duration'),
  83. t('Issues'),
  84. ...(hasAttachments ? [t('Attachment')] : []),
  85. ...(hasMultiEnv ? [t('Environment')] : []),
  86. t('Expected At'),
  87. ];
  88. const customTimezone =
  89. monitor.config.timezone && monitor.config.timezone !== user.options.timezone;
  90. return (
  91. <Fragment>
  92. <SectionHeading>{t('Recent Check-Ins')}</SectionHeading>
  93. <PanelTable headers={headers}>
  94. {isPending
  95. ? [...new Array(headers.length)].map((_, i) => (
  96. <RowPlaceholder key={i}>
  97. <Placeholder height="2rem" />
  98. </RowPlaceholder>
  99. ))
  100. : checkInList.map(checkIn => (
  101. <Fragment key={checkIn.id}>
  102. <Status>
  103. <StatusIndicator
  104. status={checkStatusToIndicatorStatus[checkIn.status]}
  105. tooltipTitle={tct('Check-in Status: [status]', {
  106. status: statusToText[checkIn.status],
  107. })}
  108. />
  109. <Text>{statusToText[checkIn.status]}</Text>
  110. </Status>
  111. {checkIn.status !== CheckInStatus.MISSED ? (
  112. <div>
  113. <Tooltip
  114. disabled={!customTimezone}
  115. title={
  116. <DateTime
  117. date={checkIn.dateCreated}
  118. forcedTimezone={monitor.config.timezone ?? 'UTC'}
  119. timeZone
  120. seconds
  121. />
  122. }
  123. >
  124. <DateTime date={checkIn.dateCreated} timeZone seconds />
  125. </Tooltip>
  126. </div>
  127. ) : (
  128. emptyCell
  129. )}
  130. {defined(checkIn.duration) ? (
  131. <div>
  132. <Tooltip title={<Duration exact seconds={checkIn.duration / 1000} />}>
  133. <Duration seconds={checkIn.duration / 1000} />
  134. </Tooltip>
  135. </div>
  136. ) : (
  137. emptyCell
  138. )}
  139. {checkIn.groups && checkIn.groups.length > 0 ? (
  140. <IssuesContainer>
  141. {checkIn.groups.map(({id, shortId}) => (
  142. <QuickContextHovercard
  143. dataRow={{
  144. ['issue.id']: id,
  145. issue: shortId,
  146. }}
  147. contextType={ContextType.ISSUE}
  148. organization={organization}
  149. key={id}
  150. >
  151. <StyledShortId
  152. shortId={shortId}
  153. avatar={
  154. <ProjectBadge
  155. project={monitor.project}
  156. hideName
  157. avatarSize={12}
  158. />
  159. }
  160. to={`/organizations/${organization.slug}/issues/${id}/`}
  161. />
  162. </QuickContextHovercard>
  163. ))}
  164. </IssuesContainer>
  165. ) : (
  166. emptyCell
  167. )}
  168. {!hasAttachments ? null : checkIn.attachmentId ? (
  169. <div>
  170. <LinkButton
  171. size="xs"
  172. icon={<IconDownload />}
  173. href={generateDownloadUrl(checkIn)}
  174. >
  175. {t('Attachment')}
  176. </LinkButton>
  177. </div>
  178. ) : (
  179. emptyCell
  180. )}
  181. {!hasMultiEnv ? null : <div>{checkIn.environment}</div>}
  182. <div>
  183. {checkIn.expectedTime ? (
  184. <Tooltip
  185. disabled={!customTimezone}
  186. title={
  187. <DateTime
  188. date={checkIn.expectedTime}
  189. forcedTimezone={monitor.config.timezone ?? 'UTC'}
  190. timeZone
  191. seconds
  192. />
  193. }
  194. >
  195. <Timestamp date={checkIn.expectedTime} timeZone seconds />
  196. </Tooltip>
  197. ) : (
  198. emptyCell
  199. )}
  200. </div>
  201. </Fragment>
  202. ))}
  203. </PanelTable>
  204. <Pagination pageLinks={getResponseHeader?.('Link')} />
  205. </Fragment>
  206. );
  207. }
  208. export default MonitorCheckIns;
  209. const Status = styled('div')`
  210. line-height: 1.1;
  211. `;
  212. const IssuesContainer = styled('div')`
  213. display: flex;
  214. flex-direction: column;
  215. `;
  216. const Timestamp = styled(DateTime)`
  217. color: ${p => p.theme.subText};
  218. `;
  219. const StyledShortId = styled(ShortId)`
  220. justify-content: flex-start;
  221. `;
  222. const RowPlaceholder = styled('div')`
  223. grid-column: 1 / -1;
  224. padding: ${space(1)};
  225. &:not(:last-child) {
  226. border-bottom: solid 1px ${p => p.theme.innerBorder};
  227. }
  228. `;