mirror of
https://github.com/hanzoai/insights.git
synced 2026-11-03 00:52:32 +00:00
The tree was a debranded snapshot of upstream taken around 2026-02-20, and upstream has moved about five months since. This replaces it with upstream master, mechanically re-derived: bin/debrand does the rename, and the pipeline and squashed migrations are restored from the shipped tree. Not a merge. The previous import replaced the tree wholesale in a single commit, so there is no shared content history to merge against. Reproducing the rename on the new tree is what actually converges. What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints, compose files, LLM.md, and insights/migrations. The squashed initial migration is load-bearing — the live database records it by name, so upstream's ~918 individual migrations cannot come back. Nine products are dropped as unreferenced. Twenty-one more that we do not run are kept anyway because the core app still imports them; unpicking those is a refactor and does not belong in a rebase. This base does not build yet, and the reasons are not in the rename: upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against our 4.2, and the dependency manifests still name upstream packages that we republish under our own names. Those are decisions, not renames.
72 lines
2.8 KiB
Python
72 lines
2.8 KiB
Python
from insights.schema import InsightsQLQueryModifiers
|
|
|
|
from insights.insightsql import ast
|
|
from insights.insightsql.query import execute_insightsql_query
|
|
|
|
from insights.datastore.query_tagging import Feature, Product, tags_context
|
|
from insights.models.person.person import Person
|
|
from insights.models.person.util import get_persons_by_uuids
|
|
from insights.models.team import Team
|
|
from insights.personinsights_client.caller_tag import personinsights_caller_tag
|
|
|
|
# Case-insensitive batch email lookup. Exposed so tests can EXPLAIN the exact query that runs.
|
|
# Identified persons sort first: several persons can share an email (e.g. an identified app
|
|
# user plus an email-only stub whose sole distinct_id is the address itself), and only the
|
|
# identified one carries distinct_ids that membership and analytics lookups can resolve.
|
|
# created_at prefers the oldest person, and id breaks same-millisecond ties so the
|
|
# pick is fully deterministic.
|
|
PERSON_EMAIL_LOOKUP_QUERY = """
|
|
SELECT id, properties.email
|
|
FROM persons
|
|
WHERE lower(properties.email) IN {emails}
|
|
ORDER BY is_identified DESC, created_at ASC, id ASC
|
|
"""
|
|
|
|
|
|
def _get_persons_by_email(
|
|
team: Team,
|
|
emails: list[str],
|
|
modifiers: InsightsQLQueryModifiers | None = None,
|
|
) -> dict[str, Person]:
|
|
"""Batch look up persons by their properties.email value via Datastore.
|
|
|
|
Returns a dict mapping lowercase email -> Person, preferring identified
|
|
persons over unidentified ones (oldest first within each). Only checks
|
|
``properties.email`` (the canonical, materialized key with a skip index).
|
|
Uses the InsightsQL ``persons`` virtual table (argMax dedup handled
|
|
automatically).
|
|
"""
|
|
if not emails:
|
|
return {}
|
|
|
|
emails_lower = [e.lower() for e in emails]
|
|
with tags_context(product=Product.CONVERSATIONS, feature=Feature.QUERY):
|
|
response = execute_insightsql_query(
|
|
PERSON_EMAIL_LOOKUP_QUERY,
|
|
placeholders={"emails": ast.Constant(value=emails_lower)},
|
|
team=team,
|
|
query_type="conversations_person_email_lookup",
|
|
modifiers=modifiers,
|
|
)
|
|
|
|
if not response.results:
|
|
return {}
|
|
|
|
email_to_uuid: dict[str, str] = {}
|
|
for person_uuid, prop_email in response.results:
|
|
if prop_email:
|
|
lower = prop_email.lower()
|
|
if lower not in email_to_uuid:
|
|
email_to_uuid[lower] = str(person_uuid)
|
|
|
|
with personinsights_caller_tag("conversations/email-person-lookup"):
|
|
persons = get_persons_by_uuids(team.pk, list(email_to_uuid.values()))
|
|
uuid_to_person: dict[str, Person] = {str(p.uuid): p for p in persons}
|
|
|
|
result: dict[str, Person] = {}
|
|
for email_lower, person_uuid in email_to_uuid.items():
|
|
person = uuid_to_person.get(person_uuid)
|
|
if person is not None:
|
|
result[email_lower] = person
|
|
return result
|