metricsExtractionRuleEditModal.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import {Fragment, useCallback, useMemo} from 'react';
  2. import {css} from '@emotion/react';
  3. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  4. import {
  5. type ModalOptions,
  6. type ModalRenderProps,
  7. openModal,
  8. } from 'sentry/actionCreators/modal';
  9. import {t} from 'sentry/locale';
  10. import type {MetricsExtractionRule} from 'sentry/types/metrics';
  11. import type {Organization} from 'sentry/types/organization';
  12. import {trackAnalytics} from 'sentry/utils/analytics';
  13. import {useCardinalityLimitedMetricVolume} from 'sentry/utils/metrics/useCardinalityLimitedMetricVolume';
  14. import {
  15. aggregatesToGroups,
  16. createCondition as createExtractionCondition,
  17. explodeAggregateGroup,
  18. type FormData,
  19. MetricsExtractionRuleForm,
  20. } from 'sentry/views/settings/projectMetrics/metricsExtractionRuleForm';
  21. import {useUpdateMetricsExtractionRules} from 'sentry/views/settings/projectMetrics/utils/useMetricsExtractionRules';
  22. interface Props {
  23. /**
  24. * The extraction rule to edit
  25. */
  26. metricExtractionRule: MetricsExtractionRule;
  27. organization: Organization;
  28. /**
  29. * Source parameter for analytics
  30. */
  31. source: string;
  32. /**
  33. * Callback when the form is submitted successfully
  34. */
  35. onSubmitSuccess?: (data: FormData) => void;
  36. }
  37. export function MetricsExtractionRuleEditModal({
  38. Header,
  39. Body,
  40. closeModal,
  41. CloseButton,
  42. metricExtractionRule,
  43. organization,
  44. onSubmitSuccess: onSubmitSuccessProp,
  45. }: Props & ModalRenderProps) {
  46. const updateExtractionRuleMutation = useUpdateMetricsExtractionRules(
  47. organization.slug,
  48. metricExtractionRule.projectId
  49. );
  50. const {data: cardinality} = useCardinalityLimitedMetricVolume({
  51. projects: [metricExtractionRule.projectId],
  52. });
  53. const initialData: FormData = useMemo(() => {
  54. return {
  55. spanAttribute: metricExtractionRule.spanAttribute,
  56. unit: metricExtractionRule.unit,
  57. aggregates: aggregatesToGroups(metricExtractionRule.aggregates),
  58. tags: metricExtractionRule.tags,
  59. conditions: metricExtractionRule.conditions.length
  60. ? metricExtractionRule.conditions
  61. : [createExtractionCondition()],
  62. };
  63. }, [metricExtractionRule]);
  64. const handleSubmit = useCallback(
  65. (
  66. data: FormData,
  67. onSubmitSuccess: (data: FormData) => void,
  68. onSubmitError: (error: any) => void
  69. ) => {
  70. const extractionRule: MetricsExtractionRule = {
  71. ...metricExtractionRule,
  72. spanAttribute: data.spanAttribute!,
  73. tags: data.tags,
  74. aggregates: data.aggregates.flatMap(explodeAggregateGroup),
  75. unit: data.unit,
  76. conditions: data.conditions,
  77. };
  78. updateExtractionRuleMutation.mutate(
  79. {
  80. metricsExtractionRules: [extractionRule],
  81. },
  82. {
  83. onSuccess: () => {
  84. onSubmitSuccess(data);
  85. onSubmitSuccessProp?.(data);
  86. addSuccessMessage(t('Metric extraction rule updated'));
  87. closeModal();
  88. },
  89. onError: error => {
  90. const message = error?.responseJSON?.detail
  91. ? (error.responseJSON.detail as string)
  92. : t('Unable to save your changes.');
  93. onSubmitError(message);
  94. addErrorMessage(message);
  95. },
  96. }
  97. );
  98. onSubmitSuccess(data);
  99. },
  100. [closeModal, metricExtractionRule, onSubmitSuccessProp, updateExtractionRuleMutation]
  101. );
  102. return (
  103. <Fragment>
  104. <Header>
  105. <h4>{t('Edit Metric')}</h4>
  106. </Header>
  107. <CloseButton />
  108. <Body>
  109. <MetricsExtractionRuleForm
  110. initialData={initialData}
  111. projectId={metricExtractionRule.projectId}
  112. submitLabel={t('Update')}
  113. cancelLabel={t('Cancel')}
  114. onCancel={closeModal}
  115. onSubmit={handleSubmit}
  116. cardinality={cardinality}
  117. submitDisabled={updateExtractionRuleMutation.isLoading}
  118. isEdit
  119. requireChanges
  120. />
  121. </Body>
  122. </Fragment>
  123. );
  124. }
  125. export const modalCss = css`
  126. width: 100%;
  127. max-width: 900px;
  128. `;
  129. export function openExtractionRuleEditModal(props: Props, options?: ModalOptions) {
  130. const {organization, metricExtractionRule, source, onSubmitSuccess} = props;
  131. trackAnalytics('ddm.span-metric.edit.open', {
  132. organization,
  133. hasFilters: metricExtractionRule.conditions.some(condition => condition.value),
  134. source,
  135. });
  136. const handleClose: ModalOptions['onClose'] = reason => {
  137. if (reason && ['close-button', 'backdrop-click', 'escape-key'].includes(reason)) {
  138. trackAnalytics('ddm.span-metric.edit.cancel', {organization});
  139. }
  140. options?.onClose?.(reason);
  141. };
  142. const handleSubmitSuccess: Props['onSubmitSuccess'] = data => {
  143. trackAnalytics('ddm.span-metric.edit.success', {
  144. organization,
  145. hasFilters: data.conditions.some(condition => condition.value),
  146. });
  147. onSubmitSuccess?.(data);
  148. };
  149. openModal(
  150. modalProps => (
  151. <MetricsExtractionRuleEditModal
  152. {...props}
  153. onSubmitSuccess={handleSubmitSuccess}
  154. {...modalProps}
  155. />
  156. ),
  157. {
  158. modalCss,
  159. ...options,
  160. onClose: handleClose,
  161. }
  162. );
  163. }