-
-
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 6 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,21 @@ | ||
| 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 ( | ||
| OPEN_PERIOD_ACTIVITY_TYPE_TO_STRING, | ||
| GroupOpenPeriodActivity, | ||
| ) | ||
| from sentry.types.group import PRIORITY_LEVEL_TO_STRING | ||
|
|
||
|
|
||
| class GroupOpenPeriodActivityResponse(TypedDict): | ||
ceorourke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| id: str | ||
| type: str | ||
| value: str | None | ||
|
|
||
|
|
||
| class GroupOpenPeriodResponse(TypedDict): | ||
|
|
@@ -13,10 +25,41 @@ class GroupOpenPeriodResponse(TypedDict): | |
| duration: timedelta | None | ||
| isOpen: bool | ||
| lastChecked: datetime | ||
| activities: GroupOpenPeriodActivityResponse | None | ||
ceorourke marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @register(GroupOpenPeriodActivity) | ||
| class GroupOpenPeriodActivitySerializer(Serializer): | ||
| def serialize( | ||
| self, obj: GroupOpenPeriodActivity, attrs: Mapping[str, Any], user, **kwargs | ||
| ) -> GroupOpenPeriodActivityResponse: | ||
| return { | ||
| "id": str(obj.id), | ||
| "type": OPEN_PERIOD_ACTIVITY_TYPE_TO_STRING[obj.type], | ||
| "value": PRIORITY_LEVEL_TO_STRING.get(obj.value) 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: | ||
|
|
@@ -27,4 +70,5 @@ def serialize( | |
| "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"), | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.