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 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. import {statusToText} from 'sentry/views/monitors/utils';
  25. type Props = {
  26. monitor: Monitor;
  27. monitorEnvs: MonitorEnvironment[];
  28. orgId: string;
  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. function MonitorCheckIns({monitor, monitorEnvs, orgId}: Props) {
  41. const location = useLocation();
  42. const queryKey = [
  43. `/organizations/${orgId}/monitors/${monitor.slug}/checkins/`,
  44. {
  45. query: {
  46. per_page: '10',
  47. environment: monitorEnvs.map(e => e.name),
  48. ...location.query,
  49. },
  50. },
  51. ] as const;
  52. const {
  53. data: checkInList,
  54. getResponseHeader,
  55. isLoading,
  56. isError,
  57. } = useApiQuery<CheckIn[]>(queryKey, {staleTime: 0});
  58. if (isLoading) {
  59. return <LoadingIndicator />;
  60. }
  61. if (isError) {
  62. return <LoadingError />;
  63. }
  64. const generateDownloadUrl = (checkin: CheckIn) =>
  65. `/api/0/organizations/${orgId}/monitors/${monitor.slug}/checkins/${checkin.id}/attachment/`;
  66. const emptyCell = <Text>{'\u2014'}</Text>;
  67. return (
  68. <React.Fragment>
  69. <SectionHeading>{t('Recent Check-Ins')}</SectionHeading>
  70. <PanelTable
  71. headers={[
  72. t('Status'),
  73. t('Started'),
  74. t('Duration'),
  75. t('Attachment'),
  76. t('Timestamp'),
  77. ]}
  78. >
  79. {checkInList.map(checkIn => (
  80. <React.Fragment key={checkIn.id}>
  81. <Status>
  82. <StatusIndicator
  83. status={checkStatusToIndicatorStatus[checkIn.status]}
  84. tooltipTitle={tct('Check In Status: [status]', {
  85. status: statusToText[checkIn.status],
  86. })}
  87. />
  88. <Text>{statusToText[checkIn.status]}</Text>
  89. </Status>
  90. {checkIn.status !== CheckInStatus.MISSED ? (
  91. <DateTime date={checkIn.dateCreated} timeOnly />
  92. ) : (
  93. emptyCell
  94. )}
  95. {defined(checkIn.duration) ? (
  96. <Duration seconds={checkIn.duration / 1000} />
  97. ) : (
  98. emptyCell
  99. )}
  100. {checkIn.attachmentId ? (
  101. <Button
  102. size="xs"
  103. icon={<IconDownload size="xs" />}
  104. href={generateDownloadUrl(checkIn)}
  105. >
  106. {t('Attachment')}
  107. </Button>
  108. ) : (
  109. emptyCell
  110. )}
  111. <Timestamp date={checkIn.dateCreated} />
  112. </React.Fragment>
  113. ))}
  114. </PanelTable>
  115. <Pagination pageLinks={getResponseHeader?.('Link')} />
  116. </React.Fragment>
  117. );
  118. }
  119. export default MonitorCheckIns;
  120. const Status = styled('div')`
  121. display: flex;
  122. align-items: center;
  123. `;
  124. const Timestamp = styled(DateTime)`
  125. color: ${p => p.theme.subText};
  126. `;