Compare commits

..

No commits in common. "ef770dde37a40740349f0b16c412fe88f8d9bed4" and "acf51a92424dcf94aa8e2431284d7e8a33c940d5" have entirely different histories.

1 changed files with 26 additions and 32 deletions

View File

@ -94,7 +94,7 @@ gate_status = GateStatus()
# Configure logging # Configure logging
def setup_logging(settings: Settings): def setup_logging(settings: Settings):
"""Configure logging based on settings""" """Configure logging based on settings"""
log_level = getattr(logging, settings.logging.level.upper(), logging.INFO) log_level = getattr(logging, settings.logging.level.upper(), logging.WARNING)
logger.setLevel(log_level) logger.setLevel(log_level)
# Remove existing handlers # Remove existing handlers
@ -197,36 +197,30 @@ async def get_settings():
return settings return settings
async def get_events(limit: int = 10, offset: int = 0): async def get_events(limit: int = 10, offset: int = 0):
"""Get events from database with pagination""" """Get recent gate events with pagination"""
try: async with get_db() as db:
async with aiosqlite.connect(DB_PATH) as db: db.row_factory = aiosqlite.Row
# Get total count
async with db.execute("SELECT COUNT(*) FROM events") as cursor:
total = (await cursor.fetchone())[0]
# Get paginated events # Get total count
async with db.execute( cursor = await db.execute("SELECT COUNT(*) as count FROM events")
"SELECT timestamp, action, source, success FROM events ORDER BY timestamp DESC LIMIT ? OFFSET ?", row = await cursor.fetchone()
(limit, offset) total_count = row['count']
) as cursor:
events = [
{
"timestamp": row[0],
"action": row[1],
"source": row[2],
"success": bool(row[3])
}
for row in await cursor.fetchall()
]
return { # Get paginated events
"events": events, cursor = await db.execute(
"total": total, """
"hasMore": total > (offset + limit) SELECT * FROM events
} ORDER BY timestamp DESC
except Exception as e: LIMIT ? OFFSET ?
logger.error(f"Failed to get events: {e}", exc_info=True) """,
raise HTTPException(status_code=500, detail="Failed to get events") (limit, offset)
)
events = await cursor.fetchall()
return {
"events": [dict(event) for event in events],
"total": total_count,
"hasMore": (offset + limit) < total_count
}
# Set up MQTT event logging # Set up MQTT event logging
async def log_mqtt_event(action: str, success: bool = True): async def log_mqtt_event(action: str, success: bool = True):
@ -447,7 +441,7 @@ async def get_status():
raise HTTPException(status_code=500, detail="Failed to get gate status") raise HTTPException(status_code=500, detail="Failed to get gate status")
@app.get("/api/events") @app.get("/api/events")
async def get_events_route(limit: int = 10, offset: int = 0): async def get_events(limit: int = 10, offset: int = 0):
"""Get recent gate events with pagination""" """Get recent gate events with pagination"""
return await get_events(limit, offset) return await get_events(limit, offset)