Skip to content

Commit

Permalink
feat(events-v2): Support ordering by a list of fields (#13627)
Browse files Browse the repository at this point in the history
  • Loading branch information
lynnagara authored Jun 12, 2019
1 parent fd00029 commit 80227d6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/sentry/api/bases/organization_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def get_snuba_query_args_v2(self, request, organization, params):
if groupby:
snuba_args['groupby'] = groupby

orderby = request.GET.get('orderby')
orderby = request.GET.getlist('orderby')
if orderby:
snuba_args['orderby'] = orderby

Expand Down
21 changes: 14 additions & 7 deletions src/sentry/utils/snuba.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def zerofill(data, start, end, rollup, orderby):
else:
rv.append({'time': key})

if orderby.startswith('-'):
if '-time' in orderby:
return list(reversed(rv))

return rv
Expand Down Expand Up @@ -456,12 +456,19 @@ def handle_condition(cond):
kwargs['having'] = having

if orderby:
order_by_column = orderby.lstrip('-')
kwargs['orderby'] = u'{}{}'.format(
'-' if orderby.startswith('-') else '',
order_by_column if order_by_column in derived_columns else get_snuba_column_name(
order_by_column)
) or None
if orderby is None:
orderby = []
orderby = orderby if isinstance(orderby, (list, tuple)) else [orderby]
translated_orderby = []

for field_with_order in orderby:
field = field_with_order.lstrip('-')
translated_orderby.append(u'{}{}'.format(
'-' if field_with_order.startswith('-') else '',
field if field in derived_columns else get_snuba_column_name(field)
))

kwargs['orderby'] = translated_orderby

kwargs['arrayjoin'] = arrayjoin_map.get(arrayjoin, arrayjoin)

Expand Down
38 changes: 38 additions & 0 deletions tests/snuba/api/endpoints/test_organization_events_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,44 @@ def test_groupby(self):
assert response.data[1]['project.id'] == project.id
assert response.data[1]['issue.id'] == groups[1].id

def test_orderby(self):
self.login_as(user=self.user)
project = self.create_project()
self.store_event(
data={
'event_id': 'a' * 32,
'timestamp': self.two_min_ago,
},
project_id=project.id,
)
self.store_event(
data={
'event_id': 'b' * 32,
'timestamp': self.min_ago,
},
project_id=project.id,
)
self.store_event(
data={
'event_id': 'c' * 32,
'timestamp': self.min_ago,
},
project_id=project.id,
)
with self.feature('organizations:events-v2'):
response = self.client.get(
self.url,
format='json',
data={
'field': ['id'],
'orderby': ['-timestamp', '-id']
},
)

assert response.data[0]['id'] == 'c' * 32
assert response.data[1]['id'] == 'b' * 32
assert response.data[2]['id'] == 'a' * 32

def test_special_fields(self):
self.login_as(user=self.user)
project = self.create_project()
Expand Down

0 comments on commit 80227d6

Please sign in to comment.