transactionThresholdModal.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import {Component, Fragment} from 'react';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import cloneDeep from 'lodash/cloneDeep';
  5. import set from 'lodash/set';
  6. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  7. import type {ModalRenderProps} from 'sentry/actionCreators/modal';
  8. import type {Client} from 'sentry/api';
  9. import {Button} from 'sentry/components/button';
  10. import ButtonBar from 'sentry/components/buttonBar';
  11. import SelectControl from 'sentry/components/forms/controls/selectControl';
  12. import FieldGroup from 'sentry/components/forms/fieldGroup';
  13. import Input from 'sentry/components/input';
  14. import Link from 'sentry/components/links/link';
  15. import {t, tct} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import type {Organization} from 'sentry/types/organization';
  18. import type {Project} from 'sentry/types/project';
  19. import {defined} from 'sentry/utils';
  20. import type EventView from 'sentry/utils/discover/eventView';
  21. import withApi from 'sentry/utils/withApi';
  22. import withProjects from 'sentry/utils/withProjects';
  23. import {transactionSummaryRouteWithQuery} from './utils';
  24. export enum TransactionThresholdMetric {
  25. TRANSACTION_DURATION = 'duration',
  26. LARGEST_CONTENTFUL_PAINT = 'lcp',
  27. }
  28. export const METRIC_CHOICES = [
  29. {label: t('Transaction Duration'), value: 'duration'},
  30. {label: t('Largest Contentful Paint'), value: 'lcp'},
  31. ];
  32. type Props = {
  33. api: Client;
  34. eventView: EventView;
  35. organization: Organization;
  36. projects: Project[];
  37. transactionName: string;
  38. transactionThreshold: number | undefined;
  39. transactionThresholdMetric: TransactionThresholdMetric | undefined;
  40. onApply?: (threshold, metric) => void;
  41. project?: string;
  42. } & ModalRenderProps;
  43. type State = {
  44. error: string | null;
  45. metric: TransactionThresholdMetric | undefined;
  46. threshold: number | undefined;
  47. };
  48. class TransactionThresholdModal extends Component<Props, State> {
  49. state: State = {
  50. threshold: this.props.transactionThreshold,
  51. metric: this.props.transactionThresholdMetric,
  52. error: null,
  53. };
  54. getProject() {
  55. const {projects, eventView, project} = this.props;
  56. if (defined(project)) {
  57. return projects.find(proj => proj.id === project);
  58. }
  59. const projectId = String(eventView.project[0]);
  60. return projects.find(proj => proj.id === projectId);
  61. }
  62. handleApply = (event: React.FormEvent) => {
  63. event.preventDefault();
  64. const {api, closeModal, organization, transactionName, onApply} = this.props;
  65. const project = this.getProject();
  66. if (!defined(project)) {
  67. return;
  68. }
  69. const transactionThresholdUrl = `/organizations/${organization.slug}/project-transaction-threshold-override/`;
  70. api
  71. .requestPromise(transactionThresholdUrl, {
  72. method: 'POST',
  73. includeAllArgs: true,
  74. query: {
  75. project: project.id,
  76. },
  77. data: {
  78. transaction: transactionName,
  79. threshold: this.state.threshold,
  80. metric: this.state.metric,
  81. },
  82. })
  83. .then(() => {
  84. closeModal();
  85. if (onApply) {
  86. onApply(this.state.threshold, this.state.metric);
  87. }
  88. })
  89. .catch(err => {
  90. this.setState({
  91. error: err,
  92. });
  93. const errorMessage =
  94. err.responseJSON?.threshold ?? err.responseJSON?.non_field_errors ?? null;
  95. addErrorMessage(errorMessage);
  96. });
  97. };
  98. handleFieldChange = (field: string) => (value: string) => {
  99. this.setState(prevState => {
  100. const newState = cloneDeep(prevState);
  101. set(newState, field, value);
  102. return {...newState, errors: undefined};
  103. });
  104. };
  105. handleReset = (event: React.FormEvent) => {
  106. event.preventDefault();
  107. const {api, closeModal, organization, transactionName, onApply} = this.props;
  108. const project = this.getProject();
  109. if (!defined(project)) {
  110. return;
  111. }
  112. const transactionThresholdUrl = `/organizations/${organization.slug}/project-transaction-threshold-override/`;
  113. api
  114. .requestPromise(transactionThresholdUrl, {
  115. method: 'DELETE',
  116. includeAllArgs: true,
  117. query: {
  118. project: project.id,
  119. },
  120. data: {
  121. transaction: transactionName,
  122. },
  123. })
  124. .then(() => {
  125. const projectThresholdUrl = `/projects/${organization.slug}/${project.slug}/transaction-threshold/configure/`;
  126. this.props.api
  127. .requestPromise(projectThresholdUrl, {
  128. method: 'GET',
  129. includeAllArgs: true,
  130. query: {
  131. project: project.id,
  132. },
  133. })
  134. .then(([data]) => {
  135. this.setState({
  136. threshold: data.threshold,
  137. metric: data.metric,
  138. });
  139. closeModal();
  140. if (onApply) {
  141. onApply(this.state.threshold, this.state.metric);
  142. }
  143. })
  144. .catch(err => {
  145. const errorMessage = err.responseJSON?.threshold ?? null;
  146. addErrorMessage(errorMessage);
  147. });
  148. })
  149. .catch(err => {
  150. this.setState({
  151. error: err,
  152. });
  153. });
  154. };
  155. renderModalFields() {
  156. return (
  157. <Fragment>
  158. <FieldGroup
  159. data-test-id="response-metric"
  160. label={t('Calculation Method')}
  161. inline={false}
  162. help={t(
  163. 'This determines which duration metric is used for the Response Time Threshold.'
  164. )}
  165. showHelpInTooltip
  166. flexibleControlStateSize
  167. stacked
  168. required
  169. >
  170. <SelectControl
  171. required
  172. options={METRIC_CHOICES.slice()}
  173. name="responseMetric"
  174. label={t('Calculation Method')}
  175. value={this.state.metric}
  176. onChange={(option: {label: string; value: string}) => {
  177. this.handleFieldChange('metric')(option.value);
  178. }}
  179. />
  180. </FieldGroup>
  181. <FieldGroup
  182. data-test-id="response-time-threshold"
  183. label={t('Response Time Threshold (ms)')}
  184. inline={false}
  185. help={t(
  186. 'The satisfactory response time for the calculation method defined above. This is used to calculate Apdex and User Misery scores.'
  187. )}
  188. showHelpInTooltip
  189. flexibleControlStateSize
  190. stacked
  191. required
  192. >
  193. <Input
  194. type="number"
  195. name="threshold"
  196. required
  197. pattern="[0-9]*(\.[0-9]*)?"
  198. onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
  199. this.handleFieldChange('threshold')(event.target.value);
  200. }}
  201. value={this.state.threshold}
  202. step={100}
  203. min={100}
  204. />
  205. </FieldGroup>
  206. </Fragment>
  207. );
  208. }
  209. render() {
  210. const {Header, Body, Footer, organization, transactionName, eventView} = this.props;
  211. const project = this.getProject();
  212. const summaryView = eventView.clone();
  213. summaryView.query = summaryView.getQueryWithAdditionalConditions();
  214. const target = transactionSummaryRouteWithQuery({
  215. orgSlug: organization.slug,
  216. transaction: transactionName,
  217. query: summaryView.generateQueryStringObject(),
  218. projectID: project?.id,
  219. });
  220. return (
  221. <Fragment>
  222. <Header closeButton>
  223. <h4>{t('Transaction Settings')}</h4>
  224. </Header>
  225. <Body>
  226. <Instruction>
  227. {tct(
  228. 'The changes below will only be applied to [transaction]. To set it at a more global level, go to [projectSettings: Project Settings].',
  229. {
  230. transaction: <Link to={target}>{transactionName}</Link>,
  231. projectSettings: (
  232. <Link
  233. to={`/settings/${organization.slug}/projects/${project?.slug}/performance/`}
  234. />
  235. ),
  236. }
  237. )}
  238. </Instruction>
  239. {this.renderModalFields()}
  240. </Body>
  241. <Footer>
  242. <ButtonBar gap={1}>
  243. <Button
  244. priority="default"
  245. onClick={this.handleReset}
  246. data-test-id="reset-all"
  247. >
  248. {t('Reset All')}
  249. </Button>
  250. <Button
  251. aria-label={t('Apply')}
  252. priority="primary"
  253. onClick={this.handleApply}
  254. data-test-id="apply-threshold"
  255. >
  256. {t('Apply')}
  257. </Button>
  258. </ButtonBar>
  259. </Footer>
  260. </Fragment>
  261. );
  262. }
  263. }
  264. const Instruction = styled('div')`
  265. margin-bottom: ${space(4)};
  266. `;
  267. export const modalCss = css`
  268. width: 100%;
  269. max-width: 650px;
  270. `;
  271. export default withApi(withProjects(TransactionThresholdModal));