monitorCheckIns.tsx 7.8 KB

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