Repository navigation
Expand file tree
/
Copy pathstorage.py
More file actions
602 lines (558 loc) · 18.8 KB
/
Copy pathstorage.py
File metadata and controls
602 lines (558 loc) · 18.8 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
from __future__ import annotations
import sqlite3
from datetime import datetime
from pathlib import Path
from typing import List, Set, Tuple
from .models import CrawlJob, CrawlStats, EmbeddingJobRecord, JobStatus
DB_PATH = Path(__file__).resolve().parent.parent / "crawler.db"
def _connect() -> sqlite3.Connection:
return sqlite3.connect(DB_PATH)
def init_db() -> None:
conn = _connect()
try:
cur = conn.cursor()
cur.execute(
"""
CREATE TABLE IF NOT EXISTS pages (
url TEXT PRIMARY KEY,
origin_url TEXT NOT NULL,
depth INTEGER NOT NULL,
title TEXT,
body_snippet TEXT
)
"""
)
cur.execute(
"""
CREATE TABLE IF NOT EXISTS jobs (
id TEXT PRIMARY KEY,
origin_url TEXT NOT NULL,
max_depth INTEGER NOT NULL,
max_urls_to_visit INTEGER,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
status TEXT NOT NULL,
rate_limit_per_sec REAL NOT NULL DEFAULT 1.0,
error_message TEXT,
processed_urls INTEGER NOT NULL DEFAULT 0,
discovered_urls INTEGER NOT NULL DEFAULT 0,
duplicate_urls INTEGER NOT NULL DEFAULT 0,
failed_urls INTEGER NOT NULL DEFAULT 0,
queued_urls INTEGER NOT NULL DEFAULT 0,
queue_max INTEGER NOT NULL DEFAULT 0,
active_workers INTEGER NOT NULL DEFAULT 0,
backpressure_state TEXT NOT NULL DEFAULT 'idle'
)
"""
)
_add_column_if_missing(cur, "jobs", "updated_at", "TEXT NOT NULL DEFAULT ''")
_add_column_if_missing(cur, "jobs", "max_urls_to_visit", "INTEGER")
_add_column_if_missing(cur, "jobs", "rate_limit_per_sec", "REAL NOT NULL DEFAULT 1.0")
_add_column_if_missing(cur, "jobs", "error_message", "TEXT")
_add_column_if_missing(cur, "jobs", "processed_urls", "INTEGER NOT NULL DEFAULT 0")
_add_column_if_missing(cur, "jobs", "discovered_urls", "INTEGER NOT NULL DEFAULT 0")
_add_column_if_missing(cur, "jobs", "duplicate_urls", "INTEGER NOT NULL DEFAULT 0")
_add_column_if_missing(cur, "jobs", "failed_urls", "INTEGER NOT NULL DEFAULT 0")
_add_column_if_missing(cur, "jobs", "queued_urls", "INTEGER NOT NULL DEFAULT 0")
_add_column_if_missing(cur, "jobs", "queue_max", "INTEGER NOT NULL DEFAULT 0")
_add_column_if_missing(cur, "jobs", "active_workers", "INTEGER NOT NULL DEFAULT 0")
_add_column_if_missing(cur, "jobs", "backpressure_state", "TEXT NOT NULL DEFAULT 'idle'")
cur.execute(
"""
CREATE TABLE IF NOT EXISTS job_visited (
job_id TEXT NOT NULL,
url TEXT NOT NULL,
PRIMARY KEY (job_id, url)
)
"""
)
cur.execute(
"""
CREATE TABLE IF NOT EXISTS job_frontier (
job_id TEXT NOT NULL,
position INTEGER NOT NULL,
url TEXT NOT NULL,
depth INTEGER NOT NULL,
origin_url TEXT NOT NULL,
PRIMARY KEY (job_id, url)
)
"""
)
cur.execute(
"""
CREATE TABLE IF NOT EXISTS job_events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
job_id TEXT NOT NULL,
created_at TEXT NOT NULL,
level TEXT NOT NULL,
message TEXT NOT NULL,
url TEXT,
depth INTEGER
)
"""
)
cur.execute(
"""
CREATE TABLE IF NOT EXISTS page_embeddings (
url TEXT PRIMARY KEY,
origin_url TEXT NOT NULL,
depth INTEGER NOT NULL,
title TEXT,
model_name TEXT NOT NULL,
vector_json TEXT NOT NULL,
updated_at TEXT NOT NULL
)
"""
)
cur.execute(
"""
CREATE TABLE IF NOT EXISTS embedding_jobs (
id TEXT PRIMARY KEY,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
status TEXT NOT NULL,
model_name TEXT NOT NULL,
rate_limit_per_sec REAL NOT NULL DEFAULT 1.0,
max_pages INTEGER,
total_pages INTEGER NOT NULL DEFAULT 0,
embedded_pages INTEGER NOT NULL DEFAULT 0,
failed_pages INTEGER NOT NULL DEFAULT 0,
error_message TEXT,
pending_urls_json TEXT NOT NULL DEFAULT '[]'
)
"""
)
conn.commit()
finally:
conn.close()
def _add_column_if_missing(cur: sqlite3.Cursor, table_name: str, column_name: str, definition: str) -> None:
cur.execute(f"PRAGMA table_info({table_name})")
existing = {str(row[1]) for row in cur.fetchall()}
if column_name not in existing:
cur.execute(f"ALTER TABLE {table_name} ADD COLUMN {column_name} {definition}")
def save_page(url: str, origin_url: str, depth: int, title: str, body_snippet: str) -> None:
conn = _connect()
try:
cur = conn.cursor()
cur.execute(
"""
INSERT OR REPLACE INTO pages (url, origin_url, depth, title, body_snippet)
VALUES (?, ?, ?, ?, ?)
""",
(url, origin_url, depth, title, body_snippet),
)
conn.commit()
finally:
conn.close()
def save_job(job: CrawlJob) -> None:
conn = _connect()
try:
cur = conn.cursor()
_upsert_job(cur, job)
conn.commit()
finally:
conn.close()
def save_job_state(
job: CrawlJob,
visited_urls: Set[str],
frontier_items: List[Tuple[str, int, str]],
) -> None:
conn = _connect()
try:
cur = conn.cursor()
_upsert_job(cur, job)
cur.execute("DELETE FROM job_visited WHERE job_id = ?", (job.id,))
cur.execute("DELETE FROM job_frontier WHERE job_id = ?", (job.id,))
cur.executemany(
"INSERT OR IGNORE INTO job_visited (job_id, url) VALUES (?, ?)",
[(job.id, url) for url in visited_urls],
)
cur.executemany(
"""
INSERT OR REPLACE INTO job_frontier (job_id, position, url, depth, origin_url)
VALUES (?, ?, ?, ?, ?)
""",
[(job.id, idx, url, depth, origin_url) for idx, (url, depth, origin_url) in enumerate(frontier_items)],
)
conn.commit()
finally:
conn.close()
def _upsert_job(cur: sqlite3.Cursor, job: CrawlJob) -> None:
cur.execute(
"""
INSERT OR REPLACE INTO jobs (
id,
origin_url,
max_depth,
max_urls_to_visit,
created_at,
updated_at,
status,
rate_limit_per_sec,
error_message,
processed_urls,
discovered_urls,
duplicate_urls,
failed_urls,
queued_urls,
queue_max,
active_workers,
backpressure_state
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
job.id,
job.origin_url,
job.max_depth,
job.max_urls_to_visit,
job.created_at.isoformat(),
job.updated_at.isoformat(),
job.status.value,
job.rate_limit_per_sec,
job.error_message,
job.stats.processed_urls,
job.stats.discovered_urls,
job.stats.duplicate_urls,
job.stats.failed_urls,
job.stats.queued_urls,
job.stats.queue_max,
job.stats.active_workers,
job.stats.backpressure_state,
),
)
def load_job_state(job_id: str) -> Tuple[Set[str], List[Tuple[str, int, str]]]:
conn = _connect()
try:
cur = conn.cursor()
cur.execute("SELECT url FROM job_visited WHERE job_id = ?", (job_id,))
visited = {str(row[0]) for row in cur.fetchall()}
cur.execute(
"""
SELECT url, depth, origin_url
FROM job_frontier
WHERE job_id = ?
ORDER BY position ASC
""",
(job_id,),
)
frontier = [(str(url), int(depth), str(origin_url)) for url, depth, origin_url in cur.fetchall()]
return visited, frontier
finally:
conn.close()
def append_job_event(
job_id: str,
level: str,
message: str,
url: str | None = None,
depth: int | None = None,
) -> None:
conn = _connect()
try:
cur = conn.cursor()
cur.execute(
"""
INSERT INTO job_events (job_id, created_at, level, message, url, depth)
VALUES (?, ?, ?, ?, ?, ?)
""",
(job_id, datetime.utcnow().isoformat(), level, message, url, depth),
)
conn.commit()
finally:
conn.close()
def load_job_events(job_id: str, limit: int = 100) -> List[Tuple[str, str, str, str, int | None]]:
conn = _connect()
try:
cur = conn.cursor()
cur.execute(
"""
SELECT created_at, level, message, url, depth
FROM job_events
WHERE job_id = ?
ORDER BY id DESC
LIMIT ?
""",
(job_id, limit),
)
rows = cur.fetchall()
rows.reverse()
return [
(str(created_at), str(level), str(message), str(url or ""), int(depth) if depth is not None else None)
for created_at, level, message, url, depth in rows
]
finally:
conn.close()
def load_pages() -> List[Tuple[str, str, int, str, str]]:
conn = _connect()
try:
cur = conn.cursor()
cur.execute("SELECT url, origin_url, depth, title, body_snippet FROM pages")
rows = list(cur.fetchall())
return [(str(u), str(o), int(d), str(t or ""), str(b or "")) for (u, o, d, t, b) in rows]
finally:
conn.close()
def load_jobs() -> List[CrawlJob]:
conn = _connect()
try:
cur = conn.cursor()
cur.execute(
"""
SELECT
id,
origin_url,
max_depth,
max_urls_to_visit,
created_at,
updated_at,
status,
rate_limit_per_sec,
error_message,
processed_urls,
discovered_urls,
duplicate_urls,
failed_urls,
queued_urls,
queue_max,
active_workers,
backpressure_state
FROM jobs
"""
)
rows = cur.fetchall()
jobs: List[CrawlJob] = []
for (
job_id,
origin_url,
max_depth,
max_urls_to_visit,
created_at,
updated_at,
status,
rate_limit_per_sec,
error_message,
processed_urls,
discovered_urls,
duplicate_urls,
failed_urls,
queued_urls,
queue_max,
active_workers,
backpressure_state,
) in rows:
jobs.append(
CrawlJob(
id=str(job_id),
origin_url=str(origin_url),
max_depth=int(max_depth),
created_at=datetime.fromisoformat(str(created_at)),
max_urls_to_visit=int(max_urls_to_visit) if max_urls_to_visit is not None else None,
status=JobStatus(str(status)),
rate_limit_per_sec=float(rate_limit_per_sec or 1.0),
error_message=str(error_message) if error_message is not None else None,
updated_at=datetime.fromisoformat(str(updated_at)) if updated_at else datetime.fromisoformat(str(created_at)),
stats=CrawlStats(
processed_urls=int(processed_urls or 0),
discovered_urls=int(discovered_urls or 0),
duplicate_urls=int(duplicate_urls or 0),
failed_urls=int(failed_urls or 0),
queued_urls=int(queued_urls or 0),
queue_max=int(queue_max or 0),
active_workers=int(active_workers or 0),
backpressure_state=str(backpressure_state or "idle"),
),
)
)
return jobs
finally:
conn.close()
def save_page_embedding(
url: str,
origin_url: str,
depth: int,
title: str,
model_name: str,
vector_json: str,
) -> None:
conn = _connect()
try:
cur = conn.cursor()
cur.execute(
"""
INSERT OR REPLACE INTO page_embeddings (url, origin_url, depth, title, model_name, vector_json, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(url, origin_url, depth, title, model_name, vector_json, datetime.utcnow().isoformat()),
)
conn.commit()
finally:
conn.close()
def load_page_embeddings() -> List[Tuple[str, str, int, str, str, str]]:
conn = _connect()
try:
cur = conn.cursor()
cur.execute("SELECT url, origin_url, depth, title, model_name, vector_json FROM page_embeddings")
rows = cur.fetchall()
return [(str(u), str(o), int(d), str(t or ""), str(m), str(v)) for u, o, d, t, m, v in rows]
finally:
conn.close()
def load_embedding_targets(
model_name: str,
limit: int | None,
only_missing: bool = True,
) -> List[Tuple[str, str, int, str, str]]:
conn = _connect()
try:
cur = conn.cursor()
if only_missing:
query = """
SELECT p.url, p.origin_url, p.depth, p.title, p.body_snippet
FROM pages p
LEFT JOIN page_embeddings e ON p.url = e.url AND e.model_name = ?
WHERE e.url IS NULL
ORDER BY p.depth ASC, p.url ASC
"""
params: Tuple[object, ...] = (model_name,)
else:
query = """
SELECT p.url, p.origin_url, p.depth, p.title, p.body_snippet
FROM pages p
ORDER BY p.depth ASC, p.url ASC
"""
params = tuple()
if limit is not None:
query += " LIMIT ?"
params = (*params, limit)
cur.execute(query, params)
rows = cur.fetchall()
return [(str(u), str(o), int(d), str(t or ""), str(b or "")) for (u, o, d, t, b) in rows]
finally:
conn.close()
def save_embedding_job(job: EmbeddingJobRecord) -> None:
conn = _connect()
try:
cur = conn.cursor()
cur.execute(
"""
INSERT OR REPLACE INTO embedding_jobs (
id,
created_at,
updated_at,
status,
model_name,
rate_limit_per_sec,
max_pages,
total_pages,
embedded_pages,
failed_pages,
error_message,
pending_urls_json
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
job.id,
job.created_at.isoformat(),
job.updated_at.isoformat(),
job.status.value,
job.model_name,
job.rate_limit_per_sec,
job.max_pages,
job.total_pages,
job.embedded_pages,
job.failed_pages,
job.error_message,
job.pending_urls_json,
),
)
conn.commit()
finally:
conn.close()
def load_embedding_jobs() -> List[EmbeddingJobRecord]:
conn = _connect()
try:
cur = conn.cursor()
cur.execute(
"""
SELECT
id,
created_at,
updated_at,
status,
model_name,
rate_limit_per_sec,
max_pages,
total_pages,
embedded_pages,
failed_pages,
error_message,
pending_urls_json
FROM embedding_jobs
ORDER BY created_at DESC
"""
)
rows = cur.fetchall()
jobs: List[EmbeddingJobRecord] = []
for (
job_id,
created_at,
updated_at,
status,
model_name,
rate_limit_per_sec,
max_pages,
total_pages,
embedded_pages,
failed_pages,
error_message,
pending_urls_json,
) in rows:
jobs.append(
EmbeddingJobRecord(
id=str(job_id),
created_at=datetime.fromisoformat(str(created_at)),
updated_at=datetime.fromisoformat(str(updated_at)),
status=JobStatus(str(status)),
model_name=str(model_name),
rate_limit_per_sec=float(rate_limit_per_sec or 1.0),
max_pages=int(max_pages) if max_pages is not None else None,
total_pages=int(total_pages or 0),
embedded_pages=int(embedded_pages or 0),
failed_pages=int(failed_pages or 0),
error_message=str(error_message) if error_message is not None else None,
pending_urls_json=str(pending_urls_json or "[]"),
)
)
return jobs
finally:
conn.close()
def count_pages() -> int:
conn = _connect()
try:
cur = conn.cursor()
cur.execute("SELECT COUNT(*) FROM pages")
row = cur.fetchone()
return int(row[0] if row and row[0] is not None else 0)
finally:
conn.close()
def count_page_embeddings(model_name: str) -> int:
conn = _connect()
try:
cur = conn.cursor()
cur.execute("SELECT COUNT(*) FROM page_embeddings WHERE model_name = ?", (model_name,))
row = cur.fetchone()
return int(row[0] if row and row[0] is not None else 0)
finally:
conn.close()
def delete_page_embeddings(model_name: str | None = None) -> int:
conn = _connect()
try:
cur = conn.cursor()
if model_name:
cur.execute("DELETE FROM page_embeddings WHERE model_name = ?", (model_name,))
else:
cur.execute("DELETE FROM page_embeddings")
deleted = int(cur.rowcount if cur.rowcount is not None else 0)
conn.commit()
return deleted
finally:
conn.close()