Spaces:
Sleeping

PERDYPTO commited on
Commit
ecab2c7
·
verified ·
1 Parent(s): 52bb87c

Update hf_space.py

Browse files
Files changed (1) hide show
  1. hf_space.py +30 -56
hf_space.py CHANGED
@@ -36,31 +36,19 @@ DUCKDB_THREADS = os.environ.get("DUCKDB_THREADS", "2")
36
  METADATA_TABLE_SCHEMA = """
37
  CREATE TABLE IF NOT EXISTS metadata (
38
  repo_id INTEGER PRIMARY KEY,
39
- full_name TEXT,
40
  description TEXT,
41
  homepage_url TEXT,
42
  stars INTEGER,
43
  forks_count INTEGER,
44
  watchers_count INTEGER,
45
- open_issues INTEGER,
46
- license TEXT,
47
- license_name TEXT,
48
  archived INTEGER,
49
  disabled INTEGER,
50
  is_fork INTEGER,
51
- is_private INTEGER,
 
 
52
  is_template INTEGER,
53
- disk_usage_kb INTEGER,
54
  primary_language TEXT,
55
- default_branch TEXT,
56
- topics_json TEXT,
57
- created_at TEXT,
58
- updated_at TEXT,
59
- pushed_at TEXT,
60
- languages_json TEXT,
61
- python_bytes INTEGER,
62
- total_lang_bytes INTEGER,
63
- python_pct REAL,
64
  fetched_at TEXT
65
  );
66
  """
