add limit
This commit is contained in:
35
app.py
35
app.py
@@ -160,14 +160,16 @@ def plext_to_dict(plext: Plext) -> dict:
|
|||||||
def get_plexts_from_db():
|
def get_plexts_from_db():
|
||||||
"""
|
"""
|
||||||
Get plexts from MongoDB with optional filters.
|
Get plexts from MongoDB with optional filters.
|
||||||
|
Returns plexts sorted by timestamp (most recent first).
|
||||||
|
|
||||||
Query Parameters:
|
Query Parameters:
|
||||||
player_name: Filter by player name (optional)
|
player_name: Filter by player name (optional)
|
||||||
timestamp_from: Minimum timestamp in milliseconds (optional)
|
timestamp_from: Minimum timestamp in milliseconds (optional)
|
||||||
timestamp_to: Maximum timestamp in milliseconds (optional)
|
timestamp_to: Maximum timestamp in milliseconds (optional)
|
||||||
|
limit: Maximum number of results to return (optional, default: 100)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
JSON response with list of plexts (without _id field)
|
JSON response with list of plexts (without _id field), limited by the limit parameter
|
||||||
"""
|
"""
|
||||||
# CORS headers
|
# CORS headers
|
||||||
cors_headers = {
|
cors_headers = {
|
||||||
@@ -185,6 +187,7 @@ def get_plexts_from_db():
|
|||||||
player_name = request.args.get("player_name")
|
player_name = request.args.get("player_name")
|
||||||
timestamp_from = request.args.get("timestamp_from")
|
timestamp_from = request.args.get("timestamp_from")
|
||||||
timestamp_to = request.args.get("timestamp_to")
|
timestamp_to = request.args.get("timestamp_to")
|
||||||
|
limit_param = request.args.get("limit")
|
||||||
|
|
||||||
# Validate and convert timestamp parameters to integers if provided
|
# Validate and convert timestamp parameters to integers if provided
|
||||||
if timestamp_from is not None:
|
if timestamp_from is not None:
|
||||||
@@ -207,6 +210,25 @@ def get_plexts_from_db():
|
|||||||
cors_headers,
|
cors_headers,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Validate and convert limit parameter to integer if provided, otherwise default to 100
|
||||||
|
if limit_param is not None:
|
||||||
|
try:
|
||||||
|
limit = int(limit_param)
|
||||||
|
if limit <= 0:
|
||||||
|
return (
|
||||||
|
jsonify({"error": "limit must be a positive integer"}),
|
||||||
|
400,
|
||||||
|
cors_headers,
|
||||||
|
)
|
||||||
|
except ValueError:
|
||||||
|
return (
|
||||||
|
jsonify({"error": "limit must be an integer"}),
|
||||||
|
400,
|
||||||
|
cors_headers,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
limit = 100
|
||||||
|
|
||||||
# Build MongoDB filter query
|
# Build MongoDB filter query
|
||||||
filter_query = {}
|
filter_query = {}
|
||||||
|
|
||||||
@@ -234,9 +256,11 @@ def get_plexts_from_db():
|
|||||||
# Projection to exclude _id field
|
# Projection to exclude _id field
|
||||||
projection = {"_id": 0}
|
projection = {"_id": 0}
|
||||||
|
|
||||||
# Execute query with sorting (timestamp DESC - most recent first)
|
# Execute query with sorting (timestamp DESC - most recent first) and dynamic limit
|
||||||
cursor = collection.find(filter=filter_query, projection=projection).sort(
|
cursor = (
|
||||||
"timestamp", -1
|
collection.find(filter=filter_query, projection=projection)
|
||||||
|
.sort("timestamp", -1)
|
||||||
|
.limit(limit)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Convert cursor to list
|
# Convert cursor to list
|
||||||
@@ -369,11 +393,12 @@ def index():
|
|||||||
},
|
},
|
||||||
"/plexts/from-db": {
|
"/plexts/from-db": {
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"description": "Get plexts from MongoDB with optional filters",
|
"description": "Get plexts from MongoDB with optional filters. Returns plexts sorted by timestamp (most recent first).",
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"player_name": "Filter by player name (optional)",
|
"player_name": "Filter by player name (optional)",
|
||||||
"timestamp_from": "Minimum timestamp in milliseconds (optional)",
|
"timestamp_from": "Minimum timestamp in milliseconds (optional)",
|
||||||
"timestamp_to": "Maximum timestamp in milliseconds (optional)",
|
"timestamp_to": "Maximum timestamp in milliseconds (optional)",
|
||||||
|
"limit": "Maximum number of results to return (optional, default: 100)",
|
||||||
},
|
},
|
||||||
"authentication": "Basic Auth required",
|
"authentication": "Basic Auth required",
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user