-
Notifications
You must be signed in to change notification settings - Fork 528
feat(experimentation): environment-scoped metrics & experiment results #7674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c961486
feat(experimentation): environment-scoped metrics & experiment-metric…
gagantrivedi dd1f915
feat(experimentation): metric direction and versioned definition
Zaimwa9 620f7b4
fix(experimentation): harden experiment-metric attach/detach and metr…
Zaimwa9 a48959c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] cb1dbe8
feat(experimentation): cache experiment object to avoid redundant loo…
Zaimwa9 b46b209
feat(experimentation): versioned metrics definition typed dict
Zaimwa9 1fd7efb
feat(experimentation): deduped metrics permissions
Zaimwa9 de3e8b9
feat(experimentation): added deletion based on experiment status tests
Zaimwa9 68e8b97
feat(experimentation): rebase
Zaimwa9 282c32f
chore: drop-stray-frontend-utils-reorder-from-backend-branch
Zaimwa9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,15 @@ | ||
| from rest_framework.routers import DefaultRouter | ||
| from rest_framework_nested import routers # type: ignore[import-untyped] | ||
|
|
||
| from experimentation.views import ExperimentViewSet | ||
| from experimentation.views import ExperimentMetricViewSet, ExperimentViewSet | ||
|
|
||
| app_name = "experiments" | ||
|
|
||
| router = DefaultRouter() | ||
| router = routers.DefaultRouter() | ||
| router.register(r"", ExperimentViewSet, basename="experiments") | ||
|
|
||
| urlpatterns = router.urls | ||
| experiments_router = routers.NestedSimpleRouter(router, r"", lookup="experiment") | ||
| experiments_router.register( | ||
| r"metrics", ExperimentMetricViewSet, basename="experiment-metrics" | ||
| ) | ||
|
|
||
| urlpatterns = router.urls + experiments_router.urls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| """Versioned schemas for ``Metric.definition``. | ||
|
|
||
| ``definition`` is a schema-less JSON column whose shape is versioned so it can | ||
| evolve without breaking stored rows. Each supported version has a validator | ||
| here; the client sends the version it built the definition with. To introduce a | ||
| new shape, add an entry to ``METRIC_DEFINITION_VALIDATORS``. | ||
| """ | ||
|
|
||
| from collections.abc import Callable | ||
|
|
||
| DefinitionValidator = Callable[[dict[str, object]], "str | None"] | ||
|
|
||
|
|
||
| def _validate_v1(definition: dict[str, object]) -> str | None: | ||
| event = definition.get("event") | ||
| if not event or not isinstance(event, str): | ||
| return "Definition must specify a non-empty 'event'." | ||
| return None | ||
|
|
||
|
|
||
| METRIC_DEFINITION_VALIDATORS: dict[int, DefinitionValidator] = { | ||
| 1: _validate_v1, | ||
| } | ||
|
|
||
|
|
||
| def validate_metric_definition(definition: object) -> str | None: | ||
| """Return an error message if ``definition`` is invalid, else ``None``.""" | ||
| if not isinstance(definition, dict): | ||
| return "Definition must be an object." | ||
|
|
||
| version = definition.get("version") | ||
| validator = ( | ||
| METRIC_DEFINITION_VALIDATORS.get(version) if isinstance(version, int) else None | ||
| ) | ||
| if validator is None: | ||
| return "Definition must specify a supported 'version'." | ||
|
|
||
| return validator(definition) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from rest_framework.routers import DefaultRouter | ||
|
|
||
| from experimentation.views import MetricViewSet | ||
|
|
||
| app_name = "experiment_metrics" | ||
|
|
||
| router = DefaultRouter() | ||
| router.register(r"", MetricViewSet, basename="metrics") | ||
|
|
||
| urlpatterns = router.urls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| # Generated by Django 5.2.14 on 2026-06-02 10:47 | ||
|
|
||
| import django.db.models.deletion | ||
| import uuid | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("environments", "0037_add_uuid_field"), | ||
| ("experimentation", "0004_experiment"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="Metric", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.AutoField( | ||
| auto_created=True, | ||
| primary_key=True, | ||
| serialize=False, | ||
| verbose_name="ID", | ||
| ), | ||
| ), | ||
| ( | ||
| "deleted_at", | ||
| models.DateTimeField( | ||
| blank=True, | ||
| db_index=True, | ||
| default=None, | ||
| editable=False, | ||
| null=True, | ||
| ), | ||
| ), | ||
| ( | ||
| "uuid", | ||
| models.UUIDField(default=uuid.uuid4, editable=False, unique=True), | ||
| ), | ||
| ("name", models.CharField(max_length=255)), | ||
| ("description", models.TextField(blank=True, default="")), | ||
| ( | ||
| "aggregation", | ||
| models.CharField( | ||
| choices=[ | ||
| ("count", "Count"), | ||
| ("sum", "Sum"), | ||
| ("mean", "Mean"), | ||
| ("occurrence", "Occurrence (event happened at least once)"), | ||
| ], | ||
| default="mean", | ||
| max_length=20, | ||
| ), | ||
| ), | ||
| ( | ||
| "direction", | ||
| models.CharField( | ||
| choices=[ | ||
| ("up", "Higher is better"), | ||
| ("down", "Lower is better"), | ||
| ("informational", "Informational only"), | ||
| ], | ||
| default="up", | ||
| max_length=20, | ||
| ), | ||
| ), | ||
| ("definition", models.JSONField()), | ||
| ("created_at", models.DateTimeField(auto_now_add=True)), | ||
| ("updated_at", models.DateTimeField(auto_now=True)), | ||
| ( | ||
| "environment", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="metrics", | ||
| to="environments.environment", | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "abstract": False, | ||
| }, | ||
| ), | ||
| migrations.CreateModel( | ||
| name="ExperimentMetric", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.AutoField( | ||
| auto_created=True, | ||
| primary_key=True, | ||
| serialize=False, | ||
| verbose_name="ID", | ||
| ), | ||
| ), | ||
| ( | ||
| "expected_direction", | ||
| models.CharField( | ||
| choices=[ | ||
| ("increase", "Increase"), | ||
| ("decrease", "Decrease"), | ||
| ("not_increase", "Should not increase"), | ||
| ("not_decrease", "Should not decrease"), | ||
| ], | ||
| max_length=20, | ||
| ), | ||
| ), | ||
| ("created_at", models.DateTimeField(auto_now_add=True)), | ||
| ( | ||
| "experiment", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="experiment_metrics", | ||
| to="experimentation.experiment", | ||
| ), | ||
| ), | ||
| ( | ||
| "metric", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="experiment_metrics", | ||
| to="experimentation.metric", | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "constraints": [ | ||
| models.UniqueConstraint( | ||
| fields=("experiment", "metric"), | ||
| name="metric_attached_once_per_experiment", | ||
| ) | ||
| ], | ||
| }, | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.