Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions experimenter/experimenter/experiments/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,8 @@ class AnalysisWindow(models.TextChoices):

DAILY_ACTIVE_USERS = "client_level_daily_active_users_v2"
DAYS_OF_USE = "days_of_use"
RETENTION = "retained"
RETENTION_WEEK_2 = "week_2_retention"
RETENTION_WEEK_4 = "week_4_retention"
RETENTION_3_DAYS = "active_in_last_3_days"
RETENTION_3_DAYS_DESKTOP = "active_in_last_3_days_legacy"
SEARCH_COUNT = "search_count"
Expand All @@ -710,8 +711,17 @@ class AnalysisWindow(models.TextChoices):
KPI_METRICS = [
{
"group": "other_metrics",
"slug": RETENTION,
"friendly_name": "Week 2 Retention",
"slug": RETENTION_WEEK_2,
"display_type": "percentage",
"description": "Users who were active in Firefox during the second week after enrollment.", # noqa
},
{
"group": "other_metrics",
"friendly_name": "Week 4 Retention",
"slug": RETENTION_WEEK_4,
"display_type": "percentage",
"description": "Users who were active in Firefox during the fourth week after enrollment.", # noqa
},
{
"group": "other_metrics",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from django.db import migrations

BATCH_SIZE = 100


def stored_analysis_start_time(results_data):
metadata = (results_data.get("v3") or {}).get("metadata")
return metadata.get("analysis_start_time") if isinstance(metadata, dict) else None


def force_results_refetch(apps, schema_editor):
NimbusExperiment = apps.get_model("experiments", "NimbusExperiment")

stale_experiments = []
for experiment in NimbusExperiment.objects.exclude(results_data=None).iterator(
chunk_size=BATCH_SIZE
):
if stored_analysis_start_time(experiment.results_data) is None:
continue

experiment.results_data["v3"]["metadata"]["analysis_start_time"] = None
stale_experiments.append(experiment)

if len(stale_experiments) == BATCH_SIZE:
NimbusExperiment.objects.bulk_update(stale_experiments, ["results_data"])
stale_experiments.clear()

NimbusExperiment.objects.bulk_update(stale_experiments, ["results_data"])


class Migration(migrations.Migration):
dependencies = [
("experiments", "0338_nimbusemail_rollout_phase_alter_nimbusemail_type"),
]

operations = [
migrations.RunPython(force_results_refetch, migrations.RunPython.noop),
]
56 changes: 48 additions & 8 deletions experimenter/experimenter/experiments/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,18 @@ def build_metric_data(branch_key="control", comparative_branch_data=None):
for metric in outcome_metrics
}

control_weekly_retention = {
f"week_{week}_retention": build_metric_data(branch_key="control")
for week in (3, 5, 6)
}
treatment_weekly_retention = {
slug: build_metric_data(
branch_key="treatment",
comparative_branch_data=control_weekly_retention[slug],
)
for slug in control_weekly_retention
}

results = {
"v3": {
"overall": {
Expand All @@ -667,8 +679,13 @@ def build_metric_data(branch_key="control", comparative_branch_data=None):
},
},
},
"retained": (
control_retained := build_metric_data(
"week_2_retention": (
control_week_2_retention := build_metric_data(
branch_key="control"
)
),
"week_4_retention": (
control_week_4_retention := build_metric_data(
branch_key="control"
)
),
Expand All @@ -682,6 +699,7 @@ def build_metric_data(branch_key="control", comparative_branch_data=None):
branch_key="control"
)
),
**control_weekly_retention,
**control_outcome_metrics,
},
"search_metrics": {
Expand Down Expand Up @@ -710,9 +728,13 @@ def build_metric_data(branch_key="control", comparative_branch_data=None):
},
},
},
"retained": build_metric_data(
"week_2_retention": build_metric_data(
branch_key="treatment",
comparative_branch_data=control_retained,
comparative_branch_data=control_week_2_retention,
),
"week_4_retention": build_metric_data(
branch_key="treatment",
comparative_branch_data=control_week_4_retention,
),
"client_level_daily_active_users_v2": (
build_metric_data(
Expand All @@ -724,6 +746,7 @@ def build_metric_data(branch_key="control", comparative_branch_data=None):
branch_key="treatment",
comparative_branch_data=control_three_day,
),
**treatment_weekly_retention,
**treatment_outcome_metrics,
},
"search_metrics": {
Expand Down Expand Up @@ -755,8 +778,13 @@ def build_metric_data(branch_key="control", comparative_branch_data=None):
},
},
},
"retained": (
control_retained := build_metric_data(
"week_2_retention": (
control_week_2_retention := build_metric_data(
branch_key="control"
)
),
"week_4_retention": (
control_week_4_retention := build_metric_data(
branch_key="control"
)
),
Expand All @@ -770,6 +798,7 @@ def build_metric_data(branch_key="control", comparative_branch_data=None):
branch_key="control"
)
),
**control_weekly_retention,
**control_outcome_metrics,
},
"search_metrics": {
Expand Down Expand Up @@ -798,9 +827,13 @@ def build_metric_data(branch_key="control", comparative_branch_data=None):
},
},
},
"retained": build_metric_data(
"week_2_retention": build_metric_data(
branch_key="treatment",
comparative_branch_data=control_retained,
comparative_branch_data=control_week_2_retention,
),
"week_4_retention": build_metric_data(
branch_key="treatment",
comparative_branch_data=control_week_4_retention,
),
"client_level_daily_active_users_v2": (
build_metric_data(
Expand All @@ -812,6 +845,7 @@ def build_metric_data(branch_key="control", comparative_branch_data=None):
branch_key="treatment",
comparative_branch_data=control_three_day,
),
**treatment_weekly_retention,
**treatment_outcome_metrics,
},
"search_metrics": {
Expand All @@ -827,6 +861,12 @@ def build_metric_data(branch_key="control", comparative_branch_data=None):
},
},
},
"other_metrics": {
"other_metrics": {
slug: " ".join(word.title() for word in slug.split("_"))
for slug in control_weekly_retention
},
},
},
}

