monitorCheckIns.tsx 3.9 KB

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