Skip to content

Commit

Permalink
support some special fields on the api
Browse files Browse the repository at this point in the history
  • Loading branch information
lynnagara committed May 29, 2019
1 parent 90858db commit 990c19e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 47 deletions.
33 changes: 30 additions & 3 deletions src/sentry/api/bases/organization_events.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
from __future__ import absolute_import

from copy import deepcopy
from rest_framework.exceptions import PermissionDenied

from sentry import features
from sentry.api.bases import OrganizationEndpoint, OrganizationEventsError
from sentry.api.event_search import get_snuba_query_args, InvalidSearchQuery
from sentry.models.project import Project

# We support 4 "special fields" on the v2 events API which perform some
# additional calculations over aggregated event data
SPECIAL_FIELDS = {
'issue_title': {
'aggregations': [['anyHeavy', 'title', 'issue_title']],
},
'last_seen': {
'aggregations': [['max', 'timestamp', 'last_seen']],
},
'event_count': {
'aggregations': [['uniq', 'id', 'event_count']],
},
'user_count': {
'aggregations': [['uniq', 'user', 'user_count']],
},
}


class OrganizationEventsEndpointBase(OrganizationEndpoint):

Expand Down Expand Up @@ -49,6 +67,9 @@ def get_snuba_query_args_v2(self, request, organization, params):
raise OrganizationEventsError(exc.message)

fields = request.GET.getlist('field')[:]
aggregations = [agg.split(',') for agg in request.GET.getlist('aggregation')]
groupby = request.GET.getlist('groupby')

if fields:
# If project.name is requested, get the project.id from Snuba so we
# can use this to look up the name in Sentry
Expand All @@ -57,13 +78,19 @@ def get_snuba_query_args_v2(self, request, organization, params):
if 'project.id' not in fields:
fields.append('project.id')

for field in SPECIAL_FIELDS:
if field in fields:
special_field = deepcopy(SPECIAL_FIELDS[field])
fields.remove(field)
fields.extend(special_field.get('fields', []))
aggregations.extend(special_field.get('aggregations', []))
groupby.extend(special_field.get('groupby', []))

snuba_args['selected_columns'] = fields

aggregations = request.GET.getlist('aggregation')
if aggregations:
snuba_args['aggregations'] = [aggregation.split(',') for aggregation in aggregations]
snuba_args['aggregations'] = aggregations

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

Expand Down
26 changes: 7 additions & 19 deletions src/sentry/static/sentry/app/views/organizationEventsV2/data.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const ALL_VIEWS = deepFreeze([
name: 'All Events',
data: {
fields: ['event', 'event.type', 'project', 'user', 'time'],
aggregations: [],
sort: '-timestamp',
},
tags: [
Expand All @@ -34,7 +33,8 @@ export const ALL_VIEWS = deepFreeze([
id: 'errors',
name: 'Errors',
data: {
fields: ['error', 'event_count', 'user_count', 'project', 'last_seen'],
fields: ['issue_title', 'event_count', 'user_count', 'project', 'last_seen'],
groupby: ['issue.id', 'project.id'],
sort: '-last_seen',
},
tags: ['error.type', 'project.name'],
Expand All @@ -43,7 +43,8 @@ export const ALL_VIEWS = deepFreeze([
id: 'csp',
name: 'CSP',
data: {
fields: ['csp', 'event_count', 'user_count', 'project', 'last_seen'],
fields: ['issue_title', 'event_count', 'user_count', 'project', 'last_seen'],
groupby: ['issue.id', 'project.id'],
sort: '-last_seen',
},
tags: [
Expand Down Expand Up @@ -118,31 +119,18 @@ export const SPECIAL_FIELDS = {
</Container>
),
},
error: {
title: 'error',
groupby: ['issue.id', 'project.id'],
aggregations: [['anyHeavy', 'title', 'issue_group']],
renderFunc: data => <Container>{data.issue_group}</Container>,
},
csp: {
title: 'csp',
groupby: ['issue.id', 'project.id'],
aggregations: [['anyHeavy', 'title', 'issue_group']],
renderFunc: data => <Container>{data.issue_group}</Container>,
},
event_count: {
title: 'events',
aggregations: [['uniq', 'id', 'event_count']],
fields: ['event_count'],
renderFunc: data => <Container>{data.event_count}</Container>,
},
user_count: {
title: 'users',
aggregations: [['uniq', 'user', 'user_count']],
fields: ['user_count'],
renderFunc: data => <Container>{data.user_count}</Container>,
},
last_seen: {
title: 'last seen',
aggregations: [['max', 'timestamp', 'last_seen']],
fields: ['last_seen'],
renderFunc: data => (
<Container>
<DynamicWrapper value={<StyledDateTime date={data.last_seen} />} fixed="time" />
Expand Down
21 changes: 0 additions & 21 deletions tests/js/spec/views/organizationEventsV2/utils.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,4 @@ describe('getQuery()', function() {
'issue.id',
]);
});

it('handles grouping by issue', function() {
const view = {
id: 'test',
name: 'test view',
data: {
fields: ['error', 'project'],
sort: 'project.id',
},
tags: [],
};

const query = getQuery(view, {});

expect(query).toEqual({
aggregation: [['anyHeavy', 'title', 'issue_group']],
field: ['project.name'],
groupby: ['issue.id', 'project.id'],
sort: 'project.id',
});
});
});
8 changes: 4 additions & 4 deletions tests/snuba/api/endpoints/test_organization_events_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_multi_project_feature_gate_rejection(self):
)

query = {
'fields': ['id', 'project.id'],
'field': ['id', 'project.id'],
'project': [project.id, project2.id],
}
with self.feature({'organizations:events-v2': True, 'organizations:global-views': False}):
Expand Down Expand Up @@ -201,7 +201,7 @@ def test_groupby(self):
assert response.data[1]['project.id'] == project.id
assert response.data[1]['environment'] == 'staging'

def test_event_and_user_counts(self):
def test_special_fields(self):
self.login_as(user=self.user)
project = self.create_project()
self.store_event(
Expand Down Expand Up @@ -245,8 +245,8 @@ def test_event_and_user_counts(self):
self.url,
format='json',
data={
'groupby': ['issue.id'],
'aggregation': ['uniq,id,event_count', 'uniq,sentry:user,user_count'],
'field': ['issue_title', 'event_count', 'user_count'],
'groupby': ['issue.id', 'project.id'],
'orderby': 'issue.id'
},
)
Expand Down

0 comments on commit 990c19e

Please sign in to comment.