-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(open periods): Add activities to open period serializer #100627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
64c2e77
aa6a16b
d899983
22e7fa8
dc299c9
6e4db95
e06d98b
de67639
b9b108e
e7e7350
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,18 @@ | ||
| from collections import defaultdict | ||
| from collections.abc import Mapping | ||
| from datetime import datetime, timedelta | ||
| from typing import Any, TypedDict | ||
|
|
||
| from sentry.api.serializers import Serializer, register | ||
| from sentry.api.serializers import Serializer, register, serialize | ||
| from sentry.models.groupopenperiod import GroupOpenPeriod, get_last_checked_for_open_period | ||
| from sentry.models.groupopenperiodactivity import GroupOpenPeriodActivity, OpenPeriodActivityType | ||
| from sentry.types.group import PriorityLevel | ||
|
|
||
|
|
||
| class GroupOpenPeriodActivityResponse(TypedDict): | ||
| id: str | ||
| type: str | ||
| value: str | None | ||
|
|
||
|
|
||
| class GroupOpenPeriodResponse(TypedDict): | ||
|
|
@@ -13,18 +22,50 @@ class GroupOpenPeriodResponse(TypedDict): | |
| duration: timedelta | None | ||
| isOpen: bool | ||
| lastChecked: datetime | ||
| activities: list[GroupOpenPeriodActivityResponse] | None | ||
|
|
||
|
|
||
| @register(GroupOpenPeriodActivity) | ||
| class GroupOpenPeriodActivitySerializer(Serializer): | ||
| def serialize( | ||
| self, obj: GroupOpenPeriodActivity, attrs: Mapping[str, Any], user, **kwargs | ||
| ) -> GroupOpenPeriodActivityResponse: | ||
| return GroupOpenPeriodActivityResponse( | ||
| id=str(obj.id), | ||
| type=OpenPeriodActivityType(obj.type).to_str(), | ||
| value=PriorityLevel(obj.value).to_str() if obj.value else None, | ||
| ) | ||
|
|
||
|
|
||
| @register(GroupOpenPeriod) | ||
| class GroupOpenPeriodSerializer(Serializer): | ||
| def get_attrs(self, item_list, user, **kwargs): | ||
| result: defaultdict[GroupOpenPeriod, dict[str, list[GroupOpenPeriodActivityResponse]]] = ( | ||
| defaultdict(dict) | ||
| ) | ||
| activities = GroupOpenPeriodActivity.objects.filter( | ||
| group_open_period__in=item_list | ||
| ).order_by("id") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could also order by date_added, but this works too :) |
||
|
|
||
| gopas = defaultdict(list) | ||
| for activity, serialized_activity in zip( | ||
| activities, serialize(list(activities), user=user, **kwargs) | ||
| ): | ||
| gopas[activity.group_open_period].append(serialized_activity) | ||
| for item in item_list: | ||
| result[item]["activities"] = gopas[item][:100] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Overfetching and Serialization IssuesThe
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. django lazy loads them |
||
|
|
||
| return result | ||
|
|
||
| def serialize( | ||
| self, obj: GroupOpenPeriod, attrs: Mapping[str, Any], user, **kwargs | ||
| ) -> GroupOpenPeriodResponse: | ||
| return { | ||
| "id": str(obj.id), | ||
| "start": obj.date_started, | ||
| "end": obj.date_ended, | ||
| "duration": obj.date_ended - obj.date_started if obj.date_ended else None, | ||
| "isOpen": obj.date_ended is None, | ||
| "lastChecked": get_last_checked_for_open_period(obj.group), | ||
| } | ||
| return GroupOpenPeriodResponse( | ||
| id=str(obj.id), | ||
| start=obj.date_started, | ||
| end=obj.date_ended, | ||
| duration=obj.date_ended - obj.date_started if obj.date_ended else None, | ||
| isOpen=obj.date_ended is None, | ||
| lastChecked=get_last_checked_for_open_period(obj.group), | ||
| activities=attrs.get("activities"), | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Serializer Fails to Handle Zero Priority Levels
The
GroupOpenPeriodActivitySerializerincorrectly treatsobj.valueas falsy when its value is0. Since0is a validPriorityLevel, this causes the serializer to returnNoneinstead of the string representation for priority level0.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0 is not a valid
PriorityLevelhttps://github.com/getsentry/sentry/blob/master/src/sentry/types/group.py#L63-L66