transactionThresholdButton.tsx 4.5 KB

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