monitorCheckIns.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {SectionHeading} from 'sentry/components/charts/styles';
  4. import {DateTime} from 'sentry/components/dateTime';
  5. import Duration from 'sentry/components/duration';
  6. import ProjectBadge from 'sentry/components/idBadge/projectBadge';
  7. import LoadingError from 'sentry/components/loadingError';
  8. import Pagination from 'sentry/components/pagination';
  9. import {PanelTable} from 'sentry/components/panels/panelTable';
  10. import Placeholder from 'sentry/components/placeholder';
  11. import ShortId from 'sentry/components/shortId';
  12. import {
  13. StatusIndicator,
  14. type StatusIndicatorProps,
  15. } from 'sentry/components/statusIndicator';
  16. import Text from 'sentry/components/text';
  17. import {Tooltip} from 'sentry/components/tooltip';
  18. import {t, tct} from 'sentry/locale';
  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 {useUser} from 'sentry/utils/useUser';
  25. import {QuickContextHovercard} from 'sentry/views/discover/table/quickContext/quickContextHovercard';
  26. import {ContextType} from 'sentry/views/discover/table/quickContext/utils';
  27. import type {CheckIn, Monitor, MonitorEnvironment} from 'sentry/views/monitors/types';
  28. import {CheckInStatus} from 'sentry/views/monitors/types';
  29. import {statusToText} from 'sentry/views/monitors/utils';
  30. type Props = {
  31. monitor: Monitor;
  32. monitorEnvs: MonitorEnvironment[];
  33. orgSlug: string;
  34. };
  35. export const checkStatusToIndicatorStatus: Record<
  36. CheckInStatus,
  37. StatusIndicatorProps['status']
  38. > = {
  39. [CheckInStatus.OK]: 'success',
  40. [CheckInStatus.ERROR]: 'error',
  41. [CheckInStatus.IN_PROGRESS]: 'muted',
  42. [CheckInStatus.MISSED]: 'warning',
  43. [CheckInStatus.TIMEOUT]: 'error',
  44. [CheckInStatus.UNKNOWN]: 'muted',
  45. };
  46. export function MonitorCheckIns({monitor, monitorEnvs, orgSlug}: Props) {
  47. const user = useUser();
  48. const location = useLocation();
  49. const organization = useOrganization();
  50. const queryKey = [
  51. `/projects/${orgSlug}/${monitor.project.slug}/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. isPending,
  65. isError,
  66. } = useApiQuery<CheckIn[]>(queryKey, {staleTime: 0});
  67. if (isError) {
  68. return <LoadingError />;
  69. }
  70. const emptyCell = <Text>{'\u2014'}</Text>;
  71. const hasMultiEnv = monitorEnvs.length > 1;
  72. const headers = [
  73. t('Status'),
  74. t('Started'),
  75. t('Duration'),
  76. t('Issues'),
  77. ...(hasMultiEnv ? [t('Environment')] : []),
  78. t('Expected At'),
  79. ];
  80. const customTimezone =
  81. monitor.config.timezone && monitor.config.timezone !== user.options.timezone;
  82. return (
  83. <Fragment>
  84. <SectionHeading>{t('Recent Check-Ins')}</SectionHeading>
  85. <PanelTable headers={headers}>
  86. {isPending
  87. ? [...new Array(headers.length)].map((_, i) => (
  88. <RowPlaceholder key={i}>
  89. <Placeholder height="2rem" />
  90. </RowPlaceholder>
  91. ))
  92. : checkInList.map(checkIn => (
  93. <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. <Tooltip
  106. disabled={!customTimezone}
  107. title={
  108. <DateTime
  109. date={checkIn.dateCreated}
  110. forcedTimezone={monitor.config.timezone ?? 'UTC'}
  111. timeZone
  112. seconds
  113. />
  114. }
  115. >
  116. <DateTime date={checkIn.dateCreated} timeZone seconds />
  117. </Tooltip>
  118. </div>
  119. ) : (
  120. emptyCell
  121. )}
  122. {defined(checkIn.duration) ? (
  123. <div>
  124. <Tooltip title={<Duration exact seconds={checkIn.duration / 1000} />}>
  125. <Duration seconds={checkIn.duration / 1000} />
  126. </Tooltip>
  127. </div>
  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={`/organizations/${organization.slug}/issues/${id}/`}
  153. />
  154. </QuickContextHovercard>
  155. ))}
  156. </IssuesContainer>
  157. ) : (
  158. emptyCell
  159. )}
  160. {!hasMultiEnv ? null : <div>{checkIn.environment}</div>}
  161. <div>
  162. {checkIn.expectedTime ? (
  163. <Tooltip
  164. disabled={!customTimezone}
  165. title={
  166. <DateTime
  167. date={checkIn.expectedTime}
  168. forcedTimezone={monitor.config.timezone ?? 'UTC'}
  169. timeZone
  170. seconds
  171. />
  172. }
  173. >
  174. <Timestamp date={checkIn.expectedTime} timeZone seconds />
  175. </Tooltip>
  176. ) : (
  177. emptyCell
  178. )}
  179. </div>
  180. </Fragment>
  181. ))}
  182. </PanelTable>
  183. <Pagination pageLinks={getResponseHeader?.('Link')} />
  184. </Fragment>
  185. );
  186. }
  187. const Status = styled('div')`
  188. line-height: 1.1;
  189. `;
  190. const IssuesContainer = styled('div')`
  191. display: flex;
  192. flex-direction: column;
  193. `;
  194. const Timestamp = styled(DateTime)`
  195. color: ${p => p.theme.subText};
  196. `;
  197. const StyledShortId = styled(ShortId)`
  198. justify-content: flex-start;
  199. `;
  200. const RowPlaceholder = styled('div')`
  201. grid-column: 1 / -1;
  202. padding: ${space(1)};
  203. &:not(:last-child) {
  204. border-bottom: solid 1px ${p => p.theme.innerBorder};
  205. }
  206. `;