monitorCheckIns.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 {space} from 'sentry/styles/space';
  19. import {defined} from 'sentry/utils';
  20. import {useApiQuery} from 'sentry/utils/queryClient';
  21. import {useLocation} from 'sentry/utils/useLocation';
  22. import useOrganization from 'sentry/utils/useOrganization';
  23. import {QuickContextHovercard} from 'sentry/views/discover/table/quickContext/quickContextHovercard';
  24. import {ContextType} from 'sentry/views/discover/table/quickContext/utils';
  25. import {
  26. CheckIn,
  27. CheckInStatus,
  28. Monitor,
  29. MonitorEnvironment,
  30. } 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. const checkStatusToIndicatorStatus: Record<
  38. CheckInStatus,
  39. 'success' | 'error' | 'muted' | 'warning'
  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. `/organizations/${orgSlug}/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. isLoading,
  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 headers = [
  77. t('Status'),
  78. t('Started'),
  79. t('Duration'),
  80. t('Issues'),
  81. ...(hasAttachments ? [t('Attachment')] : []),
  82. t('Timestamp'),
  83. ];
  84. return (
  85. <Fragment>
  86. <SectionHeading>{t('Recent Check-Ins')}</SectionHeading>
  87. <PanelTable headers={headers}>
  88. {isLoading
  89. ? [...new Array(headers.length)].map((_, i) => (
  90. <RowPlaceholder key={i}>
  91. <Placeholder height="2rem" />
  92. </RowPlaceholder>
  93. ))
  94. : checkInList.map(checkIn => (
  95. <Fragment key={checkIn.id}>
  96. <Status>
  97. <StatusIndicator
  98. status={checkStatusToIndicatorStatus[checkIn.status]}
  99. tooltipTitle={tct('Check In Status: [status]', {
  100. status: statusToText[checkIn.status],
  101. })}
  102. />
  103. <Text>{statusToText[checkIn.status]}</Text>
  104. </Status>
  105. {checkIn.status !== CheckInStatus.MISSED ? (
  106. <div>
  107. {monitor.config.timezone ? (
  108. <Tooltip
  109. title={
  110. <DateTime
  111. date={checkIn.dateCreated}
  112. forcedTimezone={monitor.config.timezone}
  113. timeZone
  114. timeOnly
  115. />
  116. }
  117. >
  118. {<DateTime date={checkIn.dateCreated} timeOnly />}
  119. </Tooltip>
  120. ) : (
  121. <DateTime date={checkIn.dateCreated} timeOnly />
  122. )}
  123. </div>
  124. ) : (
  125. emptyCell
  126. )}
  127. {defined(checkIn.duration) ? (
  128. <Duration seconds={checkIn.duration / 1000} />
  129. ) : (
  130. emptyCell
  131. )}
  132. {checkIn.groups && checkIn.groups.length > 0 ? (
  133. <IssuesContainer>
  134. {checkIn.groups.map(({id, shortId}) => (
  135. <QuickContextHovercard
  136. dataRow={{
  137. ['issue.id']: id,
  138. issue: shortId,
  139. }}
  140. contextType={ContextType.ISSUE}
  141. organization={organization}
  142. key={id}
  143. >
  144. {
  145. <StyledShortId
  146. shortId={shortId}
  147. avatar={
  148. <ProjectBadge
  149. project={monitor.project}
  150. hideName
  151. avatarSize={12}
  152. />
  153. }
  154. to={`/issues/${id}`}
  155. />
  156. }
  157. </QuickContextHovercard>
  158. ))}
  159. </IssuesContainer>
  160. ) : (
  161. emptyCell
  162. )}
  163. {!hasAttachments ? null : checkIn.attachmentId ? (
  164. <Button
  165. size="xs"
  166. icon={<IconDownload size="xs" />}
  167. href={generateDownloadUrl(checkIn)}
  168. >
  169. {t('Attachment')}
  170. </Button>
  171. ) : (
  172. emptyCell
  173. )}
  174. <Timestamp date={checkIn.dateCreated} />
  175. </Fragment>
  176. ))}
  177. </PanelTable>
  178. <Pagination pageLinks={getResponseHeader?.('Link')} />
  179. </Fragment>
  180. );
  181. }
  182. export default MonitorCheckIns;
  183. const Status = styled('div')`
  184. line-height: 1.1;
  185. `;
  186. const IssuesContainer = styled('div')`
  187. display: flex;
  188. flex-direction: column;
  189. `;
  190. const Timestamp = styled(DateTime)`
  191. color: ${p => p.theme.subText};
  192. `;
  193. const StyledShortId = styled(ShortId)`
  194. justify-content: flex-start;
  195. `;
  196. const RowPlaceholder = styled('div')`
  197. grid-column: 1 / -1;
  198. padding: ${space(1)};
  199. &:not(:last-child) {
  200. border-bottom: solid 1px ${p => p.theme.innerBorder};
  201. }
  202. `;