monitors.tsx 6.6 KB

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