neue Funkktionen für Graphendarstellung
This commit is contained in:
1 parent
93b8bc48e4
commit
72a3988e49
3 files changed
+310
-163
No files matched your search
+147
-107
@@ -1,6 +1,6 @@
|
||||
from qdrant_client import QdrantClient, models
|
||||
from streamlit_agraph import Node, Edge
|
||||
from ui_config import GRAPH_COLORS, EDGE_COLORS
|
||||
from ui_config import GRAPH_COLORS, get_edge_color, SYSTEM_EDGES
|
||||
|
||||
class GraphExplorerService:
|
||||
def __init__(self, url, api_key=None, prefix="mindnet"):
|
||||
@@ -11,100 +11,53 @@ class GraphExplorerService:
|
||||
self.edges_col = f"{prefix}_edges"
|
||||
self._note_cache = {}
|
||||
|
||||
def get_ego_graph(self, center_note_id: str):
|
||||
def get_ego_graph(self, center_note_id: str, depth=2):
|
||||
nodes_dict = {}
|
||||
unique_edges = {}
|
||||
|
||||
# 1. Center Note laden
|
||||
# --- LEVEL 1: Center & direkte Nachbarn ---
|
||||
|
||||
# 1. Center Note
|
||||
center_note = self._fetch_note_cached(center_note_id)
|
||||
if not center_note: return [], []
|
||||
self._add_node_to_dict(nodes_dict, center_note, is_center=True)
|
||||
self._add_node_to_dict(nodes_dict, center_note, level=0)
|
||||
|
||||
center_title = center_note.get("title")
|
||||
|
||||
# 2. Chunks der Center Note finden
|
||||
scroll_filter = models.Filter(
|
||||
must=[models.FieldCondition(key="note_id", match=models.MatchValue(value=center_note_id))]
|
||||
)
|
||||
chunks, _ = self.client.scroll(
|
||||
collection_name=self.chunks_col, scroll_filter=scroll_filter, limit=100, with_payload=True
|
||||
)
|
||||
center_chunk_ids = [c.id for c in chunks]
|
||||
# Wir sammeln IDs für Level 2 Suche
|
||||
level_1_ids = {center_note_id}
|
||||
|
||||
raw_edges = []
|
||||
|
||||
# 3. OUTGOING EDGES: Source = einer meiner Chunks
|
||||
if center_chunk_ids:
|
||||
out_filter = models.Filter(
|
||||
must=[models.FieldCondition(key="source_id", match=models.MatchAny(any=center_chunk_ids))]
|
||||
)
|
||||
res_out, _ = self.client.scroll(
|
||||
collection_name=self.edges_col, scroll_filter=out_filter, limit=100, with_payload=True
|
||||
)
|
||||
raw_edges.extend(res_out)
|
||||
|
||||
# 4. INCOMING EDGES: Target = Chunk, Titel oder Note-ID
|
||||
must_conditions = []
|
||||
if center_chunk_ids:
|
||||
must_conditions.append(models.FieldCondition(key="target_id", match=models.MatchAny(any=center_chunk_ids)))
|
||||
if center_title:
|
||||
must_conditions.append(models.FieldCondition(key="target_id", match=models.MatchValue(value=center_title)))
|
||||
# Suche Kanten für Center
|
||||
l1_edges = self._find_connected_edges([center_note_id], center_note.get("title"))
|
||||
|
||||
# FIX: Auch exakte Note-ID als Target prüfen
|
||||
must_conditions.append(models.FieldCondition(key="target_id", match=models.MatchValue(value=center_note_id)))
|
||||
# Verarbeite L1 Kanten
|
||||
for edge_data in l1_edges:
|
||||
src_id, tgt_id = self._process_edge(edge_data, nodes_dict, unique_edges, current_depth=1)
|
||||
if src_id: level_1_ids.add(src_id)
|
||||
if tgt_id: level_1_ids.add(tgt_id)
|
||||
|
||||
if must_conditions:
|
||||
in_filter = models.Filter(should=must_conditions) # 'should' = OR
|
||||
res_in, _ = self.client.scroll(
|
||||
collection_name=self.edges_col, scroll_filter=in_filter, limit=100, with_payload=True
|
||||
)
|
||||
raw_edges.extend(res_in)
|
||||
# --- LEVEL 2: Nachbarn der Nachbarn ---
|
||||
if depth > 1 and level_1_ids:
|
||||
# Wir suchen Kanten, bei denen Source oder Target einer der L1 Nodes ist
|
||||
# Wichtig: Wir filtern System-Edges schon in der Query oder Python, um Traffic zu sparen
|
||||
|
||||
# Um die Performance zu wahren, limitieren wir die L2 Suche auf die IDs, die wir schon haben (als Source)
|
||||
# Das ist ein "Ego-Network" Ansatz.
|
||||
|
||||
# Wir nehmen alle IDs aus Level 1 (außer Center, das haben wir schon)
|
||||
l1_subset = list(level_1_ids - {center_note_id})
|
||||
|
||||
if l1_subset:
|
||||
l2_edges = self._find_connected_edges_batch(l1_subset)
|
||||
for edge_data in l2_edges:
|
||||
self._process_edge(edge_data, nodes_dict, unique_edges, current_depth=2)
|
||||
|
||||
# 5. Verarbeitung & Auflösung
|
||||
for record in raw_edges:
|
||||
payload = record.payload
|
||||
|
||||
src_ref = payload.get("source_id")
|
||||
tgt_ref = payload.get("target_id")
|
||||
kind = payload.get("kind", "related_to")
|
||||
provenance = payload.get("provenance", "explicit")
|
||||
|
||||
src_note = self._resolve_note_from_ref(src_ref)
|
||||
tgt_note = self._resolve_note_from_ref(tgt_ref)
|
||||
|
||||
if src_note and tgt_note:
|
||||
src_id = src_note['note_id']
|
||||
tgt_id = tgt_note['note_id']
|
||||
|
||||
# Keine Self-Loops und valide Verbindung
|
||||
if src_id != tgt_id:
|
||||
self._add_node_to_dict(nodes_dict, src_note)
|
||||
self._add_node_to_dict(nodes_dict, tgt_note)
|
||||
|
||||
key = (src_id, tgt_id)
|
||||
existing = unique_edges.get(key)
|
||||
|
||||
# Deduplizierung: Explizite Kanten überschreiben Smart Edges
|
||||
is_current_explicit = (provenance == "explicit" or provenance == "rule")
|
||||
should_update = True
|
||||
|
||||
if existing:
|
||||
is_existing_explicit = (existing['provenance'] == "explicit" or existing['provenance'] == "rule")
|
||||
if is_existing_explicit and not is_current_explicit:
|
||||
should_update = False
|
||||
|
||||
if should_update:
|
||||
unique_edges[key] = {
|
||||
"source": src_id, "target": tgt_id, "kind": kind, "provenance": provenance
|
||||
}
|
||||
|
||||
# 6. Agraph Objekte bauen
|
||||
# --- GRAPH CONSTRUCTION ---
|
||||
final_edges = []
|
||||
for (src, tgt), data in unique_edges.items():
|
||||
kind = data['kind']
|
||||
prov = data['provenance']
|
||||
|
||||
color = EDGE_COLORS.get(kind, "#bdc3c7")
|
||||
# Dynamische Farbe holen
|
||||
color = get_edge_color(kind)
|
||||
is_smart = (prov != "explicit" and prov != "rule")
|
||||
|
||||
final_edges.append(Edge(
|
||||
@@ -114,9 +67,92 @@ class GraphExplorerService:
|
||||
|
||||
return list(nodes_dict.values()), final_edges
|
||||
|
||||
def _find_connected_edges(self, note_ids, note_title=None):
|
||||
"""Findet In- und Outgoing Edges für eine Liste von Note-IDs."""
|
||||
# 1. Chunks zu diesen Notes finden
|
||||
scroll_filter = models.Filter(
|
||||
must=[models.FieldCondition(key="note_id", match=models.MatchAny(any=note_ids))]
|
||||
)
|
||||
chunks, _ = self.client.scroll(
|
||||
collection_name=self.chunks_col, scroll_filter=scroll_filter, limit=200
|
||||
)
|
||||
chunk_ids = [c.id for c in chunks]
|
||||
|
||||
results = []
|
||||
|
||||
# Outgoing (Source is Chunk)
|
||||
if chunk_ids:
|
||||
out_f = models.Filter(must=[
|
||||
models.FieldCondition(key="source_id", match=models.MatchAny(any=chunk_ids)),
|
||||
# Filter System Edges
|
||||
models.FieldCondition(key="kind", match=models.MatchExcept(except_=SYSTEM_EDGES))
|
||||
])
|
||||
res_out, _ = self.client.scroll(self.edges_col, scroll_filter=out_f, limit=100, with_payload=True)
|
||||
results.extend(res_out)
|
||||
|
||||
# Incoming (Target is Chunk OR Title OR NoteID)
|
||||
shoulds = []
|
||||
if chunk_ids: shoulds.append(models.FieldCondition(key="target_id", match=models.MatchAny(any=chunk_ids)))
|
||||
if note_title: shoulds.append(models.FieldCondition(key="target_id", match=models.MatchValue(value=note_title)))
|
||||
shoulds.append(models.FieldCondition(key="target_id", match=models.MatchAny(any=note_ids)))
|
||||
|
||||
if shoulds:
|
||||
in_f = models.Filter(
|
||||
must=[models.FieldCondition(key="kind", match=models.MatchExcept(except_=SYSTEM_EDGES))],
|
||||
should=shoulds
|
||||
)
|
||||
res_in, _ = self.client.scroll(self.edges_col, scroll_filter=in_f, limit=100, with_payload=True)
|
||||
results.extend(res_in)
|
||||
|
||||
return results
|
||||
|
||||
def _find_connected_edges_batch(self, note_ids):
|
||||
"""Batch-Suche für Level 2 (nur ausgehend und eingehend auf Note-Ebene, keine Title-Suche für Performance)."""
|
||||
# Vereinfachte Suche: Wir suchen Kanten, die direkt mit den note_ids (oder deren Chunks) zu tun haben
|
||||
# Um Performance zu sparen, machen wir hier einen simpleren Lookup, wenn möglich.
|
||||
return self._find_connected_edges(note_ids)
|
||||
|
||||
def _process_edge(self, record, nodes_dict, unique_edges, current_depth):
|
||||
payload = record.payload
|
||||
src_ref = payload.get("source_id")
|
||||
tgt_ref = payload.get("target_id")
|
||||
kind = payload.get("kind")
|
||||
provenance = payload.get("provenance", "explicit")
|
||||
|
||||
# Resolve
|
||||
src_note = self._resolve_note_from_ref(src_ref)
|
||||
tgt_note = self._resolve_note_from_ref(tgt_ref)
|
||||
|
||||
if src_note and tgt_note:
|
||||
src_id = src_note['note_id']
|
||||
tgt_id = tgt_note['note_id']
|
||||
|
||||
if src_id != tgt_id:
|
||||
# Add Nodes
|
||||
self._add_node_to_dict(nodes_dict, src_note, level=current_depth)
|
||||
self._add_node_to_dict(nodes_dict, tgt_note, level=current_depth)
|
||||
|
||||
# Add Edge (Deduplication Logic)
|
||||
key = (src_id, tgt_id)
|
||||
existing = unique_edges.get(key)
|
||||
|
||||
# Update logic: Explicit > Smart
|
||||
should_update = True
|
||||
is_current_explicit = (provenance in ["explicit", "rule"])
|
||||
if existing:
|
||||
is_existing_explicit = (existing['provenance'] in ["explicit", "rule"])
|
||||
if is_existing_explicit and not is_current_explicit:
|
||||
should_update = False
|
||||
|
||||
if should_update:
|
||||
unique_edges[key] = {
|
||||
"source": src_id, "target": tgt_id, "kind": kind, "provenance": provenance
|
||||
}
|
||||
return src_id, tgt_id
|
||||
return None, None
|
||||
|
||||
def _fetch_note_cached(self, note_id):
|
||||
if note_id in self._note_cache: return self._note_cache[note_id]
|
||||
|
||||
res, _ = self.client.scroll(
|
||||
collection_name=self.notes_col,
|
||||
scroll_filter=models.Filter(must=[models.FieldCondition(key="note_id", match=models.MatchValue(value=note_id))]),
|
||||
@@ -129,53 +165,57 @@ class GraphExplorerService:
|
||||
|
||||
def _resolve_note_from_ref(self, ref_str):
|
||||
if not ref_str: return None
|
||||
|
||||
# Fall A: Chunk ID (Format: note_id#cXX)
|
||||
# ... (Logik identisch zu vorher, hier gekürzt für Übersicht)
|
||||
# Fall A: Chunk ID / Section
|
||||
if "#" in ref_str:
|
||||
# Versuch 1: Echte Chunk ID in DB
|
||||
try:
|
||||
res = self.client.retrieve(collection_name=self.chunks_col, ids=[ref_str], with_payload=True)
|
||||
if res:
|
||||
parent_id = res[0].payload.get("note_id")
|
||||
return self._fetch_note_cached(parent_id)
|
||||
except: pass
|
||||
|
||||
# Versuch 2: Section Link (note-id#Header) -> Hash abschneiden
|
||||
res = self.client.retrieve(self.chunks_col, ids=[ref_str], with_payload=True)
|
||||
if res: return self._fetch_note_cached(res[0].payload.get("note_id"))
|
||||
except: pass
|
||||
possible_note_id = ref_str.split("#")[0]
|
||||
note_by_id = self._fetch_note_cached(possible_note_id)
|
||||
if note_by_id: return note_by_id
|
||||
if self._fetch_note_cached(possible_note_id): return self._fetch_note_cached(possible_note_id)
|
||||
|
||||
# Fall B: Es ist direkt die Note ID
|
||||
note_by_id = self._fetch_note_cached(ref_str)
|
||||
if note_by_id: return note_by_id
|
||||
# Fall B: Note ID
|
||||
if self._fetch_note_cached(ref_str): return self._fetch_note_cached(ref_str)
|
||||
|
||||
# Fall C: Es ist der Titel (Wikilink)
|
||||
# Fall C: Titel
|
||||
res, _ = self.client.scroll(
|
||||
collection_name=self.notes_col,
|
||||
scroll_filter=models.Filter(must=[models.FieldCondition(key="title", match=models.MatchValue(value=ref_str))]),
|
||||
limit=1, with_payload=True
|
||||
)
|
||||
if res:
|
||||
p = res[0].payload
|
||||
self._note_cache[p['note_id']] = p
|
||||
return p
|
||||
|
||||
self._note_cache[res[0].payload['note_id']] = res[0].payload
|
||||
return res[0].payload
|
||||
return None
|
||||
|
||||
def _add_node_to_dict(self, node_dict, note_payload, is_center=False):
|
||||
def _add_node_to_dict(self, node_dict, note_payload, level=1):
|
||||
nid = note_payload.get("note_id")
|
||||
|
||||
# Wenn Node schon da ist, aber wir finden ihn auf einem "höheren" Level (näher am Zentrum), updaten wir ihn nicht zwingend,
|
||||
# außer wir wollen visuelle Eigenschaften ändern.
|
||||
if nid in node_dict: return
|
||||
|
||||
ntype = note_payload.get("type", "default")
|
||||
color = GRAPH_COLORS.get(ntype, GRAPH_COLORS["default"])
|
||||
size = 35 if is_center else 20
|
||||
|
||||
# Größe & Label basierend auf Level
|
||||
if level == 0:
|
||||
size = 40
|
||||
label_prefix = ""
|
||||
elif level == 1:
|
||||
size = 25
|
||||
label_prefix = ""
|
||||
else:
|
||||
size = 15 # Level 2 kleiner
|
||||
label_prefix = ""
|
||||
|
||||
node_dict[nid] = Node(
|
||||
id=nid,
|
||||
label=note_payload.get("title", nid),
|
||||
label=f"{label_prefix}{note_payload.get('title', nid)}",
|
||||
size=size,
|
||||
color=color,
|
||||
shape="dot" if not is_center else "diamond",
|
||||
title=f"Type: {ntype}\nTags: {note_payload.get('tags')}",
|
||||
font={'color': 'black'}
|
||||
shape="dot" if level > 0 else "diamond",
|
||||
title=f"Type: {ntype}\nLevel: {level}\nTags: {note_payload.get('tags')}",
|
||||
font={'color': 'black', 'face': 'arial', 'size': 14 if level < 2 else 10}
|
||||
)
|
||||
Reference in new issue
Block a user