: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(today): show routine insights after two days while view history is short (#118540)
Shy Altere1e60feafa8a
diff --git a/products/today/backend/logic/highlight_sources/routine.py b/products/today/backend/logic/highlight_sources/routine.pyindex 8e1aa4676f13..ce732f1c4182 100644--- a/products/today/backend/logic/highlight_sources/routine.py+++ b/products/today/backend/logic/highlight_sources/routine.py@@ -17,6 +17,9 @@ from ..highlight_types import Context, Found, SourceROUTINE_WINDOW_DAYS = 28DAILY_ROUTINE_DAYS = 8+WARMUP_ROUTINE_DAYS = 2+WARMUP_HISTORY_DAYS = 14+VIEW_DAYS_RECORDED_SINCE = date(2026, 10, 16)WEEKLY_ROUTINE_WEEKS = 3ROUTINE_CANDIDATES = 6ROUTINE_TILES = 2@@ -33,13 +36,19 @@ def find(context: Context) -> Found:return Found(pulse=tuple(metrics))-def routine_days(view_days: Sequence[date], today: date) -> int:+def routine_days(view_days: Sequence[date], today: date, warming_up: bool = False) -> int:recent = {day for day in view_days if today - timedelta(days=ROUTINE_WINDOW_DAYS) < day <= today}- daily = sum(1 for day in recent if day > today - timedelta(days=14)) >= DAILY_ROUTINE_DAYS+ needed = WARMUP_ROUTINE_DAYS if warming_up else DAILY_ROUTINE_DAYS+ daily = sum(1 for day in recent if day > today - timedelta(days=14)) >= neededweekly = len({day.isocalendar()[:2] for day in recent}) >= WEEKLY_ROUTINE_WEEKSreturn len(recent) if daily or weekly else 0+def is_warming_up(today: date) -> bool:+ """Whether view days have been recorded for too short a time for the daily rule."""+ return (today - VIEW_DAYS_RECORDED_SINCE).days < WARMUP_HISTORY_DAYS++def _routine_insights(context: Context) -> list[str]:views = FileSystemViewLog.objects.filter(team=context.team,@@ -47,8 +56,10 @@ def _routine_insights(context: Context) -> list[str]:type="insight",viewed_at__gte=timezone.now() - timedelta(days=ROUTINE_WINDOW_DAYS),).values_list("ref", "view_days", "viewed_at")+ warming_up = is_warming_up(context.today)ranked = sorted(- ((routine_days(days or [], context.today), viewed_at, ref) for ref, days, viewed_at in views), reverse=True+ ((routine_days(days or [], context.today, warming_up), viewed_at, ref) for ref, days, viewed_at in views),+ reverse=True,)return [ref for days, _viewed_at, ref in ranked if days][:ROUTINE_CANDIDATES]diff --git a/products/today/backend/tests/test_highlight_insights.py b/products/today/backend/tests/test_highlight_insights.pyindex e3b50525796c..16e701144580 100644--- a/products/today/backend/tests/test_highlight_insights.py+++ b/products/today/backend/tests/test_highlight_insights.py@@ -38,14 +38,27 @@ def insight(series: dict[str, list[tuple[date, float]]], **fields: Any) -> Insigclass TestInsightRules(SimpleTestCase):@parameterized.expand([- ("opened on most days", [TODAY - timedelta(days=d) for d in range(10)], 10),- ("opened every Monday", [TODAY - timedelta(days=1 + 7 * w) for w in range(4)], 4),- ("opened on many days of one week", [TODAY - timedelta(days=d) for d in range(5)], 0),- ("a habit that stopped a month ago", [TODAY - timedelta(days=30 + 7 * w) for w in range(4)], 0),+ ("opened on most days", [TODAY - timedelta(days=d) for d in range(10)], False, 10),+ ("opened every Monday", [TODAY - timedelta(days=1 + 7 * w) for w in range(4)], False, 4),+ ("opened on many days of one week", [TODAY - timedelta(days=d) for d in range(5)], False, 0),+ ("a habit that stopped a month ago", [TODAY - timedelta(days=30 + 7 * w) for w in range(4)], False, 0),+ ("opened on two days while history is short", [TODAY, TODAY - timedelta(days=3)], True, 2),+ ("opened on one day while history is short", [TODAY], True, 0),])- def test_routine_days(self, _name: str, view_days: list[date], expected: int) -> None:- self.assertEqual(routine.routine_days(view_days, TODAY), expected)+ def test_routine_days(self, _name: str, view_days: list[date], warming_up: bool, expected: int) -> None:+ self.assertEqual(routine.routine_days(view_days, TODAY, warming_up), expected)++ @parameterized.expand(+ [+ ("the day recording started", date(2026, 10, 16), True),+ ("the last warm-up day", date(2026, 10, 29), True),+ ("two weeks after recording started", date(2026, 10, 30), False),+ ("months later", date(2027, 3, 1), False),+ ]+ )+ def test_is_warming_up(self, _name: str, today: date, expected: bool) -> None:+ self.assertEqual(routine.is_warming_up(today), expected)@parameterized.expand([