: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
feat(dags): add max_batches knob to get_all_team_ids_op (#58169)
Robbief0a33ff27156
diff --git a/posthog/dags/common/ops.py b/posthog/dags/common/ops.pyindex 0befa9371909..c27c00ff0be1 100644--- a/posthog/dags/common/ops.py+++ b/posthog/dags/common/ops.py@@ -39,6 +39,12 @@ def _filter_team_ids_for_rollout(team_ids: list[int], rollout_percentage: float)is_required=False,description="Number of team IDs per batch.",),+ "max_batches": dagster.Field(+ dagster.Int,+ default_value=0,+ is_required=False,+ description="Cap the number of batches yielded. 0 means unlimited. Use to bound peak parallelism / memory.",+ ),"rollout_percentage": dagster.Field(dagster.Float,default_value=1.0,@@ -59,11 +65,15 @@ def get_all_team_ids_op(context: dagster.OpExecutionContext):override_team_ids = context.op_config["team_ids"]batch_size = context.op_config.get("batch_size", 1000)+ max_batches = int(context.op_config.get("max_batches", 0))rollout_percentage = float(context.op_config.get("rollout_percentage", 1.0))if rollout_percentage < 0 or rollout_percentage > 1:raise ValueError(f"rollout_percentage must be in [0, 1], got {rollout_percentage}")+ if max_batches < 0:+ raise ValueError(f"max_batches must be >= 0, got {max_batches}")+if override_team_ids:team_ids = override_team_idscontext.log.info(f"Processing {len(team_ids)} configured teams: {team_ids}")@@ -75,6 +85,11 @@ def get_all_team_ids_op(context: dagster.OpExecutionContext):team_ids = _filter_team_ids_for_rollout(team_ids, rollout_percentage)context.log.info(f"After rollout ({rollout_percentage}%), processing {len(team_ids)} teams")+ yielded = 0for i in range(0, len(team_ids), batch_size):+ if max_batches and yielded >= max_batches:+ context.log.info(f"Reached max_batches={max_batches}; stopping at team index {i} of {len(team_ids)}")+ breakbatch = team_ids[i : i + batch_size]yield dagster.DynamicOutput(batch, mapping_key=f"batch_{i // batch_size}")+ yielded += 1diff --git a/posthog/dags/tests/test_common_ops.py b/posthog/dags/tests/test_common_ops.pynew file mode 100644index 000000000000..08ef04d6ef7d--- /dev/null+++ b/posthog/dags/tests/test_common_ops.py@@ -0,0 +1,43 @@+import pytest++from dagster import build_op_context+from parameterized import parameterized++from posthog.dags.common.ops import get_all_team_ids_op+++def _collect_batches(op_config: dict) -> list[list[int]]:+ return [output.value for output in get_all_team_ids_op(build_op_context(op_config=op_config))]+++class TestGetAllTeamIdsOp:+ @parameterized.expand(+ [+ (+ "default_yields_all_batches",+ {"team_ids": [1, 2, 3, 4, 5], "batch_size": 2},+ [[1, 2], [3, 4], [5]],+ ),+ (+ "max_batches_truncates_to_first_n",+ {"team_ids": [1, 2, 3, 4, 5, 6, 7], "batch_size": 2, "max_batches": 2},+ [[1, 2], [3, 4]],+ ),+ (+ "max_batches_exceeding_total_yields_all",+ {"team_ids": [1, 2, 3], "batch_size": 1, "max_batches": 100},+ [[1], [2], [3]],+ ),+ (+ "max_batches_zero_means_unlimited",+ {"team_ids": [1, 2, 3, 4, 5], "batch_size": 1, "max_batches": 0},+ [[1], [2], [3], [4], [5]],+ ),+ ]+ )+ def test_batches(self, _name: str, op_config: dict, expected: list[list[int]]):+ assert _collect_batches(op_config) == expected++ def test_negative_max_batches_rejected(self):+ with pytest.raises(ValueError, match="max_batches must be >= 0"):+ _collect_batches({"team_ids": [1, 2, 3], "batch_size": 1, "max_batches": -1})