@@ -80,31 +68,19 @@ CREATE TABLE IF NOT EXISTS repo_ids (
80
 
81
  CREATE TABLE IF NOT EXISTS metadata (
82
  repo_id INTEGER PRIMARY KEY,
83
- full_name TEXT,
84
  description TEXT,
85
  homepage_url TEXT,
86
  stars INTEGER,
87
  forks_count INTEGER,
88
  watchers_count INTEGER,
89
- open_issues INTEGER,
90
- license TEXT,
91
- license_name TEXT,
92
  archived INTEGER,
93
  disabled INTEGER,
94
  is_fork INTEGER,
95
- is_private INTEGER,
 
 
96
  is_template INTEGER,
97
- disk_usage_kb INTEGER,
98
  primary_language TEXT,
99
- default_branch TEXT,
100
- topics_json TEXT,
101
- created_at TEXT,
102
- updated_at TEXT,
103
- pushed_at TEXT,
104
- languages_json TEXT,
105
- python_bytes INTEGER,
106
- total_lang_bytes INTEGER,
107
- python_pct REAL,
108
  fetched_at TEXT
109
  );
110
 
@@ -230,6 +206,13 @@ def run_duckdb_ingest_sync():
230
  print("[Ingest] DuckDB or HfFileSystem not installed. Ingestion disabled.")
231
  return
232
 
 
 
 
 
 
 
 
233
  print(f"[Ingest] Starting DuckDB remote join (memory_limit={DUCKDB_MEMORY_LIMIT}, threads={DUCKDB_THREADS})...")
234
  try:
235
  fs = HfFileSystem()
@@ -370,14 +353,14 @@ async def reset_errors():
370
 
371
  @app.get("/reset-metadata")
372
  async def reset_metadata():
373
- """Drops the metadata table (removing old schema) and recreates it with new columns.
374
  Resets ALL repo_ids back to pending so workers can re-scrape them with the new fields.
375
  Does NOT trigger DuckDB to re-run.
376
  """
377
  with db_lock:
378
  print("[RESET] Dropping metadata table and resetting all jobs to pending...")
379
  db_conn.execute("DROP TABLE IF EXISTS metadata;")
380
- db_conn.execute(METADATA_TABLE_SCHEMA) # Only recreates the metadata table
381
  db_conn.execute("UPDATE repo_ids SET status='pending', last_error=NULL, updated_at=?", (now(),))
382
  db_conn.commit()
383
 
@@ -386,7 +369,7 @@ async def reset_metadata():
386
  processing_ids.clear()
387
  loaded = await refill_pending_from_db(MAX_RAM_PENDING)
388
 
389
- return {"status": "success", "message": "Metadata wiped and recreated. All jobs reset to pending.", "ram_loaded": loaded}
390
 
391
  @app.post("/post-work")
392
  async def post_work(payload: WorkPayload):
@@ -399,32 +382,23 @@ async def post_work(payload: WorkPayload):
399
  db_conn.execute(
400
  """
401
  INSERT INTO metadata (
402
- repo_id, full_name, description, homepage_url, stars, forks_count,
403
- watchers_count, open_issues, license, license_name, archived, disabled,
404
- is_fork, is_private, is_template, disk_usage_kb, primary_language,
405
- default_branch, topics_json, created_at, updated_at, pushed_at,
406
- languages_json, python_bytes, total_lang_bytes, python_pct, fetched_at
407
- ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
408
  ON CONFLICT(repo_id) DO UPDATE SET
409
- full_name=excluded.full_name, description=excluded.description, homepage_url=excluded.homepage_url,
410
- stars=excluded.stars, forks_count=excluded.forks_count, watchers_count=excluded.watchers_count,
411
- open_issues=excluded.open_issues, license=excluded.license, license_name=excluded.license_name,
412
- archived=excluded.archived, disabled=excluded.disabled, is_fork=excluded.is_fork,
413
- is_private=excluded.is_private, is_template=excluded.is_template, disk_usage_kb=excluded.disk_usage_kb,
414
- primary_language=excluded.primary_language, default_branch=excluded.default_branch,
415
- topics_json=excluded.topics_json, created_at=excluded.created_at, updated_at=excluded.updated_at,
416
- pushed_at=excluded.pushed_at, languages_json=excluded.languages_json,
417
- python_bytes=excluded.python_bytes, total_lang_bytes=excluded.total_lang_bytes,
418
- python_pct=excluded.python_pct, fetched_at=excluded.fetched_at
419
  """,
420
  (
421
- rid, m.get("full_name"), m.get("description"), m.get("homepage_url"), m.get("stars"),
422
- m.get("forks_count"), m.get("watchers_count"), m.get("open_issues", 0), m.get("license"),
423
- m.get("license_name"), m.get("archived", 0), m.get("disabled", 0), m.get("is_fork", 0),
424
- m.get("is_private", 0), m.get("is_template", 0), m.get("disk_usage_kb"), m.get("primary_language"),
425
- m.get("default_branch"), m.get("topics_json"), m.get("created_at"), m.get("updated_at"),
426
- m.get("pushed_at"), m.get("languages_json"), m.get("python_bytes"), m.get("total_lang_bytes"),
427
- m.get("python_pct"), ts
428
  )
429
  )
430
  db_conn.execute("UPDATE repo_ids SET status='done', last_error=NULL, updated_at=? WHERE repo_id=?", (ts, rid))
 
36
  METADATA_TABLE_SCHEMA = """
37
  CREATE TABLE IF NOT EXISTS metadata (
38
  repo_id INTEGER PRIMARY KEY,
 
39
  description TEXT,
40
  homepage_url TEXT,
41
  stars INTEGER,
42
  forks_count INTEGER,
43
  watchers_count INTEGER,
 
 
 
44
  archived INTEGER,
45
  disabled INTEGER,
46
  is_fork INTEGER,
47
+ resolved_parent_id INTEGER,
48
+ resolved_parent_path TEXT,
49
+ pushed_at TEXT,
50
  is_template INTEGER,
 
51
  primary_language TEXT,
 
 
 
 
 
 
 
 
 
52
  fetched_at TEXT
53
  );
54
  """
 
68
 
69
  CREATE TABLE IF NOT EXISTS metadata (
70
  repo_id INTEGER PRIMARY KEY,
 
71
  description TEXT,
72
  homepage_url TEXT,
73
  stars INTEGER,
74
  forks_count INTEGER,
75
  watchers_count INTEGER,
 
 
 
76
  archived INTEGER,
77
  disabled INTEGER,
78
  is_fork INTEGER,
79
+ resolved_parent_id INTEGER,
80
+ resolved_parent_path TEXT,
81
+ pushed_at TEXT,
82
  is_template INTEGER,
 
83
  primary_language TEXT,
 
 
 
 
 
 
 
 
 
84
  fetched_at TEXT
85
  );
86
 
 
206
  print("[Ingest] DuckDB or HfFileSystem not installed. Ingestion disabled.")
207
  return
208
 
209
+ # Skip DuckDB if we already have the repo IDs
210
+ with db_lock:
211
+ count = db_conn.execute("SELECT COUNT(*) as c FROM repo_ids").fetchone()["c"]
212
+ if count > 1000000:
213
+ print(f"[Ingest] Found {count:,} existing repo_ids. Skipping DuckDB remote join.")
214
+ return
215
+
216
  print(f"[Ingest] Starting DuckDB remote join (memory_limit={DUCKDB_MEMORY_LIMIT}, threads={DUCKDB_THREADS})...")
217
  try:
218
  fs = HfFileSystem()
 
353
 
354
  @app.get("/reset-metadata")
355
  async def reset_metadata():
356
+ """Drops the metadata table (removing old schema) and recreates it with new lean columns.
357
  Resets ALL repo_ids back to pending so workers can re-scrape them with the new fields.
358
  Does NOT trigger DuckDB to re-run.
359
  """
360
  with db_lock:
361
  print("[RESET] Dropping metadata table and resetting all jobs to pending...")
362
  db_conn.execute("DROP TABLE IF EXISTS metadata;")
363
+ db_conn.execute(METADATA_TABLE_SCHEMA) # Only recreates the lean metadata table
364
  db_conn.execute("UPDATE repo_ids SET status='pending', last_error=NULL, updated_at=?", (now(),))
365
  db_conn.commit()
366
 
 
369
  processing_ids.clear()
370
  loaded = await refill_pending_from_db(MAX_RAM_PENDING)
371
 
372
+ return {"status": "success", "message": "Metadata wiped and recreated with lean schema. All jobs reset to pending.", "ram_loaded": loaded}
373
 
374
  @app.post("/post-work")
375
  async def post_work(payload: WorkPayload):
 
382
  db_conn.execute(
383
  """
384
  INSERT INTO metadata (
385
+ repo_id, description, homepage_url, stars, forks_count, watchers_count,
386
+ archived, disabled, is_fork, resolved_parent_id, resolved_parent_path,
387
+ pushed_at, is_template, primary_language, fetched_at
388
+ ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
 
 
389
  ON CONFLICT(repo_id) DO UPDATE SET
390
+ description=excluded.description, homepage_url=excluded.homepage_url, stars=excluded.stars,
391
+ forks_count=excluded.forks_count, watchers_count=excluded.watchers_count, archived=excluded.archived,
392
+ disabled=excluded.disabled, is_fork=excluded.is_fork, resolved_parent_id=excluded.resolved_parent_id,
393
+ resolved_parent_path=excluded.resolved_parent_path, pushed_at=excluded.pushed_at,
394
+ is_template=excluded.is_template, primary_language=excluded.primary_language, fetched_at=excluded.fetched_at
 
 
 
 
 
395
  """,
396
  (
397
+ rid, m.get("description"), m.get("homepage_url"), m.get("stars"),
398
+ m.get("forks_count"), m.get("watchers_count"), m.get("archived", 0),
399
+ m.get("disabled", 0), m.get("is_fork", 0), m.get("resolved_parent_id"),
400
+ m.get("resolved_parent_path"), m.get("pushed_at"), m.get("is_template", 0),
401
+ m.get("primary_language"), ts
 
 
402
  )
403
  )
404
  db_conn.execute("UPDATE repo_ids SET status='done', last_error=NULL, updated_at=? WHERE repo_id=?", (ts, rid))