File size: 2,861 Bytes
884dabc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Shared paths and the Database FK resolver for the per-entity extract scripts.

The raw data lives in a sibling Git repo (the recorder). This dataset has no
local source files; every extract script reads from `EXPORTS_DIR` and writes
to `DATA_DIR`.
"""
import json
from pathlib import Path

ROOT = Path(__file__).parent.parent
DATA_DIR = ROOT / "data"

# Sibling-repo locations. The Telemetry dataset has no local source/ tree;
# its raw inputs are the flat exports written by
# `Agentic-Additive-Manufacturing-Process-Optimization/scripts/export.py`.
# /mnt/storage2/HuggingFace/Datasets/Inova-Mk1-Telemetry → up three to /mnt/storage2.
AGENTIC_ROOT = ROOT.parent.parent.parent / "GitHub" / "Agentic-Additive-Manufacturing-Process-Optimization"
EXPORTS_DIR = AGENTIC_ROOT / "data" / "exports"

# Used only by the `builds` extract to resolve print_profile_id.
DATABASE_ROOT = ROOT.parent / "Inova-Mk1-Database"
DATABASE_PROFILES_DIR = DATABASE_ROOT / "source" / "PrintProfiles"


def iter_jsonl(path: Path):
    """Stream a JSONL file row-by-row. Skips blank lines."""
    with path.open(encoding="utf-8") as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            yield json.loads(line)


def load_build_to_profile_name() -> dict[int, str]:
    """Read upstream events.jsonl, return {build_id: printProfileName}.

    Uses the `build_start` event's `payload.printProfileName` field. Builds
    that never logged a build_start event (the recorder joined mid-run) are
    omitted from the map.
    """
    events_path = EXPORTS_DIR / "events.jsonl"
    out: dict[int, str] = {}
    for ev in iter_jsonl(events_path):
        if ev.get("kind") != "build_start":
            continue
        name = (ev.get("payload") or {}).get("printProfileName")
        if name:
            out[ev["build_id"]] = name
    return out


def load_profile_name_to_id() -> dict[str, str]:
    """Read Inova-Mk1-Database PrintProfile JSONs, return {Name: Id}.

    Matches on the in-app `Name` field exactly (slashes preserved); the
    sibling event payload uses the same form (e.g.
    "2026_05_30 20mJ/mm Formlabs PA12 GF").
    """
    out: dict[str, str] = {}
    for p in DATABASE_PROFILES_DIR.glob("*.json"):
        with p.open() as f:
            blob = json.load(f)
        if "Name" in blob and "Id" in blob:
            out[blob["Name"]] = blob["Id"]
    return out


def build_id_to_profile_id() -> dict[int, tuple[str | None, str | None]]:
    """Compose: {build_id: (print_profile_id, print_profile_name)}.

    Unresolved profile_id (no Database match) is returned as None alongside
    the still-useful name string.
    """
    b2name = load_build_to_profile_name()
    n2id = load_profile_name_to_id()
    return {
        bid: (n2id.get(name), name)
        for bid, name in b2name.items()
    }