monitorCheckIns.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Pagination from 'sentry/components/pagination';
  8. import {PanelTable} from 'sentry/components/panels';
  9. import StatusIndicator from 'sentry/components/statusIndicator';
  10. import Text from 'sentry/components/text';
  11. import {IconDownload} from 'sentry/icons';
  12. import {t, tct} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import {defined} from 'sentry/utils';
  15. import useApiRequests from 'sentry/utils/useApiRequests';
  16. import {
  17. CheckIn,
  18. CheckInStatus,
  19. Monitor,
  20. MonitorEnvironment,
  21. } from 'sentry/views/monitors/types';
  22. type Props = {
  23. monitor: Monitor;
  24. monitorEnv: MonitorEnvironment;
  25. orgId: string;
  26. };
  27. type State = {
  28. checkInList: CheckIn[];
  29. };
  30. const checkStatusToIndicatorStatus: Record<CheckInStatus, string> = {
  31. [CheckInStatus.OK]: 'success',
  32. [CheckInStatus.ERROR]: 'error',
  33. [CheckInStatus.IN_PROGRESS]: 'muted',
  34. [CheckInStatus.MISSED]: 'warning',
  35. [CheckInStatus.TIMEOUT]: 'error',
  36. };
  37. const statusToText = {
  38. [CheckInStatus.OK]: t('Okay'),
  39. [CheckInStatus.ERROR]: t('Failed'),
  40. [CheckInStatus.IN_PROGRESS]: t('In Progress'),
  41. [CheckInStatus.MISSED]: t('Missed'),
  42. [CheckInStatus.TIMEOUT]: t('Timed Out'),
  43. };
  44. function MonitorCheckIns({monitor, monitorEnv, orgId}: Props) {
  45. const {data, hasError, renderComponent} = useApiRequests<State>({
  46. endpoints: [
  47. [
  48. 'checkInList',
  49. `/organizations/${orgId}/monitors/${monitor.slug}/checkins/`,
  50. {query: {per_page: '10', environment: monitorEnv.name}},
  51. {paginate: true},
  52. ],
  53. ],
  54. });
  55. const generateDownloadUrl = (checkin: CheckIn) =>
  56. `/api/0/organizations/${orgId}/monitors/${monitor.slug}/checkins/${checkin.id}/attachment/`;
  57. const emptyCell = <Text>{'\u2014'}</Text>;
  58. const renderedComponent = renderComponent(
  59. <React.Fragment>
  60. <SectionHeading>{t('Recent Check-Ins')}</SectionHeading>
  61. <PanelTable
  62. headers={[
  63. t('Status'),
  64. t('Started'),
  65. t('Duration'),
  66. t('Attachment'),
  67. t('Timestamp'),
  68. ]}
  69. >
  70. {data.checkInList?.map(checkIn => (
  71. <React.Fragment key={checkIn.id}>
  72. <Status>
  73. <StatusIndicator
  74. status={checkStatusToIndicatorStatus[checkIn.status]}
  75. tooltipTitle={tct('Check In Status: [status]', {
  76. status: statusToText[checkIn.status],
  77. })}
  78. />
  79. <Text>{statusToText[checkIn.status]}</Text>
  80. </Status>
  81. {checkIn.status !== CheckInStatus.MISSED ? (
  82. <DateTime date={checkIn.dateCreated} timeOnly />
  83. ) : (
  84. emptyCell
  85. )}
  86. {defined(checkIn.duration) ? (
  87. <Duration seconds={checkIn.duration / 1000} />
  88. ) : (
  89. emptyCell
  90. )}
  91. {checkIn.attachmentId ? (
  92. <Button
  93. size="xs"
  94. icon={<IconDownload size="xs" />}
  95. href={generateDownloadUrl(checkIn)}
  96. >
  97. Attachment
  98. </Button>
  99. ) : (
  100. emptyCell
  101. )}
  102. <Timestamp date={checkIn.dateCreated} />
  103. </React.Fragment>
  104. ))}
  105. </PanelTable>
  106. <Pagination pageLinks={data.checkInListPageLinks} />
  107. </React.Fragment>
  108. );
  109. return hasError ? <ErrorWrapper>{renderedComponent}</ErrorWrapper> : renderedComponent;
  110. }
  111. export default MonitorCheckIns;
  112. const Status = styled('div')`
  113. display: flex;
  114. align-items: center;
  115. `;
  116. const Timestamp = styled(DateTime)`
  117. color: ${p => p.theme.subText};
  118. `;
  119. const ErrorWrapper = styled('div')`
  120. margin: ${space(3)} ${space(3)} 0;
  121. `;