monitorCheckIns.tsx 7.7 KB

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