-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Expand file tree
/
Copy pathgroup_details.py
More file actions
427 lines (383 loc) · 17.5 KB
/
group_details.py
File metadata and controls
427 lines (383 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import functools
import logging
from collections.abc import Sequence
from datetime import timedelta
from typing import Any
from django.utils import timezone
from rest_framework.request import Request
from rest_framework.response import Response
from sentry import features, tagstore, tsdb
from sentry.api import client
from sentry.api.api_publish_status import ApiPublishStatus
from sentry.api.base import region_silo_endpoint
from sentry.api.helpers.environments import get_environment_func, get_environments
from sentry.api.helpers.group_index import (
delete_group_list,
get_first_last_release,
prep_search,
update_groups_with_search_fn,
)
from sentry.api.serializers import GroupSerializer, GroupSerializerSnuba, serialize
from sentry.api.serializers.models.group_stream import get_actions, get_available_issue_plugins
from sentry.api.serializers.models.plugin import PluginSerializer
from sentry.integrations.api.serializers.models.external_issue import ExternalIssueSerializer
from sentry.integrations.models.external_issue import ExternalIssue
from sentry.issues.constants import get_issue_tsdb_group_model
from sentry.issues.endpoints.bases.group import GroupEndpoint
from sentry.issues.escalating.escalating_group_forecast import EscalatingGroupForecast
from sentry.models.activity import Activity
from sentry.models.eventattachment import EventAttachment
from sentry.models.group import Group
from sentry.models.groupinbox import get_inbox_details
from sentry.models.grouplink import GroupLink
from sentry.models.groupowner import get_owner_details
from sentry.models.groupseen import GroupSeen
from sentry.models.groupsubscription import GroupSubscriptionManager
from sentry.models.userreport import UserReport
from sentry.plugins.base import plugins
from sentry.ratelimits.config import RateLimitConfig
from sentry.sentry_apps.api.serializers.platform_external_issue import (
PlatformExternalIssueSerializer,
)
from sentry.sentry_apps.models.platformexternalissue import PlatformExternalIssue
from sentry.tasks.post_process import fetch_buffered_group_stats
from sentry.types.ratelimit import RateLimit, RateLimitCategory
from sentry.users.services.user.service import user_service
from sentry.utils import metrics
delete_logger = logging.getLogger("sentry.deletions.api")
def get_group_global_count(group: Group) -> str:
fetch_buffered_group_stats(group)
return str(group.times_seen_with_pending)
@region_silo_endpoint
class GroupDetailsEndpoint(GroupEndpoint):
publish_status = {
"DELETE": ApiPublishStatus.PRIVATE,
"GET": ApiPublishStatus.PRIVATE,
"PUT": ApiPublishStatus.PRIVATE,
}
enforce_rate_limit = True
rate_limits = RateLimitConfig(
limit_overrides={
"GET": {
RateLimitCategory.IP: RateLimit(limit=5, window=1),
RateLimitCategory.USER: RateLimit(limit=5, window=1),
RateLimitCategory.ORGANIZATION: RateLimit(limit=5, window=1),
},
"PUT": {
RateLimitCategory.IP: RateLimit(limit=5, window=1),
RateLimitCategory.USER: RateLimit(limit=5, window=1),
RateLimitCategory.ORGANIZATION: RateLimit(limit=5, window=1),
},
"DELETE": {
RateLimitCategory.IP: RateLimit(limit=5, window=5),
RateLimitCategory.USER: RateLimit(limit=5, window=5),
RateLimitCategory.ORGANIZATION: RateLimit(limit=5, window=5),
},
}
)
def _get_seen_by(self, request: Request, group: Group) -> list[dict[str, Any]]:
seen_by = list(GroupSeen.objects.filter(group=group).order_by("-last_seen"))
return [seen for seen in serialize(seen_by, request.user) if seen is not None]
def _get_context_plugins(self, request: Request, group: Group) -> list[dict[str, Any]]:
project = group.project
return serialize(
[
plugin
for plugin in plugins.for_project(project, version=None)
if plugin.has_project_conf()
and hasattr(plugin, "get_custom_contexts")
and plugin.get_custom_contexts()
],
request.user,
PluginSerializer(project),
)
@staticmethod
def __group_hourly_daily_stats(
group: Group, environment_ids: Sequence[int]
) -> tuple[list[list[float]], list[list[float]]]:
model = get_issue_tsdb_group_model(group.issue_category)
now = timezone.now()
hourly_stats = tsdb.backend.rollup(
tsdb.backend.get_range(
model=model,
keys=[group.id],
end=now,
start=now - timedelta(days=1),
environment_ids=environment_ids,
tenant_ids={"organization_id": group.project.organization_id},
),
3600,
)[group.id]
daily_stats = tsdb.backend.rollup(
tsdb.backend.get_range(
model=model,
keys=[group.id],
end=now,
start=now - timedelta(days=30),
environment_ids=environment_ids,
tenant_ids={"organization_id": group.project.organization_id},
),
3600 * 24,
)[group.id]
return hourly_stats, daily_stats
def get(self, request: Request, group: Group) -> Response:
"""
Retrieve an Issue
`````````````````
Return details on an individual issue. This returns the basic stats for
the issue (title, last seen, first seen), some overall numbers (number
of comments, user reports) as well as the summarized event data.
:pparam string organization_id_or_slug: the id or slug of the organization.
:pparam string issue_id: the ID of the issue to retrieve.
:auth: required
"""
from sentry.utils import snuba
try:
# TODO(dcramer): handle unauthenticated/public response
organization = group.project.organization
environments = get_environments(request, organization)
environment_ids = [e.id for e in environments]
expand = request.GET.getlist("expand", [])
collapse = request.GET.getlist("collapse", [])
# WARNING: the rest of this endpoint relies on this serializer
# populating the cache SO don't move this :)
data = serialize(
group, request.user, GroupSerializerSnuba(environment_ids=environment_ids)
)
# TODO: these probably should be another endpoint
activity = Activity.objects.get_activities_for_group(group, 100)
seen_by = self._get_seen_by(request, group)
if "release" not in collapse:
first_release, last_release = get_first_last_release(request, group)
data.update(
{
"firstRelease": first_release,
"lastRelease": last_release,
}
)
if "tags" not in collapse:
tags = tagstore.backend.get_group_tag_keys(
group,
environment_ids,
limit=100,
tenant_ids={"organization_id": group.project.organization_id},
)
data.update(
{
"tags": sorted(serialize(tags, request.user), key=lambda x: x["name"]),
}
)
user_reports = (
UserReport.objects.filter(group_id=group.id)
if not environment_ids
else UserReport.objects.filter(
group_id=group.id, environment_id__in=environment_ids
)
)
hourly_stats, daily_stats = self.__group_hourly_daily_stats(group, environment_ids)
if "inbox" in expand:
inbox_map = get_inbox_details([group])
inbox_reason = inbox_map.get(group.id)
data.update({"inbox": inbox_reason})
if "owners" in expand:
owner_details = get_owner_details([group])
owners = owner_details.get(group.id)
data.update({"owners": owners})
if "forecast" in expand:
fetched_forecast = EscalatingGroupForecast.fetch(group.project_id, group.id)
if fetched_forecast:
fetched_forecast_dict = fetched_forecast.to_dict()
data.update(
{
"forecast": {
"data": fetched_forecast_dict.get("forecast"),
"date_added": fetched_forecast_dict.get("date_added"),
}
}
)
if "integrationIssues" in expand:
external_issues = ExternalIssue.objects.filter(
id__in=GroupLink.objects.filter(group_id__in=[group.id]).values_list(
"linked_id", flat=True
),
)
integration_issues = serialize(
external_issues,
request.user,
serializer=ExternalIssueSerializer(),
)
data.update({"integrationIssues": integration_issues})
if "sentryAppIssues" in expand:
platform_external_issues = PlatformExternalIssue.objects.filter(group_id=group.id)
sentry_app_issues = serialize(
list(platform_external_issues),
request.user,
serializer=PlatformExternalIssueSerializer(),
)
data.update({"sentryAppIssues": sentry_app_issues})
if "latestEventHasAttachments" in expand:
if not features.has(
"organizations:event-attachments",
group.project.organization,
actor=request.user,
):
metrics.incr(
"group.get.http_response",
sample_rate=1.0,
tags={
"status": 404,
"detail": "group_details:get:no_attachments_feature_flag",
},
)
return self.respond(status=404)
latest_event = group.get_latest_event()
if latest_event is not None:
num_attachments = EventAttachment.objects.filter(
project_id=latest_event.project_id, event_id=latest_event.event_id
).count()
data.update({"latestEventHasAttachments": num_attachments > 0})
data.update(
{
"activity": serialize(activity, request.user),
"seenBy": seen_by,
"pluginActions": get_actions(group),
"pluginIssues": get_available_issue_plugins(group),
"pluginContexts": self._get_context_plugins(request, group),
"userReportCount": user_reports.count(),
"stats": {"24h": hourly_stats, "30d": daily_stats},
"count": get_group_global_count(group),
}
)
participants = user_service.serialize_many(
filter={"user_ids": GroupSubscriptionManager.get_participating_user_ids(group)},
as_user=request.user,
)
for participant in participants:
participant["type"] = "user"
data.update({"participants": participants})
metrics.incr(
"group.get.http_response",
sample_rate=1.0,
tags={"status": 200, "detail": "group_details:get:response"},
)
return Response(data)
except snuba.RateLimitExceeded:
metrics.incr(
"group.get.http_response",
sample_rate=1.0,
tags={"status": 429, "detail": "group_details:get:snuba.RateLimitExceeded"},
)
raise
except Exception:
metrics.incr(
"group.get.http_response",
sample_rate=1.0,
tags={"status": 500, "detail": "group_details:get:Exception"},
)
raise
def put(self, request: Request, group: Group) -> Response:
"""
Update an Issue
```````````````
Updates an individual issue's attributes. Only the attributes submitted
are modified.
:pparam string issue_id: the ID of the group to retrieve.
:param string status: the new status for the issue. Valid values
are ``"resolved"``, ``resolvedInNextRelease``,
``"unresolved"``, and ``"ignored"``.
:param map statusDetails: additional details about the resolution.
Valid values are ``"inRelease"``, ``"inNextRelease"``,
``"inCommit"``, ``"ignoreDuration"``, ``"ignoreCount"``,
``"ignoreWindow"``, ``"ignoreUserCount"``, and
``"ignoreUserWindow"``.
:param string assignedTo: the user or team that should be assigned to
this issue. Can be of the form ``"<user_id>"``,
``"user:<user_id>"``, ``"<username>"``,
``"<user_primary_email>"``, or ``"team:<team_id>"``.
:param string assignedBy: ``"suggested_assignee"`` | ``"assignee_selector"``
:param boolean hasSeen: in case this API call is invoked with a user
context this allows changing of the flag
that indicates if the user has seen the
event.
:param boolean isBookmarked: in case this API call is invoked with a
user context this allows changing of
the bookmark flag.
:param boolean isSubscribed:
:param boolean isPublic: sets the issue to public or private.
:param string substatus: the new substatus for the issues. Valid values
defined in GroupSubStatus.
:auth: required
"""
try:
discard = request.data.get("discard")
project = group.project
search_fn = functools.partial(prep_search, request, project)
response = update_groups_with_search_fn(
request, [group.id], [project], project.organization_id, search_fn
)
# if action was discard, there isn't a group to serialize anymore
# if response isn't 200, return the response update_groups gave us (i.e. helpful error)
# instead of serializing the updated group
if discard or response.status_code != 200:
return response
# we need to fetch the object against as the bulk mutation endpoint
# only returns a delta, and object mutation returns a complete updated
# entity.
# TODO(dcramer): we should update the API and have this be an explicit
# flag (or remove it entirely) so that delta's are the primary response
# for mutation.
group = Group.objects.get(id=group.id)
serialized = serialize(
group,
request.user,
GroupSerializer(
environment_func=get_environment_func(request, group.project.organization_id)
),
)
metrics.incr(
"group.update.http_response",
sample_rate=1.0,
tags={"status": 200, "detail": "group_details:update:Response"},
)
return Response(serialized, status=response.status_code)
except client.ApiError as e:
metrics.incr(
"group.update.http_response",
sample_rate=1.0,
tags={"status": e.status_code, "detail": "group_details:update:Response"},
)
logging.exception(
"group_details:put client.ApiError",
)
return Response(e.body, status=e.status_code)
def delete(self, request: Request, group: Group) -> Response:
"""
Remove an Issue
```````````````
Removes an individual issue.
:pparam string issue_id: the ID of the issue to delete.
:auth: required
"""
from sentry.utils import snuba
try:
delete_group_list(request, group.project, [group], "delete")
metrics.incr(
"group.delete.http_response",
sample_rate=1.0,
tags={"status": 200, "detail": "group_details:delete:Response"},
)
return Response(status=202)
except snuba.RateLimitExceeded:
metrics.incr(
"group.delete.http_response",
sample_rate=1.0,
tags={"status": 429, "detail": "group_details:delete:snuba.RateLimitExceeded"},
)
raise
except Exception:
metrics.incr(
"group.delete.http_response",
sample_rate=1.0,
tags={"status": 500, "detail": "group_details:delete:Exception"},
)
raise