: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(django): split personhog rpcs into batched calls (#60307)
Nick Best31da26d110f7
diff --git a/posthog/models/person/test/test_batched_personhog_helpers.py b/posthog/models/person/test/test_batched_personhog_helpers.pynew file mode 100644index 000000000000..cf61b52e8064--- /dev/null+++ b/posthog/models/person/test/test_batched_personhog_helpers.py@@ -0,0 +1,275 @@+from unittest.mock import MagicMock, patch++from django.test import SimpleTestCase++from parameterized import parameterized++from posthog.models.person.util import (+ _batched_get_distinct_ids_for_persons,+ _batched_get_persons_by_distinct_ids,+ _batched_get_persons_by_uuids,+ get_persons_mapped_by_distinct_id,+)+from posthog.personhog_client.fake_client import fake_personhog_client+from posthog.personhog_client.proto.generated.personhog.types.v1 import person_pb2+++class TestBatchedGetPersonsByUuids(SimpleTestCase):+ @parameterized.expand(+ [+ ("single_batch", 500, 2, 1),+ ("multiple_batches", 2, 5, 3),+ ]+ )+ def test_returns_all_persons(self, _name, batch_size, n_persons, expected_calls):+ with patch("posthog.models.person.util.PERSONHOG_BATCH_SIZE", batch_size):+ with fake_personhog_client() as fake:+ for i in range(n_persons):+ fake.add_person(team_id=1, person_id=i + 1, uuid=f"uuid-{i}", distinct_ids=[f"d{i}"])++ result = _batched_get_persons_by_uuids(1, [f"uuid-{i}" for i in range(n_persons)], "test")++ assert len(result) == n_persons+ assert {p.uuid for p in result} == {f"uuid-{i}" for i in range(n_persons)}+ fake.assert_called("get_persons_by_uuids", times=expected_calls)++ def test_empty_input(self):+ with fake_personhog_client():+ result = _batched_get_persons_by_uuids(1, [], "test")++ assert result == []++ def test_filters_wrong_team(self):+ with fake_personhog_client() as fake:+ fake.add_person(team_id=1, person_id=1, uuid="uuid-1", distinct_ids=["d1"])+ fake.add_person(team_id=999, person_id=2, uuid="uuid-2", distinct_ids=["d2"])++ result = _batched_get_persons_by_uuids(1, ["uuid-1", "uuid-2"], "test")++ assert len(result) == 1+ assert result[0].uuid == "uuid-1"++ def test_filters_persons_with_no_id(self):+ with fake_personhog_client() as fake:+ fake.add_person(team_id=1, person_id=0, uuid="uuid-zero", distinct_ids=["d0"])+ fake.add_person(team_id=1, person_id=1, uuid="uuid-1", distinct_ids=["d1"])++ result = _batched_get_persons_by_uuids(1, ["uuid-zero", "uuid-1"], "test")++ assert len(result) == 1+ assert result[0].uuid == "uuid-1"++ @patch("posthog.models.person.util.PERSONHOG_BATCH_SIZE", 2)+ def test_team_mismatch_metric_across_batches(self):+ wrong_team_person = person_pb2.Person(id=2, uuid="uuid-1", team_id=999)+ right_team_person_1 = person_pb2.Person(id=1, uuid="uuid-0", team_id=1)+ right_team_person_2 = person_pb2.Person(id=3, uuid="uuid-2", team_id=1)+ wrong_team_person_2 = person_pb2.Person(id=4, uuid="uuid-3", team_id=888)++ mock_client = MagicMock()+ mock_client.get_persons_by_uuids.side_effect = [+ person_pb2.PersonsResponse(persons=[right_team_person_1, wrong_team_person]),+ person_pb2.PersonsResponse(persons=[right_team_person_2, wrong_team_person_2]),+ ]++ with (+ patch("posthog.models.person.util._get_client", return_value=mock_client),+ patch("posthog.models.person.util.PERSONHOG_TEAM_MISMATCH_TOTAL") as mock_metric,+ ):+ result = _batched_get_persons_by_uuids(1, ["uuid-0", "uuid-1", "uuid-2", "uuid-3"], "test_op")++ assert len(result) == 2+ assert mock_metric.labels.call_count == 2+ mock_metric.labels.assert_any_call(operation="test_op", client_name="posthog-django")++ @patch("posthog.models.person.util.PERSONHOG_BATCH_SIZE", 2)+ def test_preserves_order_across_batches(self):+ with fake_personhog_client() as fake:+ for i in range(4):+ fake.add_person(team_id=1, person_id=i + 1, uuid=f"uuid-{i}", distinct_ids=[f"d{i}"])++ result = _batched_get_persons_by_uuids(1, ["uuid-0", "uuid-1", "uuid-2", "uuid-3"], "test")++ assert [p.uuid for p in result] == ["uuid-0", "uuid-1", "uuid-2", "uuid-3"]++ def test_missing_uuids_excluded(self):+ with fake_personhog_client() as fake:+ fake.add_person(team_id=1, person_id=1, uuid="uuid-1", distinct_ids=["d1"])++ result = _batched_get_persons_by_uuids(1, ["uuid-1", "uuid-missing"], "test")++ assert len(result) == 1+ assert result[0].uuid == "uuid-1"+++class TestBatchedGetPersonsByDistinctIds(SimpleTestCase):+ @parameterized.expand(+ [+ ("single_batch", 500, 2, 1),+ ("multiple_batches", 2, 5, 3),+ ]+ )+ def test_returns_all_persons(self, _name, batch_size, n_persons, expected_calls):+ with patch("posthog.models.person.util.PERSONHOG_BATCH_SIZE", batch_size):+ with fake_personhog_client() as fake:+ for i in range(n_persons):+ fake.add_person(team_id=1, person_id=i + 1, uuid=f"uuid-{i}", distinct_ids=[f"did-{i}"])++ result = _batched_get_persons_by_distinct_ids(1, [f"did-{i}" for i in range(n_persons)], "test")++ assert len(result) == n_persons+ assert {r.distinct_id for r in result} == {f"did-{i}" for i in range(n_persons)}+ fake.assert_called("get_persons_by_distinct_ids_in_team", times=expected_calls)++ def test_empty_input(self):+ with fake_personhog_client():+ result = _batched_get_persons_by_distinct_ids(1, [], "test")++ assert result == []++ @parameterized.expand(+ [+ ("single_batch", 500, 2, 1),+ ("across_batches", 2, 3, 1),+ ]+ )+ def test_deduplicates_same_person(self, _name, batch_size, n_distinct_ids, expected_result_count):+ with patch("posthog.models.person.util.PERSONHOG_BATCH_SIZE", batch_size):+ with fake_personhog_client() as fake:+ dids = [f"did-{i}" for i in range(n_distinct_ids)]+ fake.add_person(team_id=1, person_id=42, uuid="uuid-42", distinct_ids=dids)++ result = _batched_get_persons_by_distinct_ids(1, dids, "test")++ assert len(result) == expected_result_count+ assert result[0].person.id == 42++ @parameterized.expand(+ [+ ("single_batch", 500, 2),+ ("across_batches", 2, 3),+ ]+ )+ def test_no_dedup_returns_all_entries(self, _name, batch_size, n_distinct_ids):+ with patch("posthog.models.person.util.PERSONHOG_BATCH_SIZE", batch_size):+ with fake_personhog_client() as fake:+ dids = [f"did-{i}" for i in range(n_distinct_ids)]+ fake.add_person(team_id=1, person_id=42, uuid="uuid-42", distinct_ids=dids)++ result = _batched_get_persons_by_distinct_ids(1, dids, "test", deduplicate_by_person=False)++ assert len(result) == n_distinct_ids+ assert {r.distinct_id for r in result} == set(dids)+ assert all(r.person.id == 42 for r in result)++ def test_filters_wrong_team(self):+ with fake_personhog_client() as fake:+ fake.add_person(team_id=1, person_id=1, uuid="uuid-1", distinct_ids=["did-1"])+ fake.add_person(team_id=999, person_id=2, uuid="uuid-2", distinct_ids=["did-2"])++ result = _batched_get_persons_by_distinct_ids(1, ["did-1", "did-2"], "test")++ assert len(result) == 1+ assert result[0].person.uuid == "uuid-1"++ def test_missing_distinct_ids_excluded(self):+ with fake_personhog_client() as fake:+ fake.add_person(team_id=1, person_id=1, uuid="uuid-1", distinct_ids=["did-1"])++ result = _batched_get_persons_by_distinct_ids(1, ["did-1", "did-missing"], "test")++ assert len(result) == 1+++class TestBatchedGetDistinctIdsForPersons(SimpleTestCase):+ @parameterized.expand(+ [+ ("single_batch", 500, 2, 1),+ ("multiple_batches", 2, 5, 3),+ ]+ )+ def test_returns_all_distinct_ids(self, _name, batch_size, n_persons, expected_calls):+ with patch("posthog.models.person.util.PERSONHOG_BATCH_SIZE", batch_size):+ with fake_personhog_client() as fake:+ for i in range(n_persons):+ fake.add_person(team_id=1, person_id=i + 1, uuid=f"uuid-{i}", distinct_ids=[f"d{i}"])++ result = _batched_get_distinct_ids_for_persons(1, list(range(1, n_persons + 1)))++ assert len(result) == n_persons+ for i in range(n_persons):+ assert result[i + 1] == [f"d{i}"]+ fake.assert_called("get_distinct_ids_for_persons", times=expected_calls)++ def test_empty_input(self):+ with fake_personhog_client():+ result = _batched_get_distinct_ids_for_persons(1, [])++ assert result == {}++ @parameterized.expand(+ [+ ("single_batch", 500, 1),+ ("across_batches", 2, 3),+ ]+ )+ def test_limit_per_person(self, _name, batch_size, n_persons):+ with patch("posthog.models.person.util.PERSONHOG_BATCH_SIZE", batch_size):+ with fake_personhog_client() as fake:+ for i in range(n_persons):+ fake.add_person(+ team_id=1, person_id=i + 1, uuid=f"uuid-{i}", distinct_ids=[f"d{i}a", f"d{i}b", f"d{i}c"]+ )++ result = _batched_get_distinct_ids_for_persons(1, list(range(1, n_persons + 1)), limit_per_person=1)++ assert all(len(dids) == 1 for dids in result.values())++ def test_limit_per_person_none_returns_all(self):+ with fake_personhog_client() as fake:+ fake.add_person(team_id=1, person_id=1, uuid="uuid-1", distinct_ids=["d1", "d2", "d3"])++ result = _batched_get_distinct_ids_for_persons(1, [1])++ assert len(result[1]) == 3++ def test_missing_person_gets_empty_list(self):+ with fake_personhog_client() as fake:+ fake.add_person(team_id=1, person_id=1, uuid="uuid-1", distinct_ids=["d1"])++ result = _batched_get_distinct_ids_for_persons(1, [1, 999])++ assert result[1] == ["d1"]+ assert result[999] == []+++class TestGetPersonsMappedByDistinctIdDedup(SimpleTestCase):+ def test_multiple_distinct_ids_same_person_all_present(self):+ with fake_personhog_client() as fake:+ fake.add_person(+ team_id=1, person_id=42, uuid="550e8400-e29b-41d4-a716-446655440042", distinct_ids=["did-1", "did-2"]+ )++ result = get_persons_mapped_by_distinct_id(1, ["did-1", "did-2"])++ assert len(result) == 2+ assert "did-1" in result+ assert "did-2" in result+ assert result["did-1"].id == 42+ assert result["did-2"].id == 42+ assert result["did-1"].distinct_ids == ["did-1"]+ assert result["did-2"].distinct_ids == ["did-2"]++ @patch("posthog.models.person.util.PERSONHOG_BATCH_SIZE", 2)+ def test_multiple_distinct_ids_same_person_across_batches(self):+ with fake_personhog_client() as fake:+ fake.add_person(team_id=1, person_id=42, uuid="uuid-42", distinct_ids=["did-1", "did-2", "did-3"])++ results = _batched_get_persons_by_distinct_ids(+ 1, ["did-1", "did-2", "did-3"], "test", deduplicate_by_person=False+ )++ assert len(results) == 3+ assert {r.distinct_id for r in results} == {"did-1", "did-2", "did-3"}+ assert all(r.person.id == 42 for r in results)+ fake.assert_called("get_persons_by_distinct_ids_in_team", times=2)diff --git a/posthog/models/person/util.py b/posthog/models/person/util.pyindex 9673b4d0fbfd..c2acaca75ada 100644--- a/posthog/models/person/util.py+++ b/posthog/models/person/util.py@@ -1,8 +1,10 @@+from __future__ import annotations+import jsonimport datetimefrom collections.abc import Callablefrom contextlib import ExitStack-from typing import Optional, TypeVar, Union, cast+from typing import TYPE_CHECKING, Optional, TypeVar, Union, castfrom uuid import UUIDfrom zoneinfo import ZoneInfo@@ -48,6 +50,99 @@ from posthog.settings import TESTlogger = structlog.get_logger(__name__)+PERSONHOG_BATCH_SIZE = 500+++if TYPE_CHECKING:+ from posthog.personhog_client.proto.generated.personhog.types.v1 import person_pb2+++def _get_client():+ from posthog.personhog_client.client import get_personhog_client++ client = get_personhog_client()+ if client is None:+ raise RuntimeError("personhog client not configured")+ return client+++def _batched_get_persons_by_uuids(+ team_id: int,+ uuids: list[str],+ operation: str,+) -> list[person_pb2.Person]:+ client = _get_client()+ valid_persons: list[person_pb2.Person] = []+ for i in range(0, len(uuids), PERSONHOG_BATCH_SIZE):+ batch = uuids[i : i + PERSONHOG_BATCH_SIZE]+ resp = client.get_persons_by_uuids(GetPersonsByUuidsRequest(team_id=team_id, uuids=batch))++ present_persons = [p for p in resp.persons if p.id]+ batch_valid = [p for p in present_persons if p.team_id == team_id]++ mismatched = len(present_persons) - len(batch_valid)+ if mismatched:+ PERSONHOG_TEAM_MISMATCH_TOTAL.labels(operation=operation, client_name=get_client_name()).inc(mismatched)+ logger.warning("personhog_team_mismatch", operation=operation, team_id=team_id, dropped=mismatched)++ valid_persons.extend(batch_valid)++ return valid_persons+++def _batched_get_persons_by_distinct_ids(+ team_id: int,+ distinct_ids: list[str],+ operation: str,+ deduplicate_by_person: bool = True,+) -> list[person_pb2.PersonWithDistinctIds]:+ client = _get_client()+ seen_person_ids: set[int] = set()+ valid_results: list[person_pb2.PersonWithDistinctIds] = []++ for i in range(0, len(distinct_ids), PERSONHOG_BATCH_SIZE):+ batch = distinct_ids[i : i + PERSONHOG_BATCH_SIZE]+ resp = client.get_persons_by_distinct_ids_in_team(+ GetPersonsByDistinctIdsInTeamRequest(team_id=team_id, distinct_ids=batch)+ )++ present_results = [r for r in resp.results if r.person and r.person.id]+ batch_valid = [r for r in present_results if r.person.team_id == team_id]++ mismatched = len(present_results) - len(batch_valid)+ if mismatched:+ PERSONHOG_TEAM_MISMATCH_TOTAL.labels(operation=operation, client_name=get_client_name()).inc(mismatched)+ logger.warning("personhog_team_mismatch", operation=operation, team_id=team_id, dropped=mismatched)++ if deduplicate_by_person:+ for r in batch_valid:+ if r.person.id not in seen_person_ids:+ seen_person_ids.add(r.person.id)+ valid_results.append(r)+ else:+ valid_results.extend(batch_valid)++ return valid_results+++def _batched_get_distinct_ids_for_persons(+ team_id: int,+ person_ids: list[int],+ limit_per_person: int | None = None,+) -> dict[int, list[str]]:+ client = _get_client()+ distinct_ids_by_person: dict[int, list[str]] = {}+ for i in range(0, len(person_ids), PERSONHOG_BATCH_SIZE):+ batch_ids = person_ids[i : i + PERSONHOG_BATCH_SIZE]+ did_request = GetDistinctIdsForPersonsRequest(team_id=team_id, person_ids=batch_ids)+ if limit_per_person is not None:+ did_request.limit_per_person = limit_per_person+ did_resp = client.get_distinct_ids_for_persons(did_request)+ for pd in did_resp.person_distinct_ids:+ distinct_ids_by_person[pd.person_id] = [d.distinct_id for d in pd.distinct_ids]+ return distinct_ids_by_person++if TEST:# :KLUDGE: Hooks are kept around for tests. All other code goes through plugin-server or the other methods explicitly@@ -217,50 +312,15 @@ def create_person_distinct_id(def _fetch_persons_by_distinct_ids_via_personhog(team_id: int, distinct_ids: list[str], *, distinct_id_limit: int | None = None) -> list[Person]:- from posthog.personhog_client.client import get_personhog_client-- client = get_personhog_client()- if client is None:- raise RuntimeError("personhog client not configured")-- resp = client.get_persons_by_distinct_ids_in_team(- GetPersonsByDistinctIdsInTeamRequest(team_id=team_id, distinct_ids=distinct_ids)- )-- present_results = [r for r in resp.results if r.person and r.person.id]- valid_results = [r for r in present_results if r.person.team_id == team_id]-- mismatched = len(present_results) - len(valid_results)- if mismatched:- PERSONHOG_TEAM_MISMATCH_TOTAL.labels(- operation="get_persons_by_distinct_ids", client_name=get_client_name()- ).inc(mismatched)- logger.warning(- "personhog_team_mismatch", operation="get_persons_by_distinct_ids", team_id=team_id, dropped=mismatched- )-- # The RPC returns one result per distinct_id, so the same person can- # appear multiple times. Deduplicate by person_id to return unique persons.- seen_person_ids: set[int] = set()- unique_results = []- for r in valid_results:- if r.person.id not in seen_person_ids:- seen_person_ids.add(r.person.id)- unique_results.append(r)- valid_results = unique_results+ valid_results = _batched_get_persons_by_distinct_ids(team_id, distinct_ids, "get_persons_by_distinct_ids")person_ids = [r.person.id for r in valid_results]if not person_ids:return []- did_request = GetDistinctIdsForPersonsRequest(team_id=team_id, person_ids=person_ids)- if distinct_id_limit is not None:- did_request.limit_per_person = distinct_id_limit- distinct_ids_resp = client.get_distinct_ids_for_persons(did_request)-- distinct_ids_by_person: dict[int, list[str]] = {}- for pd in distinct_ids_resp.person_distinct_ids:- distinct_ids_by_person[pd.person_id] = [d.distinct_id for d in pd.distinct_ids]+ distinct_ids_by_person = _batched_get_distinct_ids_for_persons(+ team_id, person_ids, limit_per_person=distinct_id_limit+ )return [proto_person_to_model(r.person, distinct_ids=distinct_ids_by_person.get(r.person.id, [])) for r in valid_results@@ -374,31 +434,9 @@ def get_persons_mapped_by_distinct_id(return resultdef personhog_fn() -> dict[str, Person]:- from posthog.personhog_client.client import get_personhog_client-- client = get_personhog_client()- if client is None:- raise RuntimeError("personhog client not configured")-- resp = client.get_persons_by_distinct_ids_in_team(- GetPersonsByDistinctIdsInTeamRequest(team_id=team_id, distinct_ids=distinct_ids)+ valid_results = _batched_get_persons_by_distinct_ids(+ team_id, distinct_ids, "get_persons_mapped_by_distinct_id", deduplicate_by_person=False)-- present_results = [r for r in resp.results if r.person and r.person.id]- valid_results = [r for r in present_results if r.person.team_id == team_id]-- mismatched = len(present_results) - len(valid_results)- if mismatched:- PERSONHOG_TEAM_MISMATCH_TOTAL.labels(- operation="get_persons_mapped_by_distinct_id", client_name=get_client_name()- ).inc(mismatched)- logger.warning(- "personhog_team_mismatch",- operation="get_persons_mapped_by_distinct_id",- team_id=team_id,- dropped=mismatched,- )-return {r.distinct_id: proto_person_to_model(r.person, distinct_ids=[r.distinct_id]) for r in valid_results}return _personhog_routed(@@ -410,35 +448,13 @@ def get_persons_mapped_by_distinct_id(def _fetch_persons_by_uuids_via_personhog(team_id: int, uuids: list[str]) -> list[Person]:- from posthog.personhog_client.client import get_personhog_client-- client = get_personhog_client()- if client is None:- raise RuntimeError("personhog client not configured")-- resp = client.get_persons_by_uuids(GetPersonsByUuidsRequest(team_id=team_id, uuids=uuids))-- present_persons = [p for p in resp.persons if p.id]- valid_persons = [p for p in present_persons if p.team_id == team_id]-- mismatched = len(present_persons) - len(valid_persons)- if mismatched:- PERSONHOG_TEAM_MISMATCH_TOTAL.labels(operation="get_persons_by_uuids", client_name=get_client_name()).inc(- mismatched- )- logger.warning("personhog_team_mismatch", operation="get_persons_by_uuids", team_id=team_id, dropped=mismatched)+ valid_persons = _batched_get_persons_by_uuids(team_id, uuids, "get_persons_by_uuids")person_ids = [p.id for p in valid_persons]if not person_ids:return []- distinct_ids_resp = client.get_distinct_ids_for_persons(- GetDistinctIdsForPersonsRequest(team_id=team_id, person_ids=person_ids)- )-- distinct_ids_by_person: dict[int, list[str]] = {}- for pd in distinct_ids_resp.person_distinct_ids:- distinct_ids_by_person[pd.person_id] = [d.distinct_id for d in pd.distinct_ids]+ distinct_ids_by_person = _batched_get_distinct_ids_for_persons(team_id, person_ids)return [proto_person_to_model(p, distinct_ids=distinct_ids_by_person.get(p.id, [])) for p in valid_persons]@@ -570,26 +586,10 @@ def get_person_by_pk_or_uuid(team_id: int, key: str) -> Optional[Person]:def _validate_uuids_via_personhog(team_id: int, uuids: list[str]) -> list[str]:- from posthog.personhog_client.client import get_personhog_client-- client = get_personhog_client()- if client is None:- raise RuntimeError("personhog client not configured")-- resp = client.get_persons_by_uuids(GetPersonsByUuidsRequest(team_id=team_id, uuids=uuids))- valid = [p for p in resp.persons if p.team_id == team_id]- mismatched = len(resp.persons) - len(valid)- if mismatched:- PERSONHOG_TEAM_MISMATCH_TOTAL.labels(- operation="validate_person_uuids_exist", client_name=get_client_name()- ).inc(mismatched)- logger.warning(- "personhog_team_mismatch",- operation="validate_person_uuids_exist",- team_id=team_id,- dropped=mismatched,- )- return [p.uuid for p in valid]+ # _batched_get_persons_by_uuids also filters out persons with id == 0 (server "not found" sentinel),+ # which the previous single-RPC implementation did not do. This is intentionally more correct.+ valid_persons = _batched_get_persons_by_uuids(team_id, uuids, "validate_person_uuids_exist")+ return [p.uuid for p in valid_persons]def validate_person_uuids_exist(team_id: int, uuids: list[str]) -> list[str]:diff --git a/posthog/queries/actor_base_query.py b/posthog/queries/actor_base_query.pyindex c8970215d280..9cb602642e0c 100644--- a/posthog/queries/actor_base_query.py+++ b/posthog/queries/actor_base_query.py@@ -19,15 +19,9 @@ from posthog.models.filters.stickiness_filter import StickinessFilterfrom posthog.models.group import Groupfrom posthog.models.person import Personfrom posthog.models.person.person import READ_DB_FOR_PERSONS-from posthog.personhog_client.client import get_personhog_client+from posthog.models.person.util import _batched_get_distinct_ids_for_persons, _batched_get_persons_by_uuidsfrom posthog.personhog_client.converters import proto_person_to_model-from posthog.personhog_client.metrics import (- PERSONHOG_ROUTING_ERRORS_TOTAL,- PERSONHOG_ROUTING_TOTAL,- PERSONHOG_TEAM_MISMATCH_TOTAL,- get_client_name,-)-from posthog.personhog_client.proto import GetDistinctIdsForPersonsRequest, GetPersonsByUuidsRequest+from posthog.personhog_client.metrics import PERSONHOG_ROUTING_ERRORS_TOTAL, PERSONHOG_ROUTING_TOTAL, get_client_namefrom posthog.queries.insight import insight_sync_executelogger = structlog.get_logger(__name__)@@ -268,32 +262,16 @@ def get_groups(def _fetch_people_via_personhog(team_id: int, people_ids: list[Any], distinct_id_limit: int | None = 1000) -> list[Person]:- client = get_personhog_client()- if client is None:- raise RuntimeError("personhog client not configured")-uuids = [str(pid) for pid in people_ids]- resp = client.get_persons_by_uuids(GetPersonsByUuidsRequest(team_id=team_id, uuids=uuids))-- present_persons = [p for p in resp.persons if p.id]- valid_persons = [p for p in present_persons if p.team_id == team_id]-- mismatched = len(present_persons) - len(valid_persons)- if mismatched:- PERSONHOG_TEAM_MISMATCH_TOTAL.labels(operation="get_people", client_name=get_client_name()).inc(mismatched)- logger.warning("personhog_team_mismatch", operation="get_people", team_id=team_id, dropped=mismatched)+ valid_persons = _batched_get_persons_by_uuids(team_id, uuids, "get_people")person_ids = [p.id for p in valid_persons]if not person_ids:return []- request = GetDistinctIdsForPersonsRequest(team_id=team_id, person_ids=person_ids)- if distinct_id_limit is not None:- request.limit_per_person = distinct_id_limit- distinct_ids_resp = client.get_distinct_ids_for_persons(request)- distinct_ids_by_person: dict[int, list[str]] = {}- for pd in distinct_ids_resp.person_distinct_ids:- distinct_ids_by_person[pd.person_id] = [d.distinct_id for d in pd.distinct_ids]+ distinct_ids_by_person = _batched_get_distinct_ids_for_persons(+ team_id, person_ids, limit_per_person=distinct_id_limit+ )persons = [proto_person_to_model(p, distinct_ids=distinct_ids_by_person.get(p.id, [])) for p in valid_persons]persons.sort(key=lambda p: (-(p.created_at.timestamp() if p.created_at else 0), str(p.uuid)))