: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
fix(signals): let projects take turns in anomaly scoring picks (#122402)
posthog[bot]f34836541ae0
diff --git a/products/signals/backend/anomaly_scores/queue.py b/products/signals/backend/anomaly_scores/queue.pyindex fdbdd41a9ed3..dad7b1dc851c 100644--- a/products/signals/backend/anomaly_scores/queue.py+++ b/products/signals/backend/anomaly_scores/queue.py@@ -5,6 +5,7 @@ from the `$anomaly_score` events. `selection.pick_order` then orders the due ins"""import datetime+import itertoolsfrom collections.abc import Collectionfrom typing import Any, cast@@ -223,10 +224,10 @@ def select_insights(for team_id in sorted({insight.team_id for insight in due_by_id.values()}):due_ids = [insight.insight_id for insight in due_by_id.values() if insight.team_id == team_id]usage.update(insight_usage(team_id=team_id, insight_ids=due_ids, now=now))- candidates = []+ candidates_by_team: dict[int, list[Candidate]] = {}for insight in due_by_id.values():newest = newest_buckets[insight.insight_id]- candidates.append(+ candidates_by_team.setdefault(insight.team_id, []).append(Candidate(insight_id=insight.insight_id,interval=insight.interval,@@ -237,9 +238,19 @@ def select_insights(usage=usage[insight.insight_id],))- order = pick_order(- candidates, config=selection_config, tick_start=now, max_staleness=config.limits.max_backfill_buckets- )+ team_orders = [+ pick_order(+ candidates_by_team[team_id],+ config=selection_config,+ tick_start=now,+ max_staleness=config.limits.max_backfill_buckets,+ )+ for team_id in sorted(candidates_by_team)+ ]+ # Projects take turns, so one large project cannot take every slot of the tick budget.+ order = [+ insight_id for turn in itertools.zip_longest(*team_orders) for insight_id in turn if insight_id is not None+ ]picks = [InsightPick(team_id=due_by_id[insight_id].team_id, insight_id=insight_id, scored_through=state.get(insight_id, {})diff --git a/products/signals/backend/test/test_anomaly_scoring_tick.py b/products/signals/backend/test/test_anomaly_scoring_tick.pyindex a6aaa3317e0e..42fbb44e22e6 100644--- a/products/signals/backend/test/test_anomaly_scoring_tick.py+++ b/products/signals/backend/test/test_anomaly_scoring_tick.py@@ -13,7 +13,7 @@ from parameterized import parameterizedfrom posthog.schema import IntervalType-from posthog.models import Organization, Team+from posthog.models import Organization, Team, Userfrom products.dashboards.backend.models.dashboard import Dashboardfrom products.dashboards.backend.models.dashboard_tile import DashboardTile@@ -339,3 +339,35 @@ class TestScoredBuckets(ClickhouseTestMixin, BaseTest):assert [pick.insight_id for pick in selection.picks] == [insights["never_scored"].id, insights["stale"].id]assert selection.picks[1].scored_through == {"stale": today - timedelta(days=3)}++ @time_machine.travel(NOW, tick=False)+ def test_select_insights_lets_each_project_pick_before_any_project_picks_twice(self) -> None:+ small_team = Team.objects.create(organization=self.organization, name="Small")+ viewers = [+ User.objects.create_and_join(self.organization, f"viewer{index}@example.com", "password")+ for index in range(5)+ ]+ large_insights = []+ for index in range(3):+ insight = Insight.objects.create(team=self.team, name=f"large_{index}", query=_trends(interval="day"))+ for viewer in viewers:+ record_insight_view(insight_id=insight.id, team_id=self.team.id, user_id=viewer.id)+ large_insights.append(insight)+ small_insight = Insight.objects.create(team=small_team, name="small", query=_trends(interval="day"))+ small_dashboard = Dashboard.objects.create(team=small_team, name="Main")+ DashboardTile.objects.create(dashboard=small_dashboard, insight=small_insight)+ for insight in [*large_insights, small_insight]:+ cache.delete(_cooldown_key(insight.id))+ config = AnomalyScoringConfig(+ enabled=True,+ organization_ids=(str(self.organization.id),),+ allow_team_ids=(self.team.id, small_team.id),+ )++ with patch("products.signals.backend.anomaly_scores.queue.list_limited_team_attributes", return_value=[]):+ selection = select_insights(config, SelectionConfig(weighting=Weighting.WEIGHTED), NOW, limit=2)++ assert [pick.team_id for pick in selection.picks] == [self.team.id, small_team.id]+ assert selection.picks[0].insight_id in {insight.id for insight in large_insights}+ assert selection.picks[1].insight_id == small_insight.id+ assert selection.candidates == 4