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 @@ -8,7 +8,6 @@ import {
PageParamsProvider,
useExplorePageParams,
useSetExplorePageParams,
useSetExploreSortBys,
} from 'sentry/views/explore/contexts/pageParamsContext';
import {Mode} from 'sentry/views/explore/contexts/pageParamsContext/mode';
import {
Expand All @@ -18,10 +17,12 @@ import {
Visualize,
} from 'sentry/views/explore/contexts/pageParamsContext/visualizes';
import {
useSetQueryParamsAggregateSortBys,
useSetQueryParamsFields,
useSetQueryParamsGroupBys,
useSetQueryParamsMode,
useSetQueryParamsQuery,
useSetQueryParamsSortBys,
useSetQueryParamsVisualizes,
} from 'sentry/views/explore/queryParams/context';
import {SpansQueryParamsProvider} from 'sentry/views/explore/spans/spansQueryParamsProvider';
Expand Down Expand Up @@ -56,7 +57,8 @@ describe('PageParamsProvider', () => {
let setGroupBys: ReturnType<typeof useSetQueryParamsGroupBys>;
let setMode: ReturnType<typeof useSetQueryParamsMode>;
let setQuery: ReturnType<typeof useSetQueryParamsQuery>;
let setSortBys: ReturnType<typeof useSetExploreSortBys>;
let setSortBys: ReturnType<typeof useSetQueryParamsSortBys>;
let setAggregateSortBys: ReturnType<typeof useSetQueryParamsAggregateSortBys>;
let setVisualizes: ReturnType<typeof useSetQueryParamsVisualizes>;

function Component() {
Expand All @@ -66,7 +68,8 @@ describe('PageParamsProvider', () => {
setGroupBys = useSetQueryParamsGroupBys();
setMode = useSetQueryParamsMode();
setQuery = useSetQueryParamsQuery();
setSortBys = useSetExploreSortBys();
setSortBys = useSetQueryParamsSortBys();
setAggregateSortBys = useSetQueryParamsAggregateSortBys();
setVisualizes = useSetQueryParamsVisualizes();
return <br />;
}
Expand Down Expand Up @@ -393,7 +396,7 @@ describe('PageParamsProvider', () => {
],
});

act(() => setSortBys([{field: 'max(span.duration)', kind: 'desc'}]));
act(() => setAggregateSortBys([{field: 'max(span.duration)', kind: 'desc'}]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Aggregate Tests Use Incorrect Sort Method

Several aggregate mode tests incorrectly use setSortBys to update sort parameters. After recent changes, setSortBys now controls sample sorts, so these tests should instead call setAggregateSortBys to update the correct state.

Additional Locations (3)

Fix in Cursor Fix in Web


expect(pageParams).toEqual(
expect.objectContaining({
Expand Down Expand Up @@ -428,7 +431,7 @@ describe('PageParamsProvider', () => {
],
});

act(() => setSortBys([{field: 'avg(span.duration)', kind: 'desc'}]));
act(() => setAggregateSortBys([{field: 'avg(span.duration)', kind: 'desc'}]));

expect(pageParams).toEqual(
expect.objectContaining({
Expand Down Expand Up @@ -463,7 +466,7 @@ describe('PageParamsProvider', () => {
],
});

act(() => setSortBys([{field: 'sdk.name', kind: 'desc'}]));
act(() => setAggregateSortBys([{field: 'sdk.name', kind: 'desc'}]));

expect(pageParams).toEqual(
expect.objectContaining({
Expand Down Expand Up @@ -495,7 +498,7 @@ describe('PageParamsProvider', () => {
],
});

act(() => setSortBys([{field: 'sdk.version', kind: 'desc'}]));
act(() => setAggregateSortBys([{field: 'sdk.version', kind: 'desc'}]));

expect(pageParams).toEqual(
expect.objectContaining({
Expand Down
27 changes: 0 additions & 27 deletions static/app/views/explore/contexts/pageParamsContext/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,18 +249,6 @@ export function useExploreDataset(): DiscoverDatasets {
return DiscoverDatasets.SPANS;
}

export function useExploreSortBys(): Sort[] {
const pageParams = useExplorePageParams();
return pageParams.mode === Mode.AGGREGATE
? pageParams.aggregateSortBys
: pageParams.sortBys;
}

export function useExploreAggregateSortBys(): Sort[] {
const pageParams = useExplorePageParams();
return pageParams.aggregateSortBys;
}

export function useExploreTitle(): string | undefined {
const pageParams = useExplorePageParams();
return pageParams.title;
Expand Down Expand Up @@ -497,18 +485,3 @@ export function useSetExplorePageParams(): (
[location, navigate, readablePageParams, managedFields, setManagedFields]
);
}

export function useSetExploreSortBys() {
const pageParams = useExplorePageParams();
const setPageParams = useSetExplorePageParams();
return useCallback(
(sortBys: Sort[]) => {
setPageParams(
pageParams.mode === Mode.AGGREGATE
? {aggregateSortBys: sortBys}
: {sampleSortBys: sortBys}
);
},
[pageParams, setPageParams]
);
}
12 changes: 7 additions & 5 deletions static/app/views/explore/hooks/useAddToDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ import {
WidgetType,
} from 'sentry/views/dashboards/types';
import {handleAddQueryToDashboard} from 'sentry/views/discover/utils';
import {
useExploreDataset,
useExploreSortBys,
} from 'sentry/views/explore/contexts/pageParamsContext';
import {useExploreDataset} from 'sentry/views/explore/contexts/pageParamsContext';
import {Mode} from 'sentry/views/explore/contexts/pageParamsContext/mode';
import {formatSort} from 'sentry/views/explore/contexts/pageParamsContext/sortBys';
import {
useQueryParamsAggregateSortBys,
useQueryParamsGroupBys,
useQueryParamsMode,
useQueryParamsQuery,
useQueryParamsSortBys,
useQueryParamsVisualizes,
} from 'sentry/views/explore/queryParams/context';
import {ChartType} from 'sentry/views/insights/common/components/chart';
Expand All @@ -41,10 +40,13 @@ export function useAddToDashboard() {
const mode = useQueryParamsMode();
const dataset = useExploreDataset();
const groupBys = useQueryParamsGroupBys();
const sortBys = useExploreSortBys();
const sampleSortBys = useQueryParamsSortBys();
const aggregateSortBys = useQueryParamsAggregateSortBys();
const visualizes = useQueryParamsVisualizes();
const query = useQueryParamsQuery();

const sortBys = mode === Mode.SAMPLES ? sampleSortBys : aggregateSortBys;

const getEventView = useCallback(
(visualizeIndex: number) => {
const yAxis = visualizes[visualizeIndex]!.yAxis;
Expand Down
12 changes: 5 additions & 7 deletions static/app/views/explore/hooks/useExploreAggregatesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import type {NewQuery} from 'sentry/types/organization';
import {defined} from 'sentry/utils';
import EventView from 'sentry/utils/discover/eventView';
import usePageFilters from 'sentry/utils/usePageFilters';
import {
useExploreDataset,
useExploreSortBys,
} from 'sentry/views/explore/contexts/pageParamsContext';
import {useExploreDataset} from 'sentry/views/explore/contexts/pageParamsContext';
import {isGroupBy} from 'sentry/views/explore/contexts/pageParamsContext/aggregateFields';
import {formatSort} from 'sentry/views/explore/contexts/pageParamsContext/sortBys';
import type {SpansRPCQueryExtras} from 'sentry/views/explore/hooks/useProgressiveQuery';
import {useProgressiveQuery} from 'sentry/views/explore/hooks/useProgressiveQuery';
import {
useQueryParamsAggregateFields,
useQueryParamsAggregateSortBys,
useQueryParamsExtrapolate,
} from 'sentry/views/explore/queryParams/context';
import {useSpansQuery} from 'sentry/views/insights/common/queries/useSpansQuery';
Expand Down Expand Up @@ -66,7 +64,7 @@ function useExploreAggregatesTableImp({

const dataset = useExploreDataset();
const aggregateFields = useQueryParamsAggregateFields({validate: true});
const sorts = useExploreSortBys();
const aggregateSortBys = useQueryParamsAggregateSortBys();

const fields = useMemo(() => {
// When rendering the table, we want the group bys first
Expand Down Expand Up @@ -95,14 +93,14 @@ function useExploreAggregatesTableImp({
id: undefined,
name: 'Explore - Span Aggregates',
fields,
orderby: sorts.map(formatSort),
orderby: aggregateSortBys.map(formatSort),
query,
version: 2,
dataset,
};

return EventView.fromNewQueryWithPageFilters(discoverQuery, selection);
}, [dataset, fields, sorts, query, selection]);
}, [dataset, fields, aggregateSortBys, query, selection]);

const result = useSpansQuery({
enabled,
Expand Down
8 changes: 3 additions & 5 deletions static/app/views/explore/hooks/useExploreSpansTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import type {NewQuery} from 'sentry/types/organization';
import {defined} from 'sentry/utils';
import EventView from 'sentry/utils/discover/eventView';
import usePageFilters from 'sentry/utils/usePageFilters';
import {
useExploreDataset,
useExploreSortBys,
} from 'sentry/views/explore/contexts/pageParamsContext';
import {useExploreDataset} from 'sentry/views/explore/contexts/pageParamsContext';
import {
useProgressiveQuery,
type SpansRPCQueryExtras,
} from 'sentry/views/explore/hooks/useProgressiveQuery';
import {
useQueryParamsExtrapolate,
useQueryParamsFields,
useQueryParamsSortBys,
} from 'sentry/views/explore/queryParams/context';
import {useSpansQuery} from 'sentry/views/insights/common/queries/useSpansQuery';

Expand Down Expand Up @@ -66,7 +64,7 @@ function useExploreSpansTableImp({

const dataset = useExploreDataset();
const fields = useQueryParamsFields();
const sortBys = useExploreSortBys();
const sortBys = useQueryParamsSortBys();

const visibleFields = useMemo(
() => (fields.includes('id') ? fields : ['id', ...fields]),
Expand Down
8 changes: 3 additions & 5 deletions static/app/views/explore/hooks/useExploreTimeseries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import {defined} from 'sentry/utils';
import {dedupeArray} from 'sentry/utils/dedupeArray';
import {MutableSearch} from 'sentry/utils/tokenizeSearch';
import {determineSeriesSampleCountAndIsSampled} from 'sentry/views/alerts/rules/metric/utils/determineSeriesSampleCount';
import {
useExploreAggregateSortBys,
useExploreDataset,
} from 'sentry/views/explore/contexts/pageParamsContext';
import {useExploreDataset} from 'sentry/views/explore/contexts/pageParamsContext';
import {formatSort} from 'sentry/views/explore/contexts/pageParamsContext/sortBys';
import {DEFAULT_VISUALIZATION} from 'sentry/views/explore/contexts/pageParamsContext/visualizes';
import {useChartInterval} from 'sentry/views/explore/hooks/useChartInterval';
Expand All @@ -17,6 +14,7 @@ import {
} from 'sentry/views/explore/hooks/useProgressiveQuery';
import {useTopEvents} from 'sentry/views/explore/hooks/useTopEvents';
import {
useQueryParamsAggregateSortBys,
useQueryParamsExtrapolate,
useQueryParamsGroupBys,
useQueryParamsVisualizes,
Expand Down Expand Up @@ -71,7 +69,7 @@ function useExploreTimeseriesImpl({
}: UseExploreTimeseriesOptions): UseExploreTimeseriesResults {
const dataset = useExploreDataset();
const groupBys = useQueryParamsGroupBys();
const sortBys = useExploreAggregateSortBys();
const sortBys = useQueryParamsAggregateSortBys();
const visualizes = useQueryParamsVisualizes({validate: true});
const [interval] = useChartInterval();
const topEvents = useTopEvents();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ type WritableExploreQueryParts = {
fields?: string[];
groupBys?: readonly string[];
query?: string;
sortBys?: Sort[];
sortBys?: readonly Sort[];
yAxes?: string[];
};

Expand Down
11 changes: 11 additions & 0 deletions static/app/views/explore/queryParams/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,17 @@ export function useQueryParamsAggregateSortBys(): readonly Sort[] {
return queryParams.aggregateSortBys;
}

export function useSetQueryParamsAggregateSortBys() {
const setQueryParams = useSetQueryParams();

return useCallback(
(aggregateSortBys: Sort[]) => {
setQueryParams({aggregateSortBys});
},
[setQueryParams]
);
}

export function useQueryParamsAggregateCursor(): string {
const queryParams = useQueryParams();
return queryParams.aggregateCursor;
Expand Down
18 changes: 18 additions & 0 deletions static/app/views/explore/spans/spansQueryParams.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ export function getTargetWithReadableQueryParams(
updateNullableLocation(target, SPANS_MODE_KEY, writableQueryParams.mode);

updateNullableLocation(target, SPANS_FIELD_KEY, writableQueryParams.fields);
updateNullableLocation(
target,
SPANS_SORT_KEY,
writableQueryParams.sortBys === null
? null
: writableQueryParams.sortBys?.map(
sort => `${sort.kind === 'desc' ? '-' : ''}${sort.field}`
)
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Query Parameters Handle Nulls and Undefines Differently

The sortBys and aggregateSortBys query parameter updates might handle null and undefined inputs inconsistently. While null inputs explicitly result in null, undefined inputs would pass through optional chaining (?.map()) and yield undefined for updateNullableLocation, potentially causing unexpected behavior.

Additional Locations (1)

Fix in Cursor Fix in Web


updateNullableLocation(
target,
Expand All @@ -109,6 +118,15 @@ export function getTargetWithReadableQueryParams(
JSON.stringify(aggregateField)
)
);
updateNullableLocation(
target,
SPANS_AGGREGATE_SORT_KEY,
writableQueryParams.aggregateSortBys === null
? null
: writableQueryParams.aggregateSortBys?.map(
sort => `${sort.kind === 'desc' ? '-' : ''}${sort.field}`
)
);

return target;
}
Expand Down
10 changes: 4 additions & 6 deletions static/app/views/explore/tables/aggregatesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ import {
TableStatus,
useTableStyles,
} from 'sentry/views/explore/components/table';
import {
useExploreSortBys,
useSetExploreSortBys,
} from 'sentry/views/explore/contexts/pageParamsContext';
import {isGroupBy} from 'sentry/views/explore/contexts/pageParamsContext/aggregateFields';
import {useTraceItemTags} from 'sentry/views/explore/contexts/spanTagsContext';
import type {AggregatesTableResult} from 'sentry/views/explore/hooks/useExploreAggregatesTable';
Expand All @@ -42,10 +38,12 @@ import {TOP_EVENTS_LIMIT, useTopEvents} from 'sentry/views/explore/hooks/useTopE
import {
useQueryParamsAggregateCursor,
useQueryParamsAggregateFields,
useQueryParamsAggregateSortBys,
useQueryParamsFields,
useQueryParamsGroupBys,
useQueryParamsQuery,
useQueryParamsVisualizes,
useSetQueryParamsAggregateSortBys,
} from 'sentry/views/explore/queryParams/context';
import {FieldRenderer} from 'sentry/views/explore/tables/fieldRenderer';
import {prettifyAggregation, viewSamplesTarget} from 'sentry/views/explore/utils';
Expand All @@ -66,8 +64,8 @@ export function AggregatesTable({aggregatesTableResult}: AggregatesTableProps) {
const fields = useQueryParamsFields();
const groupBys = useQueryParamsGroupBys();
const visualizes = useQueryParamsVisualizes();
const sorts = useExploreSortBys();
const setSorts = useSetExploreSortBys();
const sorts = useQueryParamsAggregateSortBys();
const setSorts = useSetQueryParamsAggregateSortBys();
const query = useQueryParamsQuery();
const cursor = useQueryParamsAggregateCursor();

Expand Down
14 changes: 7 additions & 7 deletions static/app/views/explore/tables/spansTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import {
TableStatus,
useTableStyles,
} from 'sentry/views/explore/components/table';
import {
useExploreSortBys,
useSetExploreSortBys,
} from 'sentry/views/explore/contexts/pageParamsContext';
import {useTraceItemTags} from 'sentry/views/explore/contexts/spanTagsContext';
import type {SpansTableResult} from 'sentry/views/explore/hooks/useExploreSpansTable';
import {usePaginationAnalytics} from 'sentry/views/explore/hooks/usePaginationAnalytics';
import {useQueryParamsFields} from 'sentry/views/explore/queryParams/context';
import {
useQueryParamsFields,
useQueryParamsSortBys,
useSetQueryParamsSortBys,
} from 'sentry/views/explore/queryParams/context';

import {FieldRenderer} from './fieldRenderer';

Expand All @@ -39,8 +39,8 @@ interface SpansTableProps {

export function SpansTable({spansTableResult}: SpansTableProps) {
const fields = useQueryParamsFields();
const sortBys = useExploreSortBys();
const setSortBys = useSetExploreSortBys();
const sortBys = useQueryParamsSortBys();
const setSortBys = useSetQueryParamsSortBys();

const visibleFields = useMemo(
() => (fields.includes('id') ? [...fields] : ['id', ...fields]),
Expand Down
Loading
Loading