diff --git a/ee/urls.py b/ee/urls.py index fd1b248f27b9..b2cfcb73f438 100644 --- a/ee/urls.py +++ b/ee/urls.py @@ -67,11 +67,6 @@ def extend_api_router() -> None: pass from posthog.admin.admins.distinct_id_usage_admin import distinct_id_usage_view - from posthog.admin.admins.health_check_admin import ( - health_check_list_view, - health_check_runs_fragment_view, - health_check_trigger_view, - ) from posthog.admin.admins.notebook_markdown_migration_admin import ( notebook_markdown_migration_run_view, notebook_markdown_migration_stats_view, @@ -116,17 +111,17 @@ def extend_api_router() -> None: ), path( "admin/health-checks/", - admin.site.admin_view(health_check_list_view), + MovedToConsole.page_redirect("Health checks", "/admin/console/tools/health-checks/"), name="health-checks", ), path( "admin/health-checks//", - admin.site.admin_view(health_check_trigger_view), + MovedToConsole.page_redirect("Health checks", "/admin/console/tools/health-checks/?check={kind}"), name="health-check-trigger", ), path( "admin/health-checks//runs/", - admin.site.admin_view(health_check_runs_fragment_view), + MovedToConsole.page_redirect("Health checks", "/admin/console/tools/health-checks/?check={kind}"), name="health-check-runs", ), path( diff --git a/frontend/public/admin-console/console.js b/frontend/public/admin-console/console.js index a0bbf1ea309f..1299f77e7786 100644 --- a/frontend/public/admin-console/console.js +++ b/frontend/public/admin-console/console.js @@ -26,3 +26,56 @@ document.addEventListener('keydown', (event) => { }) document.querySelector('form[data-autofocus]')?.querySelector('input:not([type=hidden]), textarea, select')?.focus() + +// Live blocks ask the server for a fresh copy of themselves while their data-refresh-seconds is set, for +// example the runs of a job that is still going. Each block waits for its answer before it asks again, waits +// longer after a failure, and pauses while the tab is hidden. A response without the console's header (a login +// page after the session ended, an error page) stops the refreshes instead of replacing the block. +const LIVE_MAX_BACKOFF = 4 + +function scheduleLiveRefresh(element, failures) { + const seconds = Number(element.dataset.refreshSeconds) + if (!seconds) { + return + } + const delay = seconds * 1000 * 2 ** Math.min(failures, LIVE_MAX_BACKOFF) + setTimeout(() => refreshLiveBlock(element, failures), delay) +} + +async function refreshLiveBlock(element, failures) { + if (!element.isConnected) { + return + } + if (document.hidden) { + document.addEventListener('visibilitychange', () => refreshLiveBlock(element, failures), { once: true }) + return + } + let response + let body + try { + response = await fetch(element.dataset.consoleLive, { credentials: 'same-origin', redirect: 'manual' }) + body = await response.text() + } catch { + scheduleLiveRefresh(element, failures + 1) + return + } + if (response.status >= 500) { + scheduleLiveRefresh(element, failures + 1) + return + } + if (!response.ok || response.headers.get('X-Console-Live') !== '1') { + return + } + const template = document.createElement('template') + template.innerHTML = body.trim() + const replacement = template.content.firstElementChild + if (!replacement || !replacement.hasAttribute('data-console-live')) { + return + } + element.replaceWith(replacement) + scheduleLiveRefresh(replacement, 0) +} + +for (const element of document.querySelectorAll('[data-console-live]')) { + scheduleLiveRefresh(element, 0) +} diff --git a/posthog/admin/admins/health_check_admin.py b/posthog/admin/admins/health_check_admin.py deleted file mode 100644 index ec59d248e6dd..000000000000 --- a/posthog/admin/admins/health_check_admin.py +++ /dev/null @@ -1,195 +0,0 @@ -import asyncio -import dataclasses -from uuid import uuid4 - -from django import forms -from django.conf import settings -from django.contrib import admin, messages -from django.core.exceptions import PermissionDenied -from django.shortcuts import redirect, render - -import structlog - -from posthog.temporal.common.client import sync_connect -from posthog.temporal.health_checks.models import HealthCheckWorkflowInputs -from posthog.temporal.health_checks.registry import HEALTH_CHECKS, ensure_registry_loaded - -logger = structlog.get_logger(__name__) - - -class HealthCheckTriggerForm(forms.Form): - dry_run = forms.BooleanField(required=False, help_text="Run without writing changes to the database") - batch_size = forms.IntegerField(min_value=1, max_value=10000, help_text="Number of teams per batch") - max_concurrent = forms.IntegerField(min_value=1, max_value=20, help_text="Max concurrent batch activities") - rollout_percentage = forms.FloatField( - min_value=0.01, max_value=1.0, help_text="Fraction of teams to process (0.01 to 1.0)" - ) - active_since_days = forms.IntegerField( - required=False, - min_value=0, - help_text="Only process teams with org members who logged in within the last N days. 0 or blank = all teams.", - ) - team_ids = forms.CharField( - required=False, - widget=forms.Textarea(attrs={"rows": 3, "placeholder": "e.g. 1, 2, 3"}), - help_text="Comma-separated team IDs to target (leave blank for all teams)", - ) - - def clean_active_since_days(self) -> int | None: - value = self.cleaned_data.get("active_since_days") - if value is None or value == 0: - return None - return value - - def clean_team_ids(self) -> list[int] | None: - raw = self.cleaned_data.get("team_ids", "").strip() - if not raw: - return None - try: - return [int(x.strip()) for x in raw.split(",") if x.strip()] - except ValueError: - raise forms.ValidationError("Team IDs must be comma-separated integers") - - -def health_check_list_view(request): - # TODO: Add more granular permissions beyond is_staff (e.g. restrict to specific engineering groups) - if not request.user.is_staff: - raise PermissionDenied - - ensure_registry_loaded() - - checks = sorted(HEALTH_CHECKS.values(), key=lambda c: c.name) - - context = { - "checks": checks, - "title": "Health checks", - "has_view_permission": True, - "site_title": admin.site.site_title, - "site_header": admin.site.site_header, - "site_url": admin.site.site_url, - "has_permission": True, - } - return render(request, "admin/health_checks/list.html", context) - - -def health_check_trigger_view(request, kind: str): - if not request.user.is_staff: - raise PermissionDenied - - ensure_registry_loaded() - - config = HEALTH_CHECKS.get(kind) - if config is None: - messages.error(request, f"Health check '{kind}' not found in registry") - return redirect("health-checks") - - if request.method == "POST": - form = HealthCheckTriggerForm(request.POST) - if form.is_valid(): - workflow_inputs = HealthCheckWorkflowInputs.from_config(config) - workflow_inputs = dataclasses.replace( - workflow_inputs, - dry_run=form.cleaned_data["dry_run"], - batch_size=form.cleaned_data["batch_size"], - max_concurrent=form.cleaned_data["max_concurrent"], - rollout_percentage=form.cleaned_data["rollout_percentage"], - active_since_days=form.cleaned_data["active_since_days"], - team_ids=form.cleaned_data["team_ids"], - ) - - try: - temporal = sync_connect() - workflow_id = f"health-check-{config.name}-manual-{uuid4()}" - asyncio.run( - temporal.start_workflow( - "health-check-workflow", - dataclasses.asdict(workflow_inputs), - id=workflow_id, - task_queue=settings.HEALTH_CHECK_TASK_QUEUE, - execution_timeout=config.execution_timeout, - ) - ) - logger.info( - "Health check workflow triggered manually", - kind=kind, - workflow_id=workflow_id, - triggered_by=request.user.email, - dry_run=form.cleaned_data["dry_run"], - ) - messages.success(request, f"Workflow triggered: {workflow_id}") - except Exception as e: - logger.exception("Failed to trigger health check workflow", kind=kind, error=str(e)) - messages.error(request, f"Failed to trigger workflow: {e}") - - return redirect("health-check-trigger", kind=kind) - else: - form = HealthCheckTriggerForm( - initial={ - "dry_run": config.dry_run, - "batch_size": config.batch_size, - "max_concurrent": config.max_concurrent, - "rollout_percentage": config.rollout_percentage, - "active_since_days": config.active_since_days, - } - ) - - workflows = _get_recent_runs(config.name) - - context = { - **admin.site.each_context(request), - "config": config, - "form": form, - "workflows": workflows, - "title": f"Health check: {config.name}", - "temporal_ui_host": settings.TEMPORAL_UI_HOST, - "temporal_namespace": settings.TEMPORAL_NAMESPACE, - } - return render(request, "admin/health_checks/trigger.html", context) - - -def health_check_runs_fragment_view(request, kind: str): - if not request.user.is_staff: - raise PermissionDenied - - ensure_registry_loaded() - - config = HEALTH_CHECKS.get(kind) - if config is None: - return render(request, "admin/health_checks/_run_history.html", {"workflows": []}) - - workflows = _get_recent_runs(config.name) - context = { - "workflows": workflows, - "temporal_ui_host": settings.TEMPORAL_UI_HOST, - "temporal_namespace": settings.TEMPORAL_NAMESPACE, - } - return render(request, "admin/health_checks/_run_history.html", context) - - -def _get_recent_runs(config_name: str, limit: int = 20) -> list[dict]: - try: - temporal = sync_connect() - prefix = f"health-check-{config_name}" - query = f'WorkflowId >= "{prefix}" AND WorkflowId < "{prefix}~" ORDER BY StartTime DESC' - - async def fetch_workflows(): - workflows = [] - async for wf in temporal.list_workflows(query=query): - workflows.append( - { - "id": wf.id, - "run_id": wf.run_id, - "status": str(wf.status.name) if wf.status else "Unknown", - "start_time": wf.start_time, - "close_time": wf.close_time, - "source": "Manual" if "-manual-" in wf.id else "Scheduled", - } - ) - if len(workflows) >= limit: - break - return workflows - - return asyncio.run(fetch_workflows()) - except Exception as e: - logger.warning("Failed to fetch health check workflows", config_name=config_name, error=str(e)) - return [] diff --git a/posthog/admin/admins/organization_admin.py b/posthog/admin/admins/organization_admin.py index 9d563b2cf36b..43be4c83a632 100644 --- a/posthog/admin/admins/organization_admin.py +++ b/posthog/admin/admins/organization_admin.py @@ -1,17 +1,12 @@ -import re from datetime import UTC -from typing import Any -from uuid import UUID from django import forms from django.apps import apps from django.conf import settings from django.contrib import admin, messages -from django.core import signing from django.core.exceptions import PermissionDenied from django.core.management import call_command from django.core.management.base import CommandError -from django.db import transaction from django.http import HttpRequest, HttpResponse from django.shortcuts import redirect, render from django.template.loader import render_to_string @@ -39,7 +34,6 @@ from posthog.models.organization import Organization from posthog.person_db_router import PERSONS_DB_MODELS from posthog.tasks.ai_observability_usage_report import internal_reporting_team_id -from posthog.utils import pluralize from products.ai_observability.backend.facade.admin import dataset_retention_enabled, set_dataset_retention_enabled from products.batch_exports.backend.facade import api as batch_exports_api @@ -139,104 +133,6 @@ class DatasetRetentionForm(forms.Form): ) -class BulkDeactivateOrganizationsForm(forms.Form): - CUSTOM_REASON = "__custom__" - MAX_ORGANIZATIONS = 100 - PREVIEW_TOKEN_SALT = "posthog.admin.bulk_deactivate_organizations" - - organization_ids = forms.CharField( - label="Organization IDs", - widget=forms.Textarea( - attrs={ - "rows": 10, - "placeholder": "Paste one organization ID per line, or separate IDs with commas.", - } - ), - help_text=f"Paste up to {MAX_ORGANIZATIONS} organization IDs separated by new lines, commas, or spaces.", - ) - reason = forms.ChoiceField( - label="Reason", - choices=[*Organization.DeactivationReason.choices, (CUSTOM_REASON, "Custom reason")], - initial=Organization.DeactivationReason.DESKTOP_ABUSE, - ) - custom_reason = forms.CharField( - label="Custom reason", - required=False, - max_length=Organization._meta.get_field("is_not_active_reason").max_length, - widget=forms.Textarea(attrs={"rows": 3}), - help_text="Used only when reason is set to custom reason.", - ) - - def clean_organization_ids(self) -> list[UUID]: - raw_value = self.cleaned_data["organization_ids"] - invalid_tokens: list[str] = [] - organization_ids: list[UUID] = [] - seen_ids: set[UUID] = set() - token_count = 0 - - for match in re.finditer(r"[^,\s]+", raw_value): - token_count += 1 - if token_count > self.MAX_ORGANIZATIONS: - raise forms.ValidationError(f"Enter {self.MAX_ORGANIZATIONS} organization IDs or fewer.") - - token = match.group() - try: - organization_id = UUID(token) - except ValueError: - invalid_tokens.append(token) - continue - - if organization_id in seen_ids: - continue - - seen_ids.add(organization_id) - organization_ids.append(organization_id) - - if token_count == 0: - raise forms.ValidationError("Enter at least one organization ID.") - - if invalid_tokens: - raise forms.ValidationError( - "These organization IDs are not valid UUIDs: %(ids)s", - params={"ids": ", ".join(invalid_tokens[:10])}, - ) - - return organization_ids - - def clean(self) -> dict[str, Any]: - cleaned_data = super().clean() or {} - reason = cleaned_data.get("reason") - custom_reason = (cleaned_data.get("custom_reason") or "").strip() - - if reason == self.CUSTOM_REASON: - if not custom_reason: - self.add_error("custom_reason", "Enter a custom reason.") - return cleaned_data - cleaned_data["resolved_reason"] = custom_reason - elif reason: - cleaned_data["resolved_reason"] = reason - - return cleaned_data - - def preview_payload(self) -> dict[str, str | list[str]]: - return { - "organization_ids": [str(organization_id) for organization_id in self.cleaned_data["organization_ids"]], - "reason": self.cleaned_data["resolved_reason"], - } - - def preview_token(self) -> str: - return signing.dumps(self.preview_payload(), salt=self.PREVIEW_TOKEN_SALT) - - def preview_token_matches(self, preview_token: str) -> bool: - if not preview_token: - return False - - try: - return signing.loads(preview_token, salt=self.PREVIEW_TOKEN_SALT) == self.preview_payload() - except signing.BadSignature: - return False - - @admin.register(Organization) class OrganizationAdmin(admin.ModelAdmin): show_full_result_count = False # prevent count() queries to show the no of filtered results @@ -573,11 +469,6 @@ def get_urls(self): self.admin_site.admin_view(self.dataset_retention_view), name="ai-observability-dataset-retention", ), - path( - "bulk-deactivate/", - self.admin_site.admin_view(self.bulk_deactivate_view), - name="organization_bulk_deactivate", - ), path( "/model-counts/", self.admin_site.admin_view(self.model_counts_view), @@ -591,110 +482,6 @@ def get_urls(self): ] return custom_urls + urls - def _bulk_deactivation_candidates(self, organization_ids: list[UUID]) -> tuple[list[Organization], list[str]]: - organizations_by_id = Organization.objects.only("id", "name", "is_active", "is_not_active_reason").in_bulk( - organization_ids - ) - organizations = [ - organizations_by_id[organization_id] - for organization_id in organization_ids - if organization_id in organizations_by_id - ] - missing_ids = [ - str(organization_id) for organization_id in organization_ids if organization_id not in organizations_by_id - ] - return organizations, missing_ids - - def bulk_deactivate_view(self, request): - if not self.has_change_permission(request): - raise PermissionDenied - - form = BulkDeactivateOrganizationsForm(request.POST or None) - preview_organizations: list[Organization] = [] - deactivation_organizations: list[Organization] = [] - already_inactive_organizations: list[Organization] = [] - missing_ids: list[str] = [] - preview_token = "" - resolved_reason = "" - - if request.method == "POST" and form.is_valid(): - organization_ids = form.cleaned_data["organization_ids"] - resolved_reason = form.cleaned_data["resolved_reason"] - preview_organizations, missing_ids = self._bulk_deactivation_candidates(organization_ids) - deactivation_organizations = [ - organization for organization in preview_organizations if organization.is_active is not False - ] - already_inactive_organizations = [ - organization for organization in preview_organizations if organization.is_active is False - ] - - if not preview_organizations: - form.add_error("organization_ids", "No organizations matched these IDs.") - elif not deactivation_organizations: - form.add_error("organization_ids", "All matched organizations are already inactive.") - elif "confirm" in request.POST: - preview_token = form.preview_token() - if not form.preview_token_matches(request.POST.get("preview_token", "")): - form.add_error(None, "Review the organizations again before deactivating.") - else: - deactivation_ids = [organization.id for organization in deactivation_organizations] - with transaction.atomic(): - locked_deactivation_organizations = list( - Organization.objects.select_for_update() - .only("id", "is_active", "is_not_active_reason") - .filter(id__in=deactivation_ids) - .exclude(is_active=False) - ) - for organization in locked_deactivation_organizations: - organization.is_active = False - organization.is_not_active_reason = resolved_reason - organization.save(update_fields=["is_active", "is_not_active_reason", "updated_at"]) - - count = len(locked_deactivation_organizations) - if count: - messages.success(request, f"Deactivated {pluralize(count, 'organization')}.") - if missing_ids: - missing_count = len(missing_ids) - messages.warning( - request, - ( - f"Skipped {pluralize(missing_count, 'organization ID')} " - f"that {'were' if missing_count != 1 else 'was'} not found." - ), - ) - skipped_inactive_count = len(already_inactive_organizations) + ( - len(deactivation_organizations) - count - ) - if skipped_inactive_count: - messages.warning( - request, - ( - f"Skipped {pluralize(skipped_inactive_count, 'already inactive organization')} " - "without changing the existing reason." - ), - ) - return redirect(reverse("admin:posthog_organization_changelist")) - else: - preview_token = form.preview_token() - - return render( - request, - "admin/posthog/organization/bulk_deactivate.html", - { - **self.admin_site.each_context(request), - "title": "Bulk deactivate organizations", - "opts": self.model._meta, - "form": form, - "preview_organizations": preview_organizations, - "deactivation_organizations": deactivation_organizations, - "already_inactive_organizations": already_inactive_organizations, - "missing_ids": missing_ids, - "preview_token": preview_token, - "resolved_reason": resolved_reason, - "custom_reason_value": BulkDeactivateOrganizationsForm.CUSTOM_REASON, - }, - ) - def model_counts_view(self, request, organization_id): organization = Organization.objects.get(id=organization_id) counts = get_model_counts_for_organization(organization) diff --git a/posthog/admin/console/AGENTS.md b/posthog/admin/console/AGENTS.md index 71925b4d8a53..b7f1c3db12ca 100644 --- a/posthog/admin/console/AGENTS.md +++ b/posthog/admin/console/AGENTS.md @@ -28,9 +28,11 @@ Before you add or change a panel, operation or tool, invoke `/extending-the-admi A tool is a page that is not about one record, for example a report across all teams. 1. In `products//backend/console.py`, subclass `ConsoleTool` and decorate it with `@console.register_tool`. Set `key`, `title`, `owner` and `group`; the group is the navigation heading. `posthog/admin/console/events_retention_tool.py` is the reference. -2. `page(request)` returns a `Page` of blocks (`Notice`, `Table` with `Row` and `GroupRow`, `Paging`, `Output`). It only reads; the console serves it for GET and HEAD only. The console renders the blocks, so a tool needs no template. When a tool needs a block that does not exist, add it to `tools.py` and `tool.html` in the shell, not a template in the product. +2. `page(request)` returns a `Page` of blocks (`Notice`, `Table` with `Row` and `GroupRow`, `Paging`, `Output`, `Live`). It only reads; the console serves it for GET and HEAD only. The console renders the blocks, so a tool needs no template. When a tool needs a block that does not exist, add it to `tools.py` and `tool.html` in the shell, not a template in the product. 3. For an action that is not about one record, return a `ToolOperation` from `operations(request)`. It works like a record `Operation` without the record: `handler(request, form)`. Its handler returns `None` (back to the tool with its messages), or a `Page` that the console shows as the result. Return a result `Page` only when running the operation again is harmless, such as a dry run, because a reload submits the form again. `products/cohorts/backend/console.py` is the reference. -4. When a tool replaces a Django admin page, keep the old URL name and point it at `MovedToConsole.page_redirect`, so existing links keep working. +4. An operation that changes many records at once sets `preview`. The console shows the preview `Page` and a confirm button, and the confirm runs exactly the previewed input from a signed token. The handler still re-checks the current state, because records can change between preview and confirm. The token is readable, so a form with a preview holds no secrets. `posthog/admin/console/organization_tools.py` is the reference. +5. A block that should follow running work, such as the runs of a started job, is a `Live` block. Return it from `live(request, key)` and embed the same call in `page`. Keep `refresh_seconds` set while something runs and None once nothing does, so the page stops asking. `posthog/admin/console/health_checks_tool.py` is the reference. +6. When a tool replaces a Django admin page, keep the old path and point it at `MovedToConsole.page_redirect`, so bookmarks keep working. Keep its URL name too when the name lives in the root URLconf, such as `ee/urls.py`. A name in the `admin:` namespace only exists inside a `ModelAdmin.get_urls`, which the freeze rule flags, so drop it once `rg` finds no caller that reverses it. ## Move a Django admin action diff --git a/posthog/admin/console/confirm.py b/posthog/admin/console/confirm.py new file mode 100644 index 000000000000..6b7578150fdd --- /dev/null +++ b/posthog/admin/console/confirm.py @@ -0,0 +1,43 @@ +from django import forms +from django.core import signing +from django.http import HttpRequest, QueryDict + +# Long enough to read a preview, short enough that a forgotten tab cannot confirm a stale one tomorrow. +MAX_AGE_SECONDS = 15 * 60 +SALT = "posthog.admin.console.confirm" + + +class ConfirmationToken: + """Carries what staff submitted from the preview page to the confirm button. + + The confirm submit holds only this token, and the form is bound from it, so the operation runs exactly the + input the preview showed. The token is signed, not encrypted: a form with a preview must not hold secrets. + It is bound to the staff member, the operation and its target, so it cannot confirm anything else. The + handler still checks the current state, because the records may have changed since the preview. + """ + + def __init__(self, request: HttpRequest, target: str, operation_key: str) -> None: + self.request = request + self.scope = { + "actor": request.user.pk, + "target": target, + "operation": operation_key, + "query": request.META.get("QUERY_STRING", ""), + } + + def sign(self, form: forms.Form) -> str: + data = {name: self.request.POST.getlist(name) for name in form.fields if name in self.request.POST} + return signing.dumps({**self.scope, "data": data}, salt=SALT) + + def load(self, token: str) -> QueryDict | None: + """The submitted form data, or None when the token is forged, expired, or made for something else.""" + try: + payload = signing.loads(token, salt=SALT, max_age=MAX_AGE_SECONDS) + except signing.BadSignature: + return None + if not isinstance(payload, dict) or {key: payload.get(key) for key in self.scope} != self.scope: + return None + data = QueryDict(mutable=True) + for name, values in payload.get("data", {}).items(): + data.setlist(name, values) + return data diff --git a/posthog/admin/console/health_checks_tool.py b/posthog/admin/console/health_checks_tool.py new file mode 100644 index 000000000000..b9f427cb9fb9 --- /dev/null +++ b/posthog/admin/console/health_checks_tool.py @@ -0,0 +1,278 @@ +import asyncio +import dataclasses +from datetime import datetime, timedelta +from urllib.parse import urlencode + +from django import forms +from django.conf import settings +from django.contrib import messages +from django.http import HttpRequest, HttpResponse, HttpResponseRedirect + +import structlog +from temporalio.client import Client +from temporalio.common import WorkflowIDReusePolicy +from temporalio.exceptions import WorkflowAlreadyStartedError + +from posthog.admin.console.panels import Tone +from posthog.admin.console.registry import console +from posthog.admin.console.team_panels import DEV_EXPERIENCE +from posthog.admin.console.tools import Cell, ConsoleTool, Live, Notice, Page, Row, Table, ToolOperation +from posthog.dataclasses import frozen +from posthog.temporal.common.client import sync_connect +from posthog.temporal.health_checks.framework import HealthCheckRegistration +from posthog.temporal.health_checks.models import HealthCheckWorkflowInputs +from posthog.temporal.health_checks.registry import HEALTH_CHECKS, ensure_registry_loaded + +logger = structlog.get_logger(__name__) + +TOOL_URL = "/admin/console/tools/health-checks/" +RECENT_RUNS = 20 +REFRESH_SECONDS_WHILE_RUNNING = 10 +# Temporal can be away for a moment, so the history keeps asking, just less often. +REFRESH_SECONDS_WHILE_TEMPORAL_IS_AWAY = 30 +# Bounds the history read, so a stalled Temporal cannot hold a web worker. +TEMPORAL_READ_TIMEOUT = timedelta(seconds=5) +STATUS_TONES = { + "RUNNING": Tone.INFO, + "CONTINUED_AS_NEW": Tone.INFO, + "COMPLETED": Tone.SUCCESS, + "FAILED": Tone.DESTRUCTIVE, + "TERMINATED": Tone.DESTRUCTIVE, + "TIMED_OUT": Tone.DESTRUCTIVE, + "CANCELED": Tone.WARNING, +} + + +class HealthCheckRunForm(forms.Form): + dry_run = forms.BooleanField(required=False, help_text="Run without writing issues to the database.") + batch_size = forms.IntegerField(min_value=1, max_value=10000, help_text="Teams per batch.") + max_concurrent = forms.IntegerField(min_value=1, max_value=20, help_text="Batches that run at the same time.") + rollout_percentage = forms.FloatField( + min_value=0.01, max_value=1.0, help_text="Fraction of teams to check, from 0.01 to 1.0." + ) + active_since_days = forms.IntegerField( + required=False, + min_value=0, + help_text="Only teams with a member who logged in within this many days. 0 or empty checks every team.", + ) + team_ids = forms.CharField( + required=False, + widget=forms.Textarea(attrs={"rows": 3, "placeholder": "1, 2, 3"}), + help_text="Comma-separated team IDs to check. Leave empty for every team.", + ) + + def clean_active_since_days(self) -> int | None: + return self.cleaned_data.get("active_since_days") or None + + def clean_team_ids(self) -> list[int] | None: + raw = self.cleaned_data.get("team_ids", "").strip() + if not raw: + return None + try: + return [int(part.strip()) for part in raw.split(",") if part.strip()] + except ValueError: + raise forms.ValidationError("Enter team IDs as comma-separated numbers.") + + +@frozen +class HealthCheckRun: + workflow_id: str + run_id: str + status: str + started: datetime | None + closed: datetime | None + + +class HealthCheckRuns: + """Reads and starts health check workflows in Temporal. Workflow IDs start with the check's name.""" + + @staticmethod + def recent(check: HealthCheckRegistration) -> list[HealthCheckRun] | None: + """The latest runs, newest first, or None when Temporal cannot be reached.""" + prefix = f"health-check-{check.name}" + query = f'WorkflowId >= "{prefix}" AND WorkflowId < "{prefix}~" ORDER BY StartTime DESC' + + async def fetch(client: Client) -> list[HealthCheckRun]: + runs: list[HealthCheckRun] = [] + async for workflow in client.list_workflows( + query=query, limit=RECENT_RUNS, rpc_timeout=TEMPORAL_READ_TIMEOUT + ): + runs.append( + HealthCheckRun( + workflow_id=workflow.id, + run_id=workflow.run_id, + status=workflow.status.name if workflow.status else "UNKNOWN", + started=workflow.start_time, + closed=workflow.close_time, + ) + ) + if len(runs) >= RECENT_RUNS: + break + return runs + + try: + # Connect before the event loop starts: sync_connect refuses to run inside one. + return asyncio.run(fetch(sync_connect())) + except Exception: + logger.warning("admin_console_health_check_runs_unavailable", check=check.name, exc_info=True) + return None + + @staticmethod + def start(check: HealthCheckRegistration, form: forms.Form, form_nonce: str) -> str: + inputs = dataclasses.replace( + HealthCheckWorkflowInputs.from_config(check), + dry_run=form.cleaned_data["dry_run"], + batch_size=form.cleaned_data["batch_size"], + max_concurrent=form.cleaned_data["max_concurrent"], + rollout_percentage=form.cleaned_data["rollout_percentage"], + active_since_days=form.cleaned_data["active_since_days"], + team_ids=form.cleaned_data["team_ids"], + ) + # The form nonce names the workflow, so a double submit of one form starts one run. + workflow_id = f"health-check-{check.name}-manual-{form_nonce}" + asyncio.run( + sync_connect().start_workflow( + "health-check-workflow", + dataclasses.asdict(inputs), + id=workflow_id, + task_queue=settings.HEALTH_CHECK_TASK_QUEUE, + execution_timeout=check.execution_timeout, + # Temporal reuses a closed workflow's ID by default, so a resubmit after the run ended would start again. + id_reuse_policy=WorkflowIDReusePolicy.REJECT_DUPLICATE, + ) + ) + return workflow_id + + +class RunHealthCheck: + def __init__(self, check: HealthCheckRegistration) -> None: + self.check = check + + def __call__(self, request: HttpRequest, form: forms.Form | None) -> HttpResponse | Page | None: + assert form is not None + form_nonce = request.POST.get("form_nonce", "") + try: + workflow_id = HealthCheckRuns.start(self.check, form, form_nonce) + except WorkflowAlreadyStartedError: + messages.info(request, "This form already started a run. It shows in the list below.") + return None + except Exception as error: + logger.exception("admin_console_health_check_start_failed", check=self.check.name) + messages.error(request, f"Could not start the run: {error}") + return HttpResponse(status=502) + messages.success(request, f"Started {workflow_id}.") + # Temporal lists a new run only after a moment, so the page names it and keeps refreshing until it shows. + return HttpResponseRedirect(f"{TOOL_URL}?{urlencode({'check': self.check.kind, 'started': workflow_id})}") + + +def _run_row(run: HealthCheckRun) -> Row: + temporal_url = ( + f"{settings.TEMPORAL_UI_HOST}/namespaces/{settings.TEMPORAL_NAMESPACE}/workflows/{run.workflow_id}/{run.run_id}" + ) + return Row( + cells=( + Cell(text=run.workflow_id, href=temporal_url, mono=True), + Cell(text="Manual" if "-manual-" in run.workflow_id else "Scheduled"), + Cell(text=run.status.replace("_", " ").capitalize(), tone=STATUS_TONES.get(run.status)), + Cell(text=run.started.strftime("%Y-%m-%d %H:%M:%S") if run.started else "-", muted=run.started is None), + Cell(text=run.closed.strftime("%Y-%m-%d %H:%M:%S") if run.closed else "-", muted=run.closed is None), + ) + ) + + +def _check_row(check: HealthCheckRegistration) -> Row: + return Row( + cells=( + Cell(text=check.name, href=f"{TOOL_URL}?{urlencode({'check': check.kind})}"), + Cell(text=check.owner.value), + Cell(text=check.schedule or "Manual only", mono=bool(check.schedule), muted=not check.schedule), + Cell(text=f"{check.rollout_percentage:.2f}"), + Cell(text=f"{check.active_since_days} days" if check.active_since_days else "All teams"), + Cell(text="Yes" if check.dry_run else "No"), + ) + ) + + +@console.register_tool +class HealthChecksTool(ConsoleTool): + key = "health-checks" + title = "Health checks" + owner = DEV_EXPERIENCE + group = "Data" + description = "Runs a health check across teams and follows its runs in Temporal." + + def _check(self, request: HttpRequest) -> HealthCheckRegistration | None: + ensure_registry_loaded() + return HEALTH_CHECKS.get(request.GET.get("check", "")) + + def page(self, request: HttpRequest) -> Page: + check = self._check(request) + if check is None: + ensure_registry_loaded() + checks = sorted(HEALTH_CHECKS.values(), key=lambda registered: registered.name) + return Page( + blocks=( + Notice(text="Pick a check to run it and see its recent runs."), + Table( + columns=("Check", "Owner", "Schedule", "Rollout", "Active since", "Dry run"), + rows=tuple(_check_row(registered) for registered in checks), + empty_text="No health checks are registered. Product modules register them in the registry.", + ), + ) + ) + runs = self.live(request, "runs") + assert runs is not None + return Page( + blocks=( + Notice( + text=f"{check.name}, owned by {check.owner.value}.", link_text="All health checks", href=TOOL_URL + ), + runs, + ) + ) + + def live(self, request: HttpRequest, key: str) -> Live | None: + check = self._check(request) + if key != "runs" or check is None: + return None + runs = HealthCheckRuns.recent(check) + if runs is None: + return Live( + key="runs", + block=Notice(text="Temporal did not answer, so the runs cannot be shown. This retries on its own."), + refresh_seconds=REFRESH_SECONDS_WHILE_TEMPORAL_IS_AWAY, + ) + # A run that continued as new is closed; its successor shows as its own running row. + running = any(run.status == "RUNNING" for run in runs) + started = request.GET.get("started") + waiting_for_new_run = started is not None and all(run.workflow_id != started for run in runs) + return Live( + key="runs", + block=Table( + columns=("Workflow", "Source", "Status", "Started", "Finished"), + rows=tuple(_run_row(run) for run in runs), + empty_text="This check has not run yet.", + ), + refresh_seconds=REFRESH_SECONDS_WHILE_RUNNING if running or waiting_for_new_run else None, + ) + + def operations(self, request: HttpRequest) -> list[ToolOperation]: + check = self._check(request) + if check is None: + return [] + return [ + ToolOperation( + key="run", + label=f"Run {check.name}", + handler=RunHealthCheck(check), + description="Starts a run now, with the settings you pick.", + form_class=HealthCheckRunForm, + form_initial={ + "dry_run": check.dry_run, + "batch_size": check.batch_size, + "max_concurrent": check.max_concurrent, + "rollout_percentage": check.rollout_percentage, + "active_since_days": check.active_since_days, + }, + ) + ] diff --git a/posthog/admin/console/moved.py b/posthog/admin/console/moved.py index bb739d8824cb..46945b9b5547 100644 --- a/posthog/admin/console/moved.py +++ b/posthog/admin/console/moved.py @@ -1,4 +1,5 @@ from collections.abc import Callable +from urllib.parse import quote from django.contrib import admin, messages from django.db.models import Model @@ -17,13 +18,16 @@ class MovedToConsole: """ @staticmethod - def page_redirect(title: str, url: str) -> Callable[[HttpRequest], HttpResponse]: + def page_redirect(title: str, url: str) -> Callable[..., HttpResponse]: """A view for the old URL of a page that moved. It says so once, so a bookmark does not move silently.""" - def view(request: HttpRequest) -> HttpResponse: + def view(request: HttpRequest, **url_kwargs: object) -> HttpResponse: messages.info(request, f"{title} moved to PostHog Admin. Update your bookmark to this page.") + # Old URLs with a path parameter, such as a health check kind, name it in `url` as {kind}. + target = url.format(**{name: quote(str(value), safe="") for name, value in url_kwargs.items()}) query = request.META.get("QUERY_STRING", "") - return HttpResponseRedirect(url + (f"?{query}" if query else "")) + separator = "&" if "?" in target else "?" + return HttpResponseRedirect(target + (f"{separator}{query}" if query else "")) # Like every console route, an old URL answers only to signed-in staff. return admin.site.admin_view(view) diff --git a/posthog/admin/console/organization_tools.py b/posthog/admin/console/organization_tools.py new file mode 100644 index 000000000000..e561be2b2348 --- /dev/null +++ b/posthog/admin/console/organization_tools.py @@ -0,0 +1,202 @@ +import re +from typing import Any +from uuid import UUID + +from django import forms +from django.contrib import messages +from django.db import transaction +from django.http import HttpRequest, HttpResponse + +from posthog.admin.console.records import RECORD_TYPES +from posthog.admin.console.registry import console +from posthog.admin.console.team_panels import DEV_EXPERIENCE +from posthog.admin.console.tools import Cell, ConsoleTool, Notice, Page, Row, Table, ToolOperation +from posthog.models.organization import Organization +from posthog.utils import pluralize + + +class BulkDeactivateOrganizationsForm(forms.Form): + CUSTOM_REASON = "__custom__" + MAX_ORGANIZATIONS = 100 + + organization_ids = forms.CharField( + label="Organization IDs", + widget=forms.Textarea( + attrs={"rows": 10, "placeholder": "Paste one organization ID per line, or separate IDs with commas."} + ), + help_text=f"Up to {MAX_ORGANIZATIONS} organization IDs, separated by new lines, commas or spaces.", + ) + reason = forms.ChoiceField( + label="Reason", + choices=[*Organization.DeactivationReason.choices, (CUSTOM_REASON, "Custom reason")], + initial=Organization.DeactivationReason.DESKTOP_ABUSE, + help_text="Members of the organization see this reason.", + ) + custom_reason = forms.CharField( + label="Custom reason", + required=False, + max_length=Organization._meta.get_field("is_not_active_reason").max_length, + widget=forms.Textarea(attrs={"rows": 3}), + help_text="Used only when the reason is Custom reason.", + ) + + def clean_organization_ids(self) -> list[UUID]: + raw_value = self.cleaned_data["organization_ids"] + invalid_tokens: list[str] = [] + organization_ids: list[UUID] = [] + seen_ids: set[UUID] = set() + token_count = 0 + + for match in re.finditer(r"[^,\s]+", raw_value): + token_count += 1 + if token_count > self.MAX_ORGANIZATIONS: + raise forms.ValidationError(f"Enter {self.MAX_ORGANIZATIONS} organization IDs or fewer.") + token = match.group() + try: + organization_id = UUID(token) + except ValueError: + invalid_tokens.append(token) + continue + if organization_id not in seen_ids: + seen_ids.add(organization_id) + organization_ids.append(organization_id) + + if token_count == 0: + raise forms.ValidationError("Enter at least one organization ID.") + if invalid_tokens: + raise forms.ValidationError( + "These organization IDs are not valid UUIDs: %(ids)s", + params={"ids": ", ".join(invalid_tokens[:10])}, + ) + return organization_ids + + def clean(self) -> dict[str, Any]: + cleaned_data = super().clean() or {} + reason = cleaned_data.get("reason") + custom_reason = (cleaned_data.get("custom_reason") or "").strip() + if reason == self.CUSTOM_REASON: + if not custom_reason: + self.add_error("custom_reason", "Enter a custom reason.") + return cleaned_data + cleaned_data["resolved_reason"] = custom_reason + elif reason: + cleaned_data["resolved_reason"] = reason + return cleaned_data + + +class BulkDeactivation: + """Which of the pasted organizations a bulk deactivation changes, skips, or cannot find.""" + + def __init__(self, organization_ids: list[UUID]) -> None: + found = Organization.objects.only("id", "name", "is_active", "is_not_active_reason").in_bulk(organization_ids) + matched = [found[organization_id] for organization_id in organization_ids if organization_id in found] + self.to_deactivate = [organization for organization in matched if organization.is_active is not False] + self.already_inactive = [organization for organization in matched if organization.is_active is False] + self.missing_ids = [ + str(organization_id) for organization_id in organization_ids if organization_id not in found + ] + + +def _organization_row(organization: Organization, status: str) -> Row: + return Row( + cells=( + Cell(text=organization.name, href=RECORD_TYPES["organization"].url(organization)), + Cell(text=str(organization.id), mono=True), + Cell(text=status), + ) + ) + + +def preview_bulk_deactivation(request: HttpRequest, form: forms.Form) -> Page: + deactivation = BulkDeactivation(form.cleaned_data["organization_ids"]) + if not deactivation.to_deactivate: + message = ( + "All matched organizations are already inactive." + if deactivation.already_inactive + else "No organizations matched these IDs." + ) + form.add_error("organization_ids", message) + return Page(blocks=()) + + rows = [_organization_row(organization, "Deactivates") for organization in deactivation.to_deactivate] + rows += [ + _organization_row(organization, f"Skipped, already inactive: {organization.is_not_active_reason or '-'}") + for organization in deactivation.already_inactive + ] + blocks: list[Notice | Table] = [ + Notice( + text=( + f"Deactivates {pluralize(len(deactivation.to_deactivate), 'organization')} " + f"with the reason: {form.cleaned_data['resolved_reason']}" + ) + ), + Table(columns=("Organization", "ID", "What happens"), rows=tuple(rows)), + ] + if deactivation.missing_ids: + blocks.append(Notice(text=f"Not found, skipped: {', '.join(deactivation.missing_ids)}")) + return Page(blocks=tuple(blocks)) + + +def bulk_deactivate(request: HttpRequest, form: forms.Form | None) -> HttpResponse | Page | None: + assert form is not None + deactivation = BulkDeactivation(form.cleaned_data["organization_ids"]) + reason = form.cleaned_data["resolved_reason"] + with transaction.atomic(): + # Read the active state again under a lock, so an organization deactivated since the preview keeps + # its existing reason. No-key, because only plain columns change and child rows may keep inserting. + locked = list( + Organization.objects.select_for_update(no_key=True) + .only("id", "is_active", "is_not_active_reason") + .filter(id__in=[organization.id for organization in deactivation.to_deactivate]) + .exclude(is_active=False) + ) + for organization in locked: + organization.is_active = False + organization.is_not_active_reason = reason + organization.save(update_fields=["is_active", "is_not_active_reason", "updated_at"]) + + if locked: + messages.success(request, f"Deactivated {pluralize(len(locked), 'organization')}.") + if deactivation.missing_ids: + messages.warning( + request, f"Skipped {pluralize(len(deactivation.missing_ids), 'organization ID')} that were not found." + ) + skipped = len(deactivation.already_inactive) + len(deactivation.to_deactivate) - len(locked) + if skipped: + messages.warning(request, f"Skipped {pluralize(skipped, 'already inactive organization')}, reason unchanged.") + return None + + +@console.register_tool +class OrganizationBulkDeactivationTool(ConsoleTool): + key = "bulk-deactivate-organizations" + title = "Bulk deactivate organizations" + owner = DEV_EXPERIENCE + group = "Organizations" + description = "Deactivates organizations from a list of pasted IDs, after a preview." + + def page(self, request: HttpRequest) -> Page: + return Page( + blocks=( + Notice( + text=( + "Members of a deactivated organization cannot use PostHog until staff reactivate it. " + "You see the organizations and what happens to each before anything changes." + ) + ), + ) + ) + + def operations(self, request: HttpRequest) -> list[ToolOperation]: + return [ + ToolOperation( + key="deactivate", + label="Deactivate organizations", + handler=bulk_deactivate, + preview=preview_bulk_deactivation, + danger=True, + description="Deactivates every pasted organization that is still active.", + form_class=BulkDeactivateOrganizationsForm, + undo_hint="To undo, reactivate each organization on its Django admin page.", + ) + ] diff --git a/posthog/admin/console/registry.py b/posthog/admin/console/registry.py index cef3f423027b..ce01185785c5 100644 --- a/posthog/admin/console/registry.py +++ b/posthog/admin/console/registry.py @@ -7,7 +7,7 @@ from posthog.admin.console.tools import ConsoleTool # Navigation headings. Add one here, in the shell, only when no existing group fits a new tool. -TOOL_GROUPS = frozenset({"Data", "Billing"}) +TOOL_GROUPS = frozenset({"Data", "Billing", "Organizations"}) KEY_PATTERN = re.compile(r"^[a-z0-9]+(?:-[a-z0-9]+)*$") @@ -72,6 +72,8 @@ def discover(self) -> None: return # Core panels register first, so they lead each page. import posthog.admin.console.team_panels # noqa: F401, PLC0415 — registers the core Team panels + import posthog.admin.console.health_checks_tool # noqa: F401, PLC0415 — registers a core tool + import posthog.admin.console.organization_tools # noqa: F401, PLC0415 — registers a core tool import posthog.admin.console.usage_reports_tool # noqa: F401, PLC0415 — registers a core tool import posthog.admin.console.organization_panels # noqa: F401, PLC0415 — registers the core Organization panels import posthog.admin.console.events_retention_tool # noqa: F401, PLC0415 — registers a core tool diff --git a/posthog/admin/console/tools.py b/posthog/admin/console/tools.py index bfee891456b6..7a1034593432 100644 --- a/posthog/admin/console/tools.py +++ b/posthog/admin/console/tools.py @@ -1,3 +1,4 @@ +import re from typing import TYPE_CHECKING, ClassVar, Protocol from django import forms @@ -44,6 +45,13 @@ class Table: class Notice: kind: ClassVar[str] = "notice" text: str + # An optional link after the text, for example back to the list a sub-page came from. + link_text: str = "" + href: str | None = None + + def __post_init__(self) -> None: + if self.href is not None and (not self.href.startswith("/") or self.href.startswith("//")): + raise ValueError(f"Notice links to {self.href!r}. Link to a PostHog path that starts with /.") @frozen @@ -64,7 +72,34 @@ class Paging: next_url: str | None -Block = Notice | Table | Paging | Output +@frozen +class Live: + """A block that the page refreshes from the server while `refresh_seconds` is set, for example the runs of a + job that is still going. Set it to None when nothing is running, and the page stops asking. + + The tool builds it in `ConsoleTool.live`, and `page` embeds the same call, so the first render and every + refresh come from one place. + """ + + kind: ClassVar[str] = "live" + # Stable across requests: the page asks for the block by this key, not by its position. + key: str + block: "Notice | Table | Output" + refresh_seconds: int | None = None + + def __post_init__(self) -> None: + if not re.fullmatch(r"[a-z0-9]+(?:-[a-z0-9]+)*", self.key): + raise ValueError(f"Live block key {self.key!r} must be lowercase and dash-separated, like a URL slug") + + +Block = Notice | Table | Paging | Output | Live +INNER_BLOCKS = Notice | Table | Output + + +def _check_table(table: Table) -> None: + for row in table.rows: + if isinstance(row, Row) and len(row.cells) != len(table.columns): + raise ValueError(f"A table row has {len(row.cells)} cells for {len(table.columns)} columns") @frozen @@ -74,12 +109,15 @@ class Page: def __post_init__(self) -> None: # tool.html renders only the block kinds it knows, so an unknown block would vanish without an error. for block in self.blocks: - if not isinstance(block, Notice | Table | Paging | Output): + if not isinstance(block, Notice | Table | Paging | Output | Live): raise ValueError(f"{type(block).__name__} is not a console block. Add new blocks to the shell.") - if isinstance(block, Table): - for row in block.rows: - if isinstance(row, Row) and len(row.cells) != len(block.columns): - raise ValueError(f"A table row has {len(row.cells)} cells for {len(block.columns)} columns") + if isinstance(block, Live) and not isinstance(block.block, INNER_BLOCKS): + raise ValueError( + f"Live block {block.key!r} holds a {type(block.block).__name__}, not a table, notice or output" + ) + inner = block.block if isinstance(block, Live) else block + if isinstance(inner, Table): + _check_table(inner) class ToolOperationHandler(Protocol): @@ -93,6 +131,16 @@ class ToolOperationHandler(Protocol): def __call__(self, request: "HttpRequest", form: forms.Form | None) -> "HttpResponse | Page | None": ... +class PreviewHandler(Protocol): + """Shows what an operation would change before it runs, as a `Page`. It must not change anything. + + To refuse the input, for example when no record matches, add an error to the form; the console then shows + the form again. + """ + + def __call__(self, request: "HttpRequest", form: forms.Form) -> "Page": ... + + @frozen class ToolOperation: """An action a tool offers, for example starting a backfill. Like a record `Operation`, without a record.""" @@ -106,9 +154,13 @@ class ToolOperation: description: str = "" undo_hint: str = "" gate: Gate | None = None + # Staff confirm on a page that shows what will change. For actions on many records, such as a bulk change. + preview: PreviewHandler | None = None def __post_init__(self) -> None: check_danger(self.key, self.danger, self.form_class, self.undo_hint) + if self.preview is not None and self.form_class is None: + raise ValueError(f"Operation {self.key!r} has a preview, so it needs a form for the preview to read") class ConsoleTool: @@ -132,3 +184,7 @@ def page(self, request: "HttpRequest") -> Page: def operations(self, request: "HttpRequest") -> list[ToolOperation]: """The actions the tool page offers. The console runs them; `page` stays read-only.""" return [] + + def live(self, request: "HttpRequest", key: str) -> Live | None: + """The live block with this key, for the page's refreshes. None when the tool has no such block.""" + return None diff --git a/posthog/admin/console/urls.py b/posthog/admin/console/urls.py index 83556e7e24c1..6cd4bf9a187d 100644 --- a/posthog/admin/console/urls.py +++ b/posthog/admin/console/urls.py @@ -50,6 +50,11 @@ admin.site.admin_view(views.tool_page), name="admin_console_tool", ), + path( + "admin/console/tools//live//", + admin.site.admin_view(views.tool_live), + name="admin_console_tool_live", + ), path( "admin/console/tools///", admin.site.admin_view(views.run_tool_operation), @@ -69,6 +74,10 @@ ) for old, target in MOVED_ORGANIZATION_ACTIONS.items() ], + path( + "admin/posthog/organization/bulk-deactivate/", + MovedToConsole.page_redirect("Bulk deactivation", "/admin/console/tools/bulk-deactivate-organizations/"), + ), path( "admin/posthog/organization/send-usage-report/", MovedToConsole.page_redirect("Sending usage reports", "/admin/console/tools/usage-reports/"), diff --git a/posthog/admin/console/views.py b/posthog/admin/console/views.py index 3ec1d31ddba4..e0a8b269b144 100644 --- a/posthog/admin/console/views.py +++ b/posthog/admin/console/views.py @@ -9,7 +9,7 @@ from django.contrib.staticfiles import finders from django.core.exceptions import PermissionDenied from django.db.models import Model, Q -from django.http import Http404, HttpRequest, HttpResponse, HttpResponseNotAllowed, HttpResponseRedirect +from django.http import Http404, HttpRequest, HttpResponse, HttpResponseNotAllowed, HttpResponseRedirect, QueryDict from django.shortcuts import get_object_or_404, redirect, render from django.urls import reverse from django.views.decorators.http import require_safe @@ -17,6 +17,7 @@ import structlog from posthog.admin.console.bridge import QueuedMessages +from posthog.admin.console.confirm import ConfirmationToken from posthog.admin.console.panels import Fact, Operation, Panel, Tone from posthog.admin.console.records import RECORD_TYPES, RecordLink, RecordType from posthog.admin.console.registry import console @@ -275,6 +276,8 @@ def _operation_context( "target": target, "operation": operation, "form": form, + # Templates call callables, so the preview handler cannot be tested there directly. + "has_preview": getattr(operation, "preview", None) is not None, # Handlers that move money or send messages build an idempotency key from it, so a double submit runs once. "form_nonce": request.POST.get("form_nonce") or str(uuid.uuid4()), } @@ -334,7 +337,7 @@ def run_operation(request: HttpRequest, record: str, pk: object, panel_key: str, # A handler can fail after its state change committed, for example when a notification fails. # The operation is then no longer offered, and staff belong on the record page, not on a dead form. if form is not None and _is_offered(panel, obj, operation.key): - return render(request, "admin_console/operation.html", context, status=400) + return render(request, "admin_console/operation.html", context, status=response.status_code) if QueuedMessages.error_count(request) == 0: messages.error( request, @@ -352,10 +355,14 @@ def _get_tool(tool_key: str) -> ConsoleTool: return tool -def _with_query(url: str, request: HttpRequest) -> str: - # A tool can have sub-pages picked by query parameters, so its operations and the way back keep them. +def _query_suffix(request: HttpRequest) -> str: query = request.META.get("QUERY_STRING", "") - return url + (f"?{query}" if query else "") + return f"?{query}" if query else "" + + +def _with_query(url: str, request: HttpRequest) -> str: + # A tool can have sub-pages picked by query parameters, so its operations, live blocks and the way back keep them. + return url + _query_suffix(request) @require_safe @@ -375,8 +382,31 @@ def tool_page(request: HttpRequest, tool_key: str) -> HttpResponse: return render( request, "admin_console/tool.html", - {**ConsoleEnvironment.context(), "tool": tool, "page": tool.page(request), "operations": operations}, + { + **ConsoleEnvironment.context(), + "tool": tool, + "page": tool.page(request), + "operations": operations, + "query_suffix": _query_suffix(request), + }, + ) + + +@require_safe +def tool_live(request: HttpRequest, tool_key: str, live_key: str) -> HttpResponse: + """One live block of a tool page, for console.js to swap in. It reads only, like the page itself.""" + tool = _get_tool(tool_key) + live = tool.live(request, live_key) + if live is None: + raise Http404("Unknown live block") + live_url = _with_query( + reverse("admin_console_tool_live", kwargs={"tool_key": tool.key, "live_key": live.key}), request ) + response = render(request, "admin_console/_live.html", {"tool": tool, "live": live, "live_url": live_url}) + # console.js swaps in only a response that carries this header, never a login page or an error page. + response["X-Console-Live"] = "1" + response["Cache-Control"] = "no-store" + return response def run_tool_operation(request: HttpRequest, tool_key: str, operation_key: str) -> HttpResponse: @@ -393,9 +423,6 @@ def run_tool_operation(request: HttpRequest, tool_key: str, operation_key: str) if request.method == "GET" and operation.form_class is None: return redirect(tool_url) - form = None - if operation.form_class is not None: - form = operation.form_class(request.POST if request.method == "POST" else None, initial=operation.form_initial) target = OperationTarget( kicker="This runs from", title=tool.title, @@ -405,12 +432,36 @@ def run_tool_operation(request: HttpRequest, tool_key: str, operation_key: str) breadcrumbs=(RecordLink(label=tool.title, href=tool_url),), is_record=False, ) + confirmation = ConfirmationToken(request, f"tool:{tool.key}", operation.key) + data: QueryDict | None = request.POST if request.method == "POST" else None + confirm_token = request.POST.get("confirm_token") if request.method == "POST" else None + if confirm_token is not None: + data = confirmation.load(confirm_token) + if data is None: + messages.error( + request, "This confirmation expired or was made for something else. Review the change again." + ) + return redirect(request.get_full_path()) + + form = None + if operation.form_class is not None: + form = operation.form_class(data, initial=operation.form_initial) context = _operation_context(target, operation, form, request) if request.method == "GET": return render(request, "admin_console/operation.html", context) if form is not None and not form.is_valid(): return render(request, "admin_console/operation.html", context, status=400) + if operation.preview is not None and form is not None and confirm_token is None: + preview = operation.preview(request, form) + if form.errors: + return render(request, "admin_console/operation.html", context, status=400) + return render( + request, + "admin_console/confirm.html", + {**context, "page": preview, "confirm_token": confirmation.sign(form)}, + ) + audit = audit.bind(reason=form.cleaned_data.get("reason") if form is not None else None) result = _audited(audit, lambda: operation.handler(request, form)) if result is None: @@ -419,7 +470,7 @@ def run_tool_operation(request: HttpRequest, tool_key: str, operation_key: str) return render(request, "admin_console/result.html", {**context, "page": result}) if result.status_code >= 400: if form is not None: - return render(request, "admin_console/operation.html", context, status=400) + return render(request, "admin_console/operation.html", context, status=result.status_code) if QueuedMessages.error_count(request) == 0: messages.error(request, f"{operation.label} failed with status {result.status_code}.") return redirect(tool_url) diff --git a/posthog/admin/test_admin_console.py b/posthog/admin/test_admin_console.py index d5c91c9f11a8..7fef501a2040 100644 --- a/posthog/admin/test_admin_console.py +++ b/posthog/admin/test_admin_console.py @@ -19,7 +19,7 @@ from posthog.admin.console.panels import MAX_FACTS_PER_PANEL, Fact, Operation, Owner, Panel, ReasonForm from posthog.admin.console.records import RECORD_TYPES from posthog.admin.console.registry import ConsoleRegistry, console -from posthog.admin.console.tools import Cell, ConsoleTool, Page, Row, Table +from posthog.admin.console.tools import Cell, ConsoleTool, Live, Notice, Page, Paging, Row, Table from posthog.csp_middleware import CSPMiddleware from posthog.models.events_retention_config import OrganizationEventsRetentionConfig from posthog.models.organization import Organization @@ -159,6 +159,25 @@ def test_every_owner_crest_ships(self) -> None: "starts with /", ), ("unknown_tool_block", lambda: Page(blocks=(Fact(label="a", value="b"),)), "not a console block"), # type: ignore[arg-type] + ( + "live_block_with_a_key_that_is_not_a_slug", + lambda: Live(key="Runs list", block=Notice(text="a")), + "lowercase and dash-separated", + ), + ( + "live_block_holding_paging", + lambda: Page( + blocks=( + Live( + key="runs", + block=Paging( # type: ignore[arg-type] + number=1, num_pages=1, summary="", previous_url=None, next_url=None + ), + ), + ) + ), + "not a table, notice or output", + ), ( "table_row_missing_a_cell", lambda: Page(blocks=(Table(columns=("Team", "Months"), rows=(Row(cells=(Cell(text="a"),)),)),)), diff --git a/posthog/admin/test_health_checks_tool.py b/posthog/admin/test_health_checks_tool.py new file mode 100644 index 000000000000..c2ee3b29d7ec --- /dev/null +++ b/posthog/admin/test_health_checks_tool.py @@ -0,0 +1,111 @@ +import re +from datetime import UTC, datetime, timedelta + +from posthog.test.base import BaseTest +from unittest.mock import AsyncMock, MagicMock, patch + +from django.contrib.messages import get_messages + +from parameterized import parameterized +from temporalio.common import WorkflowIDReusePolicy +from temporalio.exceptions import WorkflowAlreadyStartedError + +from posthog.admin.console.health_checks_tool import HealthCheckRun, HealthCheckRuns +from posthog.temporal.health_checks.registry import HEALTH_CHECKS, ensure_registry_loaded + +TOOL_URL = "/admin/console/tools/health-checks/" + + +def _run(status: str) -> HealthCheckRun: + return HealthCheckRun( + workflow_id="health-check-example-manual-1", + run_id="run-1", + status=status, + started=datetime(2026, 11, 5, 12, 0, tzinfo=UTC), + closed=None if status == "RUNNING" else datetime(2026, 11, 5, 12, 5, tzinfo=UTC), + ) + + +class TestHealthChecksTool(BaseTest): + def setUp(self) -> None: + super().setUp() + self.user.is_staff = True + self.user.save() + self.client.force_login(self.user) + ensure_registry_loaded() + self.check = sorted(HEALTH_CHECKS.values(), key=lambda check: check.name)[0] + self.check_url = f"{TOOL_URL}?check={self.check.kind}" + + @parameterized.expand( + [ + ("running", [_run("RUNNING")], "", True), + ("finished", [_run("COMPLETED")], "", False), + ("continued_as_new_and_closed", [_run("CONTINUED_AS_NEW")], "", False), + ("started_run_not_listed_yet", [], "&started=health-check-example-manual-2", True), + ("started_run_listed_and_finished", [_run("COMPLETED")], "&started=health-check-example-manual-1", False), + ("temporal_away", None, "", True), + ] + ) + def test_the_runs_refresh_only_while_there_is_something_to_wait_for( + self, _name: str, runs: list[HealthCheckRun] | None, extra_query: str, refreshes: bool + ) -> None: + with patch.object(HealthCheckRuns, "recent", return_value=runs): + page = self.client.get(self.check_url + extra_query).content.decode() + live = self.client.get(f"{TOOL_URL}live/runs/?check={self.check.kind}{extra_query}") + + assert f"/admin/console/tools/health-checks/run/?check={self.check.kind}" in page + assert f'data-console-live="{TOOL_URL}live/runs/?check={self.check.kind}' in page + assert live.status_code == 200 + assert live["X-Console-Live"] == "1" + assert ("data-refresh-seconds" in live.content.decode()) == refreshes + + # Mocks the connection one level down, so the real sync_connect wrapper runs: it refuses to run inside an + # event loop, which a mock of sync_connect itself would hide. + @patch("posthog.temporal.common.client.connect") + def test_the_runs_come_from_temporal(self, connect: AsyncMock) -> None: + workflow = MagicMock(id="health-check-example-manual-1", run_id="run-1", start_time=None, close_time=None) + workflow.status.name = "RUNNING" + + timeouts = [] + + async def list_workflows(query: str, limit: int | None = None, rpc_timeout: timedelta | None = None): + timeouts.append(rpc_timeout) + yield workflow + + connect.return_value = MagicMock(list_workflows=list_workflows) + + content = self.client.get(f"{TOOL_URL}live/runs/?check={self.check.kind}").content.decode() + + assert "health-check-example-manual-1" in content + assert "data-refresh-seconds" in content + assert timeouts and all(timeout is not None for timeout in timeouts) + + @patch("posthog.admin.console.health_checks_tool.sync_connect") + def test_a_double_submitted_run_form_starts_one_workflow(self, sync_connect: MagicMock) -> None: + start_workflow = AsyncMock(side_effect=[None, WorkflowAlreadyStartedError("id", "health-check-workflow")]) + sync_connect.return_value.start_workflow = start_workflow + form = self.client.get(f"{TOOL_URL}run/?check={self.check.kind}").content.decode() + nonce = re.search(r'name="form_nonce" value="([^"]+)"', form) + assert nonce is not None + data = {"batch_size": 10, "max_concurrent": 1, "rollout_percentage": 1.0, "form_nonce": nonce.group(1)} + + responses = [self.client.post(f"{TOOL_URL}run/?check={self.check.kind}", data) for _ in range(2)] + + assert [response.status_code for response in responses] == [302, 302] + assert ( + responses[0]["Location"] + == f"{self.check_url}&started=health-check-{self.check.name}-manual-{nonce.group(1)}" + ) + workflow_ids = {call.kwargs["id"] for call in start_workflow.call_args_list} + assert workflow_ids == {f"health-check-{self.check.name}-manual-{nonce.group(1)}"} + assert {call.kwargs["id_reuse_policy"] for call in start_workflow.call_args_list} == { + WorkflowIDReusePolicy.REJECT_DUPLICATE + } + levels = [message.level_tag for message in get_messages(responses[1].wsgi_request)] + assert "info" in levels + + def test_an_old_health_check_url_keeps_its_check_and_parameters(self) -> None: + response = self.client.get(f"/admin/health-checks/{self.check.kind}/?from=bookmark") + + assert response.status_code == 302 + assert response["Location"] == f"{self.check_url}&from=bookmark" diff --git a/posthog/admin/test_organization_bulk_deactivation_admin.py b/posthog/admin/test_organization_bulk_deactivation_admin.py index 5a4f99f819e7..ecaaa43c0f8d 100644 --- a/posthog/admin/test_organization_bulk_deactivation_admin.py +++ b/posthog/admin/test_organization_bulk_deactivation_admin.py @@ -1,24 +1,18 @@ +import re from uuid import uuid4 from posthog.test.base import BaseTest from unittest.mock import patch from django.contrib import admin -from django.contrib.admin.sites import AdminSite -from django.contrib.messages.storage.fallback import FallbackStorage -from django.core.exceptions import PermissionDenied -from django.http import HttpRequest from django.template.loader import render_to_string -from django.test import RequestFactory, SimpleTestCase +from django.test import Client, SimpleTestCase from django.urls import reverse -from posthog.admin.admins.organization_admin import BulkDeactivateOrganizationsForm, OrganizationAdmin -from posthog.models import Organization +from parameterized import parameterized - -def _attach_messages(request) -> None: - request.session = {} - request._messages = FallbackStorage(request) +from posthog.admin.console.organization_tools import BulkDeactivateOrganizationsForm +from posthog.models import Organization, User class _PostHogPermissions: @@ -42,7 +36,7 @@ def test_links_to_bulk_deactivation_for_users_with_change_permission(self) -> No assert "Organization tools" in rendered assert 'id="organization-tools"' in rendered assert "Bulk deactivate organizations" in rendered - assert reverse("admin:organization_bulk_deactivate") in rendered + assert "/admin/console/tools/bulk-deactivate-organizations/" in rendered def test_hides_bulk_deactivation_without_change_permission(self) -> None: rendered = render_to_string( @@ -100,31 +94,6 @@ def test_rejects_invalid_uuid(self) -> None: assert not form.is_valid() assert "organization_ids" in form.errors - def test_preview_token_is_bound_to_ids_and_reason(self) -> None: - organization_id = uuid4() - form = BulkDeactivateOrganizationsForm( - { - "organization_ids": str(organization_id), - "reason": Organization.DeactivationReason.DESKTOP_ABUSE.value, - "custom_reason": "", - } - ) - - assert form.is_valid() - preview_token = form.preview_token() - assert form.preview_token_matches(preview_token) - - changed_form = BulkDeactivateOrganizationsForm( - { - "organization_ids": str(organization_id), - "reason": Organization.DeactivationReason.UNPAID_BALANCE.value, - "custom_reason": "", - } - ) - - assert changed_form.is_valid() - assert not changed_form.preview_token_matches(preview_token) - def test_rejects_more_than_max_organization_ids(self) -> None: form = BulkDeactivateOrganizationsForm( { @@ -140,13 +109,17 @@ def test_rejects_more_than_max_organization_ids(self) -> None: assert "organization_ids" in form.errors -class TestOrganizationAdminBulkDeactivate(BaseTest): +TOOL_URL = "/admin/console/tools/bulk-deactivate-organizations/deactivate/" +DESKTOP_ABUSE = Organization.DeactivationReason.DESKTOP_ABUSE.value +COMPLIANCE_REVIEW = Organization.DeactivationReason.COMPLIANCE_REVIEW.value + + +class TestBulkDeactivateOrganizationsTool(BaseTest): def setUp(self) -> None: super().setUp() self.user.is_staff = True self.user.save() - self.factory = RequestFactory() - self.admin = OrganizationAdmin(Organization, AdminSite()) + self.client.force_login(self.user) def test_changelist_links_to_organization_tools_module(self) -> None: self.client.force_login(self.user) @@ -171,193 +144,90 @@ def test_changelist_links_to_organization_tools_module(self) -> None: assert b"Organization tools" in response.content assert f"{reverse('admin:index')}#organization-tools".encode() in response.content - def _post(self, data: dict[str, str]) -> HttpRequest: - request = self.factory.post("/admin/posthog/organization/bulk-deactivate/", data) - request.user = self.user - _attach_messages(request) - return request - - def _preview_token(self, organization_ids: str, reason: str, custom_reason: str = "") -> str: - form = BulkDeactivateOrganizationsForm( - { - "organization_ids": organization_ids, - "reason": reason, - "custom_reason": custom_reason, - } + def _preview(self, organization_ids: str, reason: str = DESKTOP_ABUSE, custom_reason: str = ""): + return self.client.post( + TOOL_URL, {"organization_ids": organization_ids, "reason": reason, "custom_reason": custom_reason} ) - assert form.is_valid() - return form.preview_token() + + def _confirm_token(self, preview_response) -> str: + match = re.search(r'name="confirm_token" value="([^"]+)"', preview_response.content.decode()) + assert match is not None, "The preview page has no confirm button" + return match.group(1) + + def _deactivate(self, organization_ids: str, reason: str = DESKTOP_ABUSE, custom_reason: str = ""): + token = self._confirm_token(self._preview(organization_ids, reason, custom_reason)) + return self.client.post(TOOL_URL, {"confirm_token": token}) def test_preview_shows_matched_organizations_and_missing_ids_without_deactivating(self) -> None: missing_id = uuid4() - request = self._post( - { - "organization_ids": f"{self.organization.id}\n{missing_id}", - "reason": Organization.DeactivationReason.DESKTOP_ABUSE.value, - "custom_reason": "", - "preview": "1", - } - ) - response = self.admin.bulk_deactivate_view(request) + response = self._preview(f"{self.organization.id}\n{missing_id}") assert response.status_code == 200 self.organization.refresh_from_db() assert self.organization.is_active - assert str(self.organization.id).encode() in response.content - assert str(missing_id).encode() in response.content - - def test_confirm_deactivates_matched_organizations_with_selected_reason(self) -> None: - other_organization = Organization.objects.create(name="Bulk deactivate target") - missing_id = uuid4() - organization_ids = f"{self.organization.id}\n{other_organization.id}\n{missing_id}" - reason = Organization.DeactivationReason.DESKTOP_ABUSE.value - request = self._post( - { - "organization_ids": organization_ids, - "reason": reason, - "custom_reason": "", - "preview_token": self._preview_token(organization_ids, reason), - "confirm": "1", - } - ) - - response = self.admin.bulk_deactivate_view(request) - - assert response.status_code == 302 - for organization in (self.organization, other_organization): - organization.refresh_from_db() - assert organization.is_active is False - assert organization.is_not_active_reason == Organization.DeactivationReason.DESKTOP_ABUSE.value - - def test_confirm_accepts_custom_reason(self) -> None: - organization_ids = str(self.organization.id) - custom_reason = "Manual review required." - request = self._post( - { - "organization_ids": organization_ids, - "reason": BulkDeactivateOrganizationsForm.CUSTOM_REASON, - "custom_reason": custom_reason, - "preview_token": self._preview_token( - organization_ids, BulkDeactivateOrganizationsForm.CUSTOM_REASON, custom_reason - ), - "confirm": "1", - } - ) - - response = self.admin.bulk_deactivate_view(request) + assert str(self.organization.id) in response.content.decode() + assert str(missing_id) in response.content.decode() + + @parameterized.expand( + [ + ("preset_reason", DESKTOP_ABUSE, "", DESKTOP_ABUSE), + ( + "custom_reason", + BulkDeactivateOrganizationsForm.CUSTOM_REASON, + "Manual review required.", + "Manual review required.", + ), + ] + ) + def test_confirm_deactivates_active_organizations_and_keeps_inactive_ones( + self, _name: str, reason: str, custom_reason: str, expected_reason: str + ) -> None: + inactive = Organization.objects.create( + name="Already inactive", is_active=False, is_not_active_reason=COMPLIANCE_REVIEW + ) + + response = self._deactivate(f"{self.organization.id}\n{inactive.id}\n{uuid4()}", reason, custom_reason) assert response.status_code == 302 self.organization.refresh_from_db() - assert self.organization.is_active is False - assert self.organization.is_not_active_reason == "Manual review required." - - def test_confirm_skips_already_inactive_organizations(self) -> None: - inactive_organization = Organization.objects.create( - name="Already inactive target", - is_active=False, - is_not_active_reason=Organization.DeactivationReason.COMPLIANCE_REVIEW.value, - ) - organization_ids = f"{self.organization.id}\n{inactive_organization.id}" - reason = Organization.DeactivationReason.DESKTOP_ABUSE.value - request = self._post( - { - "organization_ids": organization_ids, - "reason": reason, - "custom_reason": "", - "preview_token": self._preview_token(organization_ids, reason), - "confirm": "1", - } - ) - - response = self.admin.bulk_deactivate_view(request) + inactive.refresh_from_db() + assert (self.organization.is_active, self.organization.is_not_active_reason) == (False, expected_reason) + assert (inactive.is_active, inactive.is_not_active_reason) == (False, COMPLIANCE_REVIEW) - assert response.status_code == 302 - self.organization.refresh_from_db() - inactive_organization.refresh_from_db() - assert self.organization.is_active is False - assert self.organization.is_not_active_reason == Organization.DeactivationReason.DESKTOP_ABUSE.value - assert inactive_organization.is_active is False - assert inactive_organization.is_not_active_reason == Organization.DeactivationReason.COMPLIANCE_REVIEW.value - - def test_confirm_rechecks_active_state_before_saving_reason(self) -> None: - stale_organization = Organization.objects.get(id=self.organization.id) - self.organization.is_active = False - self.organization.is_not_active_reason = Organization.DeactivationReason.COMPLIANCE_REVIEW.value - self.organization.save(update_fields=["is_active", "is_not_active_reason"]) - organization_ids = str(self.organization.id) - reason = Organization.DeactivationReason.DESKTOP_ABUSE.value - request = self._post( - { - "organization_ids": organization_ids, - "reason": reason, - "custom_reason": "", - "preview_token": self._preview_token(organization_ids, reason), - "confirm": "1", - } + def test_confirm_keeps_the_reason_of_an_organization_deactivated_since_the_preview(self) -> None: + token = self._confirm_token(self._preview(str(self.organization.id))) + Organization.objects.filter(id=self.organization.id).update( + is_active=False, is_not_active_reason=COMPLIANCE_REVIEW ) - with patch.object(self.admin, "_bulk_deactivation_candidates", return_value=([stale_organization], [])): - response = self.admin.bulk_deactivate_view(request) + self.client.post(TOOL_URL, {"confirm_token": token}) - assert response.status_code == 302 self.organization.refresh_from_db() - assert self.organization.is_active is False - assert self.organization.is_not_active_reason == Organization.DeactivationReason.COMPLIANCE_REVIEW.value + assert self.organization.is_not_active_reason == COMPLIANCE_REVIEW def test_preview_requires_at_least_one_active_organization(self) -> None: - inactive_organization = Organization.objects.create( - name="Only inactive target", - is_active=False, - is_not_active_reason=Organization.DeactivationReason.COMPLIANCE_REVIEW.value, - ) - request = self._post( - { - "organization_ids": str(inactive_organization.id), - "reason": Organization.DeactivationReason.DESKTOP_ABUSE.value, - "custom_reason": "", - "preview": "1", - } - ) + inactive = Organization.objects.create(name="Only inactive", is_active=False) - response = self.admin.bulk_deactivate_view(request) + response = self._preview(str(inactive.id)) - assert response.status_code == 200 - assert b"All matched organizations are already inactive." in response.content + assert response.status_code == 400 + assert "All matched organizations are already inactive." in response.content.decode() - def test_confirm_requires_matching_preview_token(self) -> None: - other_organization = Organization.objects.create(name="Unpreviewed target") - reason = Organization.DeactivationReason.DESKTOP_ABUSE.value - request = self._post( - { - "organization_ids": str(other_organization.id), - "reason": reason, - "custom_reason": "", - "preview_token": self._preview_token(str(self.organization.id), reason), - "confirm": "1", - } - ) + @parameterized.expand([("tampered_token", "tampered"), ("another_staff_member", "other_user")]) + def test_confirm_refuses_a_token_it_did_not_issue_for_this_user(self, _name: str, case: str) -> None: + token = self._confirm_token(self._preview(str(self.organization.id))) + client = self.client + if case == "tampered": + token = token[:-2] + ("AA" if not token.endswith("AA") else "BB") + else: + other = User.objects.create_and_join(self.organization, "other-staff@posthog.com", None, is_staff=True) + client = Client() + client.force_login(other) - response = self.admin.bulk_deactivate_view(request) + response = client.post(TOOL_URL, {"confirm_token": token}) - assert response.status_code == 200 - assert b"Review the organizations again before deactivating." in response.content + assert response.status_code == 302 + assert response["Location"] == TOOL_URL self.organization.refresh_from_db() - other_organization.refresh_from_db() assert self.organization.is_active - assert other_organization.is_active - - def test_view_requires_change_permission(self) -> None: - self.user.is_staff = False - self.user.save() - request = self._post( - { - "organization_ids": str(self.organization.id), - "reason": Organization.DeactivationReason.DESKTOP_ABUSE.value, - "custom_reason": "", - "preview": "1", - } - ) - - with self.assertRaises(PermissionDenied): - self.admin.bulk_deactivate_view(request) diff --git a/posthog/templates/admin/health_checks/_run_history.html b/posthog/templates/admin/health_checks/_run_history.html deleted file mode 100644 index 37d9bf3869fa..000000000000 --- a/posthog/templates/admin/health_checks/_run_history.html +++ /dev/null @@ -1,47 +0,0 @@ -{% if workflows %} - - - - - - - - - - - - {% for wf in workflows %} - - - - - - - - {% endfor %} - -
Workflow IDSourceStatusStartedCompleted
- {{ wf.id }} - {{ wf.source }} - {% if wf.status == "RUNNING" %} - Running - {% elif wf.status == "COMPLETED" %} - Completed - {% elif wf.status == "FAILED" %} - Failed - {% elif wf.status == "CANCELED" %} - Canceled - {% elif wf.status == "CONTINUED_AS_NEW" %} - Continued - {% elif wf.status == "TERMINATED" %} - Terminated - {% elif wf.status == "TIMED_OUT" %} - Timed out - {% else %} - {{ wf.status }} - {% endif %} - {{ wf.start_time|date:"Y-m-d H:i:s" }}{% if wf.close_time %}{{ wf.close_time|date:"Y-m-d H:i:s" }}{% else %}—{% endif %}
-{% else %} -

No health check workflows have been run yet.

-{% endif %} diff --git a/posthog/templates/admin/health_checks/list.html b/posthog/templates/admin/health_checks/list.html deleted file mode 100644 index b5d20f46844e..000000000000 --- a/posthog/templates/admin/health_checks/list.html +++ /dev/null @@ -1,91 +0,0 @@ -{% extends "admin/base_site.html" %} -{% load i18n %} -{% block extrastyle %} - {{ block.super }} - -{% endblock %} -{% block content %} -

← Back to admin

- {% if checks %} - - - - - - - - - - - - - - - - - {% for check in checks %} - - - - - - - - - - - - - {% endfor %} - -
NameKindOwnerScheduleBatch sizeMax concurrentRollout %Active sinceDry runActions
{{ check.name }}{{ check.kind }}{{ check.owner }}{% if check.schedule %}{{ check.schedule }}{% else %}Manual only{% endif %}{{ check.batch_size }}{{ check.max_concurrent }}{{ check.rollout_percentage|floatformat:2 }}{% if check.active_since_days %}{{ check.active_since_days }}d{% else %}All teams{% endif %}{% if check.dry_run %}Yes{% else %}No{% endif %}Trigger
- {% else %} -

No health checks registered. Ensure product health check modules are listed in the registry.

- {% endif %} -{% endblock %} diff --git a/posthog/templates/admin/health_checks/trigger.html b/posthog/templates/admin/health_checks/trigger.html deleted file mode 100644 index 10db04c5101e..000000000000 --- a/posthog/templates/admin/health_checks/trigger.html +++ /dev/null @@ -1,311 +0,0 @@ -{% extends "admin/base_site.html" %} -{% load i18n %} -{% block extrastyle %} - {{ block.super }} - -{% endblock %} -{% block content %} -

← Back to health checks

- {% if messages %} - {% for message in messages %} -
{{ message }}
- {% endfor %} - {% endif %} -
-
-
Kind
-
- {{ config.kind }} -
-
Name
-
- {{ config.name }} -
-
Owner
-
- {{ config.owner }} -
-
Schedule
-
{% if config.schedule %}{{ config.schedule }}{% else %}Manual only{% endif %}
-
-
-
-

Trigger workflow

-
- {% csrf_token %} -
- - {% if form.dry_run.help_text %}

{{ form.dry_run.help_text }}

{% endif %} - {{ form.dry_run.errors }} -
-
- - {{ form.batch_size }} - {% if form.batch_size.help_text %}

{{ form.batch_size.help_text }}

{% endif %} - {{ form.batch_size.errors }} -
-
- - {{ form.max_concurrent }} - {% if form.max_concurrent.help_text %}

{{ form.max_concurrent.help_text }}

{% endif %} - {{ form.max_concurrent.errors }} -
-
- - {{ form.rollout_percentage }} - {% if form.rollout_percentage.help_text %}

{{ form.rollout_percentage.help_text }}

{% endif %} - {{ form.rollout_percentage.errors }} -
-
- - {{ form.active_since_days }} - {% if form.active_since_days.help_text %}

{{ form.active_since_days.help_text }}

{% endif %} - {{ form.active_since_days.errors }} -
-
- - {{ form.team_ids }} - {% if form.team_ids.help_text %}

{{ form.team_ids.help_text }}

{% endif %} - {{ form.team_ids.errors }} -
-
- -
-
-
-
-
-

Recent runs

- -
-
{% include "admin/health_checks/_run_history.html" %}
-
- -{% endblock %} diff --git a/posthog/templates/admin/posthog/organization/bulk_deactivate.html b/posthog/templates/admin/posthog/organization/bulk_deactivate.html deleted file mode 100644 index ed497c0cc2b0..000000000000 --- a/posthog/templates/admin/posthog/organization/bulk_deactivate.html +++ /dev/null @@ -1,114 +0,0 @@ -{% extends "admin/base_site.html" %} -{% load i18n admin_urls %} -{% block extrahead %} - {{ block.super }} - -{% endblock %} -{% block content %} -
-

{{ title }}

-

Paste organization IDs, choose the reason users will see, then review the matched organizations before deactivation.

-
-
- {% csrf_token %} - {{ form.non_field_errors }} -
- {% for field in form %} -
- {{ field.errors }} - {{ field.label_tag }} {{ field }} - {% if field.help_text %} -
{{ field.help_text }}
- {% endif %} -
- {% endfor %} -
- {% if preview_organizations %} - -

Review organizations

-

This will deactivate {{ deactivation_organizations|length }} active organization{{ deactivation_organizations|length|pluralize }} with this reason:

-
- {{ resolved_reason }} -
- {% if missing_ids %} -

These IDs were not found and will be skipped:

-
    - {% for organization_id in missing_ids %} -
  • {{ organization_id }}
  • - {% endfor %} -
- {% endif %} - {% if already_inactive_organizations %} -

These organizations are already inactive and will be skipped:

-
    - {% for organization in already_inactive_organizations %} -
  • {{ organization.id }} {{ organization.name }}
  • - {% endfor %} -
- {% endif %} - - - - - - - - - - - {% for organization in preview_organizations %} - - - - - - - {% endfor %} - -
Organization IDNameCurrent statusCurrent reason
{{ organization.id }} - {{ organization.name }} - {% if organization.is_active %}Active{% else %}Not active{% endif %}{{ organization.is_not_active_reason|default:"-" }}
-
- {% if deactivation_organizations %} - - {% endif %} - - Back to organization list -
- {% else %} - - {% endif %} -
-
-{% endblock %} diff --git a/posthog/templates/admin_console/_block.html b/posthog/templates/admin_console/_block.html new file mode 100644 index 000000000000..6242fa3ceca3 --- /dev/null +++ b/posthog/templates/admin_console/_block.html @@ -0,0 +1,16 @@ +{# One console block. `block` is a Notice, Table, Output or Paging from posthog/admin/console/tools.py. #} +{% if block.kind == "notice" %} +

+ {{ block.text }} + {% if block.href %} + {{ block.link_text }} + {% endif %} +

+{% elif block.kind == "table" %} + {% include "admin_console/_table.html" with table=block %} +{% elif block.kind == "output" %} +
{{ block.text }}
+{% elif block.kind == "paging" %} + {% include "admin_console/_paging.html" with paging=block %} +{% endif %} diff --git a/posthog/templates/admin_console/_blocks.html b/posthog/templates/admin_console/_blocks.html index 11e1329e01b3..655c3055c610 100644 --- a/posthog/templates/admin_console/_blocks.html +++ b/posthog/templates/admin_console/_blocks.html @@ -1,14 +1,11 @@ {# The blocks of a console Page. `page` is a Page from posthog/admin/console/tools.py. #}
{% for block in page.blocks %} - {% if block.kind == "notice" %} -

{{ block.text }}

- {% elif block.kind == "table" %} - {% include "admin_console/_table.html" with table=block %} - {% elif block.kind == "output" %} -
{{ block.text }}
- {% elif block.kind == "paging" %} - {% include "admin_console/_paging.html" with paging=block %} + {% if block.kind == "live" %} + {% url 'admin_console_tool_live' tool_key=tool.key live_key=block.key as live_path %} + {% include "admin_console/_live.html" with live=block live_url=live_path|add:query_suffix %} + {% else %} + {% include "admin_console/_block.html" %} {% endif %} {% endfor %}
diff --git a/posthog/templates/admin_console/_live.html b/posthog/templates/admin_console/_live.html new file mode 100644 index 000000000000..36cc157d07b1 --- /dev/null +++ b/posthog/templates/admin_console/_live.html @@ -0,0 +1,4 @@ +{# A Live block. console.js fetches `live_url` again while data-refresh-seconds is set and swaps this element. #} +
{% include "admin_console/_block.html" with block=live.block %}
diff --git a/posthog/templates/admin_console/_table.html b/posthog/templates/admin_console/_table.html index 12237922506c..b8acc4369c59 100644 --- a/posthog/templates/admin_console/_table.html +++ b/posthog/templates/admin_console/_table.html @@ -21,7 +21,8 @@ {% if cell.tone %} {% include "admin_console/_badge.html" with label=cell.text tone=cell.tone %} {% elif cell.href %} - {{ cell.text }} + {{ cell.text }} {% else %} {{ cell.text }} {% endif %} diff --git a/posthog/templates/admin_console/confirm.html b/posthog/templates/admin_console/confirm.html new file mode 100644 index 000000000000..b464f45f66ab --- /dev/null +++ b/posthog/templates/admin_console/confirm.html @@ -0,0 +1,40 @@ +{% extends "admin_console/base.html" %} +{% block title %}Confirm {{ operation.label }} · {{ target.title }} · PostHog Admin{% endblock %} +{% block content %} + +

Confirm: {{ operation.label }}

+

Nothing changed yet. Check what this changes, then confirm.

+ {% include "admin_console/_blocks.html" %} +
+
+ {% csrf_token %} + + + {% if operation.undo_hint %} +
+

{{ operation.undo_hint }}

+
+ {% endif %} + +
+
+{% endblock %} diff --git a/posthog/templates/admin_console/operation.html b/posthog/templates/admin_console/operation.html index 02d47d18413a..6d84a2a644d1 100644 --- a/posthog/templates/admin_console/operation.html +++ b/posthog/templates/admin_console/operation.html @@ -44,7 +44,11 @@

{{ operation.label }}

data-slot="card-footer"> Cancel diff --git a/posthog/templates/organization_tools/app.html b/posthog/templates/organization_tools/app.html index d2cad55046ab..49ab2572451b 100644 --- a/posthog/templates/organization_tools/app.html +++ b/posthog/templates/organization_tools/app.html @@ -21,7 +21,7 @@ {% if perms.posthog.change_organization %} - Bulk deactivate organizations + Bulk deactivate organizations Deactivate organizations from pasted IDs after a preview step