monitorCheckIns.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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('Expected At'),
  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. seconds
  116. />
  117. }
  118. >
  119. {<DateTime date={checkIn.dateCreated} timeOnly seconds />}
  120. </Tooltip>
  121. ) : (
  122. <DateTime date={checkIn.dateCreated} timeOnly seconds />
  123. )}
  124. </div>
  125. ) : (
  126. emptyCell
  127. )}
  128. {defined(checkIn.duration) ? (
  129. <Duration seconds={checkIn.duration / 1000} />
  130. ) : (
  131. emptyCell
  132. )}
  133. {checkIn.groups && checkIn.groups.length > 0 ? (
  134. <IssuesContainer>
  135. {checkIn.groups.map(({id, shortId}) => (
  136. <QuickContextHovercard
  137. dataRow={{
  138. ['issue.id']: id,
  139. issue: shortId,
  140. }}
  141. contextType={ContextType.ISSUE}
  142. organization={organization}
  143. key={id}
  144. >
  145. {
  146. <StyledShortId
  147. shortId={shortId}
  148. avatar={
  149. <ProjectBadge
  150. project={monitor.project}
  151. hideName
  152. avatarSize={12}
  153. />
  154. }
  155. to={`/issues/${id}`}
  156. />
  157. }
  158. </QuickContextHovercard>
  159. ))}
  160. </IssuesContainer>
  161. ) : (
  162. emptyCell
  163. )}
  164. {!hasAttachments ? null : checkIn.attachmentId ? (
  165. <Button
  166. size="xs"
  167. icon={<IconDownload size="xs" />}
  168. href={generateDownloadUrl(checkIn)}
  169. >
  170. {t('Attachment')}
  171. </Button>
  172. ) : (
  173. emptyCell
  174. )}
  175. <Timestamp date={checkIn.expectedTime} seconds />
  176. </Fragment>
  177. ))}
  178. </PanelTable>
  179. <Pagination pageLinks={getResponseHeader?.('Link')} />
  180. </Fragment>
  181. );
  182. }
  183. export default MonitorCheckIns;
  184. const Status = styled('div')`
  185. line-height: 1.1;
  186. `;
  187. const IssuesContainer = styled('div')`
  188. display: flex;
  189. flex-direction: column;
  190. `;
  191. const Timestamp = styled(DateTime)`
  192. color: ${p => p.theme.subText};
  193. `;
  194. const StyledShortId = styled(ShortId)`
  195. justify-content: flex-start;
  196. `;
  197. const RowPlaceholder = styled('div')`
  198. grid-column: 1 / -1;
  199. padding: ${space(1)};
  200. &:not(:last-child) {
  201. border-bottom: solid 1px ${p => p.theme.innerBorder};
  202. }
  203. `;