perf: optimize revenue forecast effective document links

This commit is contained in:
plx
2026-08-14 12:06:26 +00:00
parent 69116f620c
commit 91ba85fc92
3 changed files with 188 additions and 24 deletions

View File

@@ -181,28 +181,78 @@ def prepare_effective_document_links(conn: Any) -> str:
result sets. The temporary table is session-local and is populated solely
through the group-aware resolver.
"""
opportunity_ids = {str(row[0]) for row in conn.execute(text("""
SELECT DISTINCT opportunity_id::text FROM commercial_documents
WHERE opportunity_id IS NOT NULL
""")).all()}
if document_reconciliation_v2_available(conn):
opportunity_ids.update(str(row[0]) for row in conn.execute(text("""
SELECT DISTINCT opportunity_id::text FROM opportunity_document_links
WHERE ended_at IS NULL
""")).all())
conn.execute(text("""CREATE TEMP TABLE IF NOT EXISTS _effective_document_links (
opportunity_id UUID NOT NULL, document_id UUID NOT NULL, document_kind TEXT NOT NULL,
relationship TEXT NOT NULL, ended_at TIMESTAMPTZ) ON COMMIT DROP"""))
conn.execute(text("TRUNCATE _effective_document_links"))
for opportunity_id in sorted(opportunity_ids):
for row in resolve_document_links(opportunity_id, conn=conn):
conn.execute(text("""INSERT INTO _effective_document_links
(opportunity_id,document_id,document_kind,relationship,ended_at)
VALUES(CAST(:oid AS UUID),CAST(:did AS UUID),:kind,:relationship,:ended_at)"""),
{"oid": opportunity_id, "did": row["document_id"],
"kind": row.get("document_kind") or "unknown",
"relationship": row.get("relationship") or "SECONDARY",
"ended_at": row.get("ended_at")})
legacy_relationship = """CASE
WHEN d.is_active IS FALSE OR lower(COALESCE(NULLIF(d.role, ''), 'current')) = 'detached' THEN 'REMOVED'
WHEN d.is_primary IS TRUE AND lower(COALESCE(NULLIF(d.role, ''), 'current')) IN ('current','accepted') THEN 'PRIMARY'
WHEN lower(COALESCE(NULLIF(d.role, ''), 'current')) IN ('historical','history','superseded') THEN 'HISTORICAL'
ELSE 'SECONDARY'
END"""
if document_reconciliation_v2_available(conn):
# This is the relational form of resolve_document_links(): a legacy
# group switches to v2 only when every legacy document in that exact
# opportunity/kind group has a current v2 link. V2-only groups remain
# absent, matching the rollout resolver's current behaviour.
conn.execute(text(f"""
INSERT INTO _effective_document_links
(opportunity_id, document_id, document_kind, relationship, ended_at)
WITH legacy_groups AS (
SELECT DISTINCT d.opportunity_id, COALESCE(d.document_kind, '') AS document_kind
FROM commercial_documents d
WHERE d.opportunity_id IS NOT NULL
), complete_groups AS (
SELECT g.opportunity_id, g.document_kind
FROM legacy_groups g
WHERE NOT EXISTS (
SELECT 1
FROM commercial_documents d
WHERE d.opportunity_id = g.opportunity_id
AND COALESCE(d.document_kind, '') = g.document_kind
AND NOT EXISTS (
SELECT 1
FROM opportunity_document_links l
WHERE l.opportunity_id = g.opportunity_id
AND COALESCE(l.document_kind, '') = g.document_kind
AND l.document_id = d.id
AND l.ended_at IS NULL
)
)
), effective AS (
SELECT l.opportunity_id, l.document_id, l.document_kind,
l.relationship, l.ended_at
FROM opportunity_document_links l
JOIN complete_groups g
ON g.opportunity_id = l.opportunity_id
AND g.document_kind = COALESCE(l.document_kind, '')
WHERE l.ended_at IS NULL
UNION ALL
SELECT d.opportunity_id, d.id, d.document_kind,
{legacy_relationship} AS relationship,
NULL::timestamptz AS ended_at
FROM commercial_documents d
LEFT JOIN complete_groups g
ON g.opportunity_id = d.opportunity_id
AND g.document_kind = COALESCE(d.document_kind, '')
WHERE d.opportunity_id IS NOT NULL
AND g.opportunity_id IS NULL
)
SELECT opportunity_id, document_id, COALESCE(NULLIF(document_kind, ''), 'unknown'),
relationship, ended_at
FROM effective
"""))
else:
conn.execute(text(f"""
INSERT INTO _effective_document_links
(opportunity_id, document_id, document_kind, relationship, ended_at)
SELECT d.opportunity_id, d.id, COALESCE(NULLIF(d.document_kind, ''), 'unknown'),
{legacy_relationship} AS relationship,
NULL::timestamptz
FROM commercial_documents d
WHERE d.opportunity_id IS NOT NULL
"""))
return "_effective_document_links"