feat(conflicts): added conflicts business and presentation logic
This commit is contained in:
@@ -67,11 +67,6 @@ EDITABLE_IN_DRAFT: tuple[str, ...] = (
|
||||
)
|
||||
|
||||
|
||||
class ConflictPolicy(models.TextChoices):
|
||||
MUTUAL_EXCLUSION = "mutual_exclusion", _("Mutual exclusion")
|
||||
PRIORITY = "priority", _("Priority tiers")
|
||||
|
||||
|
||||
class OutcomeType(models.TextChoices):
|
||||
ROLLOUT = "rollout", _("Rollout winner")
|
||||
ROLLBACK = "rollback", _("Rollback")
|
||||
@@ -431,87 +426,6 @@ class Approval(BaseModel):
|
||||
return f"Approval by {self.approver} for {self.experiment}"
|
||||
|
||||
|
||||
class ConflictDomain(BaseModel):
|
||||
name = models.CharField(
|
||||
max_length=200,
|
||||
unique=True,
|
||||
verbose_name=_("name"),
|
||||
)
|
||||
description = models.TextField(
|
||||
blank=True,
|
||||
verbose_name=_("description"),
|
||||
)
|
||||
policy = models.CharField(
|
||||
max_length=30,
|
||||
choices=ConflictPolicy.choices,
|
||||
default=ConflictPolicy.MUTUAL_EXCLUSION,
|
||||
verbose_name=_("conflict policy"),
|
||||
)
|
||||
max_concurrent = models.PositiveIntegerField(
|
||||
default=1,
|
||||
verbose_name=_("max concurrent experiments"),
|
||||
)
|
||||
created_at = models.DateTimeField(
|
||||
auto_now_add=True,
|
||||
verbose_name=_("created at"),
|
||||
)
|
||||
updated_at = models.DateTimeField(
|
||||
auto_now=True,
|
||||
verbose_name=_("updated at"),
|
||||
)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("conflict domain")
|
||||
verbose_name_plural = _("conflict domains")
|
||||
ordering = ["name"]
|
||||
|
||||
@override
|
||||
def __str__(self) -> str:
|
||||
return f"{self.name} ({self.policy})"
|
||||
|
||||
|
||||
class ExperimentConflictDomain(BaseModel):
|
||||
experiment = models.ForeignKey(
|
||||
Experiment,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="conflict_memberships",
|
||||
verbose_name=_("experiment"),
|
||||
)
|
||||
conflict_domain = models.ForeignKey(
|
||||
ConflictDomain,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="experiment_memberships",
|
||||
verbose_name=_("conflict domain"),
|
||||
)
|
||||
priority = models.IntegerField(
|
||||
default=0,
|
||||
verbose_name=_("priority"),
|
||||
help_text=_("Higher value wins in priority-based resolution"),
|
||||
)
|
||||
created_at = models.DateTimeField(
|
||||
auto_now_add=True,
|
||||
verbose_name=_("created at"),
|
||||
)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("experiment conflict domain")
|
||||
verbose_name_plural = _("experiment conflict domains")
|
||||
ordering = ["-priority"]
|
||||
constraints = [
|
||||
models.UniqueConstraint(
|
||||
fields=["experiment", "conflict_domain"],
|
||||
name="unique_experiment_conflict_domain",
|
||||
),
|
||||
]
|
||||
|
||||
@override
|
||||
def __str__(self) -> str:
|
||||
return (
|
||||
f"{self.experiment.name} in {self.conflict_domain.name} "
|
||||
f"(priority={self.priority})"
|
||||
)
|
||||
|
||||
|
||||
class ExperimentOutcome(BaseModel):
|
||||
experiment = models.OneToOneField(
|
||||
Experiment,
|
||||
|
||||
@@ -16,6 +16,7 @@ from apps.experiments.models import (
|
||||
OutcomeType,
|
||||
Variant,
|
||||
)
|
||||
from apps.conflicts.services import validate_domain_conflicts
|
||||
from apps.flags.models import FeatureFlag, validate_value_for_type
|
||||
from apps.notifications.services import (
|
||||
NotificationPayload,
|
||||
@@ -373,6 +374,7 @@ def experiment_request_changes(
|
||||
def experiment_start(*, experiment: Experiment, user: User) -> Experiment:
|
||||
ensure_owner_or_admin(experiment, user)
|
||||
_validate_no_active_flag_conflict(experiment)
|
||||
validate_domain_conflicts(experiment)
|
||||
experiment = _transition(
|
||||
experiment,
|
||||
ExperimentStatus.RUNNING,
|
||||
@@ -401,6 +403,7 @@ def experiment_pause(
|
||||
def experiment_resume(*, experiment: Experiment, user: User) -> Experiment:
|
||||
ensure_owner_or_admin(experiment, user)
|
||||
_validate_no_active_flag_conflict(experiment)
|
||||
validate_domain_conflicts(experiment)
|
||||
return _transition(
|
||||
experiment,
|
||||
ExperimentStatus.RUNNING,
|
||||
|
||||
Reference in New Issue
Block a user