Skip to content

Commit 30acf63

Browse files
committed
feat(events-v2) Rough in last_event field for grouped views
When looking at a grouped view we need a singular event to spawn the event modal with. This adds a `last_event` field to the events API that will allow the frontend access to a single event. Right now I'm using MAX(event_id) which is not a proper solution. I wanted to use `argMax` but I couldn't figure out how to do that with the wrappers and snuba API. I've also fixed a 500 error that occurred when the `group` query parameter was empty or a non-integer value. Refs SEN-716
1 parent 0ea81f4 commit 30acf63

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

src/sentry/api/bases/organization_events.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
'issue_title': {
1515
'aggregations': [['anyHeavy', 'title', 'issue_title']],
1616
},
17+
'last_event': {
18+
'aggregations': [['max', 'id', 'last_event']],
19+
},
1720
'last_seen': {
1821
'aggregations': [['max', 'timestamp', 'last_seen']],
1922
},
@@ -31,8 +34,13 @@ class OrganizationEventsEndpointBase(OrganizationEndpoint):
3134
def get_snuba_query_args(self, request, organization):
3235
params = self.get_filter_params(request, organization)
3336

34-
group_ids = set(map(int, request.GET.getlist('group')))
37+
group_ids = request.GET.getlist('group')
3538
if group_ids:
39+
try:
40+
group_ids = set(map(int, filter(None, group_ids)))
41+
except ValueError:
42+
raise OrganizationEventsError('Invalid group parameter. Values must be numbers')
43+
3644
projects = Project.objects.filter(
3745
organization=organization,
3846
group__id__in=group_ids,
@@ -63,6 +71,8 @@ def get_snuba_query_args_v2(self, request, organization, params):
6371
query = request.GET.get('query')
6472
try:
6573
snuba_args = get_snuba_query_args(query=query, params=params)
74+
except ValueError as exc:
75+
raise OrganizationEventsError(exc.message)
6676
except InvalidSearchQuery as exc:
6777
raise OrganizationEventsError(exc.message)
6878

tests/snuba/api/endpoints/test_organization_events.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,36 @@ def test_no_projects(self):
729729
assert response.status_code == 200, response.content
730730
assert len(response.data['data']) == 0
731731

732+
def test_groupid_filter(self):
733+
url = reverse(
734+
'sentry-api-0-organization-events-stats',
735+
kwargs={
736+
'organization_slug': self.organization.slug,
737+
}
738+
)
739+
url = '%s?%s' % (url, urlencode({
740+
'start': self.day_ago.isoformat()[:19],
741+
'end': (self.day_ago + timedelta(hours=1, minutes=59)).isoformat()[:19],
742+
'interval': '1h',
743+
'group': self.group.id
744+
}))
745+
response = self.client.get(url, format='json')
746+
747+
assert response.status_code == 200, response.content
748+
assert len(response.data['data'])
749+
750+
def test_groupid_filter_invalid_value(self):
751+
url = reverse(
752+
'sentry-api-0-organization-events-stats',
753+
kwargs={
754+
'organization_slug': self.organization.slug,
755+
}
756+
)
757+
url = '%s?group=not-a-number' % (url,)
758+
response = self.client.get(url, format='json')
759+
760+
assert response.status_code == 400, response.content
761+
732762
def test_user_count(self):
733763
self.create_event(
734764
event_id='d' * 32,

tests/snuba/api/endpoints/test_organization_events_v2.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def test_groupby(self):
204204
def test_special_fields(self):
205205
self.login_as(user=self.user)
206206
project = self.create_project()
207-
self.store_event(
207+
event1 = self.store_event(
208208
data={
209209
'event_id': 'a' * 32,
210210
'timestamp': self.min_ago,
@@ -226,7 +226,7 @@ def test_special_fields(self):
226226
},
227227
project_id=project.id,
228228
)
229-
self.store_event(
229+
event3 = self.store_event(
230230
data={
231231
'event_id': 'c' * 32,
232232
'timestamp': self.min_ago,
@@ -245,7 +245,7 @@ def test_special_fields(self):
245245
self.url,
246246
format='json',
247247
data={
248-
'field': ['issue_title', 'event_count', 'user_count'],
248+
'field': ['issue_title', 'event_count', 'user_count', 'last_event'],
249249
'groupby': ['issue.id', 'project.id'],
250250
'orderby': 'issue.id'
251251
},
@@ -256,6 +256,8 @@ def test_special_fields(self):
256256
assert response.data[0]['issue.id'] == groups[0].id
257257
assert response.data[0]['event_count'] == 1
258258
assert response.data[0]['user_count'] == 1
259+
assert response.data[0]['last_event'] == event1.event_id
259260
assert response.data[1]['issue.id'] == groups[1].id
260261
assert response.data[1]['event_count'] == 2
261262
assert response.data[1]['user_count'] == 2
263+
assert response.data[1]['last_event'] == event3.event_id

0 commit comments

Comments
 (0)