add data viewer
Browse files- app/main.py +23 -2
app/main.py
CHANGED
|
@@ -765,6 +765,19 @@ def build_chunk_tree(chunks: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
|
| 765 |
if not chunks:
|
| 766 |
return []
|
| 767 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 768 |
# Tạo dictionary để truy cập nhanh
|
| 769 |
chunks_dict = {chunk['id']: chunk for chunk in chunks}
|
| 770 |
|
|
@@ -789,21 +802,29 @@ def build_chunk_tree(chunks: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
|
| 789 |
}
|
| 790 |
|
| 791 |
# Tìm tất cả children của node này
|
|
|
|
| 792 |
for other_chunk in chunks:
|
| 793 |
if other_chunk.get('cha') == chunk_id:
|
| 794 |
child_node = build_node(other_chunk['id'])
|
| 795 |
node["children"].append(child_node)
|
|
|
|
| 796 |
|
|
|
|
| 797 |
return node
|
| 798 |
|
| 799 |
-
# Tìm tất cả root nodes (cha=None)
|
| 800 |
root_chunks = []
|
|
|
|
|
|
|
| 801 |
for chunk in chunks:
|
| 802 |
-
if chunk.get('cha') is None:
|
| 803 |
root_node = build_node(chunk['id'])
|
| 804 |
root_chunks.append(root_node)
|
|
|
|
|
|
|
| 805 |
|
| 806 |
logger.info(f"[TREE] Built tree with {len(root_chunks)} root nodes from {len(chunks)} total chunks")
|
|
|
|
| 807 |
return root_chunks
|
| 808 |
|
| 809 |
@app.get("/api/document-chunks/status")
|
|
|
|
| 765 |
if not chunks:
|
| 766 |
return []
|
| 767 |
|
| 768 |
+
# Debug: Kiểm tra tất cả chunks
|
| 769 |
+
root_count = 0
|
| 770 |
+
child_count = 0
|
| 771 |
+
for chunk in chunks:
|
| 772 |
+
if chunk.get('cha') is None:
|
| 773 |
+
root_count += 1
|
| 774 |
+
logger.debug(f"[TREE] Root chunk: {chunk.get('content', '')[:100]}")
|
| 775 |
+
else:
|
| 776 |
+
child_count += 1
|
| 777 |
+
logger.debug(f"[TREE] Child chunk: {chunk.get('content', '')[:100]} -> parent: {chunk.get('cha')}")
|
| 778 |
+
|
| 779 |
+
logger.info(f"[TREE] Found {root_count} root chunks and {child_count} child chunks from {len(chunks)} total chunks")
|
| 780 |
+
|
| 781 |
# Tạo dictionary để truy cập nhanh
|
| 782 |
chunks_dict = {chunk['id']: chunk for chunk in chunks}
|
| 783 |
|
|
|
|
| 802 |
}
|
| 803 |
|
| 804 |
# Tìm tất cả children của node này
|
| 805 |
+
children_count = 0
|
| 806 |
for other_chunk in chunks:
|
| 807 |
if other_chunk.get('cha') == chunk_id:
|
| 808 |
child_node = build_node(other_chunk['id'])
|
| 809 |
node["children"].append(child_node)
|
| 810 |
+
children_count += 1
|
| 811 |
|
| 812 |
+
logger.debug(f"[TREE] Node {chunk_id[:8]}... has {children_count} children")
|
| 813 |
return node
|
| 814 |
|
| 815 |
+
# Tìm tất cả root nodes (cha=None) và sắp xếp theo thứ tự xuất hiện
|
| 816 |
root_chunks = []
|
| 817 |
+
processed_ids = set() # Để tránh xử lý trùng lặp
|
| 818 |
+
|
| 819 |
for chunk in chunks:
|
| 820 |
+
if chunk.get('cha') is None and chunk['id'] not in processed_ids:
|
| 821 |
root_node = build_node(chunk['id'])
|
| 822 |
root_chunks.append(root_node)
|
| 823 |
+
processed_ids.add(chunk['id'])
|
| 824 |
+
logger.info(f"[TREE] Added root node: {chunk.get('content', '')[:100]}")
|
| 825 |
|
| 826 |
logger.info(f"[TREE] Built tree with {len(root_chunks)} root nodes from {len(chunks)} total chunks")
|
| 827 |
+
logger.info(f"[TREE] Root chunks: {[chunk.get('content', '')[:50] for chunk in root_chunks]}")
|
| 828 |
return root_chunks
|
| 829 |
|
| 830 |
@app.get("/api/document-chunks/status")
|