monitorCheckIns.tsx 8.0 KB

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