import {Fragment, useMemo, useState} from 'react'; import {Link} from 'react-router'; import styled from '@emotion/styled'; import Tag from 'sentry/components/badge/tag'; import {PanelTable} from 'sentry/components/panels/panelTable'; import SearchBar from 'sentry/components/searchBar'; import {TabList, TabPanels, Tabs} from 'sentry/components/tabs'; import {Tooltip} from 'sentry/components/tooltip'; import {IconArrow} from 'sentry/icons'; import {IconWarning} from 'sentry/icons/iconWarning'; import {t, tct} from 'sentry/locale'; import {space} from 'sentry/styles/space'; import type {MetricMeta} from 'sentry/types/metrics'; import type {Project} from 'sentry/types/project'; import {DEFAULT_METRICS_CARDINALITY_LIMIT} from 'sentry/utils/metrics/constants'; import {getReadableMetricType} from 'sentry/utils/metrics/formatters'; import {formatMRI} from 'sentry/utils/metrics/mri'; import {useBlockMetric} from 'sentry/utils/metrics/useBlockMetric'; import {useMetricsCardinality} from 'sentry/utils/metrics/useMetricsCardinality'; import {useMetricsMeta} from 'sentry/utils/metrics/useMetricsMeta'; import {middleEllipsis} from 'sentry/utils/string/middleEllipsis'; import {useAccess} from 'sentry/views/settings/projectMetrics/access'; import {BlockButton} from 'sentry/views/settings/projectMetrics/blockButton'; import {useSearchQueryParam} from 'sentry/views/settings/projectMetrics/utils/useSearchQueryParam'; type Props = { project: Project; }; enum BlockingStatusTab { ACTIVE = 'active', DISABLED = 'disabled', } type MetricWithCardinality = MetricMeta & {cardinality: number}; export function CustomMetricsTable({project}: Props) { const [selectedTab, setSelectedTab] = useState(BlockingStatusTab.ACTIVE); const [query, setQuery] = useSearchQueryParam('metricsQuery'); const metricsMeta = useMetricsMeta( {projects: [parseInt(project.id, 10)]}, ['custom'], false ); const metricsCardinality = useMetricsCardinality({ project, }); const isLoading = metricsMeta.isLoading || metricsCardinality.isLoading; const sortedMeta = useMemo(() => { if (!metricsMeta.data) { return []; } if (!metricsCardinality.data) { return metricsMeta.data.map(meta => ({...meta, cardinality: 0})); } return metricsMeta.data .map(({mri, ...rest}) => { return { mri, cardinality: metricsCardinality.data[mri] ?? 0, ...rest, }; }) .sort((a, b) => { return b.cardinality - a.cardinality; }) as MetricWithCardinality[]; }, [metricsCardinality.data, metricsMeta.data]); const metrics = sortedMeta.filter( ({mri, type, unit}) => mri.includes(query) || getReadableMetricType(type).includes(query) || unit.includes(query) ); return (
{t('Emitted Metrics')}
{t('Active')} {t('Disabled')} !blockingStatus[0]?.isBlocked )} isLoading={isLoading} query={query} project={project} /> blockingStatus[0]?.isBlocked)} isLoading={isLoading} query={query} project={project} />
); } interface MetricsTableProps { isLoading: boolean; metrics: MetricWithCardinality[]; project: Project; query: string; } function MetricsTable({metrics, isLoading, query, project}: MetricsTableProps) { const blockMetricMutation = useBlockMetric(project); const {hasAccess} = useAccess({access: ['project:write'], project}); const cardinalityLimit = project.relayCustomMetricCardinalityLimit ?? DEFAULT_METRICS_CARDINALITY_LIMIT; return ( {t('Cardinality')} , {t('Type')} , {t('Unit')} , {t('Actions')} , ]} emptyMessage={ query ? t('No metrics match the query.') : t('There are no custom metrics to display.') } isEmpty={metrics.length === 0} isLoading={isLoading} > {metrics.map(({mri, type, unit, cardinality, blockingStatus}) => { const isBlocked = blockingStatus[0]?.isBlocked; const isCardinalityLimited = cardinality >= cardinalityLimit; return ( {middleEllipsis(formatMRI(mri), 65, /\.|-|_/)} {isCardinalityLimited && ( )} {cardinality} {getReadableMetricType(type)} {unit} { blockMetricMutation.mutate({ mri, operationType: isBlocked ? 'unblockMetric' : 'blockMetric', }); }} /> ); })} ); } const SearchWrapper = styled('div')` display: flex; justify-content: space-between; align-items: flex-start; margin-top: ${space(4)}; margin-bottom: ${space(0)}; & > h6 { margin: 0; } `; const MetricsPanelTable = styled(PanelTable)` margin-top: ${space(2)}; grid-template-columns: 1fr repeat(4, min-content); `; const Cell = styled('div')<{right?: boolean}>` display: flex; align-items: center; align-self: stretch; gap: ${space(0.5)}; justify-content: ${p => (p.right ? 'flex-end' : 'flex-start')}; `; const StyledIconWarning = styled(IconWarning)` margin-top: ${space(0.5)}; &:hover { cursor: pointer; } `;