File size: 7,330 Bytes
ba70497
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""Knowledge-graph data for the orb graph viewer (orb-graph/public), as {nodes, links}.

Three views, chosen with ?view= on /api/knowledge_graph:

    network   every supplier, part, model, plant and dealer, and how they connect (default)
    schema    the 11 node types and 16 edge types of the ontology, sized by record count
    event     the records one event's graph paths touched (&event=E-01), or every event's (&event=all),
              read from the run traces

The full record graph has about 17,000 nodes, too many to lay out in a browser, so the network
view keeps the entities the agents reason about and leaves out orders, customers and vehicles.
"""

from __future__ import annotations

import csv
import json
import os
import re
from collections import Counter

HERE = os.path.dirname(os.path.abspath(__file__))
HITACHI = os.path.abspath(os.path.join(HERE, "..", ".."))
DATA = os.path.join(HITACHI, "Data", "hack_the_highway_package", "data")

# One fixed color per node type, shared by all three views.
COLORS = {
    "Supplier": "#ffb02e", "Part": "#2fd8ff", "Model": "#9b7bff", "Plant": "#5cff9d",
    "Dealer": "#ff4fd0", "Shipment": "#ff8a3d", "Lot": "#ff6b57", "ProductionOrder": "#4f8bff",
    "CustomerOrder": "#3ee0c0", "Customer": "#c7d2ff", "Vehicle": "#f5e15c",
}
EVENT_NODE_CAP = 400

_network = None


def _rows(name: str) -> list[dict]:
    with open(os.path.join(DATA, name), newline="", encoding="utf-8") as handle:
        return list(csv.DictReader(handle))


def network() -> dict:
    """Suppliers, parts, models, plants and dealers. Built once; the CSVs do not change."""
    global _network
    if _network is not None:
        return _network
    nodes, links, seen = [], [], set()

    def node(kind, key, label, detail):
        nodes.append({"id": f"{kind} {key}", "type": kind, "label": label, "detail": detail})

    def link(a, b, rel):
        k = (a, b, rel)
        if k not in seen:
            seen.add(k)
            links.append({"source": a, "target": b, "relation": rel})

    for r in _rows("suppliers.csv"):
        node("Supplier", r["supplier_id"], r["supplier_id"], f'{r["name"]}, {r["city"]}')
    for r in _rows("parts.csv"):
        node("Part", r["part_id"], r["part_id"], r["name"])
    for r in _rows("models.csv"):
        node("Model", r["model"], r["model"], f'{r["segment"]} model')
    for r in _rows("plants.csv"):
        node("Plant", r["plant_id"], r["plant_id"], f'{r["name"]}, {r["slots_per_day"]} slots a day')
    for r in _rows("dealers.csv"):
        node("Dealer", r["dealer_id"], r["dealer_id"], f'{r["name"]}, {r["city"]}')

    for r in _rows("bom.csv"):
        link(f'Part {r["part_id"]}', f'Model {r["model"]}', "USED_IN")
    for r in _rows("plant_models.csv"):
        link(f'Plant {r["plant_id"]}', f'Model {r["model"]}', "CAN_BUILD")
    for r in _rows("inbound_shipments.csv"):
        link(f'Supplier {r["supplier_id"]}', f'Part {r["part_id"]}', "SUPPLIES")
    # Each dealer links to the one plant that builds most of its orders. Linking every plant a
    # dealer has ever bought from ties all 50 dealers to every plant and piles them into one ball.
    po_plant = {r["production_order_id"]: r["plant_id"] for r in _rows("production_orders.csv")}
    by_dealer: dict[str, Counter] = {}
    for r in _rows("customer_orders.csv"):
        plant = po_plant.get(r["production_order_id"])
        if plant:
            by_dealer.setdefault(r["dealer_id"], Counter())[plant] += 1
    for dealer, plants in sorted(by_dealer.items()):
        plant, orders = plants.most_common(1)[0]
        link(f"Plant {plant}", f"Dealer {dealer}", "MAIN_PLANT")
        for n in nodes:
            if n["id"] == f"Dealer {dealer}":
                n["detail"] += f" · {orders} of {sum(plants.values())} orders from {plant}"

    # Wider spacing than the viewer's default: 100 orbs with hubs need room to read.
    _network = {"kicker": "Network", "title": "Company Network", "colors": COLORS, "nodes": nodes, "links": links,
                "layout": {"charge": 2.6, "distance": 1.6, "gravity": 0.5}}
    return _network


def schema(onto: dict) -> dict:
    """The ontology itself: one orb per node type, one link per edge type."""
    return {
        "kicker": "Schema",
        "title": "Ontology Schema",
        "colors": COLORS,
        "nodes": [{"id": n["type"], "type": n["type"], "label": re.sub(r"([a-z])([A-Z])", r"\1 \2", n["type"]),
                   "detail": f'{n["count"]:,} records'} for n in onto["nodes"]],
        "links": [{"source": e["from"], "target": e["to"], "relation": e["relation"]} for e in onto["edges"]],
        "layout": {"charge": 2.0, "distance": 1.8},
    }


def event(traces_path: str, event_id: str, extra: list | None = None) -> dict:
    """The records on one event's graph paths, or on every event's (event_id "all" or ""), capped at
    EVENT_NODE_CAP orbs. extra: traces of events added from the dashboard, which are part of the feed."""
    traces = []
    if os.path.isfile(traces_path):
        with open(traces_path, encoding="utf-8") as handle:
            traces = json.load(handle)
    have = {t.get("event_id") for t in traces if isinstance(t, dict)}
    traces += [t for t in (extra or []) if t.get("event_id") not in have]
    every = event_id in ("", "all")
    chosen = traces if every else [t for t in traces if t.get("event_id") == event_id]
    title = "All Events" if every else event_id
    # No records yet: the viewer shows empty space and fills in as events arrive.
    if not chosen:
        return {"kicker": "Graph Paths", "title": title, "colors": COLORS, "nodes": [], "links": []}

    ids, links, seen, truncated = {}, [], set(), False
    for result in (r for t in chosen for r in t.get("results", [])):
        for path in result.get("graph_paths", []):
            # "Dealer D18 / Customer C6387" is two records at the end of one path.
            steps = []
            for step in path:
                steps.extend(s.strip() for s in str(step).split(" / "))
            steps = [s.replace(" (BOM)", "") for s in steps if " " in s]
            new = [s for s in steps if s not in ids]
            if len(ids) + len(new) > EVENT_NODE_CAP:
                truncated = True
                break
            for s in new:
                ids[s] = s.split(" ", 1)[0]
            # A trailing Dealer/Customer pair both hang off the step before them.
            for i in range(1, len(steps)):
                a = steps[i - 1]
                if a.split(" ", 1)[0] == "Dealer" and steps[i].startswith("Customer "):
                    a = steps[i - 2] if i >= 2 else a
                k = tuple(sorted((a, steps[i])))
                if a != steps[i] and k not in seen:
                    seen.add(k)
                    links.append({"source": a, "target": steps[i]})
        if truncated:
            break
    counts = Counter(ids.values())
    return {
        "kicker": "Graph Paths",
        "title": title + (f" (first {len(ids)} records)" if truncated else ""),
        "colors": COLORS,
        "nodes": [{"id": s, "type": kind, "label": s.split(" ", 1)[1], "detail": re.sub(r"([a-z])([A-Z])", r"\1 \2", kind)} for s, kind in ids.items()],
        "links": links,
        "counts": dict(counts),
        "layout": {"charge": 1.8, "distance": 1.3, "gravity": 0.6},
    }