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.

chore(flags): move the flag edit history read to the flags facade (#123583)

Gustavo H. Strassburgerca2779b2a3ef
diff --git a/products/experiments/backend/health/exposure_gate.py b/products/experiments/backend/health/exposure_gate.py
index abb39c9beb43..c2d7251cbd3e 100644
--- a/products/experiments/backend/health/exposure_gate.py
+++ b/products/experiments/backend/health/exposure_gate.py
@@ -26,8 +26,6 @@ from time import monotonic
from typing import Any
from django.core.cache import cache
-from django.db.models import F, Func, JSONField, Q, Value
-from django.db.models.fields.json import KT
from django.utils import timezone
import pydantic
@@ -48,7 +46,6 @@ from posthog.exceptions import (
ClickHouseQueryTimeOut,
)
from posthog.exceptions_capture import capture_exception
-from posthog.models.activity_logging.activity_log import ActivityLog
from products.experiments.backend.facade.contracts import ExperimentHealthFlagChangeKind, ExperimentHealthReaderAccess
from products.experiments.backend.health.bucketing_changes import BucketingChangeShape, bucketing_net_effect
@@ -78,6 +75,7 @@ from products.experiments.backend.hogql_queries.exposure_query_logic import (
resolve_default_exposure_event,
)
from products.experiments.backend.models.experiment import Experiment
+from products.feature_flags.backend.facade.edit_history import FlagEdit, flag_edits_since
from products.feature_flags.backend.facade.edits import (
BucketingChange,
BucketingSetting,
@@ -114,13 +112,6 @@ _KINDS: dict[ExperimentHealthFlagChangeKind, type[FlagChange]] = {
ExperimentHealthFlagChangeKind.BUCKETING: BucketingChange,
ExperimentHealthFlagChangeKind.RELEASE_CONDITIONS: ConditionsChange,
}
-# The activity log fields of a flag save that can move exposed people.
-_FILTERS = "filters"
-_SETTING_FIELDS = {
- BucketingSetting.KEY: "key",
- BucketingSetting.BUCKETING_IDENTIFIER: "bucketing_identifier",
- BucketingSetting.EXPERIENCE_CONTINUITY: "ensure_experience_continuity",
-}
# Events can arrive after the window ends, so a "nobody" answer for a recent window can still change.
_SETTLED_AFTER = timedelta(hours=1)
# A recent "nobody" is kept briefly, so page loads in that hour do not each run the scan again.
@@ -478,11 +469,13 @@ def _load_flag_history(
changes=(),
)
- rows = _flag_rows_since(team_id=experiment.team_id, feature_flag_id=feature_flag_id, since=launched_at)
- too_large = _too_large(rows)
+ edits = flag_edits_since(
+ team_id=experiment.team_id, feature_flag_id=feature_flag_id, since=launched_at, limit=MAX_FLAG_SAVES + 1
+ )
+ too_large = _too_large(edits)
if too_large is not None:
return FlagHistoryNotChecked(reason=too_large, launched_at=launched_at, team_timezone=team_timezone, changes=())
- read = _flag_changes(rows, feature_flag_id=feature_flag_id)
+ read = _flag_changes(edits, feature_flag_id=feature_flag_id)
flag = experiment.feature_flag
trust_flag_aggregation = conditions_follow_flag_aggregation(flag.aggregation_group_type_index, flag.conditions)
bucketing = changes_of_kind(read, BucketingChange)
@@ -719,106 +712,45 @@ def _exposed_during_overrides(
return {window: _exposed_between(gate, timeline, start=window.start, end=window.end) for window in asked}
-_PROJECTED = (
- "before_variants",
- "after_variants",
- "before_groups",
- "after_groups",
- "before_aggregation",
- "after_aggregation",
- *(f"{side}_{setting}" for side in ("before", "after") for setting in _SETTING_FIELDS),
-)
-
-
-def _flag_rows_since(*, team_id: int, feature_flag_id: int, since: datetime) -> list[dict[str, Any]]:
- return list(
- ActivityLog.objects.filter(
- _changes_any_field(_FILTERS, *_SETTING_FIELDS.values()),
- team_id=team_id,
- scope="FeatureFlag",
- item_id=str(feature_flag_id),
- created_at__gt=since,
- )
- .order_by("created_at")
- # Only these values decide who gets which variant, and the whole `detail` of a flag edit can be large.
- .annotate(
- job_type=KT("detail__trigger__job_type"),
- before_variants=_changed(_FILTERS, "before.multivariate.variants"),
- after_variants=_changed(_FILTERS, "after.multivariate.variants"),
- before_groups=_changed(_FILTERS, "before.groups"),
- after_groups=_changed(_FILTERS, "after.groups"),
- before_aggregation=_changed(_FILTERS, "before.aggregation_group_type_index"),
- after_aggregation=_changed(_FILTERS, "after.aggregation_group_type_index"),
- **{f"before_{setting}": _changed(field, "before") for setting, field in _SETTING_FIELDS.items()},
- **{f"after_{setting}": _changed(field, "after") for setting, field in _SETTING_FIELDS.items()},
- )
- .values("created_at", "job_type", *_PROJECTED)[: MAX_FLAG_SAVES + 1]
- )
-
-
-def _too_large(rows: list[dict[str, Any]]) -> FlagHistoryNotCheckedReason | None:
- if len(rows) > MAX_FLAG_SAVES:
+def _too_large(edits: tuple[FlagEdit, ...]) -> FlagHistoryNotCheckedReason | None:
+ if len(edits) > MAX_FLAG_SAVES:
return FlagHistoryNotCheckedReason.TOO_MANY_SAVES
- for row in rows:
- for groups in (row["before_groups"], row["after_groups"]):
+ for edit in edits:
+ for groups in (edit.before.groups, edit.after.groups):
if isinstance(groups, list) and len(groups) > MAX_RELEASE_CONDITIONS:
return FlagHistoryNotCheckedReason.TOO_MANY_CONDITIONS
return None
-def _flag_changes(rows: list[dict[str, Any]], *, feature_flag_id: int) -> tuple[FlagChange, ...]:
+def _flag_changes(edits: tuple[FlagEdit, ...], *, feature_flag_id: int) -> tuple[FlagChange, ...]:
changes: list[FlagChange] = []
- for row in rows:
- changed_at = row["created_at"]
+ for edit in edits:
split_change = split_change_from_activity(
- changed_at=changed_at,
- job_type=row["job_type"],
- before_variants=row["before_variants"],
- after_variants=row["after_variants"],
+ changed_at=edit.changed_at,
+ job_type=edit.job_type,
+ before_variants=edit.before.variants,
+ after_variants=edit.after.variants,
)
if split_change is not None:
changes.append(split_change)
changes.extend(
bucketing_changes_from_activity(
- changed_at=changed_at,
+ changed_at=edit.changed_at,
feature_flag_id=feature_flag_id,
- before=_settings(row, "before"),
- after=_settings(row, "after"),
+ before=edit.before.bucketing_settings,
+ after=edit.after.bucketing_settings,
)
)
conditions_change = conditions_change_from_activity(
- changed_at=changed_at,
- job_type=row["job_type"],
- before_groups=row["before_groups"],
- after_groups=row["after_groups"],
- before_aggregation=row["before_aggregation"],
- after_aggregation=row["after_aggregation"],
- before_variants=row["before_variants"],
- after_variants=row["after_variants"],
+ changed_at=edit.changed_at,
+ job_type=edit.job_type,
+ before_groups=edit.before.groups,
+ after_groups=edit.after.groups,
+ before_aggregation=edit.before.aggregation,
+ after_aggregation=edit.after.aggregation,
+ before_variants=edit.before.variants,
+ after_variants=edit.after.variants,
)
if conditions_change is not None:
changes.append(conditions_change)
return tuple(changes)
-
-
-def _settings(row: dict[str, object], side: str) -> dict[BucketingSetting, object]:
- return {
- BucketingSetting.AGGREGATION: row[f"{side}_aggregation"],
- **{setting: row[f"{side}_{setting}"] for setting in _SETTING_FIELDS},
- }
-
-
-def _changes_any_field(*fields: str) -> Q:
- query = Q()
- for field in fields:
- query |= Q(detail__contains={"changes": [{"field": field}]})
- return query
-
-
-def _changed(field: str, path: str) -> Func:
- return Func(
- F("detail"),
- Value(f'$.changes[*] ? (@.field == "{field}").{path}'),
- function="jsonb_path_query_first",
- output_field=JSONField(),
- )
diff --git a/products/feature_flags/backend/facade/edit_history.py b/products/feature_flags/backend/facade/edit_history.py
new file mode 100644
index 000000000000..1d678fef94da
--- /dev/null
+++ b/products/feature_flags/backend/facade/edit_history.py
@@ -0,0 +1,132 @@
+"""The edits of a feature flag, read from its activity log.
+
+An edit keeps only the values that decide who gets which variant: the variant split, the release conditions, the
+flag-level aggregation, and the bucketing settings. Pass them to ``facade.edits`` to learn what the edit did.
+"""
+
+from datetime import datetime
+from typing import Any
+
+from django.db.models import F, Func, JSONField, Q, Value
+from django.db.models.fields.json import KT
+
+from posthog.dataclasses import frozen
+from posthog.models.activity_logging.activity_log import ActivityLog
+
+from products.feature_flags.backend.facade.edits import BucketingSetting
+
+# The activity log fields of a flag save that can change who gets which value.
+_FILTERS = "filters"
+_SETTING_FIELDS = {
+ BucketingSetting.KEY: "key",
+ BucketingSetting.BUCKETING_IDENTIFIER: "bucketing_identifier",
+ BucketingSetting.EXPERIENCE_CONTINUITY: "ensure_experience_continuity",
+}
+_PROJECTED = (
+ "before_variants",
+ "after_variants",
+ "before_groups",
+ "after_groups",
+ "before_aggregation",
+ "after_aggregation",
+ *(f"{side}_{setting}" for side in ("before", "after") for setting in _SETTING_FIELDS),
+)
+
+
+@frozen
+class FlagEditSide:
+ """
+ The JSON values on one side of an edit. A value is None when the edit does not change its field, or when that
+ side has no value for it.
+ """
+
+ variants: object
+ groups: object
+ aggregation: object
+ key: object
+ bucketing_identifier: object
+ experience_continuity: object
+
+ @property
+ def bucketing_settings(self) -> dict[BucketingSetting, object]:
+ return {
+ BucketingSetting.KEY: self.key,
+ BucketingSetting.AGGREGATION: self.aggregation,
+ BucketingSetting.BUCKETING_IDENTIFIER: self.bucketing_identifier,
+ BucketingSetting.EXPERIENCE_CONTINUITY: self.experience_continuity,
+ }
+
+
+@frozen
+class FlagEdit:
+ changed_at: datetime
+ # The `job_type` of the activity log trigger, or None when the trigger has no job type.
+ job_type: str | None
+ before: FlagEditSide
+ after: FlagEditSide
+
+
+def flag_edits_since(*, team_id: int, feature_flag_id: int, since: datetime, limit: int) -> tuple[FlagEdit, ...]:
+ """
+ The edits of the flag after `since` that change a value in `FlagEditSide`, oldest first, at most `limit`. Pass
+ one more than the cap you need, to know when the flag has more edits than the cap.
+ """
+ rows = (
+ ActivityLog.objects.filter(
+ _changes_any_field(_FILTERS, *_SETTING_FIELDS.values()),
+ team_id=team_id,
+ scope="FeatureFlag",
+ item_id=str(feature_flag_id),
+ created_at__gt=since,
+ )
+ .order_by("created_at")
+ # Only these values decide who gets which variant, and the whole `detail` of a flag edit can be large.
+ .annotate(
+ job_type=KT("detail__trigger__job_type"),
+ before_variants=_changed(_FILTERS, "before.multivariate.variants"),
+ after_variants=_changed(_FILTERS, "after.multivariate.variants"),
+ before_groups=_changed(_FILTERS, "before.groups"),
+ after_groups=_changed(_FILTERS, "after.groups"),
+ before_aggregation=_changed(_FILTERS, "before.aggregation_group_type_index"),
+ after_aggregation=_changed(_FILTERS, "after.aggregation_group_type_index"),
+ **{f"before_{setting}": _changed(field, "before") for setting, field in _SETTING_FIELDS.items()},
+ **{f"after_{setting}": _changed(field, "after") for setting, field in _SETTING_FIELDS.items()},
+ )
+ .values("created_at", "job_type", *_PROJECTED)[:limit]
+ )
+ return tuple(
+ FlagEdit(
+ changed_at=row["created_at"],
+ job_type=row["job_type"],
+ before=_side(row, "before"),
+ after=_side(row, "after"),
+ )
+ for row in rows
+ )
+
+
+def _side(row: dict[str, Any], side: str) -> FlagEditSide:
+ return FlagEditSide(
+ variants=row[f"{side}_variants"],
+ groups=row[f"{side}_groups"],
+ aggregation=row[f"{side}_aggregation"],
+ key=row[f"{side}_{BucketingSetting.KEY}"],
+ bucketing_identifier=row[f"{side}_{BucketingSetting.BUCKETING_IDENTIFIER}"],
+ experience_continuity=row[f"{side}_{BucketingSetting.EXPERIENCE_CONTINUITY}"],
+ )
+
+
+def _changes_any_field(*fields: str) -> Q:
+ query = Q()
+ for field in fields:
+ query |= Q(detail__contains={"changes": [{"field": field}]})
+ return query
+
+
+def _changed(field: str, path: str) -> Func:
+ return Func(
+ F("detail"),
+ Value(f'$.changes[*] ? (@.field == "{field}").{path}'),
+ function="jsonb_path_query_first",
+ output_field=JSONField(),
+ )