monitorCheckIns.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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
  86. headers={headers}
  87. isEmpty={!isPending && checkInList.length === 0}
  88. emptyMessage={t('No check-ins have been recorded for this time period.')}
  89. >
  90. {isPending
  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. <div>
  128. <Tooltip title={<Duration exact seconds={checkIn.duration / 1000} />}>
  129. <Duration seconds={checkIn.duration / 1000} />
  130. </Tooltip>
  131. </div>
  132. ) : (
  133. emptyCell
  134. )}
  135. {checkIn.groups && checkIn.groups.length > 0 ? (
  136. <IssuesContainer>
  137. {checkIn.groups.map(({id, shortId}) => (
  138. <QuickContextHovercard
  139. dataRow={{
  140. ['issue.id']: id,
  141. issue: shortId,
  142. }}
  143. contextType={ContextType.ISSUE}
  144. organization={organization}
  145. key={id}
  146. >
  147. <StyledShortId
  148. shortId={shortId}
  149. avatar={
  150. <ProjectBadge
  151. project={monitor.project}
  152. hideName
  153. avatarSize={12}
  154. />
  155. }
  156. to={`/organizations/${organization.slug}/issues/${id}/`}
  157. />
  158. </QuickContextHovercard>
  159. ))}
  160. </IssuesContainer>
  161. ) : (
  162. emptyCell
  163. )}
  164. {!hasMultiEnv ? null : <div>{checkIn.environment}</div>}
  165. <div>
  166. {checkIn.expectedTime ? (
  167. <Tooltip
  168. disabled={!customTimezone}
  169. title={
  170. <DateTime
  171. date={checkIn.expectedTime}
  172. forcedTimezone={monitor.config.timezone ?? 'UTC'}
  173. timeZone
  174. seconds
  175. />
  176. }
  177. >
  178. <Timestamp date={checkIn.expectedTime} timeZone seconds />
  179. </Tooltip>
  180. ) : (
  181. emptyCell
  182. )}
  183. </div>
  184. </Fragment>
  185. ))}
  186. </PanelTable>
  187. <Pagination pageLinks={getResponseHeader?.('Link')} />
  188. </Fragment>
  189. );
  190. }
  191. const Status = styled('div')`
  192. line-height: 1.1;
  193. `;
  194. const IssuesContainer = styled('div')`
  195. display: flex;
  196. flex-direction: column;
  197. `;
  198. const Timestamp = styled(DateTime)`
  199. color: ${p => p.theme.subText};
  200. `;
  201. const StyledShortId = styled(ShortId)`
  202. justify-content: flex-start;
  203. `;
  204. const RowPlaceholder = styled('div')`
  205. grid-column: 1 / -1;
  206. padding: ${space(1)};
  207. &:not(:last-child) {
  208. border-bottom: solid 1px ${p => p.theme.innerBorder};
  209. }
  210. `;