monitorCheckIns.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. seconds
  121. />
  122. }
  123. >
  124. {<DateTime date={checkIn.dateCreated} timeZone seconds />}
  125. </Tooltip>
  126. </div>
  127. ) : (
  128. emptyCell
  129. )}
  130. {defined(checkIn.duration) ? (
  131. <Duration seconds={checkIn.duration / 1000} />
  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. {
  148. <StyledShortId
  149. shortId={shortId}
  150. avatar={
  151. <ProjectBadge
  152. project={monitor.project}
  153. hideName
  154. avatarSize={12}
  155. />
  156. }
  157. to={`/issues/${id}`}
  158. />
  159. }
  160. </QuickContextHovercard>
  161. ))}
  162. </IssuesContainer>
  163. ) : (
  164. emptyCell
  165. )}
  166. {!hasAttachments ? null : checkIn.attachmentId ? (
  167. <div>
  168. <Button
  169. size="xs"
  170. icon={<IconDownload size="xs" />}
  171. href={generateDownloadUrl(checkIn)}
  172. >
  173. {t('Attachment')}
  174. </Button>
  175. </div>
  176. ) : (
  177. emptyCell
  178. )}
  179. {!hasMultiEnv ? null : <div>{checkIn.environment}</div>}
  180. <div>
  181. <Tooltip
  182. disabled={!customTimezone}
  183. title={
  184. <DateTime
  185. date={checkIn.expectedTime}
  186. forcedTimezone={monitor.config.timezone ?? 'UTC'}
  187. timeZone
  188. seconds
  189. />
  190. }
  191. >
  192. <Timestamp date={checkIn.expectedTime} timeZone seconds />
  193. </Tooltip>
  194. </div>
  195. </Fragment>
  196. ))}
  197. </PanelTable>
  198. <Pagination pageLinks={getResponseHeader?.('Link')} />
  199. </Fragment>
  200. );
  201. }
  202. export default MonitorCheckIns;
  203. const Status = styled('div')`
  204. line-height: 1.1;
  205. `;
  206. const IssuesContainer = styled('div')`
  207. display: flex;
  208. flex-direction: column;
  209. `;
  210. const Timestamp = styled(DateTime)`
  211. color: ${p => p.theme.subText};
  212. `;
  213. const StyledShortId = styled(ShortId)`
  214. justify-content: flex-start;
  215. `;
  216. const RowPlaceholder = styled('div')`
  217. grid-column: 1 / -1;
  218. padding: ${space(1)};
  219. &:not(:last-child) {
  220. border-bottom: solid 1px ${p => p.theme.innerBorder};
  221. }
  222. `;