mirror of
https://github.com/RoboSats/robosats.git
synced 2025-01-18 20:21:35 +00:00
Add object limit to /api/ticks (#747)
This commit is contained in:
parent
5cf894bf4d
commit
a980c15974
@ -869,6 +869,31 @@ class TickViewSchema:
|
|||||||
"description": "Get all market ticks. Returns a list of all the market ticks since inception.\n"
|
"description": "Get all market ticks. Returns a list of all the market ticks since inception.\n"
|
||||||
"CEX price is also recorded for useful insight on the historical premium of Non-KYC BTC. "
|
"CEX price is also recorded for useful insight on the historical premium of Non-KYC BTC. "
|
||||||
"Price is set when taker bond is locked.",
|
"Price is set when taker bond is locked.",
|
||||||
|
"parameters": [
|
||||||
|
OpenApiParameter(
|
||||||
|
name="start",
|
||||||
|
location=OpenApiParameter.QUERY,
|
||||||
|
description="Start date formatted as DD-MM-YYYY",
|
||||||
|
required=False,
|
||||||
|
type=str,
|
||||||
|
),
|
||||||
|
OpenApiParameter(
|
||||||
|
name="end",
|
||||||
|
location=OpenApiParameter.QUERY,
|
||||||
|
description="End date formatted as DD-MM-YYYY",
|
||||||
|
required=False,
|
||||||
|
type=str,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
"examples": [
|
||||||
|
OpenApiExample(
|
||||||
|
"Too many ticks",
|
||||||
|
value={
|
||||||
|
"bad_request": "More than 5000 market ticks have been found. Try narrowing the date range."
|
||||||
|
},
|
||||||
|
status_codes=[400],
|
||||||
|
)
|
||||||
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
30
api/views.py
30
api/views.py
@ -1091,9 +1091,33 @@ class TickView(ListAPIView):
|
|||||||
|
|
||||||
@extend_schema(**TickViewSchema.get)
|
@extend_schema(**TickViewSchema.get)
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
data = self.serializer_class(
|
start_date_str = request.query_params.get("start")
|
||||||
self.queryset.all(), many=True, read_only=True
|
end_date_str = request.query_params.get("end")
|
||||||
).data
|
|
||||||
|
# Perform the query with date range filtering
|
||||||
|
try:
|
||||||
|
if start_date_str:
|
||||||
|
start_date = datetime.strptime(start_date_str, "%d-%m-%Y").date()
|
||||||
|
self.queryset = self.queryset.filter(timestamp__gte=start_date)
|
||||||
|
if end_date_str:
|
||||||
|
end_date = datetime.strptime(end_date_str, "%d-%m-%Y").date()
|
||||||
|
self.queryset = self.queryset.filter(timestamp__lte=end_date)
|
||||||
|
except ValueError:
|
||||||
|
return Response(
|
||||||
|
{"bad_request": "Invalid date format"},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check if the number of ticks exceeds the limit
|
||||||
|
if self.queryset.count() > 5000:
|
||||||
|
return Response(
|
||||||
|
{
|
||||||
|
"bad_request": "More than 5000 market ticks have been found. Please, narrow the date range"
|
||||||
|
},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
|
)
|
||||||
|
|
||||||
|
data = self.serializer_class(self.queryset, many=True, read_only=True).data
|
||||||
return Response(data, status=status.HTTP_200_OK)
|
return Response(data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user