1
0
Fork 0

Limit requests for violator satellites on Telemetry API

Signed-off-by: Alfredos-Panagiotis Damkalis <fredy@fredy.gr>
spacecruft
Alfredos-Panagiotis Damkalis 2022-06-14 05:11:10 +03:00
parent a13e2a0392
commit 03309529a2
3 changed files with 51 additions and 6 deletions

View File

@ -1,22 +1,60 @@
"""SatNOGS DB API throttling classes, django rest framework"""
from django.core.cache import cache
from django.shortcuts import get_object_or_404
from rest_framework import throttling
from db.base.models import Satellite
class GetTelemetryAnononymousRateThrottle(throttling.AnonRateThrottle):
"""Anonymous GET Throttling"""
"""Anonymous GET Throttling for Telemetry API endpoint"""
scope = 'get_telemetry_anon'
def allow_request(self, request, view):
if request.method == "POST":
if request.method == 'POST':
return True
return super().allow_request(request, view)
class GetTelemetryUserRateThrottle(throttling.UserRateThrottle):
"""User GET Throttling"""
"""User GET Throttling for Telemetry API endpoint"""
scope = 'get_telemetry_user'
def allow_request(self, request, view):
if request.method == "POST":
if request.method == 'POST':
return True
return super().allow_request(request, view)
class GetTelemetryViolatorThrottle(throttling.BaseThrottle):
"""Violator satellites GET Throttling for Telemetry API endpoint"""
scope = 'get_telemetry_violator'
def allow_request(self, request, view):
if request.method == 'POST':
return True
satellite = request.query_params.get('satellite', None)
sat_id = request.query_params.get('sat_id', None)
violation = None
if sat_id:
violation = cache.get('violator_' + sat_id)
elif satellite:
violation = cache.get('violator_' + str(satellite))
else:
return True
if violation is None:
if sat_id:
satellite_obj = get_object_or_404(Satellite, satellite_identifier__sat_id=sat_id)
else:
satellite_obj = get_object_or_404(
Satellite, satellite_entry__norad_cat_id=satellite
)
if satellite_obj.associated_satellite:
satellite_obj = satellite_obj.associated_satellite
if satellite_obj.has_bad_transmitter:
return cache.add('violator_telemetry_' + str(satellite_obj.id), True, 86400)
elif violation['status']:
return cache.add('violator_telemetry_' + str(violation['id']), True, 86400)
return True

View File

@ -18,7 +18,8 @@ from db.api import filters, pagination, serializers
from db.api.parsers import JSONLDParser
from db.api.perms import IsAuthenticatedOrOptions, SafeMethodsWithPermission
from db.api.renderers import BrowserableJSONLDRenderer, JSONLDRenderer
from db.api.throttling import GetTelemetryAnononymousRateThrottle, GetTelemetryUserRateThrottle
from db.api.throttling import GetTelemetryAnononymousRateThrottle, GetTelemetryUserRateThrottle, \
GetTelemetryViolatorThrottle
from db.base.helpers import gridsquare
from db.base.models import SATELLITE_STATUS, SERVICE_TYPE, TRANSMITTER_STATUS, TRANSMITTER_TYPE, \
Artifact, DemodData, LatestTleSet, Mode, Satellite, SatelliteEntry, SatelliteIdentifier, \
@ -522,7 +523,10 @@ class TelemetryViewSet( # pylint: disable=R0901,R0912,R0915
serializer_class = serializers.TelemetrySerializer
filterset_class = filters.TelemetryViewFilter
permission_classes = [SafeMethodsWithPermission]
throttle_classes = [GetTelemetryAnononymousRateThrottle, GetTelemetryUserRateThrottle]
throttle_classes = [
GetTelemetryAnononymousRateThrottle, GetTelemetryUserRateThrottle,
GetTelemetryViolatorThrottle
]
parser_classes = (FormParser, MultiPartParser, FileUploadParser)
pagination_class = pagination.LinkedHeaderPageNumberPagination

View File

@ -293,6 +293,9 @@ REST_FRAMEWORK = {
'get_telemetry_user': config(
'DEFAULT_THROTTLE_RATE_GET_TELEMETRY_USER', default='6/minute'
),
'get_telemetry_violator': config(
'DEFAULT_THROTTLE_RATE_GET_TELEMETRY_VIOLATOR', default='1/day'
),
}
}