: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
perf(weekly-digest): query each team range once per generator (#104658)
Rafael Audibert3102ddb7168d
diff --git a/posthog/temporal/tests/weekly_digest/test_activities.py b/posthog/temporal/tests/weekly_digest/test_activities.pyindex 93ff5a588d41..d974cda0f3f4 100644--- a/posthog/temporal/tests/weekly_digest/test_activities.py+++ b/posthog/temporal/tests/weekly_digest/test_activities.py@@ -9,6 +9,8 @@ import pytestfrom posthog.test.base import _create_event, flush_persons_and_eventsfrom unittest.mock import MagicMock, patch+from django.db import connection+from django.test.utils import CaptureQueriesContextfrom django.utils import timezoneimport fakeredis@@ -277,16 +279,68 @@ def _make_error_issues(team: Team, other_team: Team, digest: Digest) -> list[strdef test_generic_lookup_stores_only_this_teams_rows_from_the_window(activity_fn, key_kind, payload_field, make_rows, organization, team, redis_servers, common_input, digest):- other_team = Team.objects.create(organization=organization, name="other team")+ quiet_team = _make_team(organization, "quiet team")+ other_team = _make_team(organization, "other team")expected = make_rows(team, other_team, digest)+ # Left over from an earlier attempt of the same digest; the quiet team has no rows anymore.+ redis_servers.digest.set(team_data_key(digest.key, key_kind, quiet_team.id), "[]")- run_sync(activity_fn, batch_input(team, digest, common_input))+ run_sync(+ activity_fn,+ GenerateDigestDataBatchInput(+ team_id_range=TeamIdRange(start=team.id, end=other_team.id), digest=digest, common=common_input+ ),+ )key = team_data_key(digest.key, key_kind, team.id)stored = json.loads(redis_servers.digest.get(key))assert sorted(row[payload_field] for row in stored) == sorted(expected)assert 0 < redis_servers.digest.ttl(key) <= common_input.redis_ttl- assert redis_servers.digest.get(team_data_key(digest.key, key_kind, other_team.id)) is None+ # The quiet team is in range with nothing new, so its stale key goes; the other team is out of range.+ assert team.id < quiet_team.id < other_team.id+ assert redis_servers.digest.keys(f"{digest.key}-{key_kind}-*") == [key]+++def _run_lookup(activity_fn: Callable[..., Any]) -> Callable[[TeamIdRange, Digest, CommonInput], None]:+ return lambda team_range, digest, common: run_sync(+ activity_fn, GenerateDigestDataBatchInput(team_id_range=team_range, digest=digest, common=common)+ )+++def _run_organization_digest_batch(team_range: TeamIdRange, digest: Digest, common: CommonInput) -> None:+ run_sync(+ generate_organization_digest_batch,+ GenerateOrganizationDigestInput(batch=(0, Organization.objects.count()), digest=digest, common=common),+ )+++ "run_activity,make_row",+ [+ (_run_lookup(generate_dashboard_lookup), lambda team: Dashboard.objects.create(team=team, name="Dashboard")),+ (_run_lookup(generate_error_issue_lookup), lambda team: create_issue(team_id=team.id, name="issue")),+ (+ _run_lookup(generate_filter_lookup),+ lambda team: SessionRecordingPlaylist.objects.create(team=team, name="Filter", type="filters"),+ ),+ (_run_organization_digest_batch, lambda team: None),+ ],+)+def test_batch_activities_run_a_fixed_number_of_queries_per_range(+ run_activity, make_row, organization, team, redis_servers, common_input, digest+):+ # Each activity runs a fixed set of range-wide queries, so adding teams to the range adds no queries.+ def queries_for(team_count: int) -> int:+ teams = [team, *(_make_team(organization, f"team {index}") for index in range(team_count - 1))]+ for extra_team in teams:+ make_row(extra_team)+ team_range = TeamIdRange(start=team.id, end=max(extra_team.id for extra_team in teams) + 1)+ with CaptureQueriesContext(connection) as queries:+ run_activity(team_range, digest, common_input)+ return len(queries)++ assert queries_for(1) == queries_for(4)@pytest.mark.django_db@@ -428,21 +482,22 @@ def test_generate_organization_digest_batch_defaults_missing_team_data():silent_team = _make_team(organization, "silent team")Team.objects.create(organization=organization, name="demo team", is_demo=True)+ broken_organization = Organization.objects.create(name="broken org")+ broken_team = _make_team(broken_organization, "broken team")+ redis_servers.digest.set(team_data_key(digest.key, TeamDataKey.DASHBOARDS, broken_team.id), "not json")redis_servers.digest.set(team_data_key(digest.key, TeamDataKey.DASHBOARDS, team.id), json.dumps([{"name": "Dashboard", "id": 1}]))redis_servers.digest.set(team_data_key(digest.key, TeamDataKey.EXPIRING_RECORDINGS, team.id), json.dumps({"recording_count": 7}))- organization_index = list(Organization.objects.order_by("id").values_list("id", flat=True)).index(organization.id)-run_sync(generate_organization_digest_batch,- GenerateOrganizationDigestInput(- batch=(organization_index, organization_index + 1), digest=digest, common=common_input- ),+ GenerateOrganizationDigestInput(batch=(0, Organization.objects.count()), digest=digest, common=common_input),)+ # One organization's malformed value skips that organization only.+ assert redis_servers.digest.get(org_digest_key(digest.key, broken_organization.id)) is Nonestored = json.loads(redis_servers.digest.get(org_digest_key(digest.key, organization.id)))assert stored["name"] == organization.nameassert [td["id"] for td in stored["team_digests"]] == sorted([team.id, silent_team.id])diff --git a/posthog/temporal/weekly_digest/README.md b/posthog/temporal/weekly_digest/README.mdindex a2cab2505c91..8f9099a8494e 100644--- a/posthog/temporal/weekly_digest/README.md+++ b/posthog/temporal/weekly_digest/README.md@@ -29,7 +29,10 @@ Data is stored with keys prefixed by `{digest_key}` (e.g., `weekly-digest-2024-0### Team-level data-Generated via `team_data_key(digest_key, TeamDataKey.*, team_id)`:+Generated via `team_data_key(digest_key, TeamDataKey.*, team_id)`.+Each Postgres-backed generator reads a team id range with two queries, one for the eligible teams and one for the data, and writes its keys in one Redis pipeline.+The ClickHouse-backed generators (usage trends, expiring recordings) still query per team.+A team with no data of a kind gets no key for it; organization aggregation substitutes an empty default for a missing key.| `TeamDataKey` enum | Key Pattern | Contents || ----------------------- | ---------------------------------------------- | ---------------------------------- |diff --git a/posthog/temporal/weekly_digest/activities.py b/posthog/temporal/weekly_digest/activities.pyindex db79f887f3c0..8ec519565ad7 100644--- a/posthog/temporal/weekly_digest/activities.py+++ b/posthog/temporal/weekly_digest/activities.py@@ -1,8 +1,12 @@-from collections.abc import Callable+from collections import defaultdict+from collections.abc import Callable, Iterable, Sequencefrom datetime import UTC, datetime+from typing import Any, TypeVar+from uuid import UUIDfrom django.conf import settings-from django.db.models import QuerySet+from django.db.models import F, QuerySet, Window+from django.db.models.functions import RowNumberfrom django.utils import timezoneimport redis@@ -20,9 +24,11 @@ from posthog.hogql.query import execute_hogql_queryfrom posthog.clickhouse.client import sync_executefrom posthog.clickhouse.client.connection import Workloadfrom posthog.clickhouse.query_tagging import Feature, Product, tag_queries+from posthog.constants import AvailableFeaturefrom posthog.models.messaging import MessagingRecord-from posthog.models.organization_notification_lock import notification_locks_for_users+from posthog.models.organization_notification_lock import GovernedSetting, notification_locks_for_usersfrom posthog.models.team import Team+from posthog.models.user import Userfrom posthog.ph_client import get_client as get_ph_clientfrom posthog.session_recordings.queries.session_replay_events import SessionReplayEventsfrom posthog.session_recordings.session_recording_playlist_api import PLAYLIST_COUNT_REDIS_PREFIX@@ -41,13 +47,13 @@ from posthog.temporal.weekly_digest.queries import (query_new_external_data_sources,query_new_feature_flags,query_org_members,- query_org_product_push_campaigns,- query_org_teams,query_orgs_for_digest,+ query_product_push_campaigns_for_organizations,query_saved_filters,query_surveys_launched,query_team_ids_for_digest,query_teams_for_digest,+ query_teams_for_organizations,)from posthog.temporal.weekly_digest.types import (CommonInput,@@ -84,6 +90,11 @@ from products.growth.backend.product_push.selection import project_uses_product,LOGGER = get_write_only_logger()+# Bounds the size of one Redis request when a batch reads or writes keys for thousands of teams.+REDIS_COMMAND_CHUNK_SIZE = 5000++T = TypeVar("T")+def _redis_url(common: CommonInput) -> str:return f"redis://{common.redis_host}:{common.redis_port}?decode_responses=true"@@ -93,6 +104,54 @@ def _digest_redis(common: CommonInput) -> redis.Redis:return redis.Redis.from_url(_redis_url(common))+def _chunked(items: Sequence[T], size: int) -> Iterable[Sequence[T]]:+ for start in range(0, len(items), size):+ yield items[start : start + size]+++def _mget_chunked(r: redis.Redis, keys: Sequence[str]) -> list[Any]:+ values: list[Any] = []+ for chunk in _chunked(keys, REDIS_COMMAND_CHUNK_SIZE):+ values.extend(r.mget(list(chunk)))+ return values+++def _setex_many(r: redis.Redis, ttl: int, items: Sequence[tuple[str, str]]) -> None:+ for chunk in _chunked(items, REDIS_COMMAND_CHUNK_SIZE):+ with r.pipeline(transaction=False) as pipe:+ for key, value in chunk:+ pipe.setex(key, ttl, value)+ pipe.execute()+++def _delete_many(r: redis.Redis, keys: Sequence[str]) -> None:+ for chunk in _chunked(keys, REDIS_COMMAND_CHUNK_SIZE):+ r.delete(*chunk)+++def _store_team_data(+ r: redis.Redis,+ input: GenerateDigestDataBatchInput,+ key_kind: TeamDataKey,+ payload_by_team: dict[int, str],+ eligible_team_ids: set[int],+) -> None:+ """Write one key per team with data and drop the key of every other eligible team.++ A retry of the same digest may find that a team's rows disappeared since the first attempt,+ so a key left over from that attempt would otherwise survive for the whole TTL.+ """+ _setex_many(+ r,+ input.common.redis_ttl,+ [(team_data_key(input.digest.key, key_kind, team_id), payload) for team_id, payload in payload_by_team.items()],+ )+ _delete_many(+ r,+ [team_data_key(input.digest.key, key_kind, team_id) for team_id in eligible_team_ids - payload_by_team.keys()],+ )++def _bind_batch_logger(input: GenerateDigestDataBatchInput) -> FilteringBoundLogger:bind_contextvars(digest_key=input.digest.key,@@ -104,21 +163,20 @@ def _bind_batch_logger(input: GenerateDigestDataBatchInput) -> FilteringBoundLogreturn LOGGER.bind()-def _load_playlist_counts_from_django_cache(r: redis.Redis, filters: FilterList) -> list[PlaylistCount | None]:- resp: list[bytes | None] = r.mget([f"{PLAYLIST_COUNT_REDIS_PREFIX}{_filter.short_id}" for _filter in filters.root])+def _load_playlist_counts_from_django_cache(r: redis.Redis, short_ids: Sequence[str]) -> dict[str, PlaylistCount]:+ resp: list[bytes | None] = _mget_chunked(r, [f"{PLAYLIST_COUNT_REDIS_PREFIX}{short_id}" for short_id in short_ids])- playlist_counts: list[PlaylistCount | None] = []+ playlist_counts: dict[str, PlaylistCount] = {}- for count in resp:+ for short_id, count in zip(short_ids, resp):if count is None:- playlist_counts.append(None)- else:- try:- playlist_counts.append(PlaylistCount.model_validate_json(count))- except ValidationError:- # Failure to parse means the counting job likely had an error- # Treat it the same as a missing count- playlist_counts.append(None)+ continue+ try:+ playlist_counts[short_id] = PlaylistCount.model_validate_json(count)+ except ValidationError:+ # Failure to parse means the counting job likely had an error+ # Treat it the same as a missing count+ continuereturn playlist_counts@@ -131,6 +189,32 @@ def _teams_in_range(input: GenerateDigestDataBatchInput, *, with_organization: b)+def _rows_in_range(input: GenerateDigestDataBatchInput, rows: QuerySet) -> QuerySet:+ return rows.filter(team_id__gte=input.team_id_range.start, team_id__lt=input.team_id_range.end)+++def _eligible_team_ids(input: GenerateDigestDataBatchInput) -> set[int]:+ return set(_teams_in_range(input).values_list("id", flat=True))+++def _rows_by_team(+ input: GenerateDigestDataBatchInput, rows: QuerySet, eligible_team_ids: set[int]+) -> dict[int, list[dict[str, Any]]]:+ """Group one range-wide query by team, dropping teams the digest does not cover (demo, internal)."""+ rows_by_team: dict[int, list[dict[str, Any]]] = defaultdict(list)+ for row in _rows_in_range(input, rows):+ if row["team_id"] in eligible_team_ids:+ rows_by_team[row["team_id"]].append(row)+ return rows_by_team+++def _limit_per_team(rows: QuerySet, limit: int) -> QuerySet:+ """Keep the `limit` most recently created rows of each team inside one range-wide query."""+ return rows.annotate(+ team_rank=Window(RowNumber(), partition_by=F("team_id"), order_by=F("created_at").desc())+ ).filter(team_rank__lte=limit)++def generate_digest_data_lookup(input: GenerateDigestDataBatchInput,key_kind: TeamDataKey,@@ -141,35 +225,33 @@ def generate_digest_data_lookup(logger = _bind_batch_logger(input)logger.info("Generating digest data batch", key_kind=key_kind)+ rows: QuerySet = query_func(input.digest.period_start, input.digest.period_end)+ if per_team_limit is not None:+ rows = _limit_per_team(rows, per_team_limit)+resource_count = 0- team_count = 0+ eligible_team_ids = _eligible_team_ids(input)+ payload_by_team: dict[int, str] = {}+ # Teams with nothing new get no key. Aggregation substitutes an empty default for a missing key.+ for team_id, team_rows in _rows_by_team(input, rows, eligible_team_ids).items():+ try:+ digest_data = resource_type.model_validate(team_rows)+ except ValidationError as e:+ logger.warning(+ f"Failed to generate digest data for team {team_id}, skipping...", error=str(e), team_id=team_id+ )+ continue+ payload_by_team[team_id] = digest_data.model_dump_json()+ resource_count += len(digest_data.root)with _digest_redis(input.common) as r:- db_query: QuerySet = query_func(input.digest.period_start, input.digest.period_end)-- for team in _teams_in_range(input):- try:- team_query = db_query.filter(team_id=team.id)- if per_team_limit is not None:- team_query = team_query[:per_team_limit]- digest_data = resource_type(list(team_query))-- key = team_data_key(input.digest.key, key_kind, team.id)- r.setex(key, input.common.redis_ttl, digest_data.model_dump_json())-- team_count += 1- resource_count += len(digest_data.root)- except Exception as e:- logger.warning(- f"Failed to generate digest data for team {team.id}, skipping...", error=str(e), team_id=team.id- )- continue+ _store_team_data(r, input, key_kind, payload_by_team, eligible_team_ids)logger.info("Finished generating digest data batch",key_kind=key_kind,resource_count=resource_count,- team_count=team_count,+ team_count=len(payload_by_team),)@@ -268,36 +350,39 @@ def _generate_filter_lookup(input: GenerateDigestDataBatchInput) -> None:logger.error("Unable to generate Replay filter batch, missing URL for Django Redis...")return- with (- _digest_redis(input.common) as r,- redis.Redis.from_url(input.common.django_redis_url) as django_cache,- ):- query_filters: QuerySet = query_saved_filters(input.digest.period_start, input.digest.period_end)+ eligible_team_ids = _eligible_team_ids(input)+ rows_by_team = _rows_by_team(+ input, query_saved_filters(input.digest.period_start, input.digest.period_end), eligible_team_ids+ )+ with redis.Redis.from_url(input.common.django_redis_url) as django_cache:+ playlist_counts = _load_playlist_counts_from_django_cache(+ django_cache, [row["short_id"] for rows in rows_by_team.values() for row in rows]+ )- for team in _teams_in_range(input):- try:- filters = FilterList(list(query_filters.filter(team_id=team.id)))- playlist_counts = _load_playlist_counts_from_django_cache(django_cache, filters)+ payload_by_team: dict[int, str] = {}+ for team_id, rows in rows_by_team.items():+ try:+ filters = FilterList.model_validate(rows)+ except ValidationError as e:+ logger.warning(+ f"Failed to generate Replay filters for team {team_id}, skipping...", error=str(e), team_id=team_id+ )+ continue- for filter, playlist_count in zip(filters.root, playlist_counts):- if playlist_count is not None:- filter.recording_count = len(playlist_count.session_ids)- filter.more_available = playlist_count.has_more+ for filter in filters.root:+ playlist_count = playlist_counts.get(filter.short_id)+ if playlist_count is not None:+ filter.recording_count = len(playlist_count.session_ids)+ filter.more_available = playlist_count.has_more- ordered_filters = filters.order_by_recording_count()+ ordered_filters = filters.order_by_recording_count()+ payload_by_team[team_id] = ordered_filters.model_dump_json()- key = team_data_key(input.digest.key, TeamDataKey.SAVED_FILTERS, team.id)- r.setex(key, input.common.redis_ttl, ordered_filters.model_dump_json())+ team_count += 1+ filter_count += len(ordered_filters.root)- team_count += 1- filter_count += len(ordered_filters.root)- except Exception as e:- logger.warning(- f"Failed to generate Replay filters for team {team.id}, skipping...",- error=str(e),- team_id=team.id,- )- continue+ with _digest_redis(input.common) as r:+ _store_team_data(r, input, TeamDataKey.SAVED_FILTERS, payload_by_team, eligible_team_ids)logger.info("Finished generating Replay filter batch",@@ -511,26 +596,38 @@ def _generate_user_notification_lookup(input: GenerateDigestDataBatchInput) -> Nteam_count = 0user_count = 0+ # Without ACCESS_CONTROL every team of an organization has the same users, and locks are+ # organization-scoped, so resolve both once per organization instead of once per team.+ org_users: dict[UUID, tuple[list[User], dict[int, dict[GovernedSetting, bool]]]] = {}++ def users_and_locks(team: Team) -> tuple[list[User], dict[int, dict[GovernedSetting, bool]]]:+ cacheable = not team.organization.is_feature_available(AvailableFeature.ACCESS_CONTROL)+ if cacheable and team.organization_id in org_users:+ return org_users[team.organization_id]+ users = list(team.all_users_with_access())+ locks = notification_locks_for_users([user.id for user in users], organization_id=team.organization_id)+ if cacheable:+ org_users[team.organization_id] = (users, locks)+ return users, locks+with _digest_redis(input.common) as r:for team in _teams_in_range(input, with_organization=True):try:- users = list(team.all_users_with_access())- # One lookup for the whole team instead of one per user inside should_send_notification.- locks_by_user = notification_locks_for_users(- [user.id for user in users], organization_id=team.organization_id- )- for user in users:- if should_send_notification(- user,- NotificationSetting.WEEKLY_PROJECT_DIGEST.value,- team.id,- locks=locks_by_user.get(user.id, {}),- ):- key = user_data_key(input.digest.key, UserDataKey.NOTIFY_TEAMS, user.id)- r.sadd(key, team.id)- r.expire(key, input.common.redis_ttl)-- user_count += 1+ users, locks_by_user = users_and_locks(team)+ with r.pipeline(transaction=False) as pipe:+ for user in users:+ if should_send_notification(+ user,+ NotificationSetting.WEEKLY_PROJECT_DIGEST.value,+ team.id,+ locks=locks_by_user.get(user.id, {}),+ ):+ key = user_data_key(input.digest.key, UserDataKey.NOTIFY_TEAMS, user.id)+ pipe.sadd(key, team.id)+ pipe.expire(key, input.common.redis_ttl)++ user_count += 1+ pipe.execute()team_count += 1except Exception as e:logger.warning(@@ -562,23 +659,24 @@ def _generate_product_suggestion_lookup(input: GenerateDigestDataBatchInput) ->user_count = 0suggestion_count = 0users_with_suggestion: set[int] = set()- # Campaigns are org-scoped but this batch walks teams, so cache per org.- campaigns_by_org: dict[str, list[dict]] = {}++ teams = list(_teams_in_range(input, with_organization=True))+ # The query returns the newest campaign first, so the first row seen per org is the one to keep.+ campaign_by_org: dict[str, dict[str, Any]] = {}+ for campaign in query_product_push_campaigns_for_organizations(+ {team.organization_id for team in teams}, input.digest.period_end+ ):+ campaign_by_org.setdefault(str(campaign["organization_id"]), campaign)with _digest_redis(input.common) as r:- for team in _teams_in_range(input, with_organization=True):+ for team in teams:try:organization_id = str(team.organization_id)- if organization_id not in campaigns_by_org:- campaigns_by_org[organization_id] = list(- query_org_product_push_campaigns(organization_id, input.digest.period_end)- )- campaigns = campaigns_by_org[organization_id]- if not campaigns:+ campaign = campaign_by_org.get(organization_id)+ if campaign is None:team_count += 1continue- campaign = campaigns[0]product_path = resolve_product_path(campaign["product_key"])# The push is org-wide, but a project that already uses the product# shouldn't be nudged about it - same rule the nav card applies.@@ -657,6 +755,55 @@ def list_team_id_ranges(input: CommonInput) -> list[TeamIdRange]:return _cut_team_id_ranges(list(query_team_ids_for_digest()), input.batch_size)+# Positions follow TeamDataKey order and the TeamDigest constructor below.+TEAM_DATA_DEFAULTS: list[tuple[TeamDataKey, Any]] = [+ (TeamDataKey.DASHBOARDS, DashboardList(root=[])),+ (TeamDataKey.EVENT_DEFINITIONS, EventDefinitionList(root=[])),+ (TeamDataKey.EXPERIMENTS_LAUNCHED, ExperimentList(root=[])),+ (TeamDataKey.EXPERIMENTS_COMPLETED, ExperimentList(root=[])),+ (TeamDataKey.EXTERNAL_DATA_SOURCES, ExternalDataSourceList(root=[])),+ (TeamDataKey.FEATURE_FLAGS, FeatureFlagList(root=[])),+ (TeamDataKey.SAVED_FILTERS, FilterList(root=[])),+ (TeamDataKey.EXPIRING_RECORDINGS, RecordingCount(recording_count=0)),+ (TeamDataKey.SURVEYS_LAUNCHED, SurveyList(root=[])),+ (TeamDataKey.USAGE_TRENDS, UsageTrends()),+ (TeamDataKey.ERROR_ISSUES, ErrorIssueList(root=[])),+]+++def _load_team_values(r: redis.Redis, digest_key: str, teams: Sequence[Team]) -> dict[int, list[str | None]]:+ """One Redis read for every team-level key in the batch, in TEAM_DATA_DEFAULTS order per team."""+ keys = [team_data_key(digest_key, kind, team.id) for team in teams for kind, _ in TEAM_DATA_DEFAULTS]+ values = _mget_chunked(r, keys)++ kinds_per_team = len(TEAM_DATA_DEFAULTS)+ return {team.id: values[index * kinds_per_team : (index + 1) * kinds_per_team] for index, team in enumerate(teams)}+++def _team_digest(team: Team, raw_values: list[str | None]) -> TeamDigest:+ # Parsing happens per organization, inside its exception boundary, so one malformed value+ # skips that organization instead of the whole batch. Absent keys fall back to the defaults.+ digest_data = [+ default if value is None else default.__class__.model_validate_json(value)+ for (_, default), value in zip(TEAM_DATA_DEFAULTS, raw_values)+ ]+ return TeamDigest(+ id=team.id,+ name=team.name,+ dashboards=digest_data[0],+ event_definitions=digest_data[1],+ experiments_launched=digest_data[2],+ experiments_completed=digest_data[3],+ external_data_sources=digest_data[4],+ feature_flags=digest_data[5],+ filters=digest_data[6],+ expiring_recordings=digest_data[7],+ surveys_launched=digest_data[8],+ usage_trends=digest_data[9],+ error_issues=digest_data[10],+ )++def _generate_organization_digest_batch(input: GenerateOrganizationDigestInput) -> None:bind_contextvars(digest_key=input.digest.key, batch_start=input.batch[0], batch_end=input.batch[1])logger = LOGGER.bind()@@ -665,78 +812,33 @@ def _generate_organization_digest_batch(input: GenerateOrganizationDigestInput)organization_count = 0team_count = 0- with _digest_redis(input.common) as r:- batch_start, batch_end = input.batch- for organization in query_orgs_for_digest()[batch_start:batch_end]:- try:- team_digests: list[TeamDigest] = []-- for team in query_org_teams(organization):- results: list[str | None] = r.mget(- [- team_data_key(input.digest.key, TeamDataKey.DASHBOARDS, team.id),- team_data_key(input.digest.key, TeamDataKey.EVENT_DEFINITIONS, team.id),- team_data_key(input.digest.key, TeamDataKey.EXPERIMENTS_LAUNCHED, team.id),- team_data_key(input.digest.key, TeamDataKey.EXPERIMENTS_COMPLETED, team.id),- team_data_key(input.digest.key, TeamDataKey.EXTERNAL_DATA_SOURCES, team.id),- team_data_key(input.digest.key, TeamDataKey.FEATURE_FLAGS, team.id),- team_data_key(input.digest.key, TeamDataKey.SAVED_FILTERS, team.id),- team_data_key(input.digest.key, TeamDataKey.EXPIRING_RECORDINGS, team.id),- team_data_key(input.digest.key, TeamDataKey.SURVEYS_LAUNCHED, team.id),- team_data_key(input.digest.key, TeamDataKey.USAGE_TRENDS, team.id),- team_data_key(input.digest.key, TeamDataKey.ERROR_ISSUES, team.id),- ]- )+ batch_start, batch_end = input.batch+ organizations = list(query_orgs_for_digest()[batch_start:batch_end])+ teams_by_org: dict[UUID, list[Team]] = defaultdict(list)+ for team in query_teams_for_organizations([organization.id for organization in organizations]):+ teams_by_org[team.organization_id].append(team)- defaults = [- DashboardList(root=[]),- EventDefinitionList(root=[]),- ExperimentList(root=[]),- ExperimentList(root=[]),- ExternalDataSourceList(root=[]),- FeatureFlagList(root=[]),- FilterList(root=[]),- RecordingCount(recording_count=0),- SurveyList(root=[]),- UsageTrends(),- ErrorIssueList(root=[]),- ]-- digest_data = [- default if result is None else default.__class__.model_validate_json(result)- for default, result in zip(defaults, results)- ]-- team_digests.append(- TeamDigest(- id=team.id,- name=team.name,- dashboards=digest_data[0],- event_definitions=digest_data[1],- experiments_launched=digest_data[2],- experiments_completed=digest_data[3],- external_data_sources=digest_data[4],- feature_flags=digest_data[5],- filters=digest_data[6],- expiring_recordings=digest_data[7],- surveys_launched=digest_data[8],- usage_trends=digest_data[9],- error_issues=digest_data[10],- )- )- team_count += 1+ items: list[tuple[str, str]] = []+ with _digest_redis(input.common) as r:+ team_values = _load_team_values(+ r, input.digest.key, [team for teams in teams_by_org.values() for team in teams]+ )+ for organization in organizations:+ try:+ team_digests = [+ _team_digest(team, team_values[team.id]) for team in teams_by_org.get(organization.id, [])+ ]org_digest = OrganizationDigest(id=organization.id,name=organization.name,created_at=organization.created_at,team_digests=team_digests,)-- key = org_digest_key(input.digest.key, organization.id)- r.setex(key, input.common.redis_ttl, org_digest.model_dump_json())+ items.append((org_digest_key(input.digest.key, organization.id), org_digest.model_dump_json()))organization_count += 1+ team_count += len(team_digests)except Exception as e:logger.warning(f"Failed to generate organization-level digest for organization {organization.id}, skipping...",@@ -745,6 +847,8 @@ def _generate_organization_digest_batch(input: GenerateOrganizationDigestInput))continue+ _setex_many(r, input.common.redis_ttl, items)+logger.info("Finished generating organization-level digest batch",organization_count=organization_count,@@ -825,18 +929,21 @@ def _send_weekly_digest_batch(input: SendWeeklyDigestBatchInput) -> None:)continue- for member in query_org_members(organization):+ members = list(query_org_members(organization))+ with r.pipeline(transaction=False) as pipe:+ for member in members:+ pipe.smembers(user_data_key(input.digest.key, UserDataKey.NOTIFY_TEAMS, member.user.id))+ pipe.get(user_data_key(input.digest.key, UserDataKey.PRODUCT_SUGGESTION, member.user.id))+ member_data = pipe.execute()++ for index, member in enumerate(members):_raise_if_cancelled()user = member.user- user_notify_teams: set[int] = set(- map(int, r.smembers(user_data_key(input.digest.key, UserDataKey.NOTIFY_TEAMS, user.id)))- )+ user_notify_teams: set[int] = set(map(int, member_data[index * 2]))# Load user-specific contextproduct_suggestion: DigestProductSuggestion | None = None- raw_suggestion: str | None = r.get(- user_data_key(input.digest.key, UserDataKey.PRODUCT_SUGGESTION, user.id)- )+ raw_suggestion: str | None = member_data[index * 2 + 1]if raw_suggestion:try:product_suggestion = DigestProductSuggestion.model_validate_json(raw_suggestion)diff --git a/posthog/temporal/weekly_digest/queries.py b/posthog/temporal/weekly_digest/queries.pyindex 720402dd664d..5ec339fa36db 100644--- a/posthog/temporal/weekly_digest/queries.py+++ b/posthog/temporal/weekly_digest/queries.py@@ -1,4 +1,6 @@+from collections.abc import Collectionfrom datetime import datetime+from uuid import UUIDfrom django.db.models import Count, Q, QuerySet@@ -54,8 +56,13 @@ def query_orgs_for_digest() -> QuerySet:return Organization.objects.exclude(Q(for_internal_metrics=True)).only("id", "name", "created_at").order_by("id")-def query_org_teams(organization: Organization) -> QuerySet:- return Team.objects.only("id", "name").filter(organization=organization).exclude(is_demo=True).order_by("id")+def query_teams_for_organizations(organization_ids: Collection[UUID]) -> QuerySet:+ return (+ Team.objects.only("id", "name", "organization_id")+ .filter(organization_id__in=organization_ids)+ .exclude(is_demo=True)+ .order_by("id")+ )def query_org_members(organization: Organization) -> QuerySet:@@ -148,23 +155,25 @@ def query_saved_filters(period_start: datetime, period_end: datetime) -> QuerySe),),)- .values("name", "short_id", "view_count")+ .values("team_id", "name", "short_id", "view_count").order_by("-view_count"))-def query_org_product_push_campaigns(organization_id: str, period_end: datetime) -> QuerySet:- """Product push campaigns still running at the end of the digest period.+def query_product_push_campaigns_for_organizations(+ organization_ids: Collection[UUID], period_end: datetime+) -> QuerySet:+ """Product push campaigns still running at the end of the digest period, newest first.Only ACTIVE campaigns qualify. A campaign that closed mid-period did so because the orgeither adopted the product or moved on from it, and neither is worth an email nudge."""return (ProductPushCampaign.objects.filter(- organization_id=organization_id,+ organization_id__in=organization_ids,status=ProductPushCampaign.Status.ACTIVE,started_at__lte=period_end,).order_by("-started_at")- .values("product_key", "reason_text")+ .values("organization_id", "product_key", "reason_text"))