transactionThresholdModal.tsx 8.6 KB

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