metricsExtractionRulesTable.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import {Fragment, useCallback, useEffect, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  4. import {openModal} from 'sentry/actionCreators/modal';
  5. import {Button, LinkButton} from 'sentry/components/button';
  6. import {openConfirmModal} from 'sentry/components/confirm';
  7. import {modalCss} from 'sentry/components/featureFeedback/feedbackModal';
  8. import {PanelTable} from 'sentry/components/panels/panelTable';
  9. import SearchBar from 'sentry/components/searchBar';
  10. import {IconArrow} from 'sentry/icons/iconArrow';
  11. import {IconDelete} from 'sentry/icons/iconDelete';
  12. import {IconEdit} from 'sentry/icons/iconEdit';
  13. import {t} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import type {MetricsExtractionRule} from 'sentry/types/metrics';
  16. import type {Project} from 'sentry/types/project';
  17. import {useLocation} from 'sentry/utils/useLocation';
  18. import {useNavigate} from 'sentry/utils/useNavigate';
  19. import useOrganization from 'sentry/utils/useOrganization';
  20. import {useParams} from 'sentry/utils/useParams';
  21. import {MetricsExtractionRuleEditModal} from 'sentry/views/settings/projectMetrics/metricsExtractionRuleEditModal';
  22. import {
  23. useDeleteMetricsExtractionRules,
  24. useMetricsExtractionRules,
  25. } from 'sentry/views/settings/projectMetrics/utils/api';
  26. import {useSearchQueryParam} from 'sentry/views/settings/projectMetrics/utils/useSearchQueryParam';
  27. type Props = {
  28. project: Project;
  29. };
  30. export function MetricsExtractionRulesTable({project}: Props) {
  31. const organization = useOrganization();
  32. const location = useLocation();
  33. const params = useParams();
  34. const navigate = useNavigate();
  35. const [query, setQuery] = useSearchQueryParam('query');
  36. const {data: extractionRules, isLoading} = useMetricsExtractionRules(
  37. organization.slug,
  38. project.slug
  39. );
  40. const {mutate: deleteMetricsExtractionRules} = useDeleteMetricsExtractionRules(
  41. organization.slug,
  42. project.slug
  43. );
  44. const filteredExtractionRules = useMemo(() => {
  45. return (extractionRules || []).filter(rule =>
  46. rule.spanAttribute.toLowerCase().includes(query.toLowerCase())
  47. );
  48. }, [extractionRules, query]);
  49. const handleDelete = useCallback(
  50. (rule: MetricsExtractionRule) => {
  51. openConfirmModal({
  52. onConfirm: () =>
  53. deleteMetricsExtractionRules(
  54. {metricsExtractionRules: [rule]},
  55. {
  56. onSuccess: () => {
  57. addSuccessMessage(t('Metric extraction rule deleted'));
  58. },
  59. onError: () => {
  60. addErrorMessage(t('Failed to delete metric extraction rule'));
  61. },
  62. }
  63. ),
  64. message: t('Are you sure you want to delete this extraction rule?'),
  65. confirmText: t('Delete Extraction Rule'),
  66. });
  67. },
  68. [deleteMetricsExtractionRules]
  69. );
  70. const handleEdit = useCallback(
  71. (rule: MetricsExtractionRule) => {
  72. navigate(`/settings/projects/${project.slug}/metrics/${rule.spanAttribute}/edit/`);
  73. },
  74. [project.slug, navigate]
  75. );
  76. useEffect(() => {
  77. const editPath = `/settings/projects/${project.slug}/metrics/${params.spanAttribute}/edit/`;
  78. if (location.pathname !== editPath) {
  79. return;
  80. }
  81. const rule = filteredExtractionRules.find(
  82. r => r.spanAttribute === params.spanAttribute
  83. );
  84. if (!rule) {
  85. return;
  86. }
  87. openModal(
  88. props => (
  89. <MetricsExtractionRuleEditModal
  90. project={project}
  91. metricExtractionRule={rule}
  92. {...props}
  93. />
  94. ),
  95. {
  96. modalCss,
  97. onClose: () => navigate(`/settings/projects/${project.slug}/metrics/`),
  98. }
  99. );
  100. }, [
  101. filteredExtractionRules,
  102. project,
  103. location.pathname,
  104. params.spanAttribute,
  105. navigate,
  106. ]);
  107. return (
  108. <Fragment>
  109. <SearchWrapper>
  110. <h6>{t('Span-based Metrics')}</h6>
  111. <FlexSpacer />
  112. <SearchBar
  113. placeholder={t('Search Metrics')}
  114. onChange={setQuery}
  115. query={query}
  116. size="sm"
  117. />
  118. <LinkButton
  119. to={`/settings/projects/${project.slug}/metrics/extract-metric`}
  120. priority="primary"
  121. size="sm"
  122. >
  123. {t('Add Metric')}
  124. </LinkButton>
  125. </SearchWrapper>
  126. <RulesTable
  127. isLoading={isLoading}
  128. onDelete={handleDelete}
  129. onEdit={handleEdit}
  130. extractionRules={filteredExtractionRules}
  131. hasSearch={!!query}
  132. />
  133. </Fragment>
  134. );
  135. }
  136. interface RulesTableProps {
  137. extractionRules: MetricsExtractionRule[];
  138. hasSearch: boolean;
  139. isLoading: boolean;
  140. onDelete: (rule: MetricsExtractionRule) => void;
  141. onEdit: (rule: MetricsExtractionRule) => void;
  142. }
  143. function RulesTable({
  144. extractionRules,
  145. isLoading,
  146. onDelete,
  147. onEdit,
  148. hasSearch,
  149. }: RulesTableProps) {
  150. return (
  151. <ExtractionRulesPanelTable
  152. headers={[
  153. <Cell key="spanAttribute">
  154. <IconArrow size="xs" direction="down" />
  155. {t('Span attribute')}
  156. </Cell>,
  157. <Cell right key="filters">
  158. {t('Filters')}
  159. </Cell>,
  160. <Cell right key="actions">
  161. {t('Actions')}
  162. </Cell>,
  163. ]}
  164. emptyMessage={
  165. hasSearch
  166. ? t('No metrics match the query.')
  167. : t('You have not created any span-based metrics yet.')
  168. }
  169. isEmpty={extractionRules.length === 0}
  170. isLoading={isLoading}
  171. >
  172. {extractionRules
  173. .toSorted((a, b) => a?.spanAttribute?.localeCompare(b?.spanAttribute))
  174. .map(rule => (
  175. <Fragment key={rule.spanAttribute + rule.unit}>
  176. <Cell>{rule.spanAttribute}</Cell>
  177. <Cell right>
  178. {rule.conditions.length ? (
  179. <Button priority="link" onClick={() => onEdit(rule)}>
  180. {rule.conditions.length}
  181. </Button>
  182. ) : (
  183. <NoValue>{t('(none)')}</NoValue>
  184. )}
  185. </Cell>
  186. <Cell right>
  187. <Button
  188. aria-label={t('Delete metric')}
  189. size="xs"
  190. icon={<IconDelete />}
  191. borderless
  192. onClick={() => onDelete(rule)}
  193. />
  194. <Button
  195. aria-label={t('Edit metric')}
  196. size="xs"
  197. icon={<IconEdit />}
  198. borderless
  199. onClick={() => onEdit(rule)}
  200. />
  201. </Cell>
  202. </Fragment>
  203. ))}
  204. </ExtractionRulesPanelTable>
  205. );
  206. }
  207. const SearchWrapper = styled('div')`
  208. display: flex;
  209. align-items: flex-start;
  210. margin-top: ${space(4)};
  211. margin-bottom: ${space(1)};
  212. gap: ${space(1)};
  213. & > h6 {
  214. margin: 0;
  215. }
  216. `;
  217. const FlexSpacer = styled('div')`
  218. flex: 1;
  219. `;
  220. const ExtractionRulesPanelTable = styled(PanelTable)`
  221. grid-template-columns: 1fr repeat(2, min-content);
  222. `;
  223. const Cell = styled('div')<{right?: boolean}>`
  224. display: flex;
  225. align-items: center;
  226. align-self: stretch;
  227. gap: ${space(0.5)};
  228. justify-content: ${p => (p.right ? 'flex-end' : 'flex-start')};
  229. `;
  230. const NoValue = styled('span')`
  231. color: ${p => p.theme.subText};
  232. `;