Expand Down
80 changes: 80 additions & 0 deletions experimenter/experimenter/experiments/tests/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,83 @@ def test_migration(self):
self.assertEqual(non_rollout.status, "Live")
self.assertEqual(non_rollout.status_next, "Live")
self.assertEqual(non_rollout.publish_status, "Review")


class TestForceWeeklyRetentionResultsRefetchMigration(MigratorTestCase):
migrate_from = (
"experiments",
"0338_nimbusemail_rollout_phase_alter_nimbusemail_type",
)
migrate_to = (
"experiments",
"0339_force_weekly_retention_results_refetch",
)

def prepare(self):
User = self.old_state.apps.get_model("auth", "User")
NimbusExperiment = self.old_state.apps.get_model(
"experiments", "NimbusExperiment"
)

owner, _ = User.objects.get_or_create(
username="test@example.com",
defaults={"email": "test@example.com"},
)

NimbusExperiment.objects.create(
slug="with-analysis-start-time",
name="With analysis start time",
application="firefox-desktop",
owner=owner,
results_data={
"v3": {
"metadata": {
"analysis_start_time": "2026-07-01T00:00:00+00:00",
"outcomes": {},
},
"overall": {},
}
},
)
NimbusExperiment.objects.create(
slug="without-metadata",
name="Without metadata",
application="firefox-desktop",
owner=owner,
results_data={"v3": {"overall": {}}},
)
NimbusExperiment.objects.create(
slug="null-metadata",
name="Null metadata",
application="firefox-desktop",
owner=owner,
results_data={"v3": {"metadata": None, "overall": {}}},
)
NimbusExperiment.objects.create(
slug="no-results",
name="No results",
application="firefox-desktop",
owner=owner,
results_data=None,
)

def test_migration(self):
NimbusExperiment = self.new_state.apps.get_model(
"experiments", "NimbusExperiment"
)

cleared = NimbusExperiment.objects.get(slug="with-analysis-start-time")
self.assertIsNone(cleared.results_data["v3"]["metadata"]["analysis_start_time"])
self.assertEqual(cleared.results_data["v3"]["metadata"]["outcomes"], {})
self.assertEqual(cleared.results_data["v3"]["overall"], {})

untouched = NimbusExperiment.objects.get(slug="without-metadata")
self.assertEqual(untouched.results_data, {"v3": {"overall": {}}})

null_metadata = NimbusExperiment.objects.get(slug="null-metadata")
self.assertEqual(
null_metadata.results_data, {"v3": {"metadata": None, "overall": {}}}
)

no_results = NimbusExperiment.objects.get(slug="no-results")
self.assertIsNone(no_results.results_data)
Loading
Loading