projectPerformance.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import {Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import Access from 'sentry/components/acl/access';
  5. import Feature from 'sentry/components/acl/feature';
  6. import Button from 'sentry/components/button';
  7. import Form from 'sentry/components/forms/form';
  8. import JsonForm from 'sentry/components/forms/jsonForm';
  9. import {Field} from 'sentry/components/forms/types';
  10. import ExternalLink from 'sentry/components/links/externalLink';
  11. import LoadingIndicator from 'sentry/components/loadingIndicator';
  12. import {PanelItem} from 'sentry/components/panels';
  13. import {t, tct} from 'sentry/locale';
  14. import {Organization, Project, Scope} from 'sentry/types';
  15. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  16. import routeTitleGen from 'sentry/utils/routeTitle';
  17. import AsyncView from 'sentry/views/asyncView';
  18. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  19. import PermissionAlert from 'sentry/views/settings/project/permissionAlert';
  20. type RouteParams = {orgId: string; projectId: string};
  21. type Props = RouteComponentProps<{orgId: string; projectId: string}, {}> & {
  22. organization: Organization;
  23. project: Project;
  24. };
  25. type ProjectThreshold = {
  26. metric: string;
  27. threshold: string;
  28. editedBy?: string;
  29. id?: string;
  30. };
  31. type State = AsyncView['state'] & {
  32. threshold: ProjectThreshold;
  33. };
  34. class ProjectPerformance extends AsyncView<Props, State> {
  35. getTitle() {
  36. const {projectId} = this.props.params;
  37. return routeTitleGen(t('Performance'), projectId, false);
  38. }
  39. getProjectEndpoint({orgId, projectId}: RouteParams) {
  40. return `/projects/${orgId}/${projectId}/`;
  41. }
  42. getPerformanceIssuesEndpoint({orgId, projectId}: RouteParams) {
  43. return `/projects/${orgId}/${projectId}/performance-issues/configure/`;
  44. }
  45. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  46. const {params, organization} = this.props;
  47. const {projectId} = params;
  48. const endpoints: ReturnType<AsyncView['getEndpoints']> = [
  49. [
  50. 'threshold',
  51. `/projects/${organization.slug}/${projectId}/transaction-threshold/configure/`,
  52. ],
  53. ['project', `/projects/${organization.slug}/${projectId}/`],
  54. ];
  55. if (organization.features.includes('performance-issues-dev')) {
  56. const performanceIssuesEndpoint = [
  57. 'performance_issue_settings',
  58. `/projects/${organization.slug}/${projectId}/performance-issues/configure/`,
  59. ] as [string, string];
  60. endpoints.push(performanceIssuesEndpoint);
  61. }
  62. return endpoints;
  63. }
  64. handleDelete = () => {
  65. const {orgId, projectId} = this.props.params;
  66. const {organization} = this.props;
  67. this.setState({
  68. loading: true,
  69. });
  70. this.api.request(`/projects/${orgId}/${projectId}/transaction-threshold/configure/`, {
  71. method: 'DELETE',
  72. success: () => {
  73. trackAdvancedAnalyticsEvent(
  74. 'performance_views.project_transaction_threshold.clear',
  75. {organization}
  76. );
  77. },
  78. complete: () => this.fetchData(),
  79. });
  80. };
  81. getEmptyMessage() {
  82. return t('There is no threshold set for this project.');
  83. }
  84. renderLoading() {
  85. return (
  86. <LoadingIndicatorContainer>
  87. <LoadingIndicator />
  88. </LoadingIndicatorContainer>
  89. );
  90. }
  91. get formFields(): Field[] {
  92. const fields: Field[] = [
  93. {
  94. name: 'metric',
  95. type: 'select',
  96. label: t('Calculation Method'),
  97. options: [
  98. {value: 'duration', label: t('Transaction Duration')},
  99. {value: 'lcp', label: t('Largest Contentful Paint')},
  100. ],
  101. help: tct(
  102. 'This determines which duration is used to set your thresholds. By default, we use transaction duration which measures the entire length of the transaction. You can also set this to use a [link:Web Vital].',
  103. {
  104. link: (
  105. <ExternalLink href="https://docs.sentry.io/product/performance/web-vitals/" />
  106. ),
  107. }
  108. ),
  109. },
  110. {
  111. name: 'threshold',
  112. type: 'string',
  113. label: t('Response Time Threshold (ms)'),
  114. placeholder: t('300'),
  115. help: tct(
  116. 'Define what a satisfactory response time is based on the calculation method above. This will affect how your [link1:Apdex] and [link2:User Misery] thresholds are calculated. For example, misery will be 4x your satisfactory response time.',
  117. {
  118. link1: (
  119. <ExternalLink href="https://docs.sentry.io/performance-monitoring/performance/metrics/#apdex" />
  120. ),
  121. link2: (
  122. <ExternalLink href="https://docs.sentry.io/product/performance/metrics/#user-misery" />
  123. ),
  124. }
  125. ),
  126. },
  127. ];
  128. return fields;
  129. }
  130. get performanceIssueFormFields(): Field[] {
  131. return [
  132. {
  133. name: 'performanceIssueCreationRate',
  134. type: 'range',
  135. label: t('Performance Issue Creation Rate'),
  136. min: 0.0,
  137. max: 1.0,
  138. step: 0.01,
  139. defaultValue: 0,
  140. help: t(
  141. 'This determines the rate at which performance issues are created. A rate of 0.0 will disable performance issue creation.'
  142. ),
  143. },
  144. ];
  145. }
  146. get performanceIssueDetectorsFormFields(): Field[] {
  147. return [
  148. {
  149. name: 'n_plus_one_db_detection_rate',
  150. type: 'range',
  151. label: t('N+1 (DB) Detection Rate'),
  152. min: 0.0,
  153. max: 1.0,
  154. step: 0.01,
  155. defaultValue: 0,
  156. },
  157. {
  158. name: 'n_plus_one_db_issue_rate',
  159. type: 'range',
  160. label: t('N+1 (DB) Issue Rate'),
  161. min: 0.0,
  162. max: 1.0,
  163. step: 0.01,
  164. defaultValue: 0,
  165. },
  166. {
  167. name: 'n_plus_one_db_count',
  168. type: 'number',
  169. label: t('N+1 (DB) Minimum Count'),
  170. min: 0,
  171. max: 1000,
  172. defaultValue: 5,
  173. },
  174. {
  175. name: 'n_plus_one_db_duration_threshold',
  176. type: 'number',
  177. label: t('N+1 (DB) Duration Threshold'),
  178. min: 0,
  179. max: 1000000.0,
  180. defaultValue: 500,
  181. },
  182. {
  183. name: 'n_plus_one_api_calls_detection_rate',
  184. type: 'range',
  185. label: t('N+1 API Calls Detection Rate'),
  186. min: 0.0,
  187. max: 1.0,
  188. step: 0.01,
  189. defaultValue: 0,
  190. },
  191. ];
  192. }
  193. get initialData() {
  194. const {threshold} = this.state;
  195. return {
  196. threshold: threshold.threshold,
  197. metric: threshold.metric,
  198. };
  199. }
  200. renderBody() {
  201. const {organization, project, params} = this.props;
  202. const endpoint = `/projects/${organization.slug}/${project.slug}/transaction-threshold/configure/`;
  203. const requiredScopes: Scope[] = ['project:write'];
  204. const projectEndpoint = this.getProjectEndpoint(params);
  205. const performanceIssuesEndpoint = this.getPerformanceIssuesEndpoint(params);
  206. return (
  207. <Fragment>
  208. <SettingsPageHeader title={t('Performance')} />
  209. <PermissionAlert access={requiredScopes} />
  210. <Form
  211. saveOnBlur
  212. allowUndo
  213. initialData={this.initialData}
  214. apiMethod="POST"
  215. apiEndpoint={endpoint}
  216. onSubmitSuccess={resp => {
  217. const initial = this.initialData;
  218. const changedThreshold = initial.metric === resp.metric;
  219. trackAdvancedAnalyticsEvent(
  220. 'performance_views.project_transaction_threshold.change',
  221. {
  222. organization,
  223. from: changedThreshold ? initial.threshold : initial.metric,
  224. to: changedThreshold ? resp.threshold : resp.metric,
  225. key: changedThreshold ? 'threshold' : 'metric',
  226. }
  227. );
  228. this.setState({threshold: resp});
  229. }}
  230. >
  231. <Access access={requiredScopes}>
  232. {({hasAccess}) => (
  233. <JsonForm
  234. title={t('General')}
  235. fields={this.formFields}
  236. disabled={!hasAccess}
  237. renderFooter={() => (
  238. <Actions>
  239. <Button onClick={() => this.handleDelete()}>{t('Reset All')}</Button>
  240. </Actions>
  241. )}
  242. />
  243. )}
  244. </Access>
  245. </Form>
  246. <Feature features={['organizations:performance-issues-dev']}>
  247. <Fragment>
  248. <Form
  249. saveOnBlur
  250. allowUndo
  251. initialData={{
  252. performanceIssueCreationRate:
  253. this.state.project.performanceIssueCreationRate,
  254. }}
  255. apiMethod="PUT"
  256. apiEndpoint={projectEndpoint}
  257. >
  258. <Access access={requiredScopes}>
  259. {({hasAccess}) => (
  260. <JsonForm
  261. title={t('Performance Issues - All')}
  262. fields={this.performanceIssueFormFields}
  263. disabled={!hasAccess}
  264. />
  265. )}
  266. </Access>
  267. </Form>
  268. <Form
  269. saveOnBlur
  270. allowUndo
  271. initialData={this.state.performance_issue_settings}
  272. apiMethod="PUT"
  273. apiEndpoint={performanceIssuesEndpoint}
  274. >
  275. <Access access={requiredScopes}>
  276. {({hasAccess}) => (
  277. <JsonForm
  278. title={t('Performance Issues - Detector Settings')}
  279. fields={this.performanceIssueDetectorsFormFields}
  280. disabled={!hasAccess}
  281. />
  282. )}
  283. </Access>
  284. </Form>
  285. </Fragment>
  286. </Feature>
  287. </Fragment>
  288. );
  289. }
  290. }
  291. const Actions = styled(PanelItem)`
  292. justify-content: flex-end;
  293. `;
  294. const LoadingIndicatorContainer = styled('div')`
  295. margin: 18px 18px 0;
  296. `;
  297. export default ProjectPerformance;