PERDYPTO commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,146 +1,121 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import sqlite3
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
app = FastAPI()
|
| 13 |
-
db_lock = threading.Lock()
|
| 14 |
-
|
| 15 |
-
@app.on_event("startup")
|
| 16 |
-
async def startup_event():
|
| 17 |
-
if not os.path.exists(DB_PATH):
|
| 18 |
-
print("Downloading full 3GB database from HF Bucket... (This will take a minute)")
|
| 19 |
-
token = os.environ.get("HF_TOKEN")
|
| 20 |
-
if token:
|
| 21 |
-
login(token=token, add_to_git_credential=False)
|
| 22 |
-
|
| 23 |
-
# Download the massive DB
|
| 24 |
-
download_bucket_files(BUCKET, files=[("github_pipeline.db", DB_PATH)])
|
| 25 |
-
print("✅ Database downloaded successfully!")
|
| 26 |
-
|
| 27 |
-
# Create indexes to ensure web queries are instant
|
| 28 |
-
print("Creating database indexes for fast querying...")
|
| 29 |
-
conn = sqlite3.connect(DB_PATH)
|
| 30 |
-
conn.execute("CREATE INDEX IF NOT EXISTS idx_meta_stars ON metadata(stars);")
|
| 31 |
-
conn.execute("CREATE INDEX IF NOT EXISTS idx_meta_forks ON metadata(forks_count);")
|
| 32 |
-
conn.execute("CREATE INDEX IF NOT EXISTS idx_meta_lang ON metadata(primary_language);")
|
| 33 |
-
conn.execute("CREATE INDEX IF NOT EXISTS idx_meta_fork ON metadata(is_fork);")
|
| 34 |
-
conn.execute("CREATE INDEX IF NOT EXISTS idx_meta_archived ON metadata(archived);")
|
| 35 |
-
conn.commit()
|
| 36 |
-
conn.close()
|
| 37 |
-
print("✅ Indexes ready!")
|
| 38 |
-
else:
|
| 39 |
-
print("Database already exists.")
|
| 40 |
-
|
| 41 |
-
def get_db():
|
| 42 |
-
conn = sqlite3.connect(DB_PATH, check_same_thread=False, timeout=30)
|
| 43 |
conn.row_factory = sqlite3.Row
|
| 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 |
-
db.close()
|
| 140 |
-
|
| 141 |
-
return JSONResponse({
|
| 142 |
-
"total_matches": total,
|
| 143 |
-
"limit": limit,
|
| 144 |
-
"offset": offset,
|
| 145 |
-
"repos": [dict(r) for r in repos]
|
| 146 |
-
})
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
inspect_db.py
|
| 3 |
+
|
| 4 |
+
Run this ONCE on your machine, in the same folder as github_pipeline.db
|
| 5 |
+
(or pass the path as an argument). It prints a full report of every table,
|
| 6 |
+
every column (name + type), row counts, and a sample row so you know
|
| 7 |
+
exactly what data you have.
|
| 8 |
+
|
| 9 |
+
Usage:
|
| 10 |
+
python inspect_db.py
|
| 11 |
+
python inspect_db.py /path/to/github_pipeline.db
|
| 12 |
+
|
| 13 |
+
Copy the ENTIRE output back to Claude so it can build the full explorer
|
| 14 |
+
using your real schema instead of guessing.
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
import sqlite3
|
| 18 |
+
import sys
|
| 19 |
+
import json
|
| 20 |
+
|
| 21 |
+
DB_PATH = sys.argv[1] if len(sys.argv) > 1 else "github_pipeline.db"
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def main():
|
| 25 |
+
conn = sqlite3.connect(DB_PATH)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
conn.row_factory = sqlite3.Row
|
| 27 |
+
cur = conn.cursor()
|
| 28 |
+
|
| 29 |
+
print("=" * 70)
|
| 30 |
+
print(f"DATABASE REPORT: {DB_PATH}")
|
| 31 |
+
print("=" * 70)
|
| 32 |
+
|
| 33 |
+
# 1. List all tables
|
| 34 |
+
tables = cur.execute(
|
| 35 |
+
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%';"
|
| 36 |
+
).fetchall()
|
| 37 |
+
|
| 38 |
+
if not tables:
|
| 39 |
+
print("No tables found. Is this the right database file?")
|
| 40 |
+
return
|
| 41 |
+
|
| 42 |
+
print(f"\nFound {len(tables)} table(s): {', '.join(t['name'] for t in tables)}\n")
|
| 43 |
+
|
| 44 |
+
for t in tables:
|
| 45 |
+
table_name = t["name"]
|
| 46 |
+
print("-" * 70)
|
| 47 |
+
print(f"TABLE: {table_name}")
|
| 48 |
+
print("-" * 70)
|
| 49 |
+
|
| 50 |
+
# Row count
|
| 51 |
+
try:
|
| 52 |
+
row_count = cur.execute(f'SELECT COUNT(*) FROM "{table_name}"').fetchone()[0]
|
| 53 |
+
except Exception as e:
|
| 54 |
+
row_count = f"ERROR: {e}"
|
| 55 |
+
print(f"Row count: {row_count}")
|
| 56 |
+
|
| 57 |
+
# Columns (name, type, notnull, default, pk)
|
| 58 |
+
cols = cur.execute(f'PRAGMA table_info("{table_name}")').fetchall()
|
| 59 |
+
print(f"Columns ({len(cols)}):")
|
| 60 |
+
for c in cols:
|
| 61 |
+
flags = []
|
| 62 |
+
if c["pk"]:
|
| 63 |
+
flags.append("PRIMARY KEY")
|
| 64 |
+
if c["notnull"]:
|
| 65 |
+
flags.append("NOT NULL")
|
| 66 |
+
flag_str = f" [{', '.join(flags)}]" if flags else ""
|
| 67 |
+
print(f" - {c['name']:<28} {c['type'] or '(no type)':<15}{flag_str}")
|
| 68 |
+
|
| 69 |
+
# Indexes
|
| 70 |
+
idxs = cur.execute(f'PRAGMA index_list("{table_name}")').fetchall()
|
| 71 |
+
if idxs:
|
| 72 |
+
print(f"Indexes:")
|
| 73 |
+
for ix in idxs:
|
| 74 |
+
idx_cols = cur.execute(f'PRAGMA index_info("{ix["name"]}")').fetchall()
|
| 75 |
+
col_names = ", ".join(c["name"] for c in idx_cols)
|
| 76 |
+
print(f" - {ix['name']} ON ({col_names})")
|
| 77 |
+
|
| 78 |
+
# Sample row (first non-null-heavy row)
|
| 79 |
+
try:
|
| 80 |
+
sample = cur.execute(f'SELECT * FROM "{table_name}" LIMIT 1').fetchone()
|
| 81 |
+
if sample:
|
| 82 |
+
print("Sample row:")
|
| 83 |
+
sample_dict = dict(sample)
|
| 84 |
+
# Truncate long values for readability
|
| 85 |
+
for k, v in sample_dict.items():
|
| 86 |
+
if isinstance(v, str) and len(v) > 120:
|
| 87 |
+
sample_dict[k] = v[:120] + "...(truncated)"
|
| 88 |
+
print(json.dumps(sample_dict, indent=2, default=str))
|
| 89 |
+
except Exception as e:
|
| 90 |
+
print(f"Could not fetch sample row: {e}")
|
| 91 |
+
|
| 92 |
+
# For likely-categorical text columns, show distinct value counts (cheap heuristic)
|
| 93 |
+
for c in cols:
|
| 94 |
+
col_name = c["name"]
|
| 95 |
+
col_type = (c["type"] or "").upper()
|
| 96 |
+
if "CHAR" in col_type or "TEXT" in col_type or col_type == "":
|
| 97 |
+
try:
|
| 98 |
+
distinct_count = cur.execute(
|
| 99 |
+
f'SELECT COUNT(DISTINCT "{col_name}") FROM "{table_name}"'
|
| 100 |
+
).fetchone()[0]
|
| 101 |
+
# Only show if it looks like a low-cardinality "category" column
|
| 102 |
+
if isinstance(row_count, int) and row_count > 0 and distinct_count <= 30:
|
| 103 |
+
vals = cur.execute(
|
| 104 |
+
f'SELECT DISTINCT "{col_name}" FROM "{table_name}" '
|
| 105 |
+
f'WHERE "{col_name}" IS NOT NULL LIMIT 30'
|
| 106 |
+
).fetchall()
|
| 107 |
+
val_list = [v[0] for v in vals]
|
| 108 |
+
print(f" -> '{col_name}' looks categorical ({distinct_count} distinct): {val_list}")
|
| 109 |
+
except Exception:
|
| 110 |
+
pass
|
| 111 |
+
|
| 112 |
+
print()
|
| 113 |
+
|
| 114 |
+
conn.close()
|
| 115 |
+
print("=" * 70)
|
| 116 |
+
print("END OF REPORT — copy everything above this line back to Claude")
|
| 117 |
+
print("=" * 70)
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
if __name__ == "__main__":
|
| 121 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|