Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -324,12 +324,23 @@ async def seed_test_samples():
|
|
| 324 |
@app.get("/api/health")
|
| 325 |
async def health_check():
|
| 326 |
"""Returns storage manager status and index counts."""
|
| 327 |
-
|
| 328 |
for item_type, config in STORAGE_MAP.items():
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
|
| 335 |
|
|
@@ -344,38 +355,40 @@ class SortBy(str, Enum):
|
|
| 344 |
async def list_library(
|
| 345 |
type: Optional[str] = Query(None),
|
| 346 |
genre: Optional[str] = Query(None),
|
| 347 |
-
min_rating: Optional[int] = Query(None, ge=1, le=10)
|
|
|
|
|
|
|
| 348 |
):
|
| 349 |
-
|
| 350 |
cache_key = f"library:{type or 'all'}:{sort_by}:{sort_desc}:{genre}:{min_rating}"
|
| 351 |
cached = await cache.get(cache_key)
|
| 352 |
if cached:
|
| 353 |
return cached
|
|
|
|
| 354 |
search_types = [type] if type else ["song", "pattern", "bank", "sample", "music", "shader"]
|
| 355 |
results = []
|
|
|
|
| 356 |
for t in search_types:
|
| 357 |
config = STORAGE_MAP.get(t, STORAGE_MAP["default"])
|
| 358 |
try:
|
| 359 |
-
# Fetch index file from GCS
|
| 360 |
items = await run_io(_read_json_sync, config["index"])
|
| 361 |
if isinstance(items, list):
|
| 362 |
results.extend(items)
|
| 363 |
except Exception as e:
|
| 364 |
-
|
| 365 |
-
|
| 366 |
if genre:
|
| 367 |
results = [r for r in results if r.get("genre") == genre]
|
| 368 |
-
|
| 369 |
if min_rating is not None:
|
| 370 |
results = [r for r in results if (r.get("rating") or 0) >= min_rating]
|
| 371 |
-
|
|
|
|
| 372 |
def sort_key(item):
|
| 373 |
val = item.get(sort_by.value)
|
| 374 |
-
if val is None
|
| 375 |
-
|
| 376 |
-
return (1, "")
|
| 377 |
-
return (0, val)
|
| 378 |
results.sort(key=sort_key, reverse=sort_desc)
|
|
|
|
| 379 |
await cache.set(cache_key, results, ttl=30)
|
| 380 |
return results
|
| 381 |
|
|
|
|
| 324 |
@app.get("/api/health")
|
| 325 |
async def health_check():
|
| 326 |
"""Returns storage manager status and index counts."""
|
| 327 |
+
status_report = {}
|
| 328 |
for item_type, config in STORAGE_MAP.items():
|
| 329 |
+
try:
|
| 330 |
+
# Attempt to read the index to verify connectivity
|
| 331 |
+
index_data = await run_io(_read_json_sync, config["index"])
|
| 332 |
+
status_report[item_type] = {
|
| 333 |
+
"count": len(index_data) if isinstance(index_data, list) else 0,
|
| 334 |
+
"status": "connected"
|
| 335 |
+
}
|
| 336 |
+
except Exception as e:
|
| 337 |
+
status_report[item_type] = {"count": 0, "status": "error", "error": str(e)}
|
| 338 |
+
|
| 339 |
+
return {
|
| 340 |
+
"status": "online",
|
| 341 |
+
"gcs_connected": bucket is not None,
|
| 342 |
+
"storage": status_report
|
| 343 |
+
}
|
| 344 |
|
| 345 |
|
| 346 |
|
|
|
|
| 355 |
async def list_library(
|
| 356 |
type: Optional[str] = Query(None),
|
| 357 |
genre: Optional[str] = Query(None),
|
| 358 |
+
min_rating: Optional[int] = Query(None, ge=1, le=10),
|
| 359 |
+
sort_by: SortBy = Query(SortBy.date), # Added missing param
|
| 360 |
+
sort_desc: bool = Query(True) # Added missing param
|
| 361 |
):
|
|
|
|
| 362 |
cache_key = f"library:{type or 'all'}:{sort_by}:{sort_desc}:{genre}:{min_rating}"
|
| 363 |
cached = await cache.get(cache_key)
|
| 364 |
if cached:
|
| 365 |
return cached
|
| 366 |
+
|
| 367 |
search_types = [type] if type else ["song", "pattern", "bank", "sample", "music", "shader"]
|
| 368 |
results = []
|
| 369 |
+
|
| 370 |
for t in search_types:
|
| 371 |
config = STORAGE_MAP.get(t, STORAGE_MAP["default"])
|
| 372 |
try:
|
|
|
|
| 373 |
items = await run_io(_read_json_sync, config["index"])
|
| 374 |
if isinstance(items, list):
|
| 375 |
results.extend(items)
|
| 376 |
except Exception as e:
|
| 377 |
+
logging.error(f"Error listing {t}: {e}")
|
| 378 |
+
|
| 379 |
if genre:
|
| 380 |
results = [r for r in results if r.get("genre") == genre]
|
| 381 |
+
|
| 382 |
if min_rating is not None:
|
| 383 |
results = [r for r in results if (r.get("rating") or 0) >= min_rating]
|
| 384 |
+
|
| 385 |
+
# Sorting Logic
|
| 386 |
def sort_key(item):
|
| 387 |
val = item.get(sort_by.value)
|
| 388 |
+
return (0, val) if val is not None else (1, "")
|
| 389 |
+
|
|
|
|
|
|
|
| 390 |
results.sort(key=sort_key, reverse=sort_desc)
|
| 391 |
+
|
| 392 |
await cache.set(cache_key, results, ttl=30)
|
| 393 |
return results
|
| 394 |
|