|
3 | 3 | from copy import deepcopy
|
4 | 4 | from rest_framework.exceptions import PermissionDenied
|
5 | 5 | import six
|
| 6 | +from enum import Enum |
6 | 7 |
|
7 | 8 | from sentry import features
|
8 | 9 | from sentry.api.bases import OrganizationEndpoint, OrganizationEventsError
|
|
28 | 29 | }
|
29 | 30 |
|
30 | 31 |
|
| 32 | +class Direction(Enum): |
| 33 | + NEXT = 0 |
| 34 | + PREV = 1 |
| 35 | + |
| 36 | + |
31 | 37 | class OrganizationEventsEndpointBase(OrganizationEndpoint):
|
32 | 38 |
|
33 | 39 | def get_snuba_query_args(self, request, organization):
|
@@ -111,55 +117,51 @@ def get_snuba_query_args_v2(self, request, organization, params):
|
111 | 117 | 'Boolean search operator OR and AND not allowed in this search.')
|
112 | 118 | return snuba_args
|
113 | 119 |
|
114 |
| - def next_event_id(self, request, organization, snuba_args, event): |
| 120 | + def next_event_id(self, *args): |
115 | 121 | """
|
116 | 122 | Returns the next event ID if there is a subsequent event matching the
|
117 | 123 | conditions provided
|
118 | 124 | """
|
| 125 | + return self._get_next_or_prev_id(Direction.NEXT, *args) |
119 | 126 |
|
120 |
| - conditions = snuba_args['conditions'][:] |
121 |
| - conditions.extend([ |
122 |
| - ['timestamp', '>=', event.timestamp], |
123 |
| - [['timestamp', '>', event.timestamp], ['event_id', '>', event.event_id]] |
124 |
| - ]) |
125 |
| - |
126 |
| - result = snuba.raw_query( |
127 |
| - start=max(event.datetime, snuba_args['start']), |
128 |
| - end=snuba_args['end'], |
129 |
| - selected_columns=['event_id'], |
130 |
| - conditions=conditions, |
131 |
| - filter_keys=snuba_args['filter_keys'], |
132 |
| - orderby=['timestamp', 'event_id'], |
133 |
| - limit=1, |
134 |
| - referrer='api.organization-events.next_event_id', |
135 |
| - ) |
136 |
| - |
137 |
| - if 'error' in result or len(result['data']) == 0: |
138 |
| - return None |
139 |
| - |
140 |
| - return six.text_type(result['data'][0]['event_id']) |
141 |
| - |
142 |
| - def prev_event_id(self, request, organization, snuba_args, event): |
| 127 | + def prev_event_id(self, *args): |
143 | 128 | """
|
144 | 129 | Returns the previous event ID if there is a previous event matching the
|
145 | 130 | conditions provided
|
146 | 131 | """
|
| 132 | + return self._get_next_or_prev_id(Direction.PREV, *args) |
| 133 | + |
| 134 | + def _get_next_or_prev_id(self, direction, request, organization, snuba_args, event): |
| 135 | + if (direction == Direction.NEXT): |
| 136 | + time_condition = [ |
| 137 | + ['timestamp', '>=', event.timestamp], |
| 138 | + [['timestamp', '>', event.timestamp], ['event_id', '>', event.event_id]] |
| 139 | + ] |
| 140 | + orderby = ['timestamp', 'event_id'] |
| 141 | + start = max(event.datetime, snuba_args['start']) |
| 142 | + end = snuba_args['end'] |
| 143 | + |
| 144 | + else: |
| 145 | + time_condition = [ |
| 146 | + ['timestamp', '<=', event.timestamp], |
| 147 | + [['timestamp', '<', event.timestamp], ['event_id', '<', event.event_id]] |
| 148 | + ] |
| 149 | + orderby = ['-timestamp', '-event_id'] |
| 150 | + start = snuba_args['start'] |
| 151 | + end = min(event.datetime, snuba_args['end']) |
147 | 152 |
|
148 | 153 | conditions = snuba_args['conditions'][:]
|
149 |
| - conditions.extend([ |
150 |
| - ['timestamp', '<=', event.timestamp], |
151 |
| - [['timestamp', '<', event.timestamp], ['event_id', '<', event.event_id]] |
152 |
| - ]) |
| 154 | + conditions.extend(time_condition) |
153 | 155 |
|
154 | 156 | result = snuba.raw_query(
|
155 |
| - start=snuba_args['start'], |
156 |
| - end=min(event.datetime, snuba_args['end']), |
| 157 | + start=start, |
| 158 | + end=end, |
157 | 159 | selected_columns=['event_id'],
|
158 | 160 | conditions=conditions,
|
159 | 161 | filter_keys=snuba_args['filter_keys'],
|
160 |
| - orderby=['-timestamp', '-event_id'], |
| 162 | + orderby=orderby, |
161 | 163 | limit=1,
|
162 |
| - referrer='api.organization-events.prev_event_id', |
| 164 | + referrer='api.organization-events.next-or-prev-id', |
163 | 165 | )
|
164 | 166 |
|
165 | 167 | if 'error' in result or len(result['data']) == 0:
|
|
0 commit comments