Skip to content

feat(explore): Allow separate sort bys in samples and aggregates #94577

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
@@ -0,0 +1,73 @@
import type {Location} from 'history';

import {defined} from 'sentry/utils';
import type {Sort} from 'sentry/utils/discover/fields';
import {decodeSorts} from 'sentry/utils/queryString';

import type {Visualize} from './visualizes';

export function defaultAggregateSortBys(yAxes: string[]): Sort[] {
if (yAxes[0]) {
return [
{
field: yAxes[0],
kind: 'desc' as const,
},
];
}

return [];
}

export function getAggregateSortBysFromLocation(
location: Location,
groupBys: string[],
visualizes: Visualize[]
) {
const sortBys = decodeSorts(location.query.aggregateSort);

if (sortBys.length > 0 && validateSorts(sortBys, groupBys, visualizes)) {
return sortBys;
}

// we want to check the `sort` query param for backwards compatibility
// when the both modes shared a single query param for the sort
const sortBysFallback = decodeSorts(location.query.sort);

if (
sortBysFallback.length > 0 &&
validateSorts(sortBysFallback, groupBys, visualizes)
) {
return sortBysFallback;
}

return defaultAggregateSortBys(visualizes.map(visualize => visualize.yAxis));
}

function validateSorts(
sortBys: Sort[],
groupBys: string[],
visualizes: Visualize[]
): boolean {
return sortBys.every(
sortBy =>
groupBys.includes(sortBy.field) ||
visualizes.some(visualize => visualize.yAxis === sortBy.field)
);
}

