add caching, add to stats

This commit is contained in:
C committed 2026-01-05 16:01:36 -08:00
1 parent ab3297acfb
commit dfd2c2f07d
5 files changed
+62 -6

No files matched your search

+54 -3
View File
@@ -7,6 +7,7 @@ import json
from typing import Dict, Any
from square import AsyncSquare
from . import get_sq_env
from . import abricksetapi
# Initialize Square client
client = AsyncSquare(token=get_sq_env.SQUARE_TOKEN, environment=get_sq_env.environment)
@@ -101,12 +102,62 @@ async def get_daily_stats() -> Dict[str, Any]:
}
async def get_total_items() -> int:
async def get_brickset_cache_stats() -> Dict[str, Any]:
"""
Count total items in LEGO category.
Get Brickset API cache statistics.
Returns:
int: Total number of items
dict: {
"cache_enabled": bool,
"cache_hits": int,
"cache_misses": int,
"cache_size": int,
"cache_maxsize": int,
"hit_rate": float (0-100%)
}
"""
# Create a temporary client instance to access cache info
temp_client = abricksetapi.AsyncBricksetAPI()
# Check if get_set has cache_info (it should with @alru_cache)
if hasattr(temp_client.get_set, "cache_info"):
info = await temp_client.get_set.cache_info()
hits = info.hits
misses = info.misses
total = hits + misses
hit_rate = (hits / total * 100) if total > 0 else 0.0
return {
"cache_enabled": True,
"cache_hits": hits,
"cache_misses": misses,
"cache_size": info.currsize,
"cache_maxsize": info.maxsize,
"hit_rate": round(hit_rate, 2),
}
else:
return {
"cache_enabled": False,
"cache_hits": 0,
"cache_misses": 0,
"cache_size": 0,
"cache_maxsize": 0,
"hit_rate": 0.0,
}
async def get_daily_stats() -> Dict[str, Any]:
"""
Gather all metrics for daily_stats table in a single pass.
Returns:
dict: {
"total_items": int,
"enriched_items": int,
"missing_upcs": int,
"missing_images": int,
"total_catalog_value": float
}
"""
result = await client.catalog.search(
object_types=["ITEM"],