monitorCheckIns.tsx 6.8 KB

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