monitorCheckIns.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 headers = [
  78. t('Status'),
  79. t('Started'),
  80. t('Duration'),
  81. t('Issues'),
  82. ...(hasAttachments ? [t('Attachment')] : []),
  83. t('Expected At'),
  84. ];
  85. const customTimezone =
  86. monitor.config.timezone &&
  87. monitor.config.timezone !== ConfigStore.get('user').options.timezone;
  88. return (
  89. <Fragment>
  90. <SectionHeading>{t('Recent Check-Ins')}</SectionHeading>
  91. <PanelTable headers={headers}>
  92. {isLoading
  93. ? [...new Array(headers.length)].map((_, i) => (
  94. <RowPlaceholder key={i}>
  95. <Placeholder height="2rem" />
  96. </RowPlaceholder>
  97. ))
  98. : checkInList.map(checkIn => (
  99. <Fragment key={checkIn.id}>
  100. <Status>
  101. <StatusIndicator
  102. status={checkStatusToIndicatorStatus[checkIn.status]}
  103. tooltipTitle={tct('Check In Status: [status]', {
  104. status: statusToText[checkIn.status],
  105. })}
  106. />
  107. <Text>{statusToText[checkIn.status]}</Text>
  108. </Status>
  109. {checkIn.status !== CheckInStatus.MISSED ? (
  110. <div>
  111. <Tooltip
  112. disabled={!customTimezone}
  113. title={
  114. <DateTime
  115. date={checkIn.dateCreated}
  116. forcedTimezone={monitor.config.timezone ?? 'UTC'}
  117. timeZone
  118. timeOnly
  119. seconds
  120. />
  121. }
  122. >
  123. {<DateTime date={checkIn.dateCreated} timeZone timeOnly seconds />}
  124. </Tooltip>
  125. </div>
  126. ) : (
  127. emptyCell
  128. )}
  129. {defined(checkIn.duration) ? (
  130. <Duration seconds={checkIn.duration / 1000} />
  131. ) : (
  132. emptyCell
  133. )}
  134. {checkIn.groups && checkIn.groups.length > 0 ? (
  135. <IssuesContainer>
  136. {checkIn.groups.map(({id, shortId}) => (
  137. <QuickContextHovercard
  138. dataRow={{
  139. ['issue.id']: id,
  140. issue: shortId,
  141. }}
  142. contextType={ContextType.ISSUE}
  143. organization={organization}
  144. key={id}
  145. >
  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. }
  159. </QuickContextHovercard>
  160. ))}
  161. </IssuesContainer>
  162. ) : (
  163. emptyCell
  164. )}
  165. {!hasAttachments ? null : checkIn.attachmentId ? (
  166. <div>
  167. <Button
  168. size="xs"
  169. icon={<IconDownload size="xs" />}
  170. href={generateDownloadUrl(checkIn)}
  171. >
  172. {t('Attachment')}
  173. </Button>
  174. </div>
  175. ) : (
  176. emptyCell
  177. )}
  178. <div>
  179. <Tooltip
  180. disabled={!customTimezone}
  181. title={
  182. <DateTime
  183. date={checkIn.expectedTime}
  184. forcedTimezone={monitor.config.timezone ?? 'UTC'}
  185. timeZone
  186. seconds
  187. />
  188. }
  189. >
  190. <Timestamp date={checkIn.expectedTime} timeZone seconds />
  191. </Tooltip>
  192. </div>
  193. </Fragment>
  194. ))}
  195. </PanelTable>
  196. <Pagination pageLinks={getResponseHeader?.('Link')} />
  197. </Fragment>
  198. );
  199. }
  200. export default MonitorCheckIns;
  201. const Status = styled('div')`
  202. line-height: 1.1;
  203. `;
  204. const IssuesContainer = styled('div')`
  205. display: flex;
  206. flex-direction: column;
  207. `;
  208. const Timestamp = styled(DateTime)`
  209. color: ${p => p.theme.subText};
  210. `;
  211. const StyledShortId = styled(ShortId)`
  212. justify-content: flex-start;
  213. `;
  214. const RowPlaceholder = styled('div')`
  215. grid-column: 1 / -1;
  216. padding: ${space(1)};
  217. &:not(:last-child) {
  218. border-bottom: solid 1px ${p => p.theme.innerBorder};
  219. }
  220. `;