monitors.tsx 6.5 KB

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