monitorCheckIns.tsx 8.1 KB

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