monitorCheckIns.tsx 7.8 KB

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