-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_tree.py
More file actions
404 lines (346 loc) · 14.7 KB
/
Copy pathdocument_tree.py
File metadata and controls
404 lines (346 loc) · 14.7 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
"""
Document Tree module (#7).
Builds a hierarchical folder tree from document source_uri paths.
Supports lazy loading (one level at a time) and aggregated counts
per folder for the Hierarchical Document Browser.
"""
import logging
import posixpath
from typing import Any, Dict, List, Optional, Tuple
from path_utils import folder_prefix_like_pattern, normalize_path, NORMALIZED_URI_SQL
logger = logging.getLogger(__name__)
def _get_db_connection():
"""Get a pooled database connection as a context manager.
Always use with ``with _get_db_connection() as conn:`` to ensure
the connection is returned to the pool after use.
"""
from database import get_db_manager
return get_db_manager().get_connection()
def _normalize_path(path: str) -> str:
"""Normalize a path to forward slashes for consistent tree building.
Delegates to :func:`path_utils.normalize_path` (single source of truth).
Kept as a thin wrapper for backward compatibility with tests.
"""
return normalize_path(path)
def _visibility_sql(visibility: Optional[Tuple[str, list]]) -> Tuple[str, list]:
"""Return ("AND <fragment>", params) for a visibility filter, or ("", [])."""
if visibility and visibility[0]:
return f"AND {visibility[0]}", list(visibility[1])
return "", []
def _filter_hidden_docs(
docs: List[Dict[str, Any]],
hidden_document_ids: Optional[List[str]],
) -> List[Dict[str, Any]]:
"""Drop LanceDB-sourced docs the caller must not see (private, other owner)."""
if not hidden_document_ids:
return docs
hidden = set(hidden_document_ids)
return [doc for doc in docs if doc.get("document_id") not in hidden]
def get_tree_children(
parent_path: str = "",
limit: int = 200,
offset: int = 0,
source: str = "postgres",
visibility: Optional[Tuple[str, list]] = None,
hidden_document_ids: Optional[List[str]] = None,
) -> Dict[str, Any]:
"""Get one level of the document tree under parent_path.
Returns folders and files at the immediate next level.
Each folder includes aggregated document count and latest indexed_at.
Each file includes document_id, chunk_count, and indexed_at.
Args:
parent_path: The parent folder path (empty string for root).
Uses forward slashes, no trailing slash.
limit: Max items to return.
offset: Pagination offset.
source: The database source ('postgres' or 'lancedb').
visibility: Optional (sql_fragment, params) visibility filter for the
Postgres source.
hidden_document_ids: Document ids the caller must not see; used to
filter the LanceDB source (which has no visibility columns).
Returns:
Dict with 'folders', 'files', 'total_folders', 'total_files'.
"""
try:
# Normalize parent
parent = _normalize_path(parent_path).rstrip("/")
if source == "lancedb":
from services import get_lancedb_adapter
from datetime import datetime
adapter = get_lancedb_adapter()
docs = _filter_hidden_docs(adapter.list_documents(prefix=parent), hidden_document_ids)
# Format rows: (norm_uri, document_id, chunk_count, indexed_at, last_updated)
all_rows = []
for doc in docs:
norm_uri = _normalize_path(doc["source_uri"])
# Parse datetime if present
indexed_at = None
if doc.get("indexed_at"):
try:
indexed_at = datetime.fromisoformat(doc["indexed_at"])
except Exception:
pass
all_rows.append((norm_uri, doc["document_id"], doc["chunk_count"], indexed_at, indexed_at))
# Filter in Python if parent is set
rows = []
for row in all_rows:
norm_uri = row[0]
if parent:
if norm_uri.startswith(parent + "/"):
rows.append(row)
else:
rows.append(row)
else:
with _get_db_connection() as conn:
cur = conn.cursor()
if parent:
# Escaped so folder names containing %/_ match literally;
# the Python-side startswith() below stays the exact gate.
like_prefix = folder_prefix_like_pattern(parent) or "%"
else:
like_prefix = "%"
vis_sql, vis_params = _visibility_sql(visibility)
# Get all distinct normalized source_uri paths under this parent
cur.execute(
f"""
SELECT
{NORMALIZED_URI_SQL} AS norm_uri,
document_id,
COUNT(*) AS chunk_count,
MIN(indexed_at) AS indexed_at,
MAX(indexed_at) AS last_updated
FROM document_chunks
WHERE {NORMALIZED_URI_SQL} LIKE %s {vis_sql}
GROUP BY norm_uri, document_id
ORDER BY norm_uri
""",
(like_prefix, *vis_params),
)
rows = cur.fetchall()
# Build tree level
folders: Dict[str, Dict[str, Any]] = {}
files: List[Dict[str, Any]] = []
parent_depth = len(parent.split("/")) if parent else 0
for norm_uri, document_id, chunk_count, indexed_at, last_updated in rows:
# Strip the parent prefix to get relative path
if parent:
if not norm_uri.startswith(parent + "/"):
continue
relative = norm_uri[len(parent) + 1:]
else:
relative = norm_uri
# Handle absolute Linux paths: /home/... → treat "/" as a root folder
# so the first split component isn't an empty string.
if not parent and relative.startswith("/"):
relative = relative.lstrip("/")
# Reconstruct with "/" prefix for folder_path below
_linux_root = True
else:
_linux_root = False
parts = relative.split("/")
if len(parts) == 1:
# Direct child file
files.append({
"name": parts[0],
"path": norm_uri,
"type": "file",
"document_id": document_id,
"chunk_count": chunk_count,
"indexed_at": indexed_at.isoformat() if indexed_at else None,
"last_updated": last_updated.isoformat() if last_updated else None,
})
else:
# Child is inside a subfolder
folder_name = parts[0]
if _linux_root:
# Use "/" prefix so expanding this folder fetches the right children
folder_path = "/" + folder_name
elif parent:
folder_path = parent + "/" + folder_name
else:
folder_path = folder_name
if folder_name not in folders:
folders[folder_name] = {
"name": folder_name,
"path": folder_path,
"type": "folder",
"document_count": 0,
"latest_indexed_at": None,
}
folders[folder_name]["document_count"] += 1
if indexed_at:
ts = indexed_at.isoformat()
prev = folders[folder_name]["latest_indexed_at"]
if prev is None or ts > prev:
folders[folder_name]["latest_indexed_at"] = ts
# Sort folders alphabetically, files alphabetically
sorted_folders = sorted(folders.values(), key=lambda f: f["name"].lower())
sorted_files = sorted(files, key=lambda f: f["name"].lower())
total_folders = len(sorted_folders)
total_files = len(sorted_files)
# Combine and paginate
all_items = sorted_folders + sorted_files
paginated = all_items[offset:offset + limit]
return {
"parent_path": parent,
"children": paginated,
"total_folders": total_folders,
"total_files": total_files,
"total": total_folders + total_files,
"limit": limit,
"offset": offset,
}
except Exception as e:
logger.warning("Failed to get tree children for '%s': %s", parent_path, e)
return {
"parent_path": parent_path,
"children": [],
"total_folders": 0,
"total_files": 0,
"total": 0,
"limit": limit,
"offset": offset,
}
def get_tree_stats(
source: str = "postgres",
visibility: Optional[Tuple[str, list]] = None,
hidden_document_ids: Optional[List[str]] = None,
) -> Dict[str, Any]:
"""Get overall tree statistics.
Returns:
Dict with total_documents, total_folders (top-level), total_chunks.
"""
try:
if source == "lancedb":
from services import get_lancedb_adapter
adapter = get_lancedb_adapter()
docs = _filter_hidden_docs(adapter.list_documents(), hidden_document_ids)
# Count distinct top-level folders/files
top_levels = set()
for doc in docs:
norm_uri = _normalize_path(doc["source_uri"]).lstrip("/")
parts = norm_uri.split("/")
if parts:
top_levels.add(parts[0])
top_level_count = len(top_levels)
if hidden_document_ids:
# Compute counts from the filtered listing so hidden documents
# are not reflected in the caller's totals.
total_documents = len(docs)
total_chunks = sum(doc.get("chunk_count") or 0 for doc in docs)
else:
stats = adapter.get_statistics()
total_documents = stats["total_documents"]
total_chunks = stats["total_chunks"]
return {
"total_documents": total_documents,
"total_chunks": total_chunks,
"top_level_items": top_level_count,
}
else:
vis_sql, vis_params = _visibility_sql(visibility)
vis_where = f"WHERE TRUE {vis_sql}" if vis_sql else ""
with _get_db_connection() as conn:
cur = conn.cursor()
cur.execute(
f"SELECT COUNT(DISTINCT document_id) FROM document_chunks {vis_where}",
tuple(vis_params) or None,
)
total_docs = cur.fetchone()[0]
cur.execute(
f"SELECT COUNT(*) FROM document_chunks {vis_where}",
tuple(vis_params) or None,
)
total_chunks = cur.fetchone()[0]
# Count distinct top-level folders
cur.execute(f"""
SELECT COUNT(DISTINCT
SPLIT_PART(
{NORMALIZED_URI_SQL},
'/', 1
)
)
FROM document_chunks
{vis_where}
""", tuple(vis_params) or None)
top_level_count = cur.fetchone()[0]
return {
"total_documents": total_docs,
"total_chunks": total_chunks,
"top_level_items": top_level_count,
}
except Exception as e:
logger.warning("Failed to get tree stats: %s", e)
return {
"total_documents": 0,
"total_chunks": 0,
"top_level_items": 0,
}
def search_tree(
query: str,
limit: int = 50,
source: str = "postgres",
visibility: Optional[Tuple[str, list]] = None,
hidden_document_ids: Optional[List[str]] = None,
) -> List[Dict[str, Any]]:
"""Search for documents matching a path pattern.
Returns matching documents with their full paths.
"""
try:
if source == "lancedb":
from services import get_lancedb_adapter
from datetime import datetime
adapter = get_lancedb_adapter()
docs = _filter_hidden_docs(adapter.list_documents(), hidden_document_ids)
pattern = _normalize_path(query).lower()
results = []
for doc in docs:
norm_uri = _normalize_path(doc["source_uri"])
if pattern in norm_uri.lower():
indexed_at = None
if doc.get("indexed_at"):
try:
indexed_at = datetime.fromisoformat(doc["indexed_at"])
except Exception:
pass
results.append({
"path": norm_uri,
"document_id": doc["document_id"],
"chunk_count": doc["chunk_count"],
"indexed_at": indexed_at.isoformat() if indexed_at else None,
})
results.sort(key=lambda x: x["path"])
return results[:limit]
else:
with _get_db_connection() as conn:
cur = conn.cursor()
vis_sql, vis_params = _visibility_sql(visibility)
pattern = f"%{_normalize_path(query)}%"
cur.execute(
f"""
SELECT
{NORMALIZED_URI_SQL} AS norm_uri,
document_id,
COUNT(*) AS chunk_count,
MIN(indexed_at) AS indexed_at
FROM document_chunks
WHERE {NORMALIZED_URI_SQL} ILIKE %s {vis_sql}
GROUP BY norm_uri, document_id
ORDER BY norm_uri
LIMIT %s
""",
(pattern, *vis_params, limit),
)
rows = cur.fetchall()
return [
{
"path": norm_uri,
"document_id": doc_id,
"chunk_count": chunk_count,
"indexed_at": indexed_at.isoformat() if indexed_at else None,
}
for norm_uri, doc_id, chunk_count, indexed_at in rows
]
except Exception as e:
logger.warning("Failed to search tree for '%s': %s", query, e)
return []