-
Notifications
You must be signed in to change notification settings - Fork 557
Expand file tree
/
Copy pathserializers.py
More file actions
442 lines (377 loc) · 15.2 KB
/
Copy pathserializers.py
File metadata and controls
442 lines (377 loc) · 15.2 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
from typing import Any
from django.db import transaction
from django.db.models import QuerySet
from rest_framework import serializers
from core.dataclasses import AuthorData
from environments.models import Environment
from experimentation.dataclasses import RolloutSpec, WarehouseEventStats
from experimentation.metric_definitions import validate_metric_definition
from experimentation.models import (
ExpectedDirection,
Experiment,
ExperimentExposures,
ExperimentMetric,
ExperimentResults,
ExperimentStatus,
Metric,
WarehouseConnection,
WarehouseType,
)
from experimentation.services import (
apply_experiment_rollout,
get_experiment_rollout,
)
from experimentation.types import (
SNOWFLAKE_DEFAULTS,
MetricExperimentResult,
SnowflakeConfig,
)
from features.feature_states.serializers import (
FeatureValueSerializer,
SegmentOverrideMultivariateValueSerializer,
)
from features.feature_types import MULTIVARIATE
from features.models import Feature
from features.multivariate.serializers import NestedMultivariateFeatureOptionSerializer
from features.versioning.dataclasses import MultivariateValueChangeSet
class WarehouseConnectionSerializer(serializers.ModelSerializer): # type: ignore[type-arg]
name = serializers.CharField(max_length=255, required=False)
config = serializers.JSONField(default=None, required=False, allow_null=True)
total_events_received = serializers.SerializerMethodField()
unique_events_count = serializers.SerializerMethodField()
class Meta:
model = WarehouseConnection
fields = (
"id",
"warehouse_type",
"status",
"name",
"config",
"created_at",
"total_events_received",
"unique_events_count",
)
read_only_fields = ("id", "status", "created_at")
def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:
warehouse_type: str = attrs.get(
"warehouse_type",
getattr(self.instance, "warehouse_type", ""),
)
if "config" not in attrs and self.instance is not None:
return attrs
config: dict[str, Any] | None = attrs.get("config")
if warehouse_type == WarehouseType.SNOWFLAKE:
attrs["config"] = self._validate_snowflake_config(config or {})
elif warehouse_type == WarehouseType.FLAGSMITH:
if config:
raise serializers.ValidationError(
{"config": "Flagsmith warehouse does not accept configuration."}
)
attrs["config"] = None
return attrs
def create(
self,
validated_data: dict[str, Any],
) -> WarehouseConnection:
environment: Environment = validated_data["environment"]
warehouse_type: str = validated_data["warehouse_type"]
if not validated_data.get("name"):
validated_data["name"] = self._generate_name(warehouse_type, environment)
result: WarehouseConnection = super().create(validated_data)
return result
def get_total_events_received(self, obj: WarehouseConnection) -> int | None:
stats: WarehouseEventStats | None = obj.event_stats
return stats.total_events_received if stats else None
def get_unique_events_count(self, obj: WarehouseConnection) -> int | None:
stats: WarehouseEventStats | None = obj.event_stats
return stats.unique_events_count if stats else None
@staticmethod
def _generate_name(warehouse_type: str, environment: Environment) -> str:
label = WarehouseType(warehouse_type).label
return f"{label} Warehouse - {environment.name}"
@staticmethod
def _validate_snowflake_config(config: dict[str, Any]) -> SnowflakeConfig:
account_identifier = config.get("account_identifier", "")
if not account_identifier:
raise serializers.ValidationError(
{"config": {"account_identifier": "This field is required."}}
)
merged: SnowflakeConfig = {
**SNOWFLAKE_DEFAULTS,
**config, # type: ignore[typeddict-item]
}
return merged
class MetricSerializer(serializers.ModelSerializer): # type: ignore[type-arg]
experiments = serializers.SerializerMethodField()
class Meta:
model = Metric
fields = (
"id",
"name",
"description",
"aggregation",
"direction",
"definition",
"experiments",
"created_at",
"updated_at",
)
read_only_fields = (
"id",
"experiments",
"created_at",
"updated_at",
)
def get_experiments(self, metric: Metric) -> list[MetricExperimentResult]:
return [
{
"id": experiment_metric.experiment.id,
"name": experiment_metric.experiment.name,
"status": experiment_metric.experiment.status,
}
for experiment_metric in metric.experiment_metrics.all()
]
def validate_definition(self, definition: object) -> object:
error = validate_metric_definition(definition)
if error:
raise serializers.ValidationError(error)
return definition
class ExperimentMetricSerializer(serializers.ModelSerializer): # type: ignore[type-arg]
metric = serializers.PrimaryKeyRelatedField( # type: ignore[var-annotated]
queryset=Metric.objects.all(),
)
metric_name = serializers.CharField(source="metric.name", read_only=True)
aggregation = serializers.CharField(source="metric.aggregation", read_only=True)
class Meta:
model = ExperimentMetric
fields = (
"id",
"metric",
"metric_name",
"aggregation",
"expected_direction",
"created_at",
)
read_only_fields = ("id", "created_at")
def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:
experiment: Experiment = self.context["experiment"]
if experiment.status == ExperimentStatus.COMPLETED:
raise serializers.ValidationError(
"Cannot modify metrics of a completed experiment."
)
metric: Metric = attrs.get("metric", getattr(self.instance, "metric", None))
if metric.environment_id != experiment.environment_id:
raise serializers.ValidationError(
{"metric": "Metric must belong to the experiment's environment."}
)
attached = experiment.experiment_metrics.all()
if isinstance(self.instance, ExperimentMetric):
attached = attached.exclude(pk=self.instance.pk)
if "metric" in attrs and attached.filter(metric=metric).exists():
raise serializers.ValidationError(
{"metric": "Metric is already attached to this experiment."}
)
return attrs
class _EnvironmentScopedMetricField(serializers.PrimaryKeyRelatedField[Metric]):
def get_queryset(self) -> QuerySet[Metric]:
queryset: QuerySet[Metric] = Metric.objects.filter(
environment=self.context["environment"]
)
return queryset
class ExperimentMetricInlineSerializer(serializers.Serializer): # type: ignore[type-arg]
metric = _EnvironmentScopedMetricField()
expected_direction = serializers.ChoiceField(choices=ExpectedDirection.choices)
class ExperimentRolloutSerializer(serializers.Serializer): # type: ignore[type-arg]
enabled = serializers.BooleanField(required=True)
rollout_percentage = serializers.FloatField(
required=True, min_value=0, max_value=100
)
feature_state_value = FeatureValueSerializer(required=True)
multivariate_feature_state_values = SegmentOverrideMultivariateValueSerializer(
many=True, required=False
)
@staticmethod
def to_spec(data: dict[str, Any], request: Any) -> RolloutSpec:
value = data["feature_state_value"]
return RolloutSpec(
enabled=data["enabled"],
rollout_percentage=data["rollout_percentage"],
feature_state_value=value["value"],
value_type=value["type"],
multivariate_values=[
MultivariateValueChangeSet(
multivariate_feature_option_id=mv["multivariate_feature_option"],
percentage_allocation=mv["percentage_allocation"],
)
for mv in data.get("multivariate_feature_state_values", [])
],
author=AuthorData.from_request(request),
)
class ExperimentSerializer(serializers.ModelSerializer): # type: ignore[type-arg]
# Annotated with the common base type so ExperimentListSerializer can
# override the field with a read-only representation.
metrics: serializers.BaseSerializer[Any] = ExperimentMetricInlineSerializer(
many=True,
required=False,
write_only=True,
)
experiment_rollout: Any = ExperimentRolloutSerializer(
required=False, write_only=True
)
class Meta:
model = Experiment
fields = (
"id",
"feature",
"name",
"hypothesis",
"status",
"metrics",
"experiment_rollout",
"created_at",
"updated_at",
"started_at",
"ended_at",
)
read_only_fields = (
"id",
"status",
"created_at",
"updated_at",
"started_at",
"ended_at",
)
def validate_feature(self, feature: Any) -> Any:
if feature.type != MULTIVARIATE:
raise serializers.ValidationError(
"Experiments can only be created for multivariate flags."
)
environment: Environment | None = self.context.get("environment")
if environment and feature.project_id != environment.project_id:
raise serializers.ValidationError(
"Feature does not belong to this environment's project."
)
return feature
def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:
if self.instance is not None and "feature" in attrs:
raise serializers.ValidationError(
{"feature": "Cannot change the feature of an existing experiment."}
)
if self.instance is not None and "metrics" in attrs:
raise serializers.ValidationError(
{"metrics": "Cannot change the metrics of an existing experiment."}
)
if self.instance is not None and "experiment_rollout" in attrs:
raise serializers.ValidationError(
{
"experiment_rollout": (
"Cannot change the rollout via this endpoint; "
"use the rollout endpoint instead."
)
}
)
self._validate_metrics(attrs.get("metrics") or [])
return attrs
def _validate_metrics(self, metrics: list[dict[str, Any]]) -> None:
metric_ids = [entry["metric"].id for entry in metrics]
if len(metric_ids) != len(set(metric_ids)):
raise serializers.ValidationError(
{"metrics": "Metric can only be attached once per experiment."}
)
def create(self, validated_data: dict[str, Any]) -> Experiment:
metrics: list[dict[str, Any]] = validated_data.pop("metrics", [])
rollout: dict[str, Any] | None = validated_data.pop("experiment_rollout", None)
with transaction.atomic():
experiment: Experiment = super().create(validated_data)
ExperimentMetric.objects.bulk_create(
ExperimentMetric(
experiment=experiment,
metric=entry["metric"],
expected_direction=entry["expected_direction"],
)
for entry in metrics
)
if rollout is not None:
apply_experiment_rollout(
experiment,
ExperimentRolloutSerializer.to_spec(
rollout, self.context["request"]
),
)
return experiment
class ExperimentFeatureSerializer(serializers.ModelSerializer): # type: ignore[type-arg]
multivariate_options = serializers.SerializerMethodField()
class Meta:
model = Feature
fields = ("id", "name", "type", "initial_value", "multivariate_options")
read_only_fields = fields
def get_multivariate_options(self, feature: Feature) -> list[dict[str, Any]]:
options = NestedMultivariateFeatureOptionSerializer(
feature.multivariate_options.all(), many=True
).data
environment: Environment | None = self.context.get("environment")
if not environment:
raise ValueError(
"ExperimentFeatureSerializer requires 'environment' in context."
)
env_state = (
feature.feature_states.filter(
environment=environment,
identity__isnull=True,
feature_segment__isnull=True,
)
.order_by("-live_from", "-version")
.first()
)
if not env_state:
raise ValueError(
f"No environment feature state found for feature {feature.id} "
f"in environment {environment.id}."
)
alloc_map = dict(
env_state.multivariate_feature_state_values.values_list(
"multivariate_feature_option_id", "percentage_allocation"
)
)
for option in options:
if option["id"] in alloc_map:
option["default_percentage_allocation"] = alloc_map[option["id"]]
return options # type: ignore[return-value]
class ExperimentQueryParamSerializer(serializers.Serializer): # type: ignore[type-arg]
status = serializers.ListField(
child=serializers.ChoiceField(choices=ExperimentStatus.choices),
required=False,
)
class ExperimentListSerializer(ExperimentSerializer):
feature = ExperimentFeatureSerializer(read_only=True)
metrics = ExperimentMetricSerializer(
source="experiment_metrics",
many=True,
read_only=True,
)
class ExperimentDetailSerializer(ExperimentListSerializer):
experiment_rollout = serializers.SerializerMethodField()
def get_experiment_rollout(self, experiment: Experiment) -> dict[str, Any] | None:
return get_experiment_rollout(experiment)
class ExperimentExposuresSerializer(serializers.ModelSerializer): # type: ignore[type-arg]
is_final = serializers.BooleanField(read_only=True)
class Meta:
model = ExperimentExposures
fields = (
"as_of",
"last_error_at",
"refresh_requested_at",
"payload",
"is_final",
)
class ExperimentResultsSerializer(serializers.ModelSerializer): # type: ignore[type-arg]
is_final = serializers.BooleanField(read_only=True)
class Meta:
model = ExperimentResults
fields = (
"as_of",
"last_error_at",
"refresh_requested_at",
"payload",
"is_final",
)