Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
LogAttributeTreeWrapper,
LogDetailTableBodyCell,
} from 'sentry/views/explore/logs/styles';
import {useLogAttributesTreeActions} from 'sentry/views/explore/logs/useLogAttributesTreeActions';
import {SeverityLevel} from 'sentry/views/explore/logs/utils';
import {HiddenTraceMetricDetailFields} from 'sentry/views/explore/metrics/constants';
import {useMetricTraceDetail} from 'sentry/views/explore/metrics/hooks/useMetricTraceDetail';
Expand All @@ -25,6 +24,7 @@ import {
MetricsDetailsWrapper,
} from 'sentry/views/explore/metrics/metricInfoTabs/metricInfoTabStyles';
import {TraceMetricKnownFieldKey} from 'sentry/views/explore/metrics/types';
import {useMetricAttributesTreeActions} from 'sentry/views/explore/metrics/useMetricAttributesTreeActions';

export function MetricDetails({
dataRow,
Expand All @@ -36,7 +36,7 @@ export function MetricDetails({
const theme = useTheme();
const location = useLocation();
const organization = useOrganization();
const getActions = useLogAttributesTreeActions({embedded: false});
const getActions = useMetricAttributesTreeActions();
const project = useProjectFromId({
project_id: String(dataRow[TraceMetricKnownFieldKey.PROJECT_ID] ?? ''),
});
Expand Down
Comment thread
nsdeschenes marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {useMemo, type ReactNode} from 'react';

import {renderHookWithProviders} from 'sentry-test/reactTestingLibrary';

import type {AttributesTreeContent} from 'sentry/views/explore/components/traceItemAttributes/attributesTree';
import {
defaultAggregateFields,
defaultAggregateSortBys,
defaultFields,
defaultSortBys,
} from 'sentry/views/explore/metrics/metricQuery';
import {useMetricAttributesTreeActions} from 'sentry/views/explore/metrics/useMetricAttributesTreeActions';
import {QueryParamsContextProvider} from 'sentry/views/explore/queryParams/context';
import {defaultCursor} from 'sentry/views/explore/queryParams/cursor';
import {Mode} from 'sentry/views/explore/queryParams/mode';
import {ReadableQueryParams} from 'sentry/views/explore/queryParams/readableQueryParams';

const mockSetQueryParams = jest.fn();

function Wrapper({children}: {children: ReactNode}) {
const queryParams = useMemo(
() =>
new ReadableQueryParams({
aggregateCursor: defaultCursor(),
aggregateFields: defaultAggregateFields(),
aggregateSortBys: defaultAggregateSortBys(defaultAggregateFields()),
cursor: defaultCursor(),
extrapolate: true,
fields: defaultFields(),
mode: Mode.SAMPLES,
query: '',
sortBys: defaultSortBys(defaultFields()),
}),
[]
);

return (
<QueryParamsContextProvider
isUsingDefaultFields={false}
queryParams={queryParams}
setQueryParams={mockSetQueryParams}
shouldManageFields={false}
>
{children}
</QueryParamsContextProvider>
);
}

describe('useMetricAttributesTreeActions', () => {
beforeEach(() => {
mockSetQueryParams.mockClear();
});

it('returns filter-only attribute actions', () => {
const {result} = renderHookWithProviders(useMetricAttributesTreeActions, {
additionalWrapper: Wrapper,
});

const content: AttributesTreeContent = {
originalAttribute: {
attribute_key: 'release',
attribute_value: '1.0.0',
original_attribute_key: 'release',
},
subtree: {},
value: '1.0.0',
};

const actions = result.current(content);

expect(actions.map(action => action.label)).toEqual([
'Add to filter',
'Exclude this value',
]);
});

it('returns no actions when originalAttribute is missing', () => {
const {result} = renderHookWithProviders(useMetricAttributesTreeActions, {
additionalWrapper: Wrapper,
});

const content: AttributesTreeContent = {
subtree: {},
value: '',
};

expect(result.current(content)).toEqual([]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {useCallback} from 'react';

import type {MenuItemProps} from 'sentry/components/dropdownMenu';
import {t} from 'sentry/locale';
import type {AttributesTreeContent} from 'sentry/views/explore/components/traceItemAttributes/attributesTree';
import {useAddSearchFilter} from 'sentry/views/explore/queryParams/context';

export function useMetricAttributesTreeActions() {
const addSearchFilter = useAddSearchFilter();

const addAttributeSearchFilter = useCallback(
(content: AttributesTreeContent, negated?: boolean) => {
const originalAttribute = content.originalAttribute;
if (!originalAttribute) {
return;
}

addSearchFilter({
key: originalAttribute.original_attribute_key,
value: String(content.value),
negated,
});
},
[addSearchFilter]
);

return (content: AttributesTreeContent) => {
if (!content.originalAttribute) {
return [];
}

const items: MenuItemProps[] = [
{
key: 'search-for-value',
label: t('Add to filter'),
onAction: () => addAttributeSearchFilter(content),
},
{
key: 'search-for-negated-value',
label: t('Exclude this value'),
onAction: () => addAttributeSearchFilter(content, true),
},
];

return items;
};
}
Loading