Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -307,9 +307,11 @@ def get_type_filter(search_type: str) -> str:
|
|
| 307 |
|
| 308 |
|
| 309 |
def search_repos(
|
| 310 |
-
conn, query: str, search_type: str = "all",
|
| 311 |
-
) ->
|
| 312 |
-
"""Search repositories by query string."""
|
|
|
|
|
|
|
| 313 |
search_term_like = f"%{query}%"
|
| 314 |
fts_query = " ".join(
|
| 315 |
'"' + word.replace('"', '""') + '"*' for word in query.split() if word
|
|
@@ -317,6 +319,34 @@ def search_repos(
|
|
| 317 |
|
| 318 |
type_filter = get_type_filter(search_type)
|
| 319 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
sql = f"""
|
| 321 |
WITH matched_ids AS (
|
| 322 |
SELECT repo_id FROM repo_search WHERE keywords MATCH ?1
|
|
@@ -324,17 +354,22 @@ def search_repos(
|
|
| 324 |
SELECT id FROM repos
|
| 325 |
WHERE id LIKE ?2 OR owner LIKE ?2 OR description LIKE ?2 OR primary_language LIKE ?2
|
| 326 |
),
|
| 327 |
-
|
| 328 |
-
SELECT
|
| 329 |
-
{_REPO_COLUMNS}
|
| 330 |
FROM repos r
|
| 331 |
-
{_REPO_JOIN_USERS}
|
| 332 |
JOIN matched_ids mi ON r.id = mi.repo_id
|
| 333 |
LEFT JOIN packages pkg ON r.id = pkg.repo_id
|
| 334 |
LEFT JOIN programs prog ON r.id = prog.repo_id
|
| 335 |
WHERE
|
| 336 |
r.is_disabled = 0
|
| 337 |
{type_filter}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
)
|
| 339 |
SELECT
|
| 340 |
rd.*,
|
|
@@ -347,14 +382,25 @@ def search_repos(
|
|
| 347 |
ELSE 3
|
| 348 |
END,
|
| 349 |
rd.stargazer_count DESC
|
| 350 |
-
LIMIT ?5
|
| 351 |
"""
|
| 352 |
|
| 353 |
starts_with = f"{query}%"
|
| 354 |
-
cursor = conn.execute(
|
|
|
|
|
|
|
| 355 |
rows = cursor.fetchall()
|
|
|
|
| 356 |
|
| 357 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
|
| 359 |
|
| 360 |
def get_latest_repos(
|
|
@@ -684,21 +730,23 @@ def check_db_connection():
|
|
| 684 |
@app.get("/search/packages", tags=["Search"])
|
| 685 |
async def search_packages_endpoint(
|
| 686 |
q: str = Query(..., min_length=1, description="Search query"),
|
| 687 |
-
|
|
|
|
| 688 |
):
|
| 689 |
-
"""Search for Zig packages."""
|
| 690 |
check_db_connection()
|
| 691 |
-
return search_repos(db_conn, q, search_type="package",
|
| 692 |
|
| 693 |
|
| 694 |
@app.get("/search/programs", tags=["Search"])
|
| 695 |
async def search_programs_endpoint(
|
| 696 |
q: str = Query(..., min_length=1, description="Search query"),
|
| 697 |
-
|
|
|
|
| 698 |
):
|
| 699 |
-
"""Search for Zig programs."""
|
| 700 |
check_db_connection()
|
| 701 |
-
return search_repos(db_conn, q, search_type="program",
|
| 702 |
|
| 703 |
|
| 704 |
@app.get("/packageIndexDetails", tags=["Packages"])
|
|
@@ -816,4 +864,4 @@ async def health_check():
|
|
| 816 |
if __name__ == "__main__":
|
| 817 |
import uvicorn
|
| 818 |
|
| 819 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 307 |
|
| 308 |
|
| 309 |
def search_repos(
|
| 310 |
+
conn, query: str, search_type: str = "all", per_page: int = 15, page: int = 1
|
| 311 |
+
) -> Dict[str, Any]:
|
| 312 |
+
"""Search repositories by query string with pagination."""
|
| 313 |
+
actual_per_page = min(max(per_page, 1), 15)
|
| 314 |
+
requested_page = max(page, 1)
|
| 315 |
search_term_like = f"%{query}%"
|
| 316 |
fts_query = " ".join(
|
| 317 |
'"' + word.replace('"', '""') + '"*' for word in query.split() if word
|
|
|
|
| 319 |
|
| 320 |
type_filter = get_type_filter(search_type)
|
| 321 |
|
| 322 |
+
count_sql = f"""
|
| 323 |
+
WITH matched_ids AS (
|
| 324 |
+
SELECT repo_id FROM repo_search WHERE keywords MATCH ?1
|
| 325 |
+
UNION
|
| 326 |
+
SELECT id FROM repos
|
| 327 |
+
WHERE id LIKE ?2 OR owner LIKE ?2 OR description LIKE ?2 OR primary_language LIKE ?2
|
| 328 |
+
),
|
| 329 |
+
filtered_repos AS (
|
| 330 |
+
SELECT DISTINCT r.id
|
| 331 |
+
FROM repos r
|
| 332 |
+
JOIN matched_ids mi ON r.id = mi.repo_id
|
| 333 |
+
LEFT JOIN packages pkg ON r.id = pkg.repo_id
|
| 334 |
+
LEFT JOIN programs prog ON r.id = prog.repo_id
|
| 335 |
+
WHERE
|
| 336 |
+
r.is_disabled = 0
|
| 337 |
+
{type_filter}
|
| 338 |
+
)
|
| 339 |
+
SELECT COUNT(*) FROM filtered_repos
|
| 340 |
+
"""
|
| 341 |
+
|
| 342 |
+
total_count = conn.execute(count_sql, (fts_query, search_term_like)).fetchone()[0] or 0
|
| 343 |
+
|
| 344 |
+
total_pages = (
|
| 345 |
+
(total_count + actual_per_page - 1) // actual_per_page if total_count > 0 else 0
|
| 346 |
+
)
|
| 347 |
+
current_page = min(requested_page, total_pages) if total_pages > 0 else 1
|
| 348 |
+
offset = (current_page - 1) * actual_per_page
|
| 349 |
+
|
| 350 |
sql = f"""
|
| 351 |
WITH matched_ids AS (
|
| 352 |
SELECT repo_id FROM repo_search WHERE keywords MATCH ?1
|
|
|
|
| 354 |
SELECT id FROM repos
|
| 355 |
WHERE id LIKE ?2 OR owner LIKE ?2 OR description LIKE ?2 OR primary_language LIKE ?2
|
| 356 |
),
|
| 357 |
+
filtered_repos AS (
|
| 358 |
+
SELECT DISTINCT r.id
|
|
|
|
| 359 |
FROM repos r
|
|
|
|
| 360 |
JOIN matched_ids mi ON r.id = mi.repo_id
|
| 361 |
LEFT JOIN packages pkg ON r.id = pkg.repo_id
|
| 362 |
LEFT JOIN programs prog ON r.id = prog.repo_id
|
| 363 |
WHERE
|
| 364 |
r.is_disabled = 0
|
| 365 |
{type_filter}
|
| 366 |
+
),
|
| 367 |
+
repo_data AS (
|
| 368 |
+
SELECT
|
| 369 |
+
{_REPO_COLUMNS}
|
| 370 |
+
FROM repos r
|
| 371 |
+
{_REPO_JOIN_USERS}
|
| 372 |
+
JOIN filtered_repos fr ON r.id = fr.id
|
| 373 |
)
|
| 374 |
SELECT
|
| 375 |
rd.*,
|
|
|
|
| 382 |
ELSE 3
|
| 383 |
END,
|
| 384 |
rd.stargazer_count DESC
|
| 385 |
+
LIMIT ?5 OFFSET ?6
|
| 386 |
"""
|
| 387 |
|
| 388 |
starts_with = f"{query}%"
|
| 389 |
+
cursor = conn.execute(
|
| 390 |
+
sql, (fts_query, search_term_like, query, starts_with, actual_per_page, offset)
|
| 391 |
+
)
|
| 392 |
rows = cursor.fetchall()
|
| 393 |
+
items = [row_to_repo_dict(row) for row in rows]
|
| 394 |
|
| 395 |
+
return {
|
| 396 |
+
"items": items,
|
| 397 |
+
"total": total_count,
|
| 398 |
+
"page": current_page,
|
| 399 |
+
"per_page": actual_per_page,
|
| 400 |
+
"total_pages": total_pages,
|
| 401 |
+
"has_previous_page": current_page > 1,
|
| 402 |
+
"has_next_page": current_page < total_pages,
|
| 403 |
+
}
|
| 404 |
|
| 405 |
|
| 406 |
def get_latest_repos(
|
|
|
|
| 730 |
@app.get("/search/packages", tags=["Search"])
|
| 731 |
async def search_packages_endpoint(
|
| 732 |
q: str = Query(..., min_length=1, description="Search query"),
|
| 733 |
+
per_page: int = Query(15, ge=1, le=15, description="Items per page"),
|
| 734 |
+
page: int = Query(1, ge=1, description="Page number"),
|
| 735 |
):
|
| 736 |
+
"""Search for Zig packages with pagination."""
|
| 737 |
check_db_connection()
|
| 738 |
+
return search_repos(db_conn, q, search_type="package", per_page=per_page, page=page)
|
| 739 |
|
| 740 |
|
| 741 |
@app.get("/search/programs", tags=["Search"])
|
| 742 |
async def search_programs_endpoint(
|
| 743 |
q: str = Query(..., min_length=1, description="Search query"),
|
| 744 |
+
per_page: int = Query(15, ge=1, le=15, description="Items per page"),
|
| 745 |
+
page: int = Query(1, ge=1, description="Page number"),
|
| 746 |
):
|
| 747 |
+
"""Search for Zig programs with pagination."""
|
| 748 |
check_db_connection()
|
| 749 |
+
return search_repos(db_conn, q, search_type="program", per_page=per_page, page=page)
|
| 750 |
|
| 751 |
|
| 752 |
@app.get("/packageIndexDetails", tags=["Packages"])
|
|
|
|
| 864 |
if __name__ == "__main__":
|
| 865 |
import uvicorn
|
| 866 |
|
| 867 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|