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(signals): score only active insights for anomaly scoring (#123995)

posthog[bot]11cbba9019b4
diff --git a/products/alerts/backend/facade/api.py b/products/alerts/backend/facade/api.py
index b02b7b166eca..dce4eb044fb5 100644
--- a/products/alerts/backend/facade/api.py
+++ b/products/alerts/backend/facade/api.py
@@ -93,6 +93,17 @@ def insight_ids_with_enabled_alerts(*, team_id: int, insight_ids: Collection[int
)
+def enabled_alert_insight_ids(*, team_ids: Collection[int]) -> set[int]:
+ """Insights of these teams with at least one enabled alert, in one query."""
+ if not team_ids:
+ return set()
+ return set(
+ AlertConfiguration.objects.filter(team_id__in=team_ids, enabled=True)
+ .values_list("insight_id", flat=True)
+ .distinct()
+ )
+
+
def delete_insight_alerts(insight_ids: Collection[int]) -> None:
"""Delete the alerts pointing at the given insights.
diff --git a/products/dashboards/backend/facade/api.py b/products/dashboards/backend/facade/api.py
index c3187941473f..8ee64b8f3906 100644
--- a/products/dashboards/backend/facade/api.py
+++ b/products/dashboards/backend/facade/api.py
@@ -13,6 +13,7 @@ response they become.
import re
from collections.abc import Collection, Sequence
+from datetime import datetime
from typing import TYPE_CHECKING, Any
from django.db.models import Exists, OuterRef, Prefetch, QuerySet
@@ -215,6 +216,22 @@ def dashboards_by_insight(insight_ids: Collection[int], *, team_id: int) -> dict
return {insight_id: tuple(dashboards) for insight_id, dashboards in refs.items()}
+def insight_ids_on_dashboards_accessed_since(*, team_ids: Collection[int], since: datetime) -> set[int]:
+ """Insights with a live tile on a live dashboard of these teams that someone opened since the given time.
+
+ One query. Any access counts: the app, the API, a shared link, or an embed.
+ """
+ if not team_ids:
+ return set()
+ return set(
+ DashboardTile.objects.filter(
+ dashboard__team_id__in=team_ids, dashboard__last_accessed_at__gte=since, insight_id__isnull=False
+ )
+ .values_list("insight_id", flat=True)
+ .distinct()
+ )
+
+
def dashboard_exists(dashboard_id: int) -> bool:
"""Whether a live dashboard has this id, in any team.
diff --git a/products/product_analytics/backend/facade/api.py b/products/product_analytics/backend/facade/api.py
index d99384f71ec0..41f919b017e7 100644
--- a/products/product_analytics/backend/facade/api.py
+++ b/products/product_analytics/backend/facade/api.py
@@ -28,8 +28,8 @@ from products.product_analytics.backend.facade.contracts import (
InsightSeries,
InsightVariableDefinition,
SavedInsightDefinition,
+ SavedTrendsInsight,
ViewableInsight,
- WatchedTrendsInsight,
)
from products.product_analytics.backend.models.insight import Insight
from products.product_analytics.backend.models.insight_variable import InsightVariable
@@ -132,22 +132,35 @@ def insights_including_soft_deleted_for_team(*, team_id: int, insight_ids: Colle
return logic.insights_including_soft_deleted_for_team(team_id=team_id, insight_ids=insight_ids)
-def watched_trends_insights(
+def saved_trends_insights(
*,
team_ids: Collection[int],
+ insight_ids: Collection[int],
intervals: Collection[str],
excluded_displays: Collection[str],
- viewed_since: datetime,
-) -> list[WatchedTrendsInsight]:
- """Saved trends insights that are on a live dashboard or that someone viewed since ``viewed_since``.
+) -> list[SavedTrendsInsight]:
+ """The saved trends insights among ``insight_ids``, in one query.
- Only insights with an interval in ``intervals`` and a display outside ``excluded_displays`` are kept.
+ Only insights of ``team_ids`` with an interval in ``intervals`` and a display outside ``excluded_displays`` are kept.
"""
- return logic.watched_trends_insights(
- team_ids=team_ids, intervals=intervals, excluded_displays=excluded_displays, viewed_since=viewed_since
+ return logic.saved_trends_insights(
+ team_ids=team_ids, insight_ids=insight_ids, intervals=intervals, excluded_displays=excluded_displays
)
+def insight_ids_viewed_since(*, team_ids: Collection[int], since: datetime) -> set[int]:
+ """Insights of these teams that a signed-in person viewed since the given time, in one query.
+
+ Shared and embedded views have no viewer, so they do not count.
+ """
+ return logic.insight_ids_viewed_since(team_ids=team_ids, since=since)
+
+
+def insight_ids_modified_since(*, team_ids: Collection[int], since: datetime) -> set[int]:
+ """Saved insights of these teams created or edited since the given time, in one query."""
+ return logic.insight_ids_modified_since(team_ids=team_ids, since=since)
+
+
def recent_viewers_by_insight(
*, team_id: int, insight_ids: Collection[int], since: datetime, max_per_insight: int
) -> dict[int, list["User"]]:
diff --git a/products/product_analytics/backend/facade/contracts.py b/products/product_analytics/backend/facade/contracts.py
index d81dff904855..a9bb0078780e 100644
--- a/products/product_analytics/backend/facade/contracts.py
+++ b/products/product_analytics/backend/facade/contracts.py
@@ -139,7 +139,7 @@ class InsightDataChecks:
@dataclass(frozen=True)
-class WatchedTrendsInsight:
+class SavedTrendsInsight:
team_id: int
insight_id: int
# The trends interval, "day" when the query sets none.
diff --git a/products/product_analytics/backend/logic.py b/products/product_analytics/backend/logic.py
index c8d87760b176..93105ff7b165 100644
--- a/products/product_analytics/backend/logic.py
+++ b/products/product_analytics/backend/logic.py
@@ -3,12 +3,12 @@ from datetime import datetime
from typing import TYPE_CHECKING, Any
from uuid import UUID
-from django.db.models import Count, OuterRef, Q, QuerySet, Subquery, TextField, Value
+from django.db.models import Count, OuterRef, QuerySet, Subquery, TextField, Value
from django.db.models.fields.json import KT
from django.db.models.functions import Coalesce
from django.utils.timezone import now
-from products.product_analytics.backend.facade.contracts import InsightVariableDefinition, WatchedTrendsInsight
+from products.product_analytics.backend.facade.contracts import InsightVariableDefinition, SavedTrendsInsight
from products.product_analytics.backend.models.insight import Insight, InsightViewed
from products.product_analytics.backend.models.insight_variable import InsightVariable
@@ -95,17 +95,19 @@ def insights_including_soft_deleted_for_team(*, team_id: int, insight_ids: Colle
return list(Insight.objects_including_soft_deleted.filter(team_id=team_id, id__in=insight_ids))
-def watched_trends_insights(
+def saved_trends_insights(
*,
team_ids: Collection[int],
+ insight_ids: Collection[int],
intervals: Collection[str],
excluded_displays: Collection[str],
- viewed_since: datetime,
-) -> list[WatchedTrendsInsight]:
- if not team_ids:
+) -> list[SavedTrendsInsight]:
+ if not team_ids or not insight_ids:
return []
- trends = (
- Insight.objects.filter(team_id__in=team_ids, saved=True)
+ rows = (
+ # Every row stays inside `team_ids`, which the caller resolved; the ids only narrow that set.
+ # nosemgrep: idor-lookup-without-team
+ Insight.objects.filter(team_id__in=team_ids, id__in=insight_ids, saved=True)
.annotate(
# A wrapper node holds the trends query in `source`.
trends_kind=Coalesce(KT("query__source__kind"), KT("query__kind")),
@@ -121,22 +123,35 @@ def watched_trends_insights(
)
.filter(trends_kind="TrendsQuery", trends_interval__in=intervals)
.exclude(trends_display__in=excluded_displays)
+ .order_by("team_id", "id")
+ .values_list("team_id", "id", "trends_interval")
)
- # Two queries instead of one OR, so Postgres never multiplies dashboard tiles by views.
- on_dashboard = trends.filter(
- Q(dashboard_tiles__deleted=False) | Q(dashboard_tiles__deleted__isnull=True),
- dashboard_tiles__dashboard__deleted=False,
- )
- viewed = trends.filter(insightviewed__last_viewed_at__gte=viewed_since)
- rows: set[tuple[int, int, str]] = set()
- for queryset in (on_dashboard, viewed):
- rows.update(queryset.values_list("team_id", "id", "trends_interval"))
return [
- WatchedTrendsInsight(team_id=team_id, insight_id=insight_id, interval=interval)
- for team_id, insight_id, interval in sorted(rows)
+ SavedTrendsInsight(team_id=team_id, insight_id=insight_id, interval=interval)
+ for team_id, insight_id, interval in rows
]
+def insight_ids_viewed_since(*, team_ids: Collection[int], since: datetime) -> set[int]:
+ if not team_ids:
+ return set()
+ return set(
+ InsightViewed.objects.filter(team_id__in=team_ids, last_viewed_at__gte=since, user__isnull=False)
+ .values_list("insight_id", flat=True)
+ .distinct()
+ )
+
+
+def insight_ids_modified_since(*, team_ids: Collection[int], since: datetime) -> set[int]:
+ if not team_ids:
+ return set()
+ return set(
+ Insight.objects.filter(team_id__in=team_ids, saved=True, last_modified_at__gte=since).values_list(
+ "id", flat=True
+ )
+ )
+
+
def recent_viewers_by_insight(
*, team_id: int, insight_ids: Collection[int], since: datetime, max_per_insight: int
) -> dict[int, list["User"]]:
diff --git a/products/signals/backend/anomaly_scores/config.py b/products/signals/backend/anomaly_scores/config.py
index 581960807f54..5562961c231b 100644
--- a/products/signals/backend/anomaly_scores/config.py
+++ b/products/signals/backend/anomaly_scores/config.py
@@ -144,6 +144,8 @@ class AnomalyScoringConfig:
# Per-tick picks of each pass when the split is on. Score steps are cheap, so the score pass gets most slots.
max_score_insights_per_tick: int = 20
max_train_insights_per_tick: int = 2
+ # An insight is scored only while one of its activity signals falls in this many days, see `queue.active_insights`.
+ active_window_days: int = 90
DISABLED = AnomalyScoringConfig()
@@ -156,6 +158,7 @@ MAX_SCORE_QUERIES_PER_HOUR = 5000
MAX_SCORE_QUERIES_PER_PROJECT_PER_HOUR = 1000
MAX_SCORE_INSIGHTS_PER_TICK = 200
MAX_TRAIN_INSIGHTS_PER_TICK = 50
+MAX_ACTIVE_WINDOW_DAYS = 365
def _positive_int(value: Any, default: int) -> int:
@@ -270,6 +273,9 @@ def parse_config(payload: Any) -> AnomalyScoringConfig:
),
MAX_TRAIN_INSIGHTS_PER_TICK,
),
+ active_window_days=min(
+ _positive_int(payload.get("active_window_days"), defaults.active_window_days), MAX_ACTIVE_WINDOW_DAYS
+ ),
)
diff --git a/products/signals/backend/anomaly_scores/queue.py b/products/signals/backend/anomaly_scores/queue.py
index c8bce20c9c9b..7dbb9c0eb255 100644
--- a/products/signals/backend/anomaly_scores/queue.py
+++ b/products/signals/backend/anomaly_scores/queue.py
@@ -1,4 +1,4 @@
-"""Pick the watched insights that the next anomaly scoring tick scores.
+"""Pick the active insights that the next anomaly scoring tick scores.
One query per team reads the newest scored bucket of each series back from the `$anomaly_score`
events. `selection.pick_order` then orders the due insights, and the tick picks from that order the
@@ -15,7 +15,9 @@ the score pass scores it with the old model, so the train budget does not decide
import datetime
import itertools
import dataclasses
+from collections import Counter
from collections.abc import Callable, Collection
+from enum import StrEnum
from typing import Any, cast
from django.core.cache import cache
@@ -40,10 +42,14 @@ from posthog.interval_specs import interval_spec
from posthog.models import Team
from posthog.tasks.alerts.utils import NON_TIME_SERIES_DISPLAY_TYPES
+from products.alerts.backend.facade.api import enabled_alert_insight_ids
from products.alerts_platform.backend.facade.scheduling import CalendarInterval, alert_check_offset
+from products.dashboards.backend.facade.api import insight_ids_on_dashboards_accessed_since
from products.product_analytics.backend.facade.api import (
+ insight_ids_modified_since,
+ insight_ids_viewed_since,
insights_including_soft_deleted_for_team,
- watched_trends_insights,
+ saved_trends_insights,
)
from products.signals.backend.anomaly_scores.config import AnomalyScoringConfig, ScoringRun
from products.signals.backend.anomaly_scores.scoring import bucket_end
@@ -57,6 +63,7 @@ from products.signals.backend.anomaly_scores.selection import (
score_order,
)
from products.signals.backend.anomaly_scores.state import RunPlan, load_states, plan_run, score_ready_by_insight
+from products.subscriptions.backend.facade.api import insight_ids_with_active_subscriptions
from ee.billing.quota_limiting import QuotaLimitingCaches, QuotaResource, list_limited_team_attributes
@@ -64,9 +71,7 @@ logger = structlog.get_logger(__name__)
SCORE_EVENT = "$anomaly_score"
SOURCE_INSIGHT = "insight"
-# An insight counts as watched when it is on a dashboard or someone viewed it in this window.
-VIEW_WINDOW = datetime.timedelta(days=30)
-# One row is one series. Far more than the series of every watched insight of one team.
+# One row is one series. Far more than the series of every active insight of one team.
_MAX_SCORED_SERIES = 50_000
# An insight is not picked again in this time after an attempt. Events take a moment to reach
# ClickHouse, and an insight that writes nothing would otherwise stay at the head of the queue.
@@ -111,11 +116,22 @@ LIMIT {limit}
"""
+class ActivityReason(StrEnum):
+ """Why an insight is active. An insight with no reason in the window is not scored."""
+
+ VIEWED = "viewed"
+ DASHBOARD_ACCESSED = "dashboard_accessed"
+ ENABLED_ALERT = "enabled_alert"
+ ACTIVE_SUBSCRIPTION = "active_subscription"
+ EDITED = "edited"
+
+
@frozen
-class WatchedInsight:
+class ActiveInsight:
team_id: int
insight_id: int
interval: IntervalType
+ reasons: frozenset[ActivityReason] = frozenset()
@frozen
@@ -147,6 +163,9 @@ class Selection:
# Due train candidates that the train pass did not pick: soft ones still score with a stale model, hard ones do not.
train_backlog_soft: int = 0
train_backlog_hard: int = 0
+ # Active insights of the eligible teams, and how many of them have each reason. One insight can have several.
+ active: int = 0
+ active_reasons: dict[str, int] = dataclasses.field(default_factory=dict)
@frozen
@@ -155,7 +174,7 @@ class ScorePassOrder:
order: list[int]
# Due insights with a model past a soft retrain trigger, longest overdue first.
stale_order: list[int]
- # Every watched insight with a current model, due or not.
+ # Every active insight with a current model, due or not.
ready: set[int]
@@ -178,21 +197,49 @@ def eligible_teams(config: AnomalyScoringConfig) -> list[Team]:
return [team for team in teams if team.api_token not in limited]
-def watched_insights(
- team_ids: list[int], intervals: tuple[IntervalType, ...], now: datetime.datetime
-) -> list[WatchedInsight]:
- """Saved trends insights with a scored interval that are on a dashboard or were viewed lately."""
+def active_insights(
+ team_ids: list[int], intervals: tuple[IntervalType, ...], now: datetime.datetime, window_days: int
+) -> list[ActiveInsight]:
+ """Saved trends insights with a scored interval and at least one activity reason in the last ``window_days``.
+
+ Computed fresh on each call, so an insight drops out once its last reason ages out, and comes back with a new one.
+ Its stored state stays, so a later train step picks up where the model left off.
+ """
+ if not team_ids:
+ return []
+ since = now - datetime.timedelta(days=window_days)
+ reasons_by_id: dict[int, set[ActivityReason]] = {}
+ for reason, insight_ids in (
+ (ActivityReason.VIEWED, insight_ids_viewed_since(team_ids=team_ids, since=since)),
+ (ActivityReason.DASHBOARD_ACCESSED, insight_ids_on_dashboards_accessed_since(team_ids=team_ids, since=since)),
+ (ActivityReason.ENABLED_ALERT, enabled_alert_insight_ids(team_ids=team_ids)),
+ (ActivityReason.ACTIVE_SUBSCRIPTION, insight_ids_with_active_subscriptions(team_ids=team_ids)),
+ (ActivityReason.EDITED, insight_ids_modified_since(team_ids=team_ids, since=since)),
+ ):
+ for insight_id in insight_ids:
+ reasons_by_id.setdefault(insight_id, set()).add(reason)
return [
- WatchedInsight(team_id=watched.team_id, insight_id=watched.insight_id, interval=IntervalType(watched.interval))
- for watched in watched_trends_insights(
+ ActiveInsight(
+ team_id=insight.team_id,
+ insight_id=insight.insight_id,
+ interval=IntervalType(insight.interval),
+ reasons=frozenset(reasons_by_id[insight.insight_id]),
+ )
+ for insight in saved_trends_insights(
team_ids=team_ids,
+ insight_ids=list(reasons_by_id),
intervals=[interval.value for interval in intervals],
excluded_displays=_UNSCORED_DISPLAYS,
- viewed_since=now - VIEW_WINDOW,
)
]
+def reason_counts(insights: Collection[ActiveInsight]) -> dict[str, int]:
+ """How many of the insights have each reason, with a zero for a reason none of them has."""
+ counts = Counter(reason for insight in insights for reason in insight.reasons)
+ return {reason.value: counts[reason] for reason in ActivityReason}
+
+
def scored_state_since(
config: AnomalyScoringConfig, interval: IntervalType, now: datetime.datetime
) -> datetime.datetime:
@@ -239,7 +286,7 @@ def scored_buckets(
def is_due(
- insight: WatchedInsight, newest_bucket: datetime.datetime | None, timezone: datetime.tzinfo, now: datetime.datetime
+ insight: ActiveInsight, newest_bucket: datetime.datetime | None, timezone: datetime.tzinfo, now: datetime.datetime
) -> bool:
"""The bucket after the newest scored one is complete, and this insight's slot after its end has passed."""
if newest_bucket is None:
@@ -413,7 +460,9 @@ def select_insights(
) -> Selection:
"""The picks of the tick. ``limit`` replaces the pick cap of each pass, for a manual run."""
teams = {team.id: team for team in eligible_teams(config)}
- insights = watched_insights(list(teams), config.intervals, now)
+ insights = active_insights(list(teams), config.intervals, now, config.active_window_days)
+ active = len(insights)
+ active_reasons = reason_counts(insights)
state: dict[int, dict[str, datetime.datetime]] = {}
failed_teams: set[int] = set()
# One query per team and interval, so a day or week insight does not read the months a month insight needs.
@@ -441,8 +490,8 @@ def select_insights(
for insight in insights
}
cooling = _cooling_down([insight.insight_id for insight in insights])
- watched_by_id = {insight.insight_id: insight for insight in insights if insight.insight_id not in cooling}
- team_ids = {insight_id: insight.team_id for insight_id, insight in watched_by_id.items()}
+ active_by_id = {insight.insight_id: insight for insight in insights if insight.insight_id not in cooling}
+ team_ids = {insight_id: insight.team_id for insight_id, insight in active_by_id.items()}
# Both passes share the plans, so an insight that the score pass planned is not planned again.
planned: dict[int, RunPlan] = {}
@@ -477,7 +526,7 @@ def select_insights(
score_skipped = 0
score_cap = config.max_score_insights_per_tick if limit is None else limit
if config.train_score_split:
- score_pass = _score_order(watched_by_id, teams, config, now)
+ score_pass = _score_order(active_by_id, teams, config, now)
score_order_ids, stale_order_ids, score_ready = score_pass.order, score_pass.stale_order, score_pass.ready
score_picked, score_skipped = walk(score_order_ids, ScoringRun.SCORE, score_cap)
# A spent score budget makes the walk load no plan. Plan the head of the order anyway, so that an
@@ -495,7 +544,7 @@ def select_insights(
due_by_id = {
insight_id: insight
- for insight_id, insight in watched_by_id.items()
+ for insight_id, insight in active_by_id.items()
if insight_id not in score_picked
and insight_id not in score_only
and is_due(insight, newest_buckets[insight_id], teams[insight.team_id].timezone_info, now)
@@ -557,7 +606,7 @@ def select_insights(
team_id=team_ids[insight_id],
insight_id=insight_id,
scored_through=state.get(insight_id, {}),
- interval=watched_by_id[insight_id].interval,
+ interval=active_by_id[insight_id].interval,
query_cost=plan.queries,
run=plan.run,
)
@@ -572,6 +621,8 @@ def select_insights(
scored_stale_model=len(stale_picked),
train_backlog_soft=sum(insight_id in stale_ready for insight_id in backlog),
train_backlog_hard=sum(insight_id not in stale_ready for insight_id in backlog),
+ active=active,
+ active_reasons=active_reasons,
)
@@ -581,7 +632,7 @@ def _as_score_step(plan: RunPlan) -> RunPlan:
def _score_order(
- watched_by_id: dict[int, WatchedInsight],
+ active_by_id: dict[int, ActiveInsight],
teams: dict[int, Team],
config: AnomalyScoringConfig,
now: datetime.datetime,
@@ -594,9 +645,9 @@ def _score_order(
team_orders: list[list[int]] = []
stale_orders: list[list[int]] = []
ready: set[int] = set()
- for team_id in sorted({insight.team_id for insight in watched_by_id.values()}):
+ for team_id in sorted({insight.team_id for insight in active_by_id.values()}):
team = teams[team_id]
- team_insights = [insight for insight in watched_by_id.values() if insight.team_id == team_id]
+ team_insights = [insight for insight in active_by_id.values() if insight.team_id == team_id]
scored = score_ready_by_insight(team, [insight.insight_id for insight in team_insights], config, now)
ready.update(insight_id for insight_id, state in scored.items() if not state.stale_model)
candidates = [
diff --git a/products/signals/backend/anomaly_scores/read.py b/products/signals/backend/anomaly_scores/read.py
index 3e87ea1fed94..3d84dd87edd9 100644
--- a/products/signals/backend/anomaly_scores/read.py
+++ b/products/signals/backend/anomaly_scores/read.py
@@ -30,7 +30,7 @@ from products.signals.backend.anomaly_scores.scoring import bucket_end
# The window before the requested range that `baseline_rate` covers.
BASELINE_WINDOW = datetime.timedelta(days=30)
-# One row is one series. Far more than the series of every watched insight of one project.
+# One row is one series. Far more than the series of every active insight of one project.
MAX_SERIES = 10_000
MAX_BUCKETS = 2_000
# Each rate shrinks toward its baseline by this many buckets, so 1 flagged of 1 scored does not top a ranking.
diff --git a/products/signals/backend/anomaly_scores/search.py b/products/signals/backend/anomaly_scores/search.py
index 44c68fc66d85..c5105ec987ec 100644
--- a/products/signals/backend/anomaly_scores/search.py
+++ b/products/signals/backend/anomaly_scores/search.py
@@ -22,7 +22,7 @@ from products.dashboards.backend.facade.api import dashboards_by_insight, live_t
from products.product_analytics.backend.facade.api import viewable_insights
from products.product_analytics.backend.facade.contracts import ViewableInsight
from products.signals.backend.anomaly_scores.config import MAX_LIMITS, covers_team, parse_config, read_payload
-from products.signals.backend.anomaly_scores.queue import watched_insights
+from products.signals.backend.anomaly_scores.queue import active_insights
from products.signals.backend.anomaly_scores.read import (
MAX_BUCKETS,
FlaggedPoint,
@@ -201,7 +201,7 @@ def _empty_message(*, scoring_enabled: bool, watched: int, scored_in_range: int)
if not scoring_enabled:
return SCORING_DISABLED_MESSAGE
if not scored_in_range:
- return f"No insight was scored in this range. Scoring checks {watched} watched insights, a few at a time."
+ return f"No insight was scored in this range. Scoring checks {watched} active insights, a few at a time."
return (
f"Nothing was flagged in this range among the {scored_in_range} insights scored. "
"Insights that were not scored in the range were not checked."
@@ -214,7 +214,7 @@ def search(team: Team, user: User, filters: AnomalySearchFilters, now: datetime.
scoring_enabled = covers_team(config, team)
requested = _requested_insight_ids(team, user, filters)
intervals = (filters.interval,) if filters.interval is not None else config.intervals
- watched = {insight.insight_id for insight in watched_insights([team.id], intervals, now)}
+ watched = {insight.insight_id for insight in active_insights([team.id], intervals, now, config.active_window_days)}
if requested is not None:
watched &= requested
diff --git a/products/signals/backend/anomaly_scores/tick.py b/products/signals/backend/anomaly_scores/tick.py
index dd9be1d251f8..d922b7ce6714 100644
--- a/products/signals/backend/anomaly_scores/tick.py
+++ b/products/signals/backend/anomaly_scores/tick.py
@@ -1,4 +1,4 @@
-"""The scheduled anomaly scoring tick: a Temporal workflow that scores the due watched insights.
+"""The scheduled anomaly scoring tick: a Temporal workflow that scores the due active insights.
The `anomaly-scoring` flag payload holds every lever, and a missing or malformed payload turns the
tick off. Each insight activity scores one insight and writes its new buckets as `$anomaly_score` events.
@@ -116,6 +116,8 @@ class AnomalyScoringTickPlan:
scored_stale_model: int = 0
train_backlog_soft: int = 0
train_backlog_hard: int = 0
+ active: int = 0
+ active_reasons: dict[str, int] | None = None
@frozen
@@ -161,6 +163,9 @@ class AnomalyScoringTickResult:
scored_stale_model: int = 0
train_backlog_soft: int = 0
train_backlog_hard: int = 0
+ # Active insights of the eligible teams, and how many of them have each activity reason.
+ active: int = 0
+ active_reasons: dict[str, int] | None = None
def plan_tick(limit: int | None, now: datetime.datetime) -> AnomalyScoringTickPlan:
@@ -183,6 +188,8 @@ def plan_tick(limit: int | None, now: datetime.datetime) -> AnomalyScoringTickPl
scored_stale_model=selection.scored_stale_model,
train_backlog_soft=selection.train_backlog_soft,
train_backlog_hard=selection.train_backlog_hard,
+ active=selection.active,
+ active_reasons=selection.active_reasons,
)
@@ -342,6 +349,8 @@ def capture_tick(result: AnomalyScoringTickResult) -> None:
scored_stale_model=result.scored_stale_model,
train_backlog_soft=result.train_backlog_soft,
train_backlog_hard=result.train_backlog_hard,
+ active=result.active,
+ active_reasons=result.active_reasons,
)
ph_background_capture()(
distinct_id=ANOMALY_SCORING_FLAG_DISTINCT_ID,
@@ -365,6 +374,8 @@ def capture_tick(result: AnomalyScoringTickResult) -> None:
"scored_stale_model": result.scored_stale_model,
"train_backlog_soft": result.train_backlog_soft,
"train_backlog_hard": result.train_backlog_hard,
+ "active_insights": result.active,
+ **{f"active_{reason}": count for reason, count in (result.active_reasons or {}).items()},
},
)
@@ -460,6 +471,8 @@ class AnomalyScoringTickWorkflow:
scored_stale_model=plan.scored_stale_model,
train_backlog_soft=plan.train_backlog_soft,
train_backlog_hard=plan.train_backlog_hard,
+ active=plan.active,
+ active_reasons=plan.active_reasons,
)
await workflow.execute_activity(
capture_anomaly_scoring_tick_activity,
diff --git a/products/signals/backend/presentation/anomalies.py b/products/signals/backend/presentation/anomalies.py
index 12516122e700..0726068a0333 100644
--- a/products/signals/backend/presentation/anomalies.py
+++ b/products/signals/backend/presentation/anomalies.py
@@ -220,7 +220,11 @@ class AnomalyInsightRowSerializer(serializers.Serializer):
class AnomalySearchResponseSerializer(serializers.Serializer):
scoring_enabled = serializers.BooleanField(help_text="Whether anomaly scoring covers this project.")
watched_insights = serializers.IntegerField(
- help_text="Insights that scoring watches: saved trends insights on a dashboard or viewed in the last 30 days."
+ help_text=(
+ "Active insights that scoring checks: saved trends insights that a person viewed, that sit on a "
+ "dashboard someone opened, or that were edited in the active window (90 days by default), "
+ "or that have an enabled alert or subscription."
+ )
)
insights_scored_in_range = serializers.IntegerField(
help_text="Insights with at least one scored bucket in the range. Results cover only these insights."
diff --git a/products/signals/backend/test/test_anomaly_scoring_tick.py b/products/signals/backend/test/test_anomaly_scoring_tick.py
index 6abd913e8e4b..f0b79a165e43 100644
--- a/products/signals/backend/test/test_anomaly_scoring_tick.py
+++ b/products/signals/backend/test/test_anomaly_scoring_tick.py
@@ -27,6 +27,7 @@ from posthog.exceptions import ClickHouseAtCapacity, ClickHouseClusterMemoryLimi
from posthog.models import Organization, Team, User
from posthog.tasks.alerts.detectors.base import ARTIFACT_SCHEMA_VERSION
+from products.alerts.backend.models.alert import AlertConfiguration
from products.dashboards.backend.models.dashboard import Dashboard
from products.dashboards.backend.models.dashboard_tile import DashboardTile
from products.product_analytics.backend.facade.api import record_insight_view
@@ -42,19 +43,20 @@ from products.signals.backend.anomaly_scores.config import (
from products.signals.backend.anomaly_scores.emission import SCORER_DISTINCT_ID, score_events
from products.signals.backend.anomaly_scores.queue import (
SKIP_COOLDOWN,
+ ActiveInsight,
+ ActivityReason,
InsightPick,
QueryBudget,
- WatchedInsight,
_cooldown_key,
_fleet_budget_key,
_project_budget_key,
+ active_insights,
eligible_teams,
is_due,
remaining_budget,
reserve_queries,
scored_buckets,
select_insights,
- watched_insights,
within_budget,
)
from products.signals.backend.anomaly_scores.scoring import (
@@ -81,6 +83,7 @@ from products.signals.backend.anomaly_scores.tick import (
run_insight,
)
from products.signals.backend.models import AnomalyScoringState
+from products.subscriptions.backend.facade.testing import create_subscription
NEW_YORK = ZoneInfo("America/New_York")
ORGANIZATION_ID = "4dc8564d-bd82-1065-2f40-97f7c50f67cf"
@@ -141,6 +144,7 @@ class TestTickConfig(SimpleTestCase):
"limits": {"max_backfill_buckets": 100_000, "max_series": -1},
"train_score_split": "true",
"train_every_buckets": {"hour": 100_000, "day": 0, "week": 2},
+ "active_window_days": "30",
}
)
@@ -156,6 +160,7 @@ class TestTickConfig(SimpleTestCase):
IntervalType.WEEK: 2,
IntervalType.MONTH: 3,
}
+ assert config.active_window_days == 90
@parameterized.expand(
[
@@ -167,6 +172,7 @@ class TestTickConfig(SimpleTestCase):
("max_score_queries_per_project_per_hour", 1001, 1000),
("max_score_insights_per_tick", 201, 200),
("max_train_insights_per_tick", 51, 50),
+ ("active_window_days", 1000, 365),
]
)
def test_tick_levers_above_their_maximum_parse_to_the_maximum(self, field: str, value: int, expected: int) -> None:
@@ -397,7 +403,7 @@ class TestTickSelection(SimpleTestCase):
now: datetime,
due: bool,
) -> None:
- insight = WatchedInsight(team_id=1, insight_id=7, interval=interval)
+ insight = ActiveInsight(team_id=1, insight_id=7, interval=interval)
assert is_due(insight, newest_bucket, timezone, now) is due
@@ -676,38 +682,115 @@ class TestEligibleTeams(BaseTest):
assert eligible == {teams[name].id for name in expected}
-class TestWatchedInsights(BaseTest):
+_DEFAULT_INTERVALS = (IntervalType.DAY, IntervalType.WEEK, IntervalType.MONTH)
+_LONG_AGO = NOW - timedelta(days=91)
+
+
+class TestActiveInsights(BaseTest):
@time_machine.travel(NOW, tick=False)
- def test_only_watched_time_series_trends_with_a_scored_interval_are_eligible(self) -> None:
- dashboard = Dashboard.objects.create(team=self.team, name="Main")
+ def test_only_time_series_trends_with_a_scored_interval_are_eligible(self) -> None:
insights = {
name: Insight.objects.create(team=self.team, name=name, query=query)
for name, query in {
- "on_dashboard": _trends(interval="day"),
- "viewed": _trends(interval="week"),
+ "day": _trends(interval="day"),
+ "week": _trends(interval="week"),
"default_interval": _trends(),
"hourly": _trends(interval="hour"),
"funnel": {"kind": "InsightVizNode", "source": {"kind": "FunnelsQuery", "series": []}},
"bold_number": _trends(interval="day", trendsFilter={"display": "BoldNumber"}),
"cumulative": _trends(interval="day", trendsFilter={"display": "ActionsLineGraphCumulative"}),
- "deleted_tile": _trends(interval="day"),
- "unwatched": _trends(interval="day"),
}.items()
}
- for name in ("on_dashboard", "default_interval", "hourly", "funnel", "bold_number", "cumulative"):
- DashboardTile.objects.create(dashboard=dashboard, insight=insights[name])
- DashboardTile.objects.create(dashboard=dashboard, insight=insights["deleted_tile"], deleted=True)
- record_insight_view(insight_id=insights["viewed"].id, team_id=self.team.id, user_id=self.user.id)
- watched = watched_insights([self.team.id], (IntervalType.DAY, IntervalType.WEEK, IntervalType.MONTH), NOW)
+ active = active_insights([self.team.id], _DEFAULT_INTERVALS, NOW, 90)
names = {insight.id: name for name, insight in insights.items()}
- assert {(names[insight.insight_id], insight.interval) for insight in watched} == {
- ("on_dashboard", IntervalType.DAY),
- ("viewed", IntervalType.WEEK),
+ assert {(names[insight.insight_id], insight.interval) for insight in active} == {
+ ("day", IntervalType.DAY),
+ ("week", IntervalType.WEEK),
("default_interval", IntervalType.DAY),
}
+ @parameterized.expand(
+ [
+ ("viewed", {ActivityReason.VIEWED}),
+ ("viewed_anonymously", set()),
+ ("viewed_before_the_window", set()),
+ ("on_an_accessed_dashboard", {ActivityReason.DASHBOARD_ACCESSED}),
+ ("on_a_dashboard_nobody_opened", set()),
+ ("on_a_dashboard_opened_before_the_window", set()),
+ ("hidden_tile_on_an_accessed_dashboard", set()),
+ ("enabled_alert", {ActivityReason.ENABLED_ALERT}),
+ ("disabled_alert", set()),
+ ("subscription", {ActivityReason.ACTIVE_SUBSCRIPTION}),
+ ("dashboard_subscription", {ActivityReason.ACTIVE_SUBSCRIPTION}),
+ ("disabled_subscription", set()),
+ ("edited", {ActivityReason.EDITED}),
+ ("viewed_and_edited", {ActivityReason.VIEWED, ActivityReason.EDITED}),
+ ]
+ )
+ @time_machine.travel(NOW, tick=False)
+ def test_an_insight_is_active_only_with_a_signal_in_the_window(
+ self, signal: str, expected: set[ActivityReason]
+ ) -> None:
+ insight = Insight.objects.create(team=self.team, name="insight", query=_trends(interval="day"))
+ Insight.objects.filter(id=insight.id).update(last_modified_at=_LONG_AGO)
+ dashboard = Dashboard.objects.create(team=self.team, name="Main", last_accessed_at=NOW - timedelta(days=1))
+
+ if signal in ("viewed", "viewed_and_edited"):
+ record_insight_view(insight_id=insight.id, team_id=self.team.id, user_id=self.user.id)
+ elif signal == "viewed_anonymously":
+ record_insight_view(insight_id=insight.id, team_id=self.team.id, user_id=None)
+ elif signal == "viewed_before_the_window":
+ with time_machine.travel(_LONG_AGO, tick=False):
+ record_insight_view(insight_id=insight.id, team_id=self.team.id, user_id=self.user.id)
+ elif signal == "on_an_accessed_dashboard":
+ DashboardTile.objects.create(dashboard=dashboard, insight=insight)
+ elif signal in ("on_a_dashboard_nobody_opened", "on_a_dashboard_opened_before_the_window"):
+ dashboard.last_accessed_at = None if signal == "on_a_dashboard_nobody_opened" else _LONG_AGO
+ dashboard.save(update_fields=["last_accessed_at"])
+ DashboardTile.objects.create(dashboard=dashboard, insight=insight)
+ elif signal == "hidden_tile_on_an_accessed_dashboard":
+ DashboardTile.objects.create(dashboard=dashboard, insight=insight, deleted=True)
+ elif signal in ("enabled_alert", "disabled_alert"):
+ AlertConfiguration.objects.create(
+ team=self.team, insight=insight, name="alert", enabled=signal == "enabled_alert"
+ )
+ elif signal in ("subscription", "disabled_subscription"):
+ create_subscription(team_id=self.team.id, insight_id=insight.id, enabled=signal == "subscription")
+ elif signal == "dashboard_subscription":
+ dashboard.last_accessed_at = None
+ dashboard.save(update_fields=["last_accessed_at"])
+ DashboardTile.objects.create(dashboard=dashboard, insight=insight)
+ create_subscription(team_id=self.team.id, dashboard_id=dashboard.id)
+ if signal in ("edited", "viewed_and_edited"):
+ Insight.objects.filter(id=insight.id).update(last_modified_at=NOW - timedelta(days=1))
+
+ active = active_insights([self.team.id], _DEFAULT_INTERVALS, NOW, 90)
+
+ assert {insight.insight_id: set(insight.reasons) for insight in active} == (
+ {insight.id: expected} if expected else {}
+ )
+
+ def test_an_insight_drops_out_when_its_signal_ages_out_and_comes_back_with_a_new_one(self) -> None:
+ insight = Insight.objects.create(team=self.team, name="insight", query=_trends(interval="day"))
+ Insight.objects.filter(id=insight.id).update(last_modified_at=_LONG_AGO)
+ with time_machine.travel(NOW - timedelta(days=29), tick=False):
+ record_insight_view(insight_id=insight.id, team_id=self.team.id, user_id=self.user.id)
+
+ assert [active.insight_id for active in active_insights([self.team.id], _DEFAULT_INTERVALS, NOW, 30)] == [
+ insight.id
+ ]
+ later = NOW + timedelta(days=2)
+ assert active_insights([self.team.id], _DEFAULT_INTERVALS, later, 30) == []
+
+ with time_machine.travel(later, tick=False):
+ record_insight_view(insight_id=insight.id, team_id=self.team.id, user_id=self.user.id)
+
+ assert [active.insight_id for active in active_insights([self.team.id], _DEFAULT_INTERVALS, later, 30)] == [
+ insight.id
+ ]
+
class TestScoredBuckets(ClickhouseTestMixin, BaseTest):
def test_reads_the_newest_scored_bucket_of_each_series(self) -> None:
@@ -751,11 +834,13 @@ class TestScoredBuckets(ClickhouseTestMixin, BaseTest):
dashboard = Dashboard.objects.create(team=self.team, name="Main")
insights = {
name: Insight.objects.create(team=self.team, name=name, query=_trends(interval="day"))
- for name in ("never_scored", "stale", "fresh", "past_day_window")
+ for name in ("never_scored", "stale", "fresh", "past_day_window", "inactive")
}
for insight in insights.values():
DashboardTile.objects.create(dashboard=dashboard, insight=insight)
cache.delete(_cooldown_key(insight.id))
+ # Never scored, so it would go first, but nobody opened its dashboard and nobody touched it in the window.
+ Insight.objects.filter(id=insights["inactive"].id).update(last_modified_at=_LONG_AGO)
cache.delete_many([_fleet_budget_key(NOW), _project_budget_key(self.team.id, NOW)])
today = NOW.replace(hour=0)
for name, bucket in (
@@ -784,6 +869,10 @@ class TestScoredBuckets(ClickhouseTestMixin, BaseTest):
picks = {pick.insight_id: pick for pick in selection.picks}
assert set(picks) == {insights[name].id for name in ("never_scored", "stale", "past_day_window")}
+ assert (selection.active, selection.active_reasons) == (
+ 4,
+ {reason.value: 4 if reason == ActivityReason.EDITED else 0 for reason in ActivityReason},
+ )
assert picks[insights["stale"].id].scored_through == {"stale": today - timedelta(days=3)}
assert picks[insights["past_day_window"].id].scored_through == {}
diff --git a/products/signals/frontend/generated/api.schemas.ts b/products/signals/frontend/generated/api.schemas.ts
index 0d1ce76b909e..7964334c3a77 100644
--- a/products/signals/frontend/generated/api.schemas.ts
+++ b/products/signals/frontend/generated/api.schemas.ts
@@ -614,7 +614,7 @@ export interface AnomalyInsightRowApi {
export interface AnomalySearchResponseApi {
/** Whether anomaly scoring covers this project. */
scoring_enabled: boolean
- /** Insights that scoring watches: saved trends insights on a dashboard or viewed in the last 30 days. */
+ /** Active insights that scoring checks: saved trends insights that a person viewed, that sit on a dashboard someone opened, or that were edited in the active window (90 days by default), or that have an enabled alert or subscription. */
watched_insights: number
/** Insights with at least one scored bucket in the range. Results cover only these insights. */
insights_scored_in_range: number
diff --git a/products/subscriptions/backend/facade/api.py b/products/subscriptions/backend/facade/api.py
index 3ee47fe2c005..8efe6ab3a840 100644
--- a/products/subscriptions/backend/facade/api.py
+++ b/products/subscriptions/backend/facade/api.py
@@ -202,6 +202,11 @@ def insight_has_active_subscription(insight_id: int) -> bool:
return queries.insight_has_active_subscription(insight_id)
+def insight_ids_with_active_subscriptions(*, team_ids: Collection[int]) -> set[int]:
+ """Insights of these teams that an enabled subscription delivers, directly or as a live dashboard tile."""
+ return queries.insight_ids_with_active_subscriptions(team_ids=team_ids)
+
+
def dashboard_has_active_full_subscription(dashboard_id: int) -> bool:
"""Whether an enabled subscription delivers every insight on this dashboard."""
return queries.dashboard_has_active_full_subscription(dashboard_id)
diff --git a/products/subscriptions/backend/logic/queries.py b/products/subscriptions/backend/logic/queries.py
index b44735b9ddf8..f99e1e911390 100644
--- a/products/subscriptions/backend/logic/queries.py
+++ b/products/subscriptions/backend/logic/queries.py
@@ -1,7 +1,7 @@
from collections.abc import Collection
from datetime import datetime
-from django.db.models import Count, Max, QuerySet
+from django.db.models import Count, Max, Q, QuerySet
from products.subscriptions.backend.facade.contracts import FailedDeliveryCount
from products.subscriptions.backend.models.subscription import Subscription, SubscriptionDelivery
@@ -75,6 +75,31 @@ def insight_has_active_subscription(insight_id: int) -> bool:
)
+def insight_ids_with_active_subscriptions(*, team_ids: Collection[int]) -> set[int]:
+ """The insights that an enabled subscription of these teams delivers today.
+
+ The three cases of `insight_has_active_subscription` apply, but only live tiles of a live dashboard
+ count, because a hidden tile is not delivered.
+ """
+ if not team_ids:
+ return set()
+ active = Subscription.objects.filter(team_id__in=team_ids, deleted=False, enabled=True)
+ insight_ids = set(active.filter(insight_id__isnull=False).values_list("insight_id", flat=True))
+ insight_ids.update(
+ active.filter(dashboard_export_insights__deleted=False).values_list("dashboard_export_insights", flat=True)
+ )
+ insight_ids.update(
+ active.exclude(dashboard_export_insights__deleted=False)
+ .filter(
+ Q(dashboard__tiles__deleted=False) | Q(dashboard__tiles__deleted__isnull=True),
+ dashboard__deleted=False,
+ dashboard__tiles__insight_id__isnull=False,
+ )
+ .values_list("dashboard__tiles__insight_id", flat=True)
+ )
+ return insight_ids
+
+
def dashboard_has_active_full_subscription(dashboard_id: int) -> bool:
"""Decide if an enabled subscription delivers every insight on this dashboard. Such a
subscription also delivers an insight that is added after the subscription was saved.
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 032c00362ff0..2c2418779cf0 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -13667,7 +13667,7 @@ export namespace Schemas {
export interface AnomalySearchResponse {
/** Whether anomaly scoring covers this project. */
scoring_enabled: boolean;
- /** Insights that scoring watches: saved trends insights on a dashboard or viewed in the last 30 days. */
+ /** Active insights that scoring checks: saved trends insights that a person viewed, that sit on a dashboard someone opened, or that were edited in the active window (90 days by default), or that have an enabled alert or subscription. */
watched_insights: number;
/** Insights with at least one scored bucket in the range. Results cover only these insights. */
insights_scored_in_range: number;
diff --git a/tach.toml b/tach.toml
index 9169ea139cff..0e7d1dca350e 100644
--- a/tach.toml
+++ b/tach.toml
@@ -946,6 +946,8 @@ depends_on = [
# SignalSourceConfig row, so onboarding has to ask it whether any scanner is emitting.
"products.replay_vision",
"products.skills",
+ # Facade-only: anomaly scoring counts an insight that an enabled subscription delivers as active.
+ "products.subscriptions",
"products.surveys",
"products.tasks",
"products.warehouse_sources",