/
serializers.py
209 lines (166 loc) · 6.67 KB
/
serializers.py
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
from collections import defaultdict
from datetime import datetime
from typing import Any, Dict, List
from django.db.models import prefetch_related_objects
from typing_extensions import TypedDict
from sentry.api.serializers import ProjectSerializerResponse, Serializer, register, serialize
from sentry.models import Project
from sentry.monitors.utils import fetch_associated_groups
from .models import Monitor, MonitorCheckIn, MonitorEnvironment, MonitorStatus
class MonitorEnvironmentSerializerResponse(TypedDict):
name: str
status: str
dateCreated: datetime
lastCheckIn: datetime
nextCheckIn: datetime
@register(MonitorEnvironment)
class MonitorEnvironmentSerializer(Serializer):
def serialize(self, obj, attrs, user, **kwargs) -> MonitorEnvironmentSerializerResponse:
return {
"name": obj.environment.name,
"status": obj.get_status_display(),
"lastCheckIn": obj.last_checkin,
"nextCheckIn": obj.next_checkin,
"dateCreated": obj.monitor.date_added,
}
class MonitorSerializerResponseOptional(TypedDict, total=False):
alertRule: Any # TODO: Find out what type this is
class MonitorSerializerResponse(MonitorSerializerResponseOptional):
id: str
name: str
slug: str
status: str
type: str
config: Any
dateCreated: datetime
project: ProjectSerializerResponse
environments: MonitorEnvironmentSerializerResponse
@register(Monitor)
class MonitorSerializer(Serializer):
def __init__(self, environments=None, expand=None):
self.environments = environments
self.expand = expand
def get_attrs(self, item_list, user, **kwargs):
# TODO(dcramer): assert on relations
projects = {
p["id"]: p
for p in serialize(
list(Project.objects.filter(id__in=[i.project_id for i in item_list])), user
)
}
serialized_monitor_environments = defaultdict(list)
monitor_environments = (
MonitorEnvironment.objects.filter(monitor__in=item_list)
.select_related("environment")
.order_by("-last_checkin")
.exclude(
status__in=[MonitorStatus.PENDING_DELETION, MonitorStatus.DELETION_IN_PROGRESS]
)
)
if self.environments:
monitor_environments = monitor_environments.filter(environment__in=self.environments)
for monitor_environment in monitor_environments:
# individually serialize as related objects are prefetched
serialized_monitor_environments[monitor_environment.monitor_id].append(
serialize(
monitor_environment,
user,
)
)
environment_data = {
str(item.id): serialized_monitor_environments.get(item.id, []) for item in item_list
}
attrs = {
item: {
"project": projects[str(item.project_id)] if item.project_id else None,
"environments": environment_data[str(item.id)],
}
for item in item_list
}
if self._expand("alertRule"):
for item in item_list:
attrs[item]["alertRule"] = item.get_alert_rule_data()
return attrs
def serialize(self, obj, attrs, user, **kwargs) -> MonitorSerializerResponse:
config = obj.config.copy()
if "schedule_type" in config:
config["schedule_type"] = obj.get_schedule_type_display()
result: MonitorSerializerResponse = {
"id": str(obj.guid),
"status": obj.get_status_display(),
"type": obj.get_type_display(),
"name": obj.name,
"slug": obj.slug,
"config": config,
"dateCreated": obj.date_added,
"project": attrs["project"],
"environments": attrs["environments"],
}
if self._expand("alertRule"):
result["alertRule"] = attrs["alertRule"]
return result
def _expand(self, key) -> bool:
if self.expand is None:
return False
return key in self.expand
class MonitorCheckInSerializerResponseOptional(TypedDict, total=False):
groups: List[str]
class MonitorCheckInSerializerResponse(MonitorCheckInSerializerResponseOptional):
id: str
environment: str
status: str
duration: int
dateCreated: datetime
attachmentId: str
expectedTime: datetime
monitorConfig: Any
@register(MonitorCheckIn)
class MonitorCheckInSerializer(Serializer):
def __init__(self, start=None, end=None, expand=None, organization_id=None, project_id=None):
self.start = start # timestamp of the beginning of the specified date range
self.end = end # timestamp of the end of the specified date range
self.expand = expand
self.organization_id = organization_id
self.project_id = project_id
def get_attrs(self, item_list, user, **kwargs):
# prefetch monitor environment data
prefetch_related_objects(item_list, "monitor_environment__environment")
attrs = {}
if self._expand("groups") and self.start and self.end:
# aggregate all the trace_ids in the given set of check-ins
trace_ids = []
trace_groups: Dict[str, List[Dict[str, int]]] = defaultdict(list)
for item in item_list:
if item.trace_id:
trace_ids.append(item.trace_id.hex)
if trace_ids:
trace_groups = fetch_associated_groups(
trace_ids, self.organization_id, self.project_id, self.start, self.end
)
attrs = {
item: {
"groups": trace_groups.get(item.trace_id.hex, []) if item.trace_id else [],
}
for item in item_list
}
return attrs
def serialize(self, obj, attrs, user, **kwargs) -> MonitorCheckInSerializerResponse:
result: MonitorCheckInSerializerResponse = {
"id": str(obj.guid),
"environment": obj.monitor_environment.environment.name
if obj.monitor_environment
else None,
"status": obj.get_status_display(),
"duration": obj.duration,
"dateCreated": obj.date_added,
"attachmentId": obj.attachment_id,
"expectedTime": obj.expected_time,
"monitorConfig": obj.monitor_config or {},
}
if self._expand("groups"):
result["groups"] = attrs.get("groups", [])
return result
def _expand(self, key) -> bool:
if self.expand is None:
return False
return key in self.expand