transactionThresholdButton.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import {Component} from 'react';
  2. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  3. import {openModal} from 'sentry/actionCreators/modal';
  4. import {Client} from 'sentry/api';
  5. import {Button} from 'sentry/components/button';
  6. import {IconSettings} from 'sentry/icons';
  7. import {t} from 'sentry/locale';
  8. import {Organization, Project} from 'sentry/types';
  9. import {defined} from 'sentry/utils';
  10. import EventView from 'sentry/utils/discover/eventView';
  11. import withApi from 'sentry/utils/withApi';
  12. import withProjects from 'sentry/utils/withProjects';
  13. import TransactionThresholdModal, {
  14. modalCss,
  15. TransactionThresholdMetric,
  16. } from './transactionThresholdModal';
  17. type Props = {
  18. api: Client;
  19. eventView: EventView;
  20. organization: Organization;
  21. projects: Project[];
  22. transactionName: string;
  23. onChangeThreshold?: (threshold: number, metric: TransactionThresholdMetric) => void;
  24. };
  25. type State = {
  26. loadingThreshold: boolean;
  27. transactionThreshold: number | undefined;
  28. transactionThresholdMetric: TransactionThresholdMetric | undefined;
  29. };
  30. class TransactionThresholdButton extends Component<Props, State> {
  31. state: State = {
  32. transactionThreshold: undefined,
  33. transactionThresholdMetric: undefined,
  34. loadingThreshold: false,
  35. };
  36. componentDidMount() {
  37. this.fetchTransactionThreshold();
  38. }
  39. getProject() {
  40. const {projects, eventView} = this.props;
  41. if (!defined(eventView)) {
  42. return undefined;
  43. }
  44. const projectId = String(eventView.project[0]);
  45. const project = projects.find(proj => proj.id === projectId);
  46. return project;
  47. }
  48. fetchTransactionThreshold = () => {
  49. const {api, organization, transactionName} = this.props;
  50. const project = this.getProject();
  51. if (!defined(project)) {
  52. return;
  53. }
  54. const transactionThresholdUrl = `/organizations/${organization.slug}/project-transaction-threshold-override/`;
  55. this.setState({loadingThreshold: true});
  56. api
  57. .requestPromise(transactionThresholdUrl, {
  58. method: 'GET',
  59. includeAllArgs: true,
  60. query: {
  61. project: project.id,
  62. transaction: transactionName,
  63. },
  64. })
  65. .then(([data]) => {
  66. this.setState({
  67. loadingThreshold: false,
  68. transactionThreshold: data.threshold,
  69. transactionThresholdMetric: data.metric,
  70. });
  71. })
  72. .catch(() => {
  73. const projectThresholdUrl = `/projects/${organization.slug}/${project.slug}/transaction-threshold/configure/`;
  74. this.props.api
  75. .requestPromise(projectThresholdUrl, {
  76. method: 'GET',
  77. includeAllArgs: true,
  78. query: {
  79. project: project.id,
  80. },
  81. })
  82. .then(([data]) => {
  83. this.setState({
  84. loadingThreshold: false,
  85. transactionThreshold: data.threshold,
  86. transactionThresholdMetric: data.metric,
  87. });
  88. })
  89. .catch(err => {
  90. this.setState({loadingThreshold: false});
  91. const errorMessage = err.responseJSON?.threshold ?? null;
  92. addErrorMessage(errorMessage);
  93. });
  94. });
  95. };
  96. onChangeThreshold(threshold: number, metric: TransactionThresholdMetric) {
  97. const {onChangeThreshold} = this.props;
  98. this.setState({
  99. transactionThreshold: threshold,
  100. transactionThresholdMetric: metric,
  101. });
  102. if (defined(onChangeThreshold)) {
  103. onChangeThreshold(threshold, metric);
  104. }
  105. }
  106. openModal() {
  107. const {organization, transactionName, eventView} = this.props;
  108. const {transactionThreshold, transactionThresholdMetric} = this.state;
  109. openModal(
  110. modalProps => (
  111. <TransactionThresholdModal
  112. {...modalProps}
  113. organization={organization}
  114. transactionName={transactionName}
  115. eventView={eventView}
  116. transactionThreshold={transactionThreshold}
  117. transactionThresholdMetric={transactionThresholdMetric}
  118. onApply={(threshold, metric) => this.onChangeThreshold(threshold, metric)}
  119. />
  120. ),
  121. {modalCss, closeEvents: 'escape-key'}
  122. );
  123. }
  124. render() {
  125. const {loadingThreshold} = this.state;
  126. return (
  127. <Button
  128. size="sm"
  129. onClick={() => this.openModal()}
  130. icon={<IconSettings />}
  131. disabled={loadingThreshold}
  132. aria-label={t('Settings')}
  133. data-test-id="set-transaction-threshold"
  134. />
  135. );
  136. }
  137. }
  138. export default withApi(withProjects(TransactionThresholdButton));