-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathserializers.py
More file actions
219 lines (192 loc) · 8.54 KB
/
serializers.py
File metadata and controls
219 lines (192 loc) · 8.54 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
import json
import logging
from asgiref.sync import async_to_sync
from rest_framework import serializers
from temporalio.client import WorkflowExecutionStatus
from temporalio.service import RPCError, RPCStatusCode
from posthog.temporal.ai.video_segment_clustering.constants import clustering_workflow_id
from posthog.temporal.common.client import sync_connect
from .models import SignalReport, SignalReportArtefact, SignalSourceConfig
from .report_generation.resolve_reviewers import enrich_reviewer_dicts_with_org_members
logger = logging.getLogger(__name__)
# Maps (source_product, source_type) → (ExternalDataSourceType value, schema name)
_DATA_IMPORT_SOURCE_MAP: dict[tuple[str, str], tuple[str, str]] = {
(SignalSourceConfig.SourceProduct.GITHUB, SignalSourceConfig.SourceType.ISSUE): ("Github", "issues"),
(SignalSourceConfig.SourceProduct.LINEAR, SignalSourceConfig.SourceType.ISSUE): ("Linear", "issues"),
(SignalSourceConfig.SourceProduct.ZENDESK, SignalSourceConfig.SourceType.TICKET): ("Zendesk", "tickets"),
}
class SignalSourceConfigSerializer(serializers.ModelSerializer):
status = serializers.SerializerMethodField()
class Meta:
model = SignalSourceConfig
fields = [
"id",
"source_product",
"source_type",
"enabled",
"config",
"created_at",
"updated_at",
"status",
]
read_only_fields = ["id", "created_at", "updated_at", "status"]
def get_status(self, obj: SignalSourceConfig) -> str | None:
if obj.source_type == SignalSourceConfig.SourceType.SESSION_ANALYSIS_CLUSTER:
return self._get_clustering_status(obj)
mapping = _DATA_IMPORT_SOURCE_MAP.get((obj.source_product, obj.source_type))
if mapping is None:
return None
ext_source_type, schema_name = mapping
return self._get_data_import_status(obj.team_id, ext_source_type, schema_name)
def _get_clustering_status(self, obj: SignalSourceConfig) -> str | None:
workflow_id = clustering_workflow_id(obj.team_id, obj.id)
try:
client = sync_connect()
handle = client.get_workflow_handle(workflow_id)
desc = async_to_sync(handle.describe)()
status = desc.status
if status == WorkflowExecutionStatus.RUNNING:
return "running"
if status == WorkflowExecutionStatus.COMPLETED:
return "completed"
if status in (
WorkflowExecutionStatus.FAILED,
WorkflowExecutionStatus.TERMINATED,
WorkflowExecutionStatus.CANCELED,
WorkflowExecutionStatus.TIMED_OUT,
):
return "failed"
return None
except RPCError as e:
if e.status == RPCStatusCode.NOT_FOUND:
return None
logger.warning("Failed to fetch clustering workflow status: %s", e)
return None
def _get_data_import_status(self, team_id: int, ext_source_type: str, schema_name: str) -> str | None:
from products.data_warehouse.backend.models.external_data_schema import ExternalDataSchema
schema = (
ExternalDataSchema.objects.filter(
team_id=team_id,
source__source_type=ext_source_type,
name=schema_name,
)
.exclude(source__deleted=True)
.first()
)
if schema is None:
return None
if schema.status == ExternalDataSchema.Status.RUNNING:
return "running"
if schema.status == ExternalDataSchema.Status.COMPLETED:
return "completed"
if schema.status in (
ExternalDataSchema.Status.FAILED,
ExternalDataSchema.Status.BILLING_LIMIT_REACHED,
ExternalDataSchema.Status.BILLING_LIMIT_TOO_LOW,
):
return "failed"
return None
def validate(self, attrs: dict) -> dict:
source_product = attrs.get("source_product", getattr(self.instance, "source_product", None))
config = attrs.get("config", {})
if source_product == SignalSourceConfig.SourceProduct.SESSION_REPLAY and config:
recording_filters = config.get("recording_filters")
if recording_filters is not None and not isinstance(recording_filters, dict):
raise serializers.ValidationError({"config": "recording_filters must be a JSON object"})
return attrs
class SignalReportSerializer(serializers.ModelSerializer):
artefact_count = serializers.IntegerField(read_only=True)
priority = serializers.SerializerMethodField(
help_text="P0–P4 from the latest priority judgment artefact (when present).",
)
actionability = serializers.SerializerMethodField(
help_text="Actionability choice from the latest actionability judgment artefact (when present).",
)
already_addressed = serializers.SerializerMethodField(
help_text="Whether the issue appears already fixed, from the actionability judgment artefact.",
)
is_suggested_reviewer = serializers.BooleanField(read_only=True, default=False)
class Meta:
model = SignalReport
fields = [
"id",
"title",
"summary",
"status",
"total_weight", # Used for priority scoring
"signal_count", # Used for occurrence count
"signals_at_run", # Snooze threshold: re-promote when signal_count >= this value
"created_at",
"updated_at",
"artefact_count",
"priority",
"actionability",
"already_addressed",
"is_suggested_reviewer",
]
read_only_fields = fields
def _get_actionability_artefact_data(self, obj: SignalReport) -> dict | None:
prefetched = getattr(obj, "prefetched_actionability_artefacts", None)
if prefetched is not None:
art = prefetched[0] if prefetched else None
else:
art = (
obj.artefacts.filter(type=SignalReportArtefact.ArtefactType.ACTIONABILITY_JUDGMENT)
.order_by("-created_at")
.first()
)
if art is None:
return None
try:
data = json.loads(art.content)
except (json.JSONDecodeError, TypeError, ValueError):
return None
return data if isinstance(data, dict) else None
def get_priority(self, obj: SignalReport) -> str | None:
prefetched = getattr(obj, "prefetched_priority_artefacts", None)
if prefetched is not None:
art = prefetched[0] if prefetched else None
else:
art = (
obj.artefacts.filter(type=SignalReportArtefact.ArtefactType.PRIORITY_JUDGMENT)
.order_by("-created_at")
.first()
)
if art is None:
return None
try:
data = json.loads(art.content)
except (json.JSONDecodeError, TypeError, ValueError):
return None
if not isinstance(data, dict):
return None
p = data.get("priority")
return p if isinstance(p, str) else None
def get_actionability(self, obj: SignalReport) -> str | None:
data = self._get_actionability_artefact_data(obj)
if data is None:
return None
# Support both agentic ("actionability") and legacy ("choice") field names
value = data.get("actionability") or data.get("choice")
return value if isinstance(value, str) else None
def get_already_addressed(self, obj: SignalReport) -> bool | None:
data = self._get_actionability_artefact_data(obj)
if data is None:
return None
value = data.get("already_addressed")
return value if isinstance(value, bool) else None
class SignalReportArtefactSerializer(serializers.ModelSerializer):
content = serializers.SerializerMethodField()
class Meta:
model = SignalReportArtefact
fields = ["id", "type", "content", "created_at"]
read_only_fields = fields
def get_content(self, obj: SignalReportArtefact) -> dict | list:
try:
parsed = json.loads(obj.content)
except (json.JSONDecodeError, ValueError):
return {}
# Enrich suggested_reviewers with fresh PostHog user info at read time
if obj.type == SignalReportArtefact.ArtefactType.SUGGESTED_REVIEWERS and isinstance(parsed, list):
return enrich_reviewer_dicts_with_org_members(obj.team_id, parsed)
return parsed