feat(engine): import compliant data engine (services/api) + infra
FastAPI orchestrator (health/sources/estimates/orders/jobs per docs/04), adaptive router (compliant methods only, no bypass tiers), pipeline (sourcing->extraction->structuring/dedup->fusion->QA with per-field provenance, drops < confidence target), live connectors GLEIF/Brreg/Companies House (PRH/CVR/Bolagsverket gated to level-0 until terms verified). sources.yaml registry. infra/docker-compose + Caddyfile for the VPS. FlareSolverr/stealth/proxy/fake-account paths intentionally NOT carried over (CLAUDE.md §1). py_compile clean. Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
# 0pt data engine — workers package (router + pipeline).
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,23 @@
|
||||
"""
|
||||
Adaptive router — chooses among COMPLIANT methods only (docs/06).
|
||||
Order of preference: official API > bulk > authorized public web > licensed.
|
||||
There are NO bypass/stealth/proxy-rotation tiers here, by design (CLAUDE.md §1).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
# market -> ordered list of source_ids to try (compliant connectors only)
|
||||
MARKET_SOURCES: dict[str, list[str]] = {
|
||||
"NO": ["brreg", "gleif"],
|
||||
"GB": ["companies_house", "gleif"],
|
||||
"FI": ["prh", "gleif"],
|
||||
"DK": ["cvr", "gleif"],
|
||||
"SE": ["gleif"], # Bolagsverket bulk is licensed; SE company core via GLEIF + licensed/partner
|
||||
}
|
||||
|
||||
# Access rank: lower = preferred (cheapest compliant path first).
|
||||
ACCESS_RANK = {"api": 0, "bulk": 1, "authorized_web": 2, "licensed": 3}
|
||||
|
||||
|
||||
def plan_for_market(market: str) -> list[str]:
|
||||
"""Return the ordered, compliant source plan for a market."""
|
||||
return MARKET_SOURCES.get(market, ["gleif"])
|
||||
@@ -0,0 +1,89 @@
|
||||
"""
|
||||
Production pipeline (docs/06): sourcing -> extraction -> structuring ->
|
||||
enrichment/fusion -> QA. Compliant connectors only; every field gets provenance;
|
||||
rows below the order's confidence target are dropped (deliver fewer, never fabricate).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from rapidfuzz import fuzz
|
||||
|
||||
from sources.connectors import (
|
||||
CompanyResult,
|
||||
brreg_by_orgnr,
|
||||
companies_house_by_number,
|
||||
gleif_by_lei,
|
||||
)
|
||||
from workers.adaptive_router import plan_for_market
|
||||
|
||||
# Minimal built-in seed so a job is demonstrable without customer input.
|
||||
DEMO_SEEDS: dict[str, list[str]] = {
|
||||
"NO": ["923609016"], # brreg orgnr (example)
|
||||
"GB": ["00000006"], # companies house number (example)
|
||||
}
|
||||
|
||||
|
||||
async def _fetch(source_id: str, key: str) -> CompanyResult | None:
|
||||
if source_id == "gleif":
|
||||
return await gleif_by_lei(key)
|
||||
if source_id == "brreg":
|
||||
return await brreg_by_orgnr(key)
|
||||
if source_id == "companies_house":
|
||||
return await companies_house_by_number(key)
|
||||
return None
|
||||
|
||||
|
||||
async def run_job(order_id: str, config: dict[str, Any]) -> dict[str, Any]:
|
||||
markets: list[str] = config.get("markets", []) or ["NO"]
|
||||
threshold = float(config.get("confidence", 92)) / 100.0
|
||||
seeds: dict[str, list[str]] = config.get("seeds") or DEMO_SEEDS
|
||||
|
||||
records: list[dict[str, Any]] = []
|
||||
per_market: dict[str, int] = {}
|
||||
|
||||
# 1) sourcing + 2) extraction
|
||||
for market in markets:
|
||||
keys = seeds.get(market, [])
|
||||
plan = plan_for_market(market)
|
||||
got = 0
|
||||
for key in keys:
|
||||
for source_id in plan:
|
||||
try:
|
||||
res = await _fetch(source_id, key)
|
||||
except Exception:
|
||||
res = None
|
||||
if res and res.name:
|
||||
records.append({
|
||||
"market": market, "name": res.name, "status": res.status,
|
||||
"lei": res.lei, "org_nr": res.org_nr, "source": res.source,
|
||||
"provenance": [p.__dict__ for p in res.provenance],
|
||||
"confidence": max((p.confidence for p in res.provenance), default=0.0),
|
||||
})
|
||||
got += 1
|
||||
break # first compliant source that answers wins
|
||||
per_market[market] = got
|
||||
|
||||
# 3) structuring — dedup by fuzzy name within market
|
||||
deduped: list[dict[str, Any]] = []
|
||||
for r in records:
|
||||
dup = next((d for d in deduped if d["market"] == r["market"] and fuzz.ratio(d["name"], r["name"]) > 92), None)
|
||||
if dup:
|
||||
dup["confidence"] = min(0.99, dup["confidence"] + 0.1) # corroboration bump
|
||||
else:
|
||||
deduped.append(r)
|
||||
|
||||
# 4) enrichment/fusion already folded into confidence above.
|
||||
# 5) QA — drop rows below the order's confidence threshold.
|
||||
delivered = [r for r in deduped if r["confidence"] >= threshold]
|
||||
dropped = len(deduped) - len(delivered)
|
||||
avg_conf = round(sum(r["confidence"] for r in delivered) / len(delivered), 3) if delivered else 0.0
|
||||
|
||||
return {
|
||||
"stage": "qa", "pct": 100, "state": "done",
|
||||
"metrics": {
|
||||
"rows": len(delivered), "dropped_below_threshold": dropped,
|
||||
"per_market": per_market, "avg_confidence": avg_conf,
|
||||
},
|
||||
"records": delivered,
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
"""
|
||||
Worker entrypoint. In production this drains a queue of order jobs and runs the
|
||||
compliant pipeline. For now it exposes a CLI to run a single job locally.
|
||||
python -m workers.run <order_id>
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
|
||||
from workers.pipeline import run_job
|
||||
|
||||
|
||||
async def _main() -> None:
|
||||
order_id = sys.argv[1] if len(sys.argv) > 1 else "demo"
|
||||
result = await run_job(order_id, {"markets": ["NO", "GB"], "confidence": 90})
|
||||
print(result["metrics"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(_main())
|
||||
Reference in New Issue
Block a user