: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
import re
from datetime import datetime, timedelta
from django.utils import timezone
from products.experiments.backend.facade.api import (
count_enrolled_people,
list_ended_experiments_without_conclusion,
list_running_experiments,
)
from products.experiments.backend.facade.contracts import EndedExperiment, RunningExperiment
from products.today.backend.facade.contracts import HighlightCandidate, HighlightContext, HighlightFindings
from products.today.backend.facade.enums import HighlightLabel
from products.today.backend.facade.highlight_helpers import short, users
ENDED = "Experiment ‘{experiment}’ ended {when}"
ENDED_DETAIL = "Read the results and decide whether to release the change"
FEW_USERS = "Experiment ‘{experiment}’ reached {enrolled} in the last 14 days"
NO_USERS = "Experiment ‘{experiment}’ reached no users in the last 14 days"
FEW_USERS_DETAIL = "Stop it or increase the rollout"
ENDED_WINDOW_DAYS = 7
LONG_RUNNING_DAYS = 60
JUST_CROSSED_DAYS = 67
MIN_PEOPLE = 100
def _days_since(context: HighlightContext, moment: datetime) -> int:
return (context.today - timezone.localtime(moment, context.team.timezone_info).date()).days
def _when(days: int) -> str:
return "today" if days <= 0 else "yesterday" if days == 1 else f"{days} days ago"
def _name(name: str, limit: int) -> str:
return short(re.sub(r"(?i)\s+experiment$", "", name) or name, limit)
def _ended(context: HighlightContext, experiment: EndedExperiment) -> HighlightCandidate:
return HighlightCandidate(
key=f"experiment_ended:{experiment.id}",
label=HighlightLabel.OPPORTUNITY,
family="experiments",
importance=0.6,
relevance=1.0,
title=ENDED.format(
experiment=_name(experiment.name, 60), when=_when(_days_since(context, experiment.ended_at))
),
detail=ENDED_DETAIL,
url=f"/experiments/{experiment.id}",
)
def _few_users(experiment: RunningExperiment, enrolled: int) -> HighlightCandidate:
return HighlightCandidate(
key=f"experiment_stale:{experiment.id}",
label=HighlightLabel.RISK,
family="experiments",
importance=0.3,
relevance=1.0,
title=(FEW_USERS if enrolled else NO_USERS).format(
experiment=_name(experiment.name, 50), enrolled=users(enrolled)
),
detail=FEW_USERS_DETAIL,
url=f"/experiments/{experiment.id}",
)
def find_experiment_highlights(context: HighlightContext) -> HighlightFindings:
now = timezone.now()
ended = list_ended_experiments_without_conclusion(
team_id=context.team.id, user_id=context.user.id, since=now - timedelta(days=ENDED_WINDOW_DAYS), limit=20
)
running = list_running_experiments(
team_id=context.team.id,
user_id=context.user.id,
started_after=None if context.today.weekday() == 0 else now - timedelta(days=JUST_CROSSED_DAYS),
started_before=now - timedelta(days=LONG_RUNNING_DAYS),
limit=100,
)
enrolled = context.guard(
"experiments_enrolled",
lambda: count_enrolled_people(
team=context.team, user=context.user, flag_keys=[experiment.flag_key for experiment in running]
),
None,
)
counted = [] if enrolled is None else [(experiment, enrolled.get(experiment.flag_key, 0)) for experiment in running]
return HighlightFindings(
items=(
*(_ended(context, experiment) for experiment in ended),
*(_few_users(experiment, people) for experiment, people in counted if people < MIN_PEOPLE),
)
)