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.

fix(data-modeling): open node detail from failure emails and notifications (#122265)

Jovan Sakovic73aa5b10f148
diff --git a/posthog/tasks/email.py b/posthog/tasks/email.py
index cf25670f49e7..3b37f96dc0c1 100644
--- a/posthog/tasks/email.py
+++ b/posthog/tasks/email.py
@@ -63,6 +63,7 @@ from products.cdp.backend.models.plugin import Plugin, PluginConfig
from products.conversations.backend.models import Ticket
from products.data_modeling.backend.facade.api import (
declared_targets_by_saved_query,
+ get_node_ids_for_saved_queries,
suspended_saved_query_ids_by_team,
suspension_state_for_saved_query,
)
@@ -1481,6 +1482,7 @@ def send_team_matview_failure_digest(team_id: int, failed_query_ids: list[str],
}
new_failure_cutoff = timezone.now() - datetime.timedelta(hours=24)
declared_targets = declared_targets_by_saved_query(team_id, all_ids)
+ node_ids = get_node_ids_for_saved_queries(team_id, all_ids)
latest_jobs: dict[str, DataModelingJob] = {}
for latest_job in (
@@ -1535,7 +1537,7 @@ def send_team_matview_failure_digest(team_id: int, failed_query_ids: list[str],
"section": section,
"suspended": suspended,
"schedule": schedule,
- "url": f"{settings.SITE_URL}/project/{team_id}/sql?open_view={sq.id}",
+ "url": settings.SITE_URL + matview_view_path(team_id, qid, node_ids.get(qid)),
}
)
@@ -1600,6 +1602,16 @@ def send_team_matview_failure_digest(team_id: int, failed_query_ids: list[str],
)
+def matview_view_path(team_id: int, saved_query_id: str, node_id: str | None) -> str:
+ """Where a failure email or notification sends the reader: the view's node detail, which shows its runs and error.
+
+ A view with no DAG node has no node detail page, so it falls back to the SQL editor.
+ """
+ if node_id:
+ return f"/project/{team_id}/models/{node_id}"
+ return f"/project/{team_id}/sql?open_view={saved_query_id}"
+
+
@shared_task(**EMAIL_TASK_KWARGS)
@skip_team_scope_audit
def send_matview_failure_immediate_email(
@@ -1638,6 +1650,7 @@ def send_matview_failure_immediate_email(
job = DataModelingJob.objects.filter(id=job_id, team_id=team_id).first()
error = (job.error if job else None) or saved_query.latest_error or ""
+ node_id = get_node_ids_for_saved_queries(team_id, [saved_query.id]).get(str(saved_query.id))
if len(error) > MAX_ERROR_CHARS:
error = error[: MAX_ERROR_CHARS - 3] + "..."
@@ -1655,6 +1668,7 @@ def send_matview_failure_immediate_email(
"team": team,
"saved_query_name": saved_query.name,
"saved_query_id": str(saved_query.id),
+ "view_url": settings.SITE_URL + matview_view_path(team_id, str(saved_query.id), node_id),
"error": error,
"suspended": suspended,
},
diff --git a/posthog/tasks/test/test_email.py b/posthog/tasks/test/test_email.py
index bdb877ec111a..c946bc1c51bc 100644
--- a/posthog/tasks/test/test_email.py
+++ b/posthog/tasks/test/test_email.py
@@ -2767,6 +2767,10 @@ class TestEmail(APIBaseTest, ClickhouseTestMixin):
ongoing_row = next(v for v in message.properties["views"] if v["name"] == "nightly_revenue_rollup")
assert ongoing_row["failed_runs"] == 3
assert ongoing_row["failing_since"] == streak_started_at.strftime("%b %d, %H:%M UTC")
+ # A view with a DAG node opens its node detail; one without falls back to the SQL editor.
+ urls = {v["name"]: v["url"] for v in message.properties["views"]}
+ assert urls[tiered.name].endswith(f"/project/{self.team.id}/models/{tiered_node.id}")
+ assert urls[paused.name].endswith(f"/project/{self.team.id}/sql?open_view={paused.id}")
html = message.html_body
assert html.count("Not scheduled.") == len(unscheduled_cases)
diff --git a/posthog/templates/email/saved_query_materialization_failure.html b/posthog/templates/email/saved_query_materialization_failure.html
index d40969059135..db4d5d3ebd08 100644
--- a/posthog/templates/email/saved_query_materialization_failure.html
+++ b/posthog/templates/email/saved_query_materialization_failure.html
@@ -41,14 +41,13 @@
word-break: break-word">{{ error }}</div>
{% endif %}
<div class="mb mt text-center">
- <a href="{% absolute_uri '/project' %}/{{ team.id }}/sql?open_view={{ saved_query_id }}"
- target="_blank"><b>Open the view</b></a>
+ <a href="{{ view_url }}" target="_blank"><b>Open the view</b></a>
</div>
<div class="mt">
<p>
If the button above doesn't work, paste this link into your browser:
<br />
- <a href="{% absolute_uri '/project' %}/{{ team.id }}/sql?open_view={{ saved_query_id }}">{% absolute_uri '/project' %}/{{ team.id }}/sql?open_view={{ saved_query_id }}</a>
+ <a href="{{ view_url }}">{{ view_url }}</a>
</p>
</div>
<p>If you can't diagnose the issue, please contact us for help.</p>
diff --git a/posthog/temporal/data_modeling/activities/notify_materialization_failure.py b/posthog/temporal/data_modeling/activities/notify_materialization_failure.py
index 1411a7fce71d..ded0257a984f 100644
--- a/posthog/temporal/data_modeling/activities/notify_materialization_failure.py
+++ b/posthog/temporal/data_modeling/activities/notify_materialization_failure.py
@@ -22,9 +22,10 @@ from temporalio import activity
from posthog.exceptions_capture import capture_exception
from posthog.models import Team, User
from posthog.sync import database_sync_to_async_pool
-from posthog.tasks.email import send_matview_failure_immediate_email
+from posthog.tasks.email import matview_view_path, send_matview_failure_immediate_email
from products.access_control.backend.facade.user_access_control import UserAccessControl
+from products.data_modeling.backend.facade.api import get_node_ids_for_saved_queries
from products.data_modeling.backend.facade.models import (
DataModelingJob,
DataModelingJobEngine,
@@ -191,7 +192,7 @@ def notify_node_suspended(
target_id=str(team_id),
resource_type="warehouse_objects",
resource_id=str(saved_query.id),
- source_url=f"/project/{team_id}/sql?open_view={saved_query.id}",
+ source_url=_view_path(team_id, saved_query),
source_id=str(job.id),
# A retry of the suspending run sends again, so this dedupes it. Sharing the failure
# notification's key also means one run never produces both.
@@ -201,6 +202,11 @@ def notify_node_suspended(
)
+def _view_path(team_id: int, saved_query: DataWarehouseSavedQuery) -> str:
+ node_ids = get_node_ids_for_saved_queries(team_id, [saved_query.id])
+ return matview_view_path(team_id, str(saved_query.id), node_ids.get(str(saved_query.id)))
+
+
def _failure_copy(views: list[_FailedView]) -> tuple[str, str]:
names = [view.saved_query.name for view in views]
if len(names) == 1:
@@ -236,9 +242,7 @@ def _failure_notification(
priority: Priority = Priority.NORMAL,
) -> NotificationData:
title, body = _failure_copy(views)
- source_url = f"/project/{team_id}/sql"
- if len(views) == 1:
- source_url = f"{source_url}?open_view={views[0].saved_query.id}"
+ source_url = _view_path(team_id, views[0].saved_query) if len(views) == 1 else f"/project/{team_id}/models"
return NotificationData(
team_id=team_id,
notification_type=NotificationType.MATERIALIZATION_FAILURE,