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