metricsExtractionRulesTable.tsx 6.2 KB

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