Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(cohorts): add ignore backfilled dates option to event backfill admin (#69803)

Gustavo H. Strassburgerf99991886dae
diff --git a/posthog/admin/admins/backfill_precalculated_events_admin.py b/posthog/admin/admins/backfill_precalculated_events_admin.py
index 4d9791c6a6c9..c78b9dafb888 100644
--- a/posthog/admin/admins/backfill_precalculated_events_admin.py
+++ b/posthog/admin/admins/backfill_precalculated_events_admin.py
@@ -31,6 +31,12 @@ class BackfillPrecalculatedEventsForm(forms.Form):
help_text="Number of concurrent child workflows to run (1-100, default: 5)",
label="Concurrent workflows",
)
+ ignore_backfilled_dates = forms.BooleanField(
+ required=False,
+ initial=False,
+ help_text="Skip the already-backfilled check and reprocess all days unconditionally",
+ label="Ignore backfilled dates",
+ )
def clean_concurrent_workflows(self):
value = self.cleaned_data.get("concurrent_workflows")
@@ -58,6 +64,9 @@ def backfill_precalculated_events_view(request):
command_args.extend(["--concurrent-workflows", str(form.cleaned_data["concurrent_workflows"])])
+ if form.cleaned_data.get("ignore_backfilled_dates"):
+ command_args.append("--ignore-backfilled-dates")
+
try:
call_command("backfill_precalculated_events", *command_args)
@@ -71,9 +80,10 @@ def backfill_precalculated_events_view(request):
if form.cleaned_data.get("days")
else " (auto-computed window)"
)
+ force_info = ", ignoring backfilled dates" if form.cleaned_data.get("ignore_backfilled_dates") else ""
messages.success(
request,
- f"Event backfill started successfully for {cohort_info}{days_info} "
+ f"Event backfill started successfully for {cohort_info}{days_info}{force_info} "
f"(team {form.cleaned_data['team_id']}) "
f"with {form.cleaned_data['concurrent_workflows']} concurrent workflows. "
f"Check Temporal UI for progress.",
diff --git a/posthog/admin/test_backfill_precalculated_events_admin.py b/posthog/admin/test_backfill_precalculated_events_admin.py
new file mode 100644
index 000000000000..f61ba83ecd9a
--- /dev/null
+++ b/posthog/admin/test_backfill_precalculated_events_admin.py
@@ -0,0 +1,43 @@
+from posthog.test.base import BaseTest
+from unittest.mock import patch
+
+from django.contrib.messages.storage.fallback import FallbackStorage
+from django.test import RequestFactory
+
+from parameterized import parameterized
+
+from posthog.admin.admins.backfill_precalculated_events_admin import backfill_precalculated_events_view
+
+
+def _attach_messages(request) -> None:
+ request.session = {}
+ request._messages = FallbackStorage(request)
+
+
+class TestBackfillPrecalculatedEventsAdmin(BaseTest):
+ def setUp(self) -> None:
+ super().setUp()
+ self.user.is_staff = True
+ self.user.save()
+ self.factory = RequestFactory()
+
+ @parameterized.expand([(True,), (False,)])
+ def test_ignore_backfilled_dates_checkbox_controls_command_flag(self, ignore_backfilled_dates: bool) -> None:
+ data = {"team_id": "2", "cohort_id": "123", "concurrent_workflows": "5"}
+ if ignore_backfilled_dates:
+ data["ignore_backfilled_dates"] = "on"
+ request = self.factory.post("/admin/backfill-precalculated-events/", data)
+ request.user = self.user
+ _attach_messages(request)
+
+ with (
+ patch("posthog.admin.admins.backfill_precalculated_events_admin.call_command") as mock_call_command,
+ patch("posthog.admin.admins.backfill_precalculated_events_admin.redirect") as mock_redirect,
+ ):
+ backfill_precalculated_events_view(request)
+
+ mock_call_command.assert_called_once()
+ command_args = mock_call_command.call_args[0]
+ assert command_args[0] == "backfill_precalculated_events"
+ assert ("--ignore-backfilled-dates" in command_args) is ignore_backfilled_dates
+ mock_redirect.assert_called_once_with("backfill-precalculated-events")
diff --git a/posthog/templates/admin/backfill_precalculated_events.html b/posthog/templates/admin/backfill_precalculated_events.html
index d1d78a079b7c..e32207f3d1d1 100644
--- a/posthog/templates/admin/backfill_precalculated_events.html
+++ b/posthog/templates/admin/backfill_precalculated_events.html
@@ -95,6 +95,17 @@
{% endif %}
{{ form.concurrent_workflows.errors }}
</div>
+
+ <div class="form-row">
+ <label for="{{ form.ignore_backfilled_dates.id_for_label }}">
+ {{ form.ignore_backfilled_dates.label }}:
+ </label>
+ {{ form.ignore_backfilled_dates }}
+ {% if form.ignore_backfilled_dates.help_text %}
+ <p class="help">{{ form.ignore_backfilled_dates.help_text }}</p>
+ {% endif %}
+ {{ form.ignore_backfilled_dates.errors }}
+ </div>
</fieldset>
<div class="submit-row">