monitors.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import {Fragment} from 'react';
  2. import {WithRouterProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import * as qs from 'query-string';
  5. import onboardingImg from 'sentry-images/spot/onboarding-preview.svg';
  6. import {Button, ButtonProps} from 'sentry/components/button';
  7. import ButtonBar from 'sentry/components/buttonBar';
  8. import FeatureBadge from 'sentry/components/featureBadge';
  9. import IdBadge from 'sentry/components/idBadge';
  10. import * as Layout from 'sentry/components/layouts/thirds';
  11. import Link from 'sentry/components/links/link';
  12. import NoProjectMessage from 'sentry/components/noProjectMessage';
  13. import OnboardingPanel from 'sentry/components/onboardingPanel';
  14. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  15. import {PageHeadingQuestionTooltip} from 'sentry/components/pageHeadingQuestionTooltip';
  16. import Pagination from 'sentry/components/pagination';
  17. import {PanelTable} from 'sentry/components/panels';
  18. import ProjectPageFilter from 'sentry/components/projectPageFilter';
  19. import SearchBar from 'sentry/components/searchBar';
  20. import TimeSince from 'sentry/components/timeSince';
  21. import {t} from 'sentry/locale';
  22. import space from 'sentry/styles/space';
  23. import {Organization} from 'sentry/types';
  24. import {decodeScalar} from 'sentry/utils/queryString';
  25. import withRouteAnalytics, {
  26. WithRouteAnalyticsProps,
  27. } from 'sentry/utils/routeAnalytics/withRouteAnalytics';
  28. import useOrganization from 'sentry/utils/useOrganization';
  29. import withOrganization from 'sentry/utils/withOrganization';
  30. // eslint-disable-next-line no-restricted-imports
  31. import withSentryRouter from 'sentry/utils/withSentryRouter';
  32. import AsyncView from 'sentry/views/asyncView';
  33. import CronsFeedbackButton from './cronsFeedbackButton';
  34. import MonitorIcon from './monitorIcon';
  35. import {Monitor} from './types';
  36. type Props = AsyncView['props'] &
  37. WithRouteAnalyticsProps &
  38. WithRouterProps<{}> & {
  39. organization: Organization;
  40. };
  41. type State = AsyncView['state'] & {
  42. monitorList: Monitor[] | null;
  43. };
  44. function NewMonitorButton(props: ButtonProps) {
  45. const organization = useOrganization();
  46. return (
  47. <Button
  48. to={`/organizations/${organization.slug}/crons/create/`}
  49. priority="primary"
  50. {...props}
  51. >
  52. {props.children}
  53. </Button>
  54. );
  55. }
  56. class Monitors extends AsyncView<Props, State> {
  57. get orgSlug() {
  58. return this.props.organization.slug;
  59. }
  60. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  61. const {location} = this.props;
  62. return [
  63. [
  64. 'monitorList',
  65. `/organizations/${this.orgSlug}/monitors/`,
  66. {
  67. query: location.query,
  68. },
  69. ],
  70. ];
  71. }
  72. getTitle() {
  73. return `Crons - ${this.orgSlug}`;
  74. }
  75. onRequestSuccess(response): void {
  76. this.props.setEventNames('monitors.page_viewed', 'Monitors: Page Viewed');
  77. this.props.setRouteAnalyticsParams({
  78. empty_state: response.data.length === 0,
  79. });
  80. }
  81. handleSearch = (query: string) => {
  82. const {location, router} = this.props;
  83. router.push({
  84. pathname: location.pathname,
  85. query: normalizeDateTimeParams({
  86. ...(location.query || {}),
  87. query,
  88. }),
  89. });
  90. };
  91. renderBody() {
  92. const {monitorList, monitorListPageLinks} = this.state;
  93. const {organization} = this.props;
  94. return (
  95. <Layout.Page>
  96. <NoProjectMessage organization={organization}>
  97. <Layout.Header>
  98. <Layout.HeaderContent>
  99. <Layout.Title>
  100. {t('Cron Monitors')}
  101. <PageHeadingQuestionTooltip
  102. title={t(
  103. 'Scheduled monitors that check in on recurring jobs and tell you if they’re running on schedule, failing, or succeeding.'
  104. )}
  105. docsUrl="https://docs.sentry.io/product/crons/"
  106. />
  107. <FeatureBadge type="beta" />
  108. </Layout.Title>
  109. </Layout.HeaderContent>
  110. <Layout.HeaderActions>
  111. <ButtonBar gap={1}>
  112. <NewMonitorButton size="sm">{t('Set Up Cron Monitor')}</NewMonitorButton>
  113. <CronsFeedbackButton />
  114. </ButtonBar>
  115. </Layout.HeaderActions>
  116. </Layout.Header>
  117. <Layout.Body>
  118. <Layout.Main fullWidth>
  119. <Filters>
  120. <ProjectPageFilter resetParamsOnChange={['cursor']} />
  121. <SearchBar
  122. query={decodeScalar(qs.parse(location.search)?.query, '')}
  123. placeholder={t('Search for monitors.')}
  124. onSearch={this.handleSearch}
  125. />
  126. </Filters>
  127. {monitorList?.length ? (
  128. <Fragment>
  129. <StyledPanelTable
  130. headers={[t('Monitor Name'), t('Last Check-In'), t('Project')]}
  131. >
  132. {monitorList?.map(monitor => (
  133. <Fragment key={monitor.id}>
  134. <MonitorName>
  135. <MonitorIcon status={monitor.status} size={16} />
  136. <StyledLink
  137. to={`/organizations/${organization.slug}/crons/${monitor.id}/`}
  138. >
  139. {monitor.name}
  140. </StyledLink>
  141. </MonitorName>
  142. {monitor.nextCheckIn ? (
  143. <StyledTimeSince date={monitor.lastCheckIn} />
  144. ) : (
  145. <div>{t('n/a')}</div>
  146. )}
  147. <IdBadge
  148. project={monitor.project}
  149. avatarSize={18}
  150. avatarProps={{hasTooltip: true, tooltip: monitor.project.slug}}
  151. />
  152. </Fragment>
  153. ))}
  154. </StyledPanelTable>
  155. {monitorListPageLinks && (
  156. <Pagination pageLinks={monitorListPageLinks} {...this.props} />
  157. )}
  158. </Fragment>
  159. ) : (
  160. <OnboardingPanel image={<img src={onboardingImg} />}>
  161. <h3>{t('Let Sentry monitor your recurring jobs')}</h3>
  162. <p>
  163. {t(
  164. "We'll tell you if your recurring jobs are running on schedule, failing, or succeeding."
  165. )}
  166. </p>
  167. <ButtonList gap={1}>
  168. <NewMonitorButton>{t('Set up first cron monitor')}</NewMonitorButton>
  169. <Button href="https://docs.sentry.io/product/crons" external>
  170. {t('Read docs')}
  171. </Button>
  172. </ButtonList>
  173. </OnboardingPanel>
  174. )}
  175. </Layout.Main>
  176. </Layout.Body>
  177. </NoProjectMessage>
  178. </Layout.Page>
  179. );
  180. }
  181. }
  182. const StyledLink = styled(Link)`
  183. flex: 1;
  184. margin-left: ${space(2)};
  185. `;
  186. const StyledTimeSince = styled(TimeSince)`
  187. font-variant-numeric: tabular-nums;
  188. `;
  189. const Filters = styled('div')`
  190. display: grid;
  191. grid-template-columns: minmax(auto, 300px) 1fr;
  192. gap: ${space(1.5)};
  193. margin-bottom: ${space(2)};
  194. `;
  195. const MonitorName = styled('div')`
  196. display: flex;
  197. align-items: center;
  198. `;
  199. const StyledPanelTable = styled(PanelTable)`
  200. grid-template-columns: 1fr max-content max-content;
  201. `;
  202. const ButtonList = styled(ButtonBar)`
  203. grid-template-columns: repeat(auto-fit, minmax(130px, max-content));
  204. `;
  205. export default withRouteAnalytics(withSentryRouter(withOrganization(Monitors)));