monitorCheckIns.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 LoadingError from 'sentry/components/loadingError';
  8. import LoadingIndicator from 'sentry/components/loadingIndicator';
  9. import Pagination from 'sentry/components/pagination';
  10. import {PanelTable} from 'sentry/components/panels';
  11. import StatusIndicator from 'sentry/components/statusIndicator';
  12. import Text from 'sentry/components/text';
  13. import {IconDownload} from 'sentry/icons';
  14. import {t, tct} from 'sentry/locale';
  15. import {defined} from 'sentry/utils';
  16. import {useApiQuery} from 'sentry/utils/queryClient';
  17. import {useLocation} from 'sentry/utils/useLocation';
  18. import {
  19. CheckIn,
  20. CheckInStatus,
  21. Monitor,
  22. MonitorEnvironment,
  23. } from 'sentry/views/monitors/types';
  24. type Props = {
  25. monitor: Monitor;
  26. monitorEnvs: MonitorEnvironment[];
  27. orgId: string;
  28. };
  29. const checkStatusToIndicatorStatus: Record<
  30. CheckInStatus,
  31. 'success' | 'error' | 'muted' | 'warning'
  32. > = {
  33. [CheckInStatus.OK]: 'success',
  34. [CheckInStatus.ERROR]: 'error',
  35. [CheckInStatus.IN_PROGRESS]: 'muted',
  36. [CheckInStatus.MISSED]: 'warning',
  37. [CheckInStatus.TIMEOUT]: 'error',
  38. };
  39. const statusToText: Record<CheckInStatus, string> = {
  40. [CheckInStatus.OK]: t('Okay'),
  41. [CheckInStatus.ERROR]: t('Failed'),
  42. [CheckInStatus.IN_PROGRESS]: t('In Progress'),
  43. [CheckInStatus.MISSED]: t('Missed'),
  44. [CheckInStatus.TIMEOUT]: t('Timed Out'),
  45. };
  46. function MonitorCheckIns({monitor, monitorEnvs, orgId}: Props) {
  47. const location = useLocation();
  48. const queryKey = [
  49. `/organizations/${orgId}/monitors/${monitor.slug}/checkins/`,
  50. {
  51. query: {
  52. per_page: '10',
  53. environment: monitorEnvs.map(e => e.name),
  54. ...location.query,
  55. },
  56. },
  57. ] as const;
  58. const {
  59. data: checkInList,
  60. getResponseHeader,
  61. isLoading,
  62. isError,
  63. } = useApiQuery<CheckIn[]>(queryKey, {staleTime: 0});
  64. if (isLoading) {
  65. return <LoadingIndicator />;
  66. }
  67. if (isError) {
  68. return <LoadingError />;
  69. }
  70. const generateDownloadUrl = (checkin: CheckIn) =>
  71. `/api/0/organizations/${orgId}/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('Attachment'),
  82. t('Timestamp'),
  83. ]}
  84. >
  85. {checkInList.map(checkIn => (
  86. <React.Fragment key={checkIn.id}>
  87. <Status>
  88. <StatusIndicator
  89. status={checkStatusToIndicatorStatus[checkIn.status]}
  90. tooltipTitle={tct('Check In Status: [status]', {
  91. status: statusToText[checkIn.status],
  92. })}
  93. />
  94. <Text>{statusToText[checkIn.status]}</Text>
  95. </Status>
  96. {checkIn.status !== CheckInStatus.MISSED ? (
  97. <DateTime date={checkIn.dateCreated} timeOnly />
  98. ) : (
  99. emptyCell
  100. )}
  101. {defined(checkIn.duration) ? (
  102. <Duration seconds={checkIn.duration / 1000} />
  103. ) : (
  104. emptyCell
  105. )}
  106. {checkIn.attachmentId ? (
  107. <Button
  108. size="xs"
  109. icon={<IconDownload size="xs" />}
  110. href={generateDownloadUrl(checkIn)}
  111. >
  112. {t('Attachment')}
  113. </Button>
  114. ) : (
  115. emptyCell
  116. )}
  117. <Timestamp date={checkIn.dateCreated} />
  118. </React.Fragment>
  119. ))}
  120. </PanelTable>
  121. <Pagination pageLinks={getResponseHeader?.('Link')} />
  122. </React.Fragment>
  123. );
  124. }
  125. export default MonitorCheckIns;
  126. const Status = styled('div')`
  127. display: flex;
  128. align-items: center;
  129. `;
  130. const Timestamp = styled(DateTime)`
  131. color: ${p => p.theme.subText};
  132. `;