monitorCheckIns.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 LoadingIndicator from 'sentry/components/loadingIndicator';
  10. import Pagination from 'sentry/components/pagination';
  11. import PanelTable from 'sentry/components/panels/panelTable';
  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 {defined} from 'sentry/utils';
  19. import {useApiQuery} from 'sentry/utils/queryClient';
  20. import {useLocation} from 'sentry/utils/useLocation';
  21. import useOrganization from 'sentry/utils/useOrganization';
  22. import {QuickContextHovercard} from 'sentry/views/discover/table/quickContext/quickContextHovercard';
  23. import {ContextType} from 'sentry/views/discover/table/quickContext/utils';
  24. import {
  25. CheckIn,
  26. CheckInStatus,
  27. Monitor,
  28. MonitorEnvironment,
  29. } from 'sentry/views/monitors/types';
  30. import {statusToText} from 'sentry/views/monitors/utils';
  31. type Props = {
  32. monitor: Monitor;
  33. monitorEnvs: MonitorEnvironment[];
  34. orgId: string;
  35. };
  36. const checkStatusToIndicatorStatus: Record<
  37. CheckInStatus,
  38. 'success' | 'error' | 'muted' | 'warning'
  39. > = {
  40. [CheckInStatus.OK]: 'success',
  41. [CheckInStatus.ERROR]: 'error',
  42. [CheckInStatus.IN_PROGRESS]: 'muted',
  43. [CheckInStatus.MISSED]: 'warning',
  44. [CheckInStatus.TIMEOUT]: 'error',
  45. };
  46. function MonitorCheckIns({monitor, monitorEnvs, orgId}: Props) {
  47. const location = useLocation();
  48. const organization = useOrganization();
  49. const queryKey = [
  50. `/organizations/${orgId}/monitors/${monitor.slug}/checkins/`,
  51. {
  52. query: {
  53. per_page: '10',
  54. environment: monitorEnvs.map(e => e.name),
  55. expand: 'groups',
  56. ...location.query,
  57. },
  58. },
  59. ] as const;
  60. const {
  61. data: checkInList,
  62. getResponseHeader,
  63. isLoading,
  64. isError,
  65. } = useApiQuery<CheckIn[]>(queryKey, {staleTime: 0});
  66. if (isLoading) {
  67. return <LoadingIndicator />;
  68. }
  69. if (isError) {
  70. return <LoadingError />;
  71. }
  72. const generateDownloadUrl = (checkin: CheckIn) =>
  73. `/api/0/organizations/${orgId}/monitors/${monitor.slug}/checkins/${checkin.id}/attachment/`;
  74. const emptyCell = <Text>{'\u2014'}</Text>;
  75. return (
  76. <React.Fragment>
  77. <SectionHeading>{t('Recent Check-Ins')}</SectionHeading>
  78. <PanelTable
  79. headers={[
  80. t('Status'),
  81. t('Started'),
  82. t('Duration'),
  83. t('Issues'),
  84. t('Attachment'),
  85. t('Timestamp'),
  86. ]}
  87. >
  88. {checkInList.map(checkIn => (
  89. <React.Fragment key={checkIn.id}>
  90. <Status>
  91. <StatusIndicator
  92. status={checkStatusToIndicatorStatus[checkIn.status]}
  93. tooltipTitle={tct('Check In Status: [status]', {
  94. status: statusToText[checkIn.status],
  95. })}
  96. />
  97. <Text>{statusToText[checkIn.status]}</Text>
  98. </Status>
  99. {checkIn.status !== CheckInStatus.MISSED ? (
  100. <div>
  101. {monitor.config.timezone ? (
  102. <Tooltip
  103. title={
  104. <DateTime
  105. date={checkIn.dateCreated}
  106. forcedTimezone={monitor.config.timezone}
  107. timeZone
  108. timeOnly
  109. />
  110. }
  111. >
  112. {<DateTime date={checkIn.dateCreated} timeOnly />}
  113. </Tooltip>
  114. ) : (
  115. <DateTime date={checkIn.dateCreated} timeOnly />
  116. )}
  117. </div>
  118. ) : (
  119. emptyCell
  120. )}
  121. {defined(checkIn.duration) ? (
  122. <Duration seconds={checkIn.duration / 1000} />
  123. ) : (
  124. emptyCell
  125. )}
  126. {checkIn.groups && checkIn.groups.length > 0 ? (
  127. <IssuesContainer>
  128. {checkIn.groups.map(({id, shortId}) => (
  129. <QuickContextHovercard
  130. dataRow={{
  131. ['issue.id']: id,
  132. issue: shortId,
  133. }}
  134. contextType={ContextType.ISSUE}
  135. organization={organization}
  136. key={id}
  137. >
  138. {
  139. <StyledShortId
  140. shortId={shortId}
  141. avatar={
  142. <ProjectBadge
  143. project={monitor.project}
  144. hideName
  145. avatarSize={12}
  146. />
  147. }
  148. to={`/issues/${id}`}
  149. />
  150. }
  151. </QuickContextHovercard>
  152. ))}
  153. </IssuesContainer>
  154. ) : (
  155. emptyCell
  156. )}
  157. {checkIn.attachmentId ? (
  158. <Button
  159. size="xs"
  160. icon={<IconDownload size="xs" />}
  161. href={generateDownloadUrl(checkIn)}
  162. >
  163. {t('Attachment')}
  164. </Button>
  165. ) : (
  166. emptyCell
  167. )}
  168. <Timestamp date={checkIn.dateCreated} />
  169. </React.Fragment>
  170. ))}
  171. </PanelTable>
  172. <Pagination pageLinks={getResponseHeader?.('Link')} />
  173. </React.Fragment>
  174. );
  175. }
  176. export default MonitorCheckIns;
  177. const Status = styled('div')`
  178. line-height: 1.1;
  179. `;
  180. const IssuesContainer = styled('div')`
  181. display: flex;
  182. flex-direction: column;
  183. `;
  184. const Timestamp = styled(DateTime)`
  185. color: ${p => p.theme.subText};
  186. `;
  187. const StyledShortId = styled(ShortId)`
  188. justify-content: flex-start;
  189. `;