Add bulk packet retrieval API with gzip compression and pagination support.

This commit is contained in:
Lloyd
2026-02-03 10:21:59 +00:00
parent 3adfee5160
commit b0e19b13af
3 changed files with 45 additions and 3 deletions
+4 -2
View File
@@ -560,7 +560,8 @@ class SQLiteHandler:
route: Optional[int] = None,
start_timestamp: Optional[float] = None,
end_timestamp: Optional[float] = None,
limit: int = 1000) -> list:
limit: int = 1000,
offset: int = 0) -> list:
try:
with sqlite3.connect(self.sqlite_path) as conn:
conn.row_factory = sqlite3.Row
@@ -599,8 +600,9 @@ class SQLiteHandler:
else:
query = base_query
query += " ORDER BY timestamp DESC LIMIT ?"
query += " ORDER BY timestamp DESC LIMIT ? OFFSET ?"
params.append(limit)
params.append(offset)
packets = conn.execute(query, params).fetchall()
@@ -216,9 +216,10 @@ class StorageCollector:
start_timestamp: Optional[float] = None,
end_timestamp: Optional[float] = None,
limit: int = 1000,
offset: int = 0,
) -> list:
return self.sqlite_handler.get_filtered_packets(
packet_type, route, start_timestamp, end_timestamp, limit
packet_type, route, start_timestamp, end_timestamp, limit, offset
)
def get_packet_by_hash(self, packet_hash: str) -> Optional[dict]:
+39
View File
@@ -841,6 +841,45 @@ class APIEndpoints:
logger.error(f"Error getting recent packets: {e}")
return self._error(e)
@cherrypy.expose
@cherrypy.tools.gzip(compress_level=6)
@cherrypy.tools.json_out()
def bulk_packets(self, limit=1000, offset=0):
"""
Optimized bulk packet retrieval with gzip compression and DB-level pagination.
"""
try:
# Enforce reasonable limits
limit = min(int(limit), 10000)
offset = max(int(offset), 0)
# Get packets from storage with TRUE DB-level pagination
# Uses SQL "LIMIT ? OFFSET ?" - no Python slicing needed!
storage = self._get_storage()
packets = storage.get_filtered_packets(
packet_type=None,
route=None,
start_timestamp=None,
end_timestamp=None,
limit=limit,
offset=offset
)
response = {
"success": True,
"data": packets,
"count": len(packets),
"offset": offset,
"limit": limit,
"compressed": True
}
return response
except Exception as e:
logger.error(f"Error getting bulk packets: {e}")
return self._error(e)
@cherrypy.expose
@cherrypy.tools.json_out()
def filtered_packets(self, start_timestamp=None, end_timestamp=None, limit=1000, type=None, route=None):