123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- import {Fragment, useCallback, useEffect, useMemo, useRef, useState} from 'react';
- import {css} from '@emotion/react';
- import styled from '@emotion/styled';
- import partition from 'lodash/partition';
- import {Button} from 'sentry/components/button';
- import FieldGroup from 'sentry/components/forms/fieldGroup';
- import LoadingIndicator from 'sentry/components/loadingIndicator';
- import Panel from 'sentry/components/panels/panel';
- import {Tooltip} from 'sentry/components/tooltip';
- import {t} from 'sentry/locale';
- import {space} from 'sentry/styles/space';
- import useProjects from 'sentry/utils/useProjects';
- import {PercentInput} from 'sentry/views/settings/dynamicSampling/percentInput';
- import {ProjectsTable} from 'sentry/views/settings/dynamicSampling/projectsTable';
- import {SamplingBreakdown} from 'sentry/views/settings/dynamicSampling/samplingBreakdown';
- import {useHasDynamicSamplingWriteAccess} from 'sentry/views/settings/dynamicSampling/utils/access';
- import {formatPercent} from 'sentry/views/settings/dynamicSampling/utils/formatPercent';
- import {parsePercent} from 'sentry/views/settings/dynamicSampling/utils/parsePercent';
- import {projectSamplingForm} from 'sentry/views/settings/dynamicSampling/utils/projectSamplingForm';
- import {scaleSampleRates} from 'sentry/views/settings/dynamicSampling/utils/scaleSampleRates';
- import type {ProjectSampleCount} from 'sentry/views/settings/dynamicSampling/utils/useProjectSampleCounts';
- interface Props {
- editMode: 'single' | 'bulk';
- isLoading: boolean;
- onEditModeChange: (mode: 'single' | 'bulk') => void;
- sampleCounts: ProjectSampleCount[];
- }
- const {useFormField} = projectSamplingForm;
- const EMPTY_ARRAY = [];
- export function ProjectsEditTable({
- isLoading: isLoadingProp,
- sampleCounts,
- editMode,
- onEditModeChange,
- }: Props) {
- const {projects, fetching} = useProjects();
- const hasAccess = useHasDynamicSamplingWriteAccess();
- const {value, initialValue, error, onChange} = useFormField('projectRates');
- const [isBulkEditEnabled, setIsBulkEditEnabled] = useState(false);
- const inputRef = useRef<HTMLInputElement>(null);
- const [orgRate, setOrgRate] = useState<string>('');
- const projectRateSnapshotRef = useRef<Record<string, string>>({});
- const dataByProjectId = useMemo(
- () =>
- sampleCounts.reduce(
- (acc, item) => {
- acc[item.project.id] = item;
- return acc;
- },
- {} as Record<string, (typeof sampleCounts)[0]>
- ),
- [sampleCounts]
- );
- useEffect(() => {
- if (isBulkEditEnabled) {
- inputRef.current?.focus();
- }
- }, [isBulkEditEnabled]);
- const handleProjectChange = useCallback(
- (projectId: string, newRate: string) => {
- onChange(prev => ({
- ...prev,
- [projectId]: newRate,
- }));
- onEditModeChange('single');
- },
- [onChange, onEditModeChange]
- );
- const handleOrgChange = useCallback(
- (event: React.ChangeEvent<HTMLInputElement>) => {
- const newRate = event.target.value;
- if (editMode === 'single') {
- projectRateSnapshotRef.current = value;
- }
- const cappedOrgRate = parsePercent(newRate, 1);
- const scalingItems = Object.entries(projectRateSnapshotRef.current)
- .map(([projectId, rate]) => ({
- id: projectId,
- sampleRate: rate ? parsePercent(rate) : 0,
- count: dataByProjectId[projectId]?.count ?? 0,
- }))
- // We do not wan't to bulk edit inactive projects as they have no effect on the outcome
- .filter(item => item.count !== 0);
- const {scaledItems} = scaleSampleRates({
- items: scalingItems,
- sampleRate: cappedOrgRate,
- });
- const newProjectValues = scaledItems.reduce((acc, item) => {
- acc[item.id] = formatPercent(item.sampleRate);
- return acc;
- }, {});
- onChange(prev => {
- return {...prev, ...newProjectValues};
- });
- setOrgRate(newRate);
- onEditModeChange('bulk');
- },
- [dataByProjectId, editMode, onChange, onEditModeChange, value]
- );
- const handleOrgBlur = useCallback(() => {
- setIsBulkEditEnabled(false);
- // Parse to ensure valid values
- setOrgRate(rate => (parsePercent(rate, 1) * 100).toString());
- }, []);
- const items = useMemo(
- () =>
- projects.map(project => {
- const item = dataByProjectId[project.id] as
- | (typeof dataByProjectId)[string]
- | undefined;
- return {
- id: project.slug,
- name: project.slug,
- count: item?.count || 0,
- ownCount: item?.ownCount || 0,
- subProjects: item?.subProjects ?? EMPTY_ARRAY,
- project: project,
- initialSampleRate: initialValue[project.id],
- sampleRate: value[project.id],
- error: error?.[project.id],
- };
- }),
- [dataByProjectId, error, initialValue, projects, value]
- );
- const totalSpanCount = useMemo(
- () => items.reduce((acc, item) => acc + item.count, 0),
- [items]
- );
- const projectedOrgRate = useMemo(() => {
- if (editMode === 'bulk') {
- return orgRate;
- }
- const totalSampledSpans = items.reduce(
- (acc, item) => acc + item.count * parsePercent(value[item.project.id], 1),
- 0
- );
- return formatPercent(totalSampledSpans / totalSpanCount);
- }, [editMode, items, orgRate, totalSpanCount, value]);
- const initialOrgRate = useMemo(() => {
- const totalSampledSpans = items.reduce(
- (acc, item) => acc + item.count * parsePercent(initialValue[item.project.id], 1),
- 0
- );
- return formatPercent(totalSampledSpans / totalSpanCount);
- }, [initialValue, items, totalSpanCount]);
- const breakdownSampleRates = useMemo(
- () =>
- Object.entries(value).reduce(
- (acc, [projectId, rate]) => {
- acc[projectId] = parsePercent(rate);
- return acc;
- },
- {} as Record<string, number>
- ),
- [value]
- );
- const [activeItems, inactiveItems] = useMemo(
- () => partition(items, item => item.count > 0 || item.initialSampleRate !== '100'),
- [items]
- );
- const isLoading = fetching || isLoadingProp;
- return (
- <Fragment>
- <BreakdownPanel>
- {isLoading ? (
- <LoadingIndicator
- css={css`
- margin: 60px 0;
- `}
- />
- ) : (
- <Fragment>
- <BreakdownWrapper>
- <SamplingBreakdown
- sampleCounts={sampleCounts}
- sampleRates={breakdownSampleRates}
- />
- </BreakdownWrapper>
- <FieldGroup
- label={t('Estimated Organization Rate')}
- help={t('An estimate of the combined sample rate for all projects.')}
- flexibleControlStateSize
- alignRight
- >
- <InputWrapper>
- <Tooltip
- disabled={hasAccess}
- title={t('You do not have permission to change the sample rate')}
- >
- <PercentInput
- type="number"
- ref={inputRef}
- disabled={!hasAccess || !isBulkEditEnabled}
- size="sm"
- onChange={handleOrgChange}
- value={projectedOrgRate}
- onKeyDown={event => {
- if (event.key === 'Enter') {
- event.preventDefault();
- inputRef.current?.blur();
- }
- }}
- onBlur={handleOrgBlur}
- />
- </Tooltip>
- <FlexRow>
- <PreviousValue>
- {initialOrgRate !== projectedOrgRate
- ? t('previous: %f%%', initialOrgRate)
- : // Placeholder char to prevent the line from collapsing
- '\u200b'}
- </PreviousValue>
- {hasAccess && !isBulkEditEnabled && (
- <BulkEditButton
- size="zero"
- tooltipProps={{
- position: 'bottom',
- }}
- title={t('Proportionally scale project rates')}
- priority="link"
- onClick={() => {
- setIsBulkEditEnabled(true);
- }}
- >
- {t('edit')}
- </BulkEditButton>
- )}
- </FlexRow>
- </InputWrapper>
- </FieldGroup>
- </Fragment>
- )}
- </BreakdownPanel>
- <ProjectsTable
- rateHeader={t('Target Rate')}
- canEdit={!isBulkEditEnabled}
- onChange={handleProjectChange}
- emptyMessage={t('No active projects found in the selected period.')}
- isLoading={isLoading}
- items={activeItems}
- inactiveItems={inactiveItems}
- />
- </Fragment>
- );
- }
- const BreakdownPanel = styled(Panel)`
- margin-bottom: ${space(3)};
- `;
- const BreakdownWrapper = styled('div')`
- padding: ${space(2)};
- border-bottom: 1px solid ${p => p.theme.innerBorder};
- `;
- const InputWrapper = styled('div')`
- display: flex;
- flex-direction: column;
- gap: ${space(0.5)};
- `;
- const FlexRow = styled('div')`
- display: flex;
- align-items: center;
- justify-content: space-between;
- gap: ${space(1)};
- `;
- const PreviousValue = styled('span')`
- font-size: ${p => p.theme.fontSizeExtraSmall};
- color: ${p => p.theme.subText};
- `;
- const BulkEditButton = styled(Button)`
- font-size: ${p => p.theme.fontSizeExtraSmall};
- padding: 0;
- border: none;
- `;
|