Skip to content

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

feat(weekly-digest): route digests through a workflows webhook behind a flag (#124980)

Harley Alexanderd5335606e1e6
diff --git a/posthog/settings/temporal.py b/posthog/settings/temporal.py
index 32d85ba7250a..d78d8a727f7a 100644
--- a/posthog/settings/temporal.py
+++ b/posthog/settings/temporal.py
@@ -482,6 +482,10 @@ REPLAY_VISION_TASK_QUEUE = _set_temporal_task_queue("replay-vision-task-queue")
# no dedicated pod needed (see surfacing_scoring_sweep/README.md).
SURFACING_SCORING_SWEEP_TASK_QUEUE = SESSION_REPLAY_TASK_QUEUE
WEEKLY_DIGEST_TASK_QUEUE = _set_temporal_task_queue("weekly-digest-task-queue")
+# The digest workflow's webhook in the US project, and the Authorization value it requires. Both regions post
+# there. Without both, every digest goes through Customer.io.
+WEEKLY_DIGEST_WORKFLOW_WEBHOOK_URL: str = get_from_env("WEEKLY_DIGEST_WORKFLOW_WEBHOOK_URL", "")
+WEEKLY_DIGEST_WORKFLOW_WEBHOOK_SECRET: str = get_from_env("WEEKLY_DIGEST_WORKFLOW_WEBHOOK_SECRET", "")
LLMA_EVALS_TASK_QUEUE = _set_temporal_task_queue("llm-analytics-evals-task-queue")
LLMA_TASK_QUEUE = _set_temporal_task_queue("llm-analytics-task-queue")
# Units one evaluation backfill dispatches per tick, one tick per BACKFILL_TICK_INTERVAL.
diff --git a/posthog/temporal/tests/weekly_digest/test_activities.py b/posthog/temporal/tests/weekly_digest/test_activities.py
index 24a31ce34ea2..15c294e80745 100644
--- a/posthog/temporal/tests/weekly_digest/test_activities.py
+++ b/posthog/temporal/tests/weekly_digest/test_activities.py
@@ -11,6 +11,7 @@ from posthog.test.base import _create_event, flush_persons_and_events
from unittest.mock import MagicMock, patch
from django.db import connection
+from django.test import override_settings
from django.test.utils import CaptureQueriesContext
from django.utils import timezone
@@ -40,6 +41,7 @@ from posthog.temporal.weekly_digest.activities import (
_redis_url,
_teams_in_range,
_usage_trend_metric,
+ _wait_for_workflows_post,
generate_dashboard_lookup,
generate_error_issue_lookup,
generate_event_definition_lookup,
@@ -68,6 +70,7 @@ from posthog.temporal.weekly_digest.types import (
TeamIdRange,
UsageTrends,
)
+from posthog.token_bucket import BucketDecision, BucketUnavailable
from products.dashboards.backend.models.dashboard import Dashboard
from products.error_tracking.backend.facade.testing import create_issue
@@ -631,7 +634,7 @@ def _org_digest_json(organization: Organization, team: Team) -> str:
"id": team.id,
"name": team.name,
"dashboards": [{"name": "Test Dashboard", "id": 1}],
- "event_definitions": [],
+ "event_definitions": [{"name": f"event {index}", "id": str(uuid4())} for index in range(3)],
"experiments_launched": [
{"name": "Experiment A", "id": 1, "start_date": "2024-01-01T00:00:00Z"},
{"name": "Experiment B", "id": 2, "start_date": "2024-01-01T00:00:00Z"},
@@ -661,19 +664,41 @@ def _send_input(
)
+WEBHOOK_SETTINGS = {
+ "WEEKLY_DIGEST_WORKFLOW_WEBHOOK_URL": "https://webhooks.example.com/public/webhooks/digest",
+ "WEEKLY_DIGEST_WORKFLOW_WEBHOOK_SECRET": "test-webhook-secret",
+}
+
+
@pytest.mark.django_db
@pytest.mark.parametrize(
- "dry_run,already_sent,expected_captures",
+ "dry_run,already_sent,flag_enabled,webhook_configured,has_distinct_id,expected_route",
[
- (False, False, 1),
- (True, False, 0),
- (False, True, 0),
+ (False, False, False, True, True, "customer_io"),
+ (False, False, True, True, True, "workflows"),
+ (False, False, True, True, False, "workflows"),
+ (False, False, True, False, True, "customer_io"),
+ (True, False, True, True, True, None),
+ (False, True, True, True, True, None),
],
)
def test_send_weekly_digest_batch_emails_subscribed_members_once(
- dry_run, already_sent, expected_captures, organization, team, redis_servers, common_input, digest
+ dry_run,
+ already_sent,
+ flag_enabled,
+ webhook_configured,
+ has_distinct_id,
+ expected_route,
+ organization,
+ team,
+ redis_servers,
+ common_input,
+ digest,
):
subscribed = create_user(organization, "[email protected]")
+ if not has_distinct_id:
+ User.objects.filter(id=subscribed.id).update(distinct_id=None)
+ subscribed.refresh_from_db()
create_user(organization, "[email protected]")
redis_servers.digest.set(org_digest_key(digest.key, organization.id), _org_digest_json(organization, team))
redis_servers.digest.sadd(user_data_key(digest.key, UserDataKey.NOTIFY_TEAMS, subscribed.id), team.id)
@@ -686,33 +711,80 @@ def test_send_weekly_digest_batch_emails_subscribed_members_once(
record.sent_at = timezone.now()
record.save()
ph_client = MagicMock()
+ session = MagicMock()
+ session.post.return_value.status_code = 201
- with patch("posthog.temporal.weekly_digest.activities.get_ph_client", return_value=ph_client):
+ with (
+ override_settings(**(WEBHOOK_SETTINGS if webhook_configured else {})),
+ patch("posthog.temporal.weekly_digest.activities.get_ph_client", return_value=ph_client),
+ patch("posthog.temporal.weekly_digest.activities.posthoganalytics.feature_enabled", return_value=flag_enabled),
+ patch("posthog.temporal.weekly_digest.activities.requests.Session", return_value=session),
+ patch("posthog.temporal.weekly_digest.activities.WORKFLOWS_EVENT_DEFINITIONS_LIMIT", 2),
+ ):
run_sync(send_weekly_digest_batch, _send_input(organization, digest, common_input, dry_run=dry_run))
- assert ph_client.capture.call_count == expected_captures
+ # Customer.io's destination listens to the captured event and the digest workflow to the webhook, so nobody gets both.
+ assert ph_client.capture.call_count == (expected_route == "customer_io")
+ assert session.post.call_count == (expected_route == "workflows")
assert ph_client.shutdown.called
record = MessagingRecord.objects.get(campaign_key=digest.key)
# A dry run must not stamp the record, or the real run that follows would skip the organization.
assert (record.sent_at is not None) is (not dry_run)
- if expected_captures == 0:
+ if expected_route is None:
return
- method_names = [name for name, _, _ in ph_client.method_calls]
- assert method_names.index("capture") < method_names.index("flush")
-
- capture = ph_client.capture.call_args.kwargs
- assert capture["distinct_id"] == subscribed.distinct_id
- assert capture["event"] == "transactional email"
- assert capture["groups"]["organization"] == str(organization.id)
- (team_payload,) = capture["properties"]["teams"]
+ if expected_route == "customer_io":
+ method_names = [name for name, _, _ in ph_client.method_calls]
+ assert method_names.index("capture") < method_names.index("flush")
+ capture = ph_client.capture.call_args.kwargs
+ assert capture["distinct_id"] == subscribed.distinct_id
+ assert capture["event"] == "transactional email"
+ assert capture["groups"]["organization"] == str(organization.id)
+ properties = capture["properties"]
+ assert "to" not in properties
+ else:
+ post = session.post.call_args
+ assert post.args == (WEBHOOK_SETTINGS["WEEKLY_DIGEST_WORKFLOW_WEBHOOK_URL"],)
+ assert post.kwargs["headers"] == {
+ "Authorization": "Bearer test-webhook-secret",
+ "Idempotency-Key": f"{digest.key}:{subscribed.id}",
+ }
+ assert post.kwargs["json"]["event"] == "weekly digest ready"
+ assert post.kwargs["json"]["distinct_id"] == (subscribed.distinct_id or subscribed.email)
+ properties = post.kwargs["json"]["properties"]
+ assert properties["to"] == subscribed.email
+ (team_payload,) = properties["teams"]
assert team_payload["team_id"] == team.id
+ # The webhook refuses large bodies, so Workflows gets the first events and the exact count.
+ assert len(team_payload["report"]["new_event_definitions"]) == (2 if expected_route == "workflows" else 3)
+ assert team_payload["report"]["new_event_definitions_count"] == 3
assert team_payload["report"]["new_product_suggestion"] == {
"product_path": "Error tracking",
"reason_text": DEFAULT_PRODUCT_SUGGESTION_TEXT,
}
+ "first_answer",
+ [
+ BucketUnavailable(error="connection refused"),
+ BucketDecision(allowed=False, remaining=0, limit=500, retry_after=1, reset=10),
+ ],
+)
+def test_wait_for_workflows_post_waits_until_the_bucket_grants_a_token(first_answer):
+ granted = BucketDecision(allowed=True, remaining=10, limit=500, retry_after=0, reset=1)
+
+ with (
+ patch("posthog.temporal.weekly_digest.activities.consume", side_effect=[first_answer, granted]) as consume,
+ patch("posthog.temporal.weekly_digest.activities.time.sleep") as sleep,
+ ):
+ _wait_for_workflows_post(MagicMock(), "digest-key")
+
+ # An unreadable bucket must not let unpaced posts through to the webhook.
+ assert consume.call_count == 2
+ assert sleep.call_count == 1
+
+
@activity.defn(name="send-weekly-digest-batch-body", no_thread_cancel_exception=True)
def send_weekly_digest_batch_body(input: SendWeeklyDigestBatchInput) -> None:
# Runs the sync body under a test activity context. Without the thread cancel exception, cancelling
@@ -751,6 +823,7 @@ def test_send_weekly_digest_batch_stops_sending_once_the_activity_is_cancelled(
("failed_upload", 1),
("queue_full", 1),
("oversized", 0),
+ ("failed_webhook", 0),
],
)
def test_send_weekly_digest_batch_leaves_organization_unsent_when_an_event_is_dropped(
@@ -774,12 +847,19 @@ def test_send_weekly_digest_batch_leaves_organization_unsent_when_an_event_is_dr
ph_client.capture.return_value = None
return ph_client
+ session = MagicMock()
+ session.post.return_value.status_code = 503
+
with (
+ override_settings(**WEBHOOK_SETTINGS),
patch("posthog.temporal.weekly_digest.activities.get_ph_client", side_effect=make_client),
patch(
"posthog.temporal.weekly_digest.activities.DIGEST_PAYLOAD_SIZE_LIMIT",
100 if drop == "oversized" else DIGEST_PAYLOAD_SIZE_LIMIT,
),
+ patch("posthog.temporal.weekly_digest.activities._routes_to_workflows", return_value=drop == "failed_webhook"),
+ patch("posthog.temporal.weekly_digest.activities.requests.Session", return_value=session),
+ patch("posthog.temporal.weekly_digest.activities.time.sleep"),
):
run_sync(send_weekly_digest_batch, _send_input(organization, digest, common_input, dry_run=False))
diff --git a/posthog/temporal/tests/weekly_digest/test_types.py b/posthog/temporal/tests/weekly_digest/test_types.py
index afcab03bdbd3..cf9219a56951 100644
--- a/posthog/temporal/tests/weekly_digest/test_types.py
+++ b/posthog/temporal/tests/weekly_digest/test_types.py
@@ -210,7 +210,9 @@ def test_team_digest_render_payload():
report = payload["report"]
assert isinstance(report, dict)
- counted, uncounted = report["new_error_issues"]
+ error_issues = report["new_error_issues"]
+ assert isinstance(error_issues, list)
+ counted, uncounted = error_issues
assert counted["description"].startswith("Request failed at fetch xxx")
assert len(counted["description"]) == 140
assert (counted["occurrence_count"], counted["occurrence_count_display"]) == (1_525, "1.52K")
@@ -227,10 +229,11 @@ def test_team_digest_render_payload():
assert "report" in payload
report = payload["report"]
assert isinstance(report, dict)
- assert len(report["new_dashboards"]) == 1
- assert len(report["new_event_definitions"]) == 1
- assert len(report["new_feature_flags"]) == 1
- assert len(report["interesting_saved_filters"]) == 1
+ for key in ("new_dashboards", "new_event_definitions", "new_feature_flags", "interesting_saved_filters"):
+ items = report[key]
+ assert isinstance(items, list)
+ assert len(items) == 1
+ assert report["new_event_definitions_count"] == 1
assert "expiring_recordings" in report
expiring_recordings = report["expiring_recordings"]
assert isinstance(expiring_recordings, dict)
diff --git a/posthog/temporal/weekly_digest/README.md b/posthog/temporal/weekly_digest/README.md
index 72bec480de98..b2a42fff2f78 100644
--- a/posthog/temporal/weekly_digest/README.md
+++ b/posthog/temporal/weekly_digest/README.md
@@ -21,6 +21,19 @@ Generation has a 15-hour run timeout and a 15-hour execution timeout shared acro
A healthy run can use the full generation budget without restarting and regenerating completed batches after six hours.
Sending starts only after generation succeeds and has separate six-hour run and 15-hour execution timeouts.
+### Delivery
+
+The send activity checks the `transactional-emails-via-workflows` flag for each recipient, with `transactional_email_template` set to `weekly_digest_report` and the recipient's `email` and `region`.
+When the flag is off, it captures `transactional email` in the US project, and the Customer.io destination forwards it to the Customer.io digest campaign.
+When the flag is on, it posts the digest to the webhook of the digest workflow in the US project, with the recipient address in `to`.
+Both regions post to that webhook. `WEEKLY_DIGEST_WORKFLOW_WEBHOOK_URL` and `WEEKLY_DIGEST_WORKFLOW_WEBHOOK_SECRET` configure it, and without both every digest goes through Customer.io.
+The secret goes in the `Authorization` header, so only the digest send can start the workflow.
+Each post carries an `Idempotency-Key` made from the digest key and the user id, so a retried post starts no second run.
+An organization stays unsent when a post fails after its retries, so the next attempt resends it.
+All send batches share one token bucket in the digest Redis that keeps posts to about 3,000 a minute, and the send waits when that Redis can't answer.
+The webhook refuses a body above 512,000 bytes, and a project can create thousands of events in a week. The Workflows payload carries the first six new event definitions and their total in `new_event_definitions_count`, which is what the email shows.
+The workflow paces the emails themselves with its own sending rate limit.
+
## Redis Storage Structure
Data is stored with keys prefixed by `{digest_key}` (e.g., `weekly-digest-2024-01`).
diff --git a/posthog/temporal/weekly_digest/activities.py b/posthog/temporal/weekly_digest/activities.py
index 7afd4b56e798..947c7f55f3eb 100644
--- a/posthog/temporal/weekly_digest/activities.py
+++ b/posthog/temporal/weekly_digest/activities.py
@@ -13,6 +13,8 @@ from django.db.models.functions import RowNumber
from django.utils import timezone
import redis
+import requests
+import posthoganalytics
from posthoganalytics import Posthog
from posthoganalytics.consumer import MAX_MSG_SIZE
from pydantic import ValidationError
@@ -29,6 +31,7 @@ from posthog.clickhouse.client import sync_execute
from posthog.clickhouse.client.connection import Workload
from posthog.clickhouse.query_tagging import Feature, Product, tag_queries
from posthog.constants import AvailableFeature
+from posthog.email import WORKFLOWS_TRANSACTIONAL_EMAIL_FLAG
from posthog.exceptions import ClickHouseAtCapacity
from posthog.models.messaging import MessagingRecord
from posthog.models.organization_notification_lock import GovernedSetting, notification_locks_for_users
@@ -41,6 +44,7 @@ from posthog.tasks.email import NotificationSetting, should_send_notification
from posthog.tasks.email_utils import compute_week_over_week_change
from posthog.temporal.common.heartbeat_sync import HeartbeaterSync
from posthog.temporal.common.logger import get_write_only_logger
+from posthog.temporal.common.metrics import get_metric_meter
from posthog.temporal.common.utils import asyncify, make_sync_retryable_with_exponential_backoff
from posthog.temporal.weekly_digest.keys import TeamDataKey, UserDataKey, org_digest_key, team_data_key, user_data_key
from posthog.temporal.weekly_digest.queries import (
@@ -84,6 +88,8 @@ from posthog.temporal.weekly_digest.types import (
UsageTrends,
UserDigestContext,
)
+from posthog.token_bucket import BucketDecision, Budget, consume
+from posthog.utils import get_instance_region
from products.error_tracking.backend.facade.api import get_new_issues_for_team
from products.growth.backend.product_push.selection import project_uses_product, resolve_product_path
@@ -984,6 +990,17 @@ DRAIN_EVENT_COUNT = 500
DIGEST_PAYLOAD_SIZE_LIMIT = MAX_MSG_SIZE - 16 * 1024
UPLOAD_CLEANUP_SECONDS = 120
DRAIN_SLICE_SECONDS = 5
+DIGEST_TEMPLATE_NAME = "weekly_digest_report"
+CUSTOMER_IO_DIGEST_EVENT = "transactional email"
+WORKFLOWS_DIGEST_EVENT = "weekly digest ready"
+# Every send batch runs at once, so they share one budget for the posts to the digest workflow's webhook.
+WORKFLOWS_CAPTURE_BUDGET = Budget(burst=500, per_hour=180_000)
+# The webhook endpoint refuses a body above 512,000 bytes.
+WORKFLOWS_WEBHOOK_BODY_LIMIT = 500_000
+WORKFLOWS_WEBHOOK_TIMEOUT_SECONDS = 10
+WORKFLOWS_WEBHOOK_ATTEMPTS = 3
+# The Workflows email lists six new events and reads the total from new_event_definitions_count.
+WORKFLOWS_EVENT_DEFINITIONS_LIMIT = 6
# These derive from BaseException, so that the per-organization `except Exception` handler cannot swallow them.
@@ -1024,6 +1041,88 @@ def _drain(ph_client: Posthog, deadline: float) -> bool:
return True
+class WorkflowsWebhookError(Exception):
+ pass
+
+
+def _workflows_distinct_id(user: User) -> str:
+ # The webhook refuses a post without a distinct id. The other transactional emails fall back to the email too.
+ return user.distinct_id or user.email
+
+
+def _routes_to_workflows(user: User) -> bool:
+ if not settings.WEEKLY_DIGEST_WORKFLOW_WEBHOOK_URL or not settings.WEEKLY_DIGEST_WORKFLOW_WEBHOOK_SECRET:
+ return False
+ # The same flag and person properties as the other transactional emails, so one flag condition ramps the digest.
+ person_properties = {"email": user.email, "transactional_email_template": DIGEST_TEMPLATE_NAME}
+ if region := get_instance_region():
+ person_properties["region"] = region
+ try:
+ return bool(
+ posthoganalytics.feature_enabled(
+ WORKFLOWS_TRANSACTIONAL_EMAIL_FLAG,
+ _workflows_distinct_id(user),
+ person_properties=person_properties,
+ send_feature_flag_events=False,
+ )
+ )
+ except Exception as err:
+ LOGGER.warning("Failed to check the digest routing flag, sending through Customer.io", error=str(err))
+ return False
+
+
+def _wait_for_workflows_post(r: redis.Redis, digest_key: str) -> None:
+ # The bucket is in the digest Redis, which the send already depends on. When it can't answer, the send
+ # waits instead of posting to the webhook without a pace.
+ while True:
+ _raise_if_cancelled()
+ decision = consume(f"weekly_digest_workflows_capture:{digest_key}", WORKFLOWS_CAPTURE_BUDGET, client=r)
+ if isinstance(decision, BucketDecision) and decision.allowed:
+ return
+ retry_after = decision.retry_after if isinstance(decision, BucketDecision) else DRAIN_SLICE_SECONDS
+ time.sleep(min(retry_after, DRAIN_SLICE_SECONDS))
+
+
+def _record_workflows_send_failure(reason: str) -> None:
+ if not activity.in_activity():
+ return
+ get_metric_meter({"reason": reason}).create_counter(
+ "weekly_digest_workflows_send_failed",
+ "Weekly digests that the send could not hand to the digest workflow, by reason.",
+ ).add(1)
+
+
+def _post_to_workflows(session: requests.Session, body: dict[str, Any], idempotency_key: str) -> None:
+ # The secret authenticates the post, so only the digest send can start the workflow. The idempotency key
+ # makes a retried post, in this attempt or the next one, start no second run.
+ headers = {
+ "Authorization": f"Bearer {settings.WEEKLY_DIGEST_WORKFLOW_WEBHOOK_SECRET}",
+ "Idempotency-Key": idempotency_key,
+ }
+ reason = "connection_error"
+ for attempt in range(WORKFLOWS_WEBHOOK_ATTEMPTS):
+ if attempt:
+ time.sleep(2**attempt)
+ _raise_if_cancelled()
+ try:
+ response = session.post(
+ settings.WEEKLY_DIGEST_WORKFLOW_WEBHOOK_URL,
+ json=body,
+ headers=headers,
+ timeout=WORKFLOWS_WEBHOOK_TIMEOUT_SECONDS,
+ )
+ except requests.RequestException:
+ continue
+ if response.status_code < 300:
+ return
+ reason = f"http_{response.status_code}"
+ # A 409 means an earlier post with this key is still queueing, so a retry can still succeed.
+ if response.status_code < 500 and response.status_code not in (409, 429):
+ break
+ _record_workflows_send_failure(reason)
+ raise WorkflowsWebhookError(reason)
+
+
def _send_weekly_digest_batch(input: SendWeeklyDigestBatchInput) -> None:
bind_contextvars(digest_key=input.digest.key, batch_start=input.batch[0], batch_end=input.batch[1])
logger = LOGGER.bind()
@@ -1051,6 +1150,7 @@ def _send_weekly_digest_batch(input: SendWeeklyDigestBatchInput) -> None:
messaging_record_batch: list[tuple[str, MessagingRecord]] = []
deadline = _upload_deadline()
queued_event_count = 0
+ workflows_session = requests.Session()
def drain() -> None:
nonlocal queued_event_count
@@ -1146,6 +1246,31 @@ def _send_weekly_digest_batch(input: SendWeeklyDigestBatchInput) -> None:
digest=payload,
user_email=user.email,
)
+ elif _routes_to_workflows(user):
+ workflows_payload = digest_for_user.render_payload(
+ input.digest, event_definitions_limit=WORKFLOWS_EVENT_DEFINITIONS_LIMIT
+ )
+ body = {
+ "event": WORKFLOWS_DIGEST_EVENT,
+ "distinct_id": _workflows_distinct_id(user),
+ "properties": {**workflows_payload, "to": user.email},
+ }
+ if len(json.dumps(body, default=str).encode()) > WORKFLOWS_WEBHOOK_BODY_LIMIT:
+ logger.error(
+ "Digest exceeds the Workflows webhook size limit, leaving the organization unsent",
+ organization_id=organization.id,
+ user_id=user.id,
+ )
+ _record_workflows_send_failure("oversized")
+ failed_organization_ids.add(str(organization.id))
+ continue
+ _wait_for_workflows_post(r, input.digest.key)
+ partial = True
+ try:
+ _post_to_workflows(workflows_session, body, f"{input.digest.key}:{user.id}")
+ except WorkflowsWebhookError:
+ failed_organization_ids.add(str(organization.id))
+ raise
elif len(json.dumps(payload, default=str).encode()) > DIGEST_PAYLOAD_SIZE_LIMIT:
logger.error(
"Digest exceeds the event size limit, leaving the organization unsent",
@@ -1158,7 +1283,7 @@ def _send_weekly_digest_batch(input: SendWeeklyDigestBatchInput) -> None:
partial = True
queued = ph_client.capture(
distinct_id=user.distinct_id,
- event="transactional email",
+ event=CUSTOMER_IO_DIGEST_EVENT,
properties=payload,
groups={
"organization": str(organization.id),
@@ -1192,6 +1317,7 @@ def _send_weekly_digest_batch(input: SendWeeklyDigestBatchInput) -> None:
record_sent(messaging_record_batch)
finally:
+ workflows_session.close()
if ph_client is not None:
ph_client.shutdown()
diff --git a/posthog/temporal/weekly_digest/types.py b/posthog/temporal/weekly_digest/types.py
index f9d36f31c350..a6f93ed11cff 100644
--- a/posthog/temporal/weekly_digest/types.py
+++ b/posthog/temporal/weekly_digest/types.py
@@ -318,11 +318,19 @@ class TeamDigest(BaseModel):
return sum(len(f.root) for f in self._fields())
def render_payload(
- self, product_suggestion: "DigestProductSuggestion | None" = None
- ) -> dict[str, str | int | dict[str, list | dict]]:
- report: dict[str, list | dict] = {
+ self,
+ product_suggestion: "DigestProductSuggestion | None" = None,
+ event_definitions_limit: int | None = None,
+ ) -> dict[str, str | int | dict[str, list | dict | int]]:
+ # A project can create thousands of events in a week, and the email lists only the first few.
+ # The count carries the rest, so a limited payload still renders the same email.
+ event_definitions = self.event_definitions.model_dump()
+ if event_definitions_limit is not None:
+ event_definitions = event_definitions[:event_definitions_limit]
+ report: dict[str, list | dict | int] = {
"new_dashboards": self.dashboards.model_dump(),
- "new_event_definitions": self.event_definitions.model_dump(),
+ "new_event_definitions": event_definitions,
+ "new_event_definitions_count": len(self.event_definitions.root),
"new_experiments_launched": self.experiments_launched.model_dump(),
"new_experiments_completed": self.experiments_completed.model_dump(),
"new_external_data_sources": self.external_data_sources.model_dump(),
@@ -402,7 +410,9 @@ class UserSpecificDigest(BaseModel):
def count_items(self) -> int:
return sum(td.count_items() for td in self.team_digests)
- def render_payload(self, digest: Digest) -> dict[str, str | list | dict[str, str] | int | None]:
+ def render_payload(
+ self, digest: Digest, event_definitions_limit: int | None = None
+ ) -> dict[str, str | list | dict[str, str] | int | None]:
teams = []
product_suggestion = self.context.product_suggestion
for td in self.team_digests:
@@ -411,7 +421,11 @@ class UserSpecificDigest(BaseModel):
suggestion_for_team = (
product_suggestion if product_suggestion and product_suggestion.team_id == td.id else None
)
- teams.append(td.render_payload(product_suggestion=suggestion_for_team))
+ teams.append(
+ td.render_payload(
+ product_suggestion=suggestion_for_team, event_definitions_limit=event_definitions_limit
+ )
+ )
return {
"organization_name": self.name,
diff --git a/posthog/temporal/weekly_digest/workflows.py b/posthog/temporal/weekly_digest/workflows.py
index 3cf5680e0893..953c6d0dfd0e 100644
--- a/posthog/temporal/weekly_digest/workflows.py
+++ b/posthog/temporal/weekly_digest/workflows.py
@@ -260,7 +260,8 @@ class SendWeeklyDigestWorkflow(PostHogWorkflow):
digest=input.digest,
common=input.common,
),
- start_to_close_timeout=timedelta(minutes=30),
+ # Batches share the pace of the captures for Workflows, so a batch can wait well past 30 minutes.
+ start_to_close_timeout=timedelta(hours=2),
retry_policy=common.RetryPolicy(
maximum_attempts=2,
initial_interval=timedelta(minutes=1),