Add object limit to /api/ticks (#747)

This commit is contained in:
Reckless_Satoshi 2023-07-23 19:27:37 +00:00 committed by GitHub
parent 5cf894bf4d
commit a980c15974
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 3 deletions

View File

@ -869,6 +869,31 @@ class TickViewSchema:
"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. "
"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],
)
],
}

View File

@ -1091,9 +1091,33 @@ class TickView(ListAPIView):
@extend_schema(**TickViewSchema.get)
def get(self, request):
data = self.serializer_class(
self.queryset.all(), many=True, read_only=True
).data
start_date_str = request.query_params.get("start")
end_date_str = request.query_params.get("end")
# 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)