diff --git a/django/src/rdwatch/api.py b/django/src/rdwatch/api.py index 21011677..6d0a0f19 100644 --- a/django/src/rdwatch/api.py +++ b/django/src/rdwatch/api.py @@ -4,6 +4,7 @@ from .views.model_run import router as model_run_router from .views.performer import router as performer_router from .views.region import router as region_router +from .views.server_status import router as server_status_router from .views.site_evaluation import router as site_evaluation_router from .views.site_image import router as images_router from .views.site_observation import router as site_observation_router @@ -16,6 +17,7 @@ api.add_router('/performers/', performer_router) api.add_router('/evaluations/images/', images_router) api.add_router('/regions/', region_router) +api.add_router('/status/', server_status_router) # useful for getting information back about validation errors diff --git a/django/src/rdwatch/urls.py b/django/src/rdwatch/urls.py index 5ceba08f..4ba99beb 100644 --- a/django/src/rdwatch/urls.py +++ b/django/src/rdwatch/urls.py @@ -17,7 +17,6 @@ ), name='openapi-schema', ), - path('status', views.RetrieveServerStatus.as_view()), # type: ignore path('satellite-image/timestamps', views.satelliteimage_time_list), path('satellite-image/all-timestamps', views.all_satellite_timestamps), path( diff --git a/django/src/rdwatch/views/__init__.py b/django/src/rdwatch/views/__init__.py index dd8c709e..4dfc880c 100644 --- a/django/src/rdwatch/views/__init__.py +++ b/django/src/rdwatch/views/__init__.py @@ -7,11 +7,9 @@ satelliteimage_visual_tile, satelliteimage_visual_time_list, ) -from .server_status import RetrieveServerStatus from .vector_tile import vector_tile __all__ = [ - 'RetrieveServerStatus', 'satelliteimage_raster_tile', 'satelliteimage_raster_bbox', 'satelliteimage_time_list', diff --git a/django/src/rdwatch/views/server_status.py b/django/src/rdwatch/views/server_status.py index 51c15015..671b0b10 100644 --- a/django/src/rdwatch/views/server_status.py +++ b/django/src/rdwatch/views/server_status.py @@ -1,70 +1,35 @@ import datetime import socket -from dataclasses import dataclass -from rest_framework import permissions, serializers -from rest_framework.response import Response -from rest_framework.schemas.openapi import AutoSchema -from rest_framework.views import APIView - -SERVER_INSTANCE_EPOCH = datetime.datetime.now() - - -@dataclass -class ServerStatus: - uptime: datetime.timedelta - hostname: str - ip: str +from ninja import Field, Router, Schema +from django.http import HttpRequest -class UptimeSerializer(serializers.Serializer): - iso8601 = serializers.DurationField(source='*') - days = serializers.IntegerField() - seconds = serializers.IntegerField() - useconds = serializers.IntegerField(source='microseconds') +router = Router() +SERVER_INSTANCE_EPOCH = datetime.datetime.now() -class ServerStatusSerializer(serializers.Serializer): - uptime = UptimeSerializer(required=True) - hostname = serializers.CharField(required=True) - ip = serializers.IPAddressField(required=True) - - def create(self, validated_data): - instance = ServerStatus( - uptime=validated_data['uptime']['iso8601'], - hostname=validated_data['hostname'], - ip=validated_data['iso8601'], - ) - # we'd save it here if this were a Django model - # instance.save() - return instance - - def update(self, instance, validated_data): - instance.uptime = validated_data.get('uptime', instance.uptime) - instance.hostname = validated_data.get('hostname', instance.hostname) - instance.ip = validated_data.get('ip', instance.ip) - # we'd save it here if this were a Django model - # instance.save() - return instance +class UptimeSchema(Schema): + iso8601: str + days: int + seconds: int + useconds: int = Field(..., alias='microseconds') -class ServerStatusSchema(AutoSchema): - def get_operation_id(self, *args): - return 'getStatus' + @staticmethod + def resolve_iso8601(obj: datetime.timedelta): + return str(obj) - def get_serializer(self, *args): - return ServerStatusSerializer() +class ServerStatusSchema(Schema): + uptime: UptimeSchema + hostname: str + ip: str -class RetrieveServerStatus(APIView): - permission_classes = [permissions.AllowAny] - schema = ServerStatusSchema() - action = 'retrieve' - def get(self, request): - uptime = datetime.datetime.now() - SERVER_INSTANCE_EPOCH - hostname = socket.gethostname() - ip = socket.gethostbyname(hostname) - server_status = ServerStatus(uptime, hostname, ip) - serializer = ServerStatusSerializer(server_status) - return Response(serializer.data) +@router.get('/', response=ServerStatusSchema) +def get_status(request: HttpRequest): + uptime = datetime.datetime.now() - SERVER_INSTANCE_EPOCH + hostname = socket.gethostname() + ip = socket.gethostbyname(hostname) + return ServerStatusSchema(uptime=uptime, hostname=hostname, ip=ip)