export function updateLocationWithAggregateSortBys(
location: Location,
sortBys: Sort[] | null | undefined
) {
if (defined(sortBys)) {
location.query.aggregateSort = sortBys.map(sortBy =>
sortBy.kind === 'desc' ? `-${sortBy.field}` : sortBy.field
);

// make sure to clear the cursor every time the query is updated
delete location.query.cursor;
} else if (sortBys === null) {
delete location.query.aggregateSort;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ describe('PageParamsProvider', function () {
fields: ['id', 'timestamp'],
mode: Mode.AGGREGATE,
query: '',
sortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
sampleSortBys: [{field: 'timestamp', kind: 'asc'}],
aggregateSortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
aggregateFields: [
{groupBy: 'span.op'},
{
Expand Down Expand Up @@ -111,7 +112,8 @@ describe('PageParamsProvider', function () {
],
mode: Mode.SAMPLES,
query: '',
sortBys: [{field: 'timestamp', kind: 'desc'}],
sampleSortBys: [{field: 'timestamp', kind: 'desc'}],
aggregateSortBys: [{field: 'count(span.duration)', kind: 'desc'}],
aggregateFields: [{groupBy: ''}, new Visualize('count(span.duration)')],
})
);
Expand All @@ -128,7 +130,8 @@ describe('PageParamsProvider', function () {
fields: ['id', 'span.op', 'timestamp'],
mode: Mode.AGGREGATE,
query: '',
sortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
sampleSortBys: [{field: 'timestamp', kind: 'asc'}],
aggregateSortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
aggregateFields: [
{groupBy: 'span.op'},
new Visualize('count(span.self_time)', {
Expand All @@ -150,7 +153,8 @@ describe('PageParamsProvider', function () {
fields: ['id', 'timestamp', 'span.self_time'],
mode: Mode.AGGREGATE,
query: '',
sortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
sampleSortBys: [{field: 'timestamp', kind: 'asc'}],
aggregateSortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
aggregateFields: [
{groupBy: 'browser.name'},
new Visualize('count(span.self_time)', {
Expand All @@ -173,7 +177,8 @@ describe('PageParamsProvider', function () {
fields: ['id', 'timestamp', 'span.self_time'],
mode: Mode.AGGREGATE,
query: '',
sortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
sampleSortBys: [{field: 'timestamp', kind: 'asc'}],
aggregateSortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
aggregateFields: [
new Visualize('count(span.self_time)', {
chartType: ChartType.AREA,
Expand All @@ -195,7 +200,8 @@ describe('PageParamsProvider', function () {
fields: ['id', 'timestamp', 'span.self_time'],
mode: Mode.AGGREGATE,
query: '',
sortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
sampleSortBys: [{field: 'timestamp', kind: 'asc'}],
aggregateSortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
aggregateFields: [
{groupBy: ''},
new Visualize('count(span.self_time)', {
Expand All @@ -217,7 +223,8 @@ describe('PageParamsProvider', function () {
fields: ['id', 'timestamp', 'span.self_time'],
mode: Mode.AGGREGATE,
query: '',
sortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
sampleSortBys: [{field: 'timestamp', kind: 'asc'}],
aggregateSortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
aggregateFields: [
{groupBy: 'span.op'},
new Visualize('count(span.self_time)', {
Expand All @@ -238,7 +245,8 @@ describe('PageParamsProvider', function () {
yAxes: ['count(span.self_time)'],
},
],
sortBys: null,
sampleSortBys: null,
aggregateSortBys: null,
});

act(() => setMode(Mode.SAMPLES));
Expand All @@ -249,7 +257,8 @@ describe('PageParamsProvider', function () {
fields: ['id', 'timestamp', 'span.self_time'],
mode: Mode.SAMPLES,
query: '',
sortBys: [{field: 'timestamp', kind: 'desc'}],
sampleSortBys: [{field: 'timestamp', kind: 'desc'}],
aggregateSortBys: [{field: 'count(span.self_time)', kind: 'desc'}],
aggregateFields: [
{groupBy: ''},
new Visualize('count(span.self_time)', {
Expand All @@ -263,7 +272,8 @@ describe('PageParamsProvider', function () {
it('correctly updates mode from aggregates to sample with group bys', function () {
renderTestComponent({
mode: Mode.AGGREGATE,
sortBys: null,
sampleSortBys: null,
aggregateSortBys: null,
fields: ['id', 'sdk.name', 'sdk.version', 'timestamp'],
aggregateFields: [
{groupBy: 'sdk.name'},
Expand Down Expand Up @@ -292,7 +302,8 @@ describe('PageParamsProvider', function () {
],
mode: Mode.SAMPLES,
query: '',
sortBys: [{field: 'timestamp', kind: 'desc'}],
sampleSortBys: [{field: 'timestamp', kind: 'desc'}],
aggregateSortBys: [{field: 'count(span.self_time)', kind: 'desc'}],
aggregateFields: [
{groupBy: 'sdk.name'},
{groupBy: 'sdk.version'},
Expand All @@ -317,7 +328,8 @@ describe('PageParamsProvider', function () {
fields: ['id', 'timestamp', 'span.self_time'],
mode: Mode.AGGREGATE,
query: 'foo:bar',
sortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
sampleSortBys: [{field: 'timestamp', kind: 'asc'}],
aggregateSortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
aggregateFields: [
{groupBy: 'span.op'},
new Visualize('count(span.self_time)', {
Expand All @@ -339,7 +351,8 @@ describe('PageParamsProvider', function () {
fields: ['id', 'timestamp', 'span.self_time'],
mode: Mode.SAMPLES,
query: '',
sortBys: [{field: 'id', kind: 'desc'}],
sampleSortBys: [{field: 'id', kind: 'desc'}],
aggregateSortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
aggregateFields: [
{groupBy: 'span.op'},
new Visualize('count(span.self_time)', {
Expand All @@ -361,7 +374,8 @@ describe('PageParamsProvider', function () {
fields: ['id', 'timestamp', 'span.self_time'],
mode: Mode.SAMPLES,
query: '',
sortBys: [{field: 'timestamp', kind: 'desc'}],
sampleSortBys: [{field: 'timestamp', kind: 'desc'}],
aggregateSortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
aggregateFields: [
{groupBy: 'span.op'},
new Visualize('count(span.self_time)', {
Expand Down Expand Up @@ -392,7 +406,8 @@ describe('PageParamsProvider', function () {
fields: ['id', 'timestamp', 'span.self_time'],
mode: Mode.AGGREGATE,
query: '',
sortBys: [{field: 'max(span.duration)', kind: 'desc'}],
sampleSortBys: [{field: 'timestamp', kind: 'asc'}],
aggregateSortBys: [{field: 'max(span.duration)', kind: 'desc'}],
aggregateFields: [
{groupBy: 'span.op'},
new Visualize('min(span.self_time)', {
Expand Down Expand Up @@ -426,7 +441,8 @@ describe('PageParamsProvider', function () {
fields: ['id', 'timestamp', 'span.self_time'],
mode: Mode.AGGREGATE,
query: '',
sortBys: [{field: 'min(span.self_time)', kind: 'desc'}],
sampleSortBys: [{field: 'timestamp', kind: 'asc'}],
aggregateSortBys: [{field: 'min(span.self_time)', kind: 'desc'}],
aggregateFields: [
{groupBy: 'span.op'},
new Visualize('min(span.self_time)', {
Expand Down Expand Up @@ -460,7 +476,8 @@ describe('PageParamsProvider', function () {
fields: ['id', 'timestamp', 'span.self_time'],
mode: Mode.AGGREGATE,
query: '',
sortBys: [{field: 'sdk.name', kind: 'desc'}],
sampleSortBys: [{field: 'timestamp', kind: 'asc'}],
aggregateSortBys: [{field: 'sdk.name', kind: 'desc'}],
aggregateFields: [
{groupBy: 'sdk.name'},
new Visualize('count(span.self_time)', {
Expand Down Expand Up @@ -491,7 +508,8 @@ describe('PageParamsProvider', function () {
fields: ['id', 'timestamp', 'span.self_time'],
mode: Mode.AGGREGATE,
query: '',
sortBys: [{field: 'count(span.self_time)', kind: 'desc'}],
sampleSortBys: [{field: 'timestamp', kind: 'asc'}],
aggregateSortBys: [{field: 'count(span.self_time)', kind: 'desc'}],
aggregateFields: [
{groupBy: 'sdk.name'},
new Visualize('count(span.self_time)', {
Expand All @@ -513,7 +531,8 @@ describe('PageParamsProvider', function () {
fields: ['id', 'timestamp'],
mode: Mode.AGGREGATE,
query: '',
sortBys: [{field: 'count(span.duration)', kind: 'desc'}],
sampleSortBys: [{field: 'timestamp', kind: 'asc'}],
aggregateSortBys: [{field: 'count(span.duration)', kind: 'desc'}],
aggregateFields: [{groupBy: 'span.op'}, new Visualize('count(span.duration)')],
})
);
Expand Down Expand Up @@ -541,7 +560,8 @@ describe('PageParamsProvider', function () {
fields: ['id', 'timestamp', 'span.self_time', 'span.duration'],
mode: Mode.AGGREGATE,
query: '',
sortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
sampleSortBys: [{field: 'timestamp', kind: 'asc'}],
aggregateSortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
aggregateFields: [
{groupBy: 'span.op'},
new Visualize('count(span.self_time)', {
Expand Down
Loading
Loading