|
15 | 15 | add_environment_key_to_ingestion, |
16 | 16 | delete_environment_key_from_ingestion, |
17 | 17 | ) |
| 18 | +from experimentation.types import MetricDefinition |
18 | 19 |
|
19 | 20 | if typing.TYPE_CHECKING: |
20 | 21 | from experimentation.dataclasses import WarehouseEventStats |
@@ -126,3 +127,78 @@ class Meta: |
126 | 127 | name="unique_active_experiment_per_feature_env", |
127 | 128 | ), |
128 | 129 | ] |
| 130 | + |
| 131 | + |
| 132 | +class MetricAggregation(models.TextChoices): |
| 133 | + COUNT = "count", "Count" |
| 134 | + SUM = "sum", "Sum" |
| 135 | + MEAN = "mean", "Mean" |
| 136 | + OCCURRENCE = "occurrence", "Occurrence (event happened at least once)" |
| 137 | + |
| 138 | + |
| 139 | +class MetricDirection(models.TextChoices): |
| 140 | + """A metric's inherent polarity — which way is "better".""" |
| 141 | + |
| 142 | + UP = "up", "Higher is better" |
| 143 | + DOWN = "down", "Lower is better" |
| 144 | + INFORMATIONAL = "informational", "Informational only" |
| 145 | + |
| 146 | + |
| 147 | +class ExpectedDirection(models.TextChoices): |
| 148 | + """The guardrail direction expected of a metric within an experiment.""" |
| 149 | + |
| 150 | + INCREASE = "increase", "Increase" |
| 151 | + DECREASE = "decrease", "Decrease" |
| 152 | + NOT_INCREASE = "not_increase", "Should not increase" |
| 153 | + NOT_DECREASE = "not_decrease", "Should not decrease" |
| 154 | + |
| 155 | + |
| 156 | +class Metric(SoftDeleteExportableModel): |
| 157 | + environment = models.ForeignKey( |
| 158 | + Environment, |
| 159 | + on_delete=models.CASCADE, |
| 160 | + related_name="metrics", |
| 161 | + ) |
| 162 | + name = models.CharField(max_length=255) |
| 163 | + description = models.TextField(blank=True, default="") |
| 164 | + aggregation = models.CharField( |
| 165 | + max_length=20, |
| 166 | + choices=MetricAggregation.choices, |
| 167 | + default=MetricAggregation.MEAN, |
| 168 | + ) |
| 169 | + direction = models.CharField( |
| 170 | + max_length=20, |
| 171 | + choices=MetricDirection.choices, |
| 172 | + default=MetricDirection.UP, |
| 173 | + ) |
| 174 | + definition: models.JSONField[MetricDefinition, MetricDefinition] = ( |
| 175 | + models.JSONField() |
| 176 | + ) |
| 177 | + created_at = models.DateTimeField(auto_now_add=True) |
| 178 | + updated_at = models.DateTimeField(auto_now=True) |
| 179 | + |
| 180 | + |
| 181 | +class ExperimentMetric(models.Model): |
| 182 | + experiment = models.ForeignKey( |
| 183 | + Experiment, |
| 184 | + on_delete=models.CASCADE, |
| 185 | + related_name="experiment_metrics", |
| 186 | + ) |
| 187 | + metric = models.ForeignKey( |
| 188 | + Metric, |
| 189 | + on_delete=models.CASCADE, |
| 190 | + related_name="experiment_metrics", |
| 191 | + ) |
| 192 | + expected_direction = models.CharField( |
| 193 | + max_length=20, |
| 194 | + choices=ExpectedDirection.choices, |
| 195 | + ) |
| 196 | + created_at = models.DateTimeField(auto_now_add=True) |
| 197 | + |
| 198 | + class Meta: |
| 199 | + constraints = [ |
| 200 | + models.UniqueConstraint( |
| 201 | + fields=["experiment", "metric"], |
| 202 | + name="metric_attached_once_per_experiment", |
| 203 | + ), |
| 204 | + ] |
0 commit comments