monitorQuickStartGuide.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import {useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import partition from 'lodash/partition';
  4. import {CompactSelect} from 'sentry/components/compactSelect';
  5. import {t} from 'sentry/locale';
  6. import {space} from 'sentry/styles/space';
  7. import type {PlatformKey, ProjectKey} from 'sentry/types/project';
  8. import {useApiQuery} from 'sentry/utils/queryClient';
  9. import useOrganization from 'sentry/utils/useOrganization';
  10. import type {QuickStartProps} from 'sentry/views/monitors/components/quickStartEntries';
  11. import {
  12. CLICronQuickStart,
  13. CurlCronQuickStart,
  14. GoCronQuickStart,
  15. JavaCronQuickStart,
  16. JavaQuartzCronQuickStart,
  17. JavaSpringBootCronQuickStart,
  18. NodeJSCronQuickStart,
  19. PHPCronQuickStart,
  20. PHPLaravelCronQuickStart,
  21. PythonCeleryCronQuickStart,
  22. PythonCronQuickStart,
  23. RubyCronQuickStart,
  24. RubyRailsCronQuickStart,
  25. RubySidekiqCronQuickStart,
  26. } from 'sentry/views/monitors/components/quickStartEntries';
  27. import type {Monitor} from '../types';
  28. interface Props {
  29. monitor: Monitor;
  30. }
  31. interface OnboardingGuide {
  32. Guide: React.ComponentType<QuickStartProps>;
  33. label: string;
  34. platforms?: Set<PlatformKey>;
  35. }
  36. const onboardingGuides: Record<string, OnboardingGuide> = {
  37. cli: {
  38. label: 'Sentry CLI',
  39. Guide: CLICronQuickStart,
  40. },
  41. curl: {
  42. label: 'cURL',
  43. Guide: CurlCronQuickStart,
  44. },
  45. python: {
  46. label: 'Python',
  47. Guide: PythonCronQuickStart,
  48. platforms: new Set([
  49. 'python',
  50. 'python-django',
  51. 'python-flask',
  52. 'python-fastapi',
  53. 'python-starlette',
  54. 'python-sanic',
  55. 'python-bottle',
  56. 'python-pylons',
  57. 'python-pyramid',
  58. 'python-tornado',
  59. 'python-rq',
  60. ]),
  61. },
  62. pythonCelery: {
  63. label: 'Celery',
  64. Guide: PythonCeleryCronQuickStart,
  65. platforms: new Set(['python-celery']),
  66. },
  67. php: {
  68. label: 'PHP',
  69. Guide: PHPCronQuickStart,
  70. platforms: new Set(['php', 'php-monolog', 'php-symfony']),
  71. },
  72. phpLaravel: {
  73. label: 'Laravel',
  74. Guide: PHPLaravelCronQuickStart,
  75. platforms: new Set(['php-laravel']),
  76. },
  77. nodeJs: {
  78. label: 'Node',
  79. Guide: NodeJSCronQuickStart,
  80. platforms: new Set(['node']),
  81. },
  82. go: {
  83. label: 'Go',
  84. Guide: GoCronQuickStart,
  85. platforms: new Set(['go']),
  86. },
  87. java: {
  88. label: 'Java',
  89. Guide: JavaCronQuickStart,
  90. platforms: new Set(['java', 'java-log4j2', 'java-logback']),
  91. },
  92. javaSpringBoot: {
  93. label: 'Spring',
  94. Guide: JavaSpringBootCronQuickStart,
  95. platforms: new Set(['java-spring-boot', 'java-spring']),
  96. },
  97. javaQuartz: {
  98. label: 'Quartz',
  99. Guide: JavaQuartzCronQuickStart,
  100. platforms: new Set([
  101. 'java',
  102. 'java-log4j2',
  103. 'java-logback',
  104. 'java-spring-boot',
  105. 'java-spring',
  106. ]),
  107. },
  108. ruby: {
  109. label: 'Ruby',
  110. Guide: RubyCronQuickStart,
  111. platforms: new Set(['ruby']),
  112. },
  113. rubyRails: {
  114. label: 'Rails',
  115. Guide: RubyRailsCronQuickStart,
  116. platforms: new Set(['ruby', 'ruby-rails']),
  117. },
  118. rubySidekiq: {
  119. label: 'Sidekiq',
  120. Guide: RubySidekiqCronQuickStart,
  121. platforms: new Set(['ruby', 'ruby-rails']),
  122. },
  123. };
  124. /**
  125. * The platforms that are well supported for crons
  126. */
  127. export const platformsWithGuides = Array.from(
  128. Object.values(onboardingGuides).reduce((combinedPlatforms, guide) => {
  129. guide.platforms?.forEach(platform => combinedPlatforms.add(platform));
  130. return combinedPlatforms;
  131. }, new Set())
  132. );
  133. const guideToSelectOption = ({key, label}) => ({label, value: key});
  134. export default function MonitorQuickStartGuide({monitor}: Props) {
  135. const org = useOrganization();
  136. const {data: projectKeys} = useApiQuery<Array<ProjectKey>>(
  137. [`/projects/${org.slug}/${monitor.project.slug}/keys/`],
  138. {staleTime: Infinity}
  139. );
  140. const guideList = Object.entries(onboardingGuides).map(([key, guide]) => ({
  141. ...guide,
  142. key,
  143. }));
  144. const [genericGuides, platformGuides] = partition(
  145. guideList,
  146. guide => guide.platforms === undefined
  147. );
  148. const exampleOptions = [
  149. {label: t('Platform Specfiic'), options: platformGuides.map(guideToSelectOption)},
  150. {label: t('Generic'), options: genericGuides.map(guideToSelectOption)},
  151. ];
  152. const platformSpecific = platformGuides.filter(guide =>
  153. guide.platforms?.has(monitor.project.platform ?? 'other')
  154. );
  155. const defaultExample = platformSpecific.length > 0 ? platformSpecific[0].key : 'cli';
  156. const [selectedGuide, setSelectedGuide] = useState(defaultExample);
  157. const {Guide} = onboardingGuides[selectedGuide];
  158. return (
  159. <Container>
  160. <CompactSelect
  161. options={exampleOptions}
  162. value={selectedGuide}
  163. onChange={({value}) => setSelectedGuide(value)}
  164. />
  165. <Guide
  166. slug={monitor.slug}
  167. orgSlug={org.slug}
  168. orgId={org.id}
  169. projectId={monitor.project.id}
  170. cronsUrl={projectKeys?.[0].dsn.crons}
  171. dsnKey={projectKeys?.[0].dsn.public}
  172. />
  173. </Container>
  174. );
  175. }
  176. const Container = styled('div')`
  177. display: flex;
  178. flex-direction: column;
  179. gap: ${space(2)};
  180. `;