Skip to content

fix(widget-builder): Only allow count(spans) in sort #91065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2025
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
10 changes: 10 additions & 0 deletions static/app/views/dashboards/datasetConfig/spans.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ function filterAggregateParams(option: FieldValueOption, fieldValue?: QueryField
return true;
}

if (
fieldValue?.kind === 'function' &&
fieldValue?.function[0] === AggregationKey.COUNT
) {
return (
option.value.meta.name === 'span.duration' ||
fieldValue.function[1] === option.value.meta.name
);
}

const expectedDataType =
fieldValue?.kind === 'function' &&
fieldValue?.function[0] === AggregationKey.COUNT_UNIQUE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useEffect, useState} from 'react';
import {useEffect, useMemo, useState} from 'react';
import styled from '@emotion/styled';
import trimStart from 'lodash/trimStart';
import uniqBy from 'lodash/uniqBy';
Expand Down Expand Up @@ -27,6 +27,7 @@ import {
} from 'sentry/views/dashboards/widgetBuilder/utils';
import ArithmeticInput from 'sentry/views/discover/table/arithmeticInput';
import {QueryField} from 'sentry/views/discover/table/queryField';
import type {FieldValue} from 'sentry/views/discover/table/types';

import {CUSTOM_EQUATION_VALUE} from '.';

Expand All @@ -48,6 +49,10 @@ interface Props {
hasGroupBy?: boolean;
}

// Lock the sort by parameter option when the value is `count(span.duration)`
// because we do not want to expose the concept of counting by other fields
const LOCKED_SPAN_COUNT_SORT = 'count(span.duration)';

export function SortBySelectors({
values,
widgetType,
Expand Down Expand Up @@ -78,6 +83,22 @@ export function SortBySelectors({
setShowCustomEquation(isSortingByEquation);
}, [values.sortBy, values.sortDirection]);

const timeseriesSortOptions = useMemo(() => {
let options: Record<string, SelectValue<FieldValue>> = {};
if (displayType !== DisplayType.TABLE) {
options = datasetConfig.getTimeseriesSortOptions!(organization, widgetQuery, tags);
if (widgetType === WidgetType.SPANS && options['measurement:span.duration']) {
// Re-map the span duration measurement label so we can simply render
// `spans` in the parameter UI
options['measurement:span.duration'] = {
...options['measurement:span.duration'],
label: t('spans'),
};
}
}
return options;
}, [datasetConfig, organization, tags, widgetQuery, widgetType, displayType]);

return (
<Wrapper>
<Tooltip
Expand Down Expand Up @@ -137,17 +158,16 @@ export function SortBySelectors({
? explodeField({field: CUSTOM_EQUATION_VALUE})
: explodeField({field: values.sortBy})
}
fieldOptions={datasetConfig.getTimeseriesSortOptions!(
organization,
widgetQuery,
tags
)}
fieldOptions={timeseriesSortOptions}
filterPrimaryOptions={
datasetConfig.filterSeriesSortOptions
? datasetConfig.filterSeriesSortOptions(columnSet)
: undefined
}
filterAggregateParameters={datasetConfig.filterAggregateParams}
disableParameterSelector={
widgetType === WidgetType.SPANS && values.sortBy === LOCKED_SPAN_COUNT_SORT
}
onChange={value => {
if (value.alias && isEquationAlias(value.alias)) {
onChange({
Expand Down
4 changes: 3 additions & 1 deletion static/app/views/discover/table/queryField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type Props = {
fieldValue: QueryFieldValue;
onChange: (fieldValue: QueryFieldValue) => void;
className?: string;
disableParameterSelector?: boolean;
disabled?: boolean;
error?: string;
/**
Expand Down Expand Up @@ -439,6 +440,7 @@ class _QueryField extends Component<Props> {
fieldValue,
useMenuPortal,
theme,
disableParameterSelector,
} = this.props;

const inputs = parameters.map((descriptor: ParameterDescription, index: number) => {
Expand Down Expand Up @@ -483,7 +485,7 @@ class _QueryField extends Component<Props> {
required={descriptor.required}
onChange={this.handleFieldParameterChange}
inFieldLabel={inFieldLabels ? t('Parameter: ') : undefined}
disabled={disabled}
disabled={disabled || disableParameterSelector}
menuPortalTarget={portalProps.menuPortalTarget}
styles={{
...portalProps.styles,
Expand Down
Loading