175 lines
5.8 KiB
Python
175 lines
5.8 KiB
Python
from __future__ import annotations
|
|
|
|
from django.conf import settings
|
|
from django.core.exceptions import ValidationError
|
|
from rest_framework import status, viewsets
|
|
from rest_framework.decorators import action
|
|
from rest_framework.permissions import AllowAny
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
|
|
from apps.farms.models import Cycle
|
|
from apps.operations.models import (
|
|
AIInsight,
|
|
ChickenCounting,
|
|
ChickenWeight,
|
|
FeedSacks,
|
|
IotPanel,
|
|
KPI,
|
|
ManualInput,
|
|
)
|
|
from apps.operations.serializers import (
|
|
AIInsightSerializer,
|
|
ChickenCountingSerializer,
|
|
ChickenWeightSerializer,
|
|
FeedSacksSerializer,
|
|
IotPanelSerializer,
|
|
KPISerializer,
|
|
ManualInputSerializer,
|
|
)
|
|
from apps.operations.services.karung_web import KarungWebError, request_karung
|
|
|
|
|
|
class CycleScopedViewSet(viewsets.ModelViewSet):
|
|
def get_queryset(self):
|
|
qs = super().get_queryset()
|
|
params = self.request.query_params
|
|
if params.get("cycle_id"):
|
|
qs = qs.filter(cycle_id=params["cycle_id"])
|
|
if params.get("flock_id"):
|
|
qs = qs.filter(cycle__flocks__flock_id=params["flock_id"]).distinct()
|
|
if params.get("date"):
|
|
qs = qs.filter(date=params["date"])
|
|
return qs
|
|
|
|
|
|
class KPIViewSet(CycleScopedViewSet):
|
|
queryset = KPI.objects.select_related("cycle").all()
|
|
serializer_class = KPISerializer
|
|
|
|
|
|
class ManualInputViewSet(CycleScopedViewSet):
|
|
queryset = ManualInput.objects.select_related("cycle").all()
|
|
serializer_class = ManualInputSerializer
|
|
|
|
|
|
class ChickenCountingViewSet(CycleScopedViewSet):
|
|
queryset = ChickenCounting.objects.select_related("cycle").all()
|
|
serializer_class = ChickenCountingSerializer
|
|
|
|
|
|
class ChickenWeightViewSet(CycleScopedViewSet):
|
|
queryset = ChickenWeight.objects.select_related("cycle").all()
|
|
serializer_class = ChickenWeightSerializer
|
|
|
|
@action(detail=False, methods=["get"], url_path="latest-avg")
|
|
def latest_average(self, request):
|
|
cycle_id = request.query_params.get("cycle_id")
|
|
if not cycle_id:
|
|
return Response(
|
|
{"detail": "cycle_id is required."},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|
|
row = self.get_queryset().filter(cycle_id=cycle_id).order_by("-date", "-pk").first()
|
|
if row is None:
|
|
return Response(
|
|
{"detail": "No chicken weight exists for this cycle."},
|
|
status=status.HTTP_404_NOT_FOUND,
|
|
)
|
|
return Response(self.get_serializer(row).data)
|
|
|
|
|
|
class FeedSacksViewSet(CycleScopedViewSet):
|
|
queryset = FeedSacks.objects.select_related("cycle").all()
|
|
serializer_class = FeedSacksSerializer
|
|
|
|
@action(detail=False, methods=["get"], url_path="latest")
|
|
def latest(self, request):
|
|
cycle_id = request.query_params.get("cycle_id")
|
|
if not cycle_id:
|
|
return Response(
|
|
{"detail": "cycle_id is required."},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|
|
row = self.get_queryset().filter(cycle_id=cycle_id).order_by("-date", "-pk").first()
|
|
if row is None:
|
|
return Response(
|
|
{"detail": "No feed sacks row exists for this cycle."},
|
|
status=status.HTTP_404_NOT_FOUND,
|
|
)
|
|
return Response(self.get_serializer(row).data)
|
|
|
|
@action(detail=False, methods=["post"], url_path="request")
|
|
def request_sync(self, request):
|
|
cycle_id = request.data.get("cycle_id")
|
|
if not cycle_id:
|
|
return Response(
|
|
{"detail": "cycle_id is required."},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|
|
try:
|
|
cycle = Cycle.objects.get(pk=cycle_id)
|
|
except Cycle.DoesNotExist:
|
|
return Response({"detail": "Cycle not found."}, status=status.HTTP_404_NOT_FOUND)
|
|
section = request.data.get("section") or "today"
|
|
try:
|
|
feed_sack, summary = request_karung(cycle, section=section)
|
|
except (KarungWebError, ValidationError) as exc:
|
|
code = getattr(exc, "status_code", None)
|
|
return Response(
|
|
{"detail": str(exc)},
|
|
status=code if code and code >= 400 else status.HTTP_502_BAD_GATEWAY,
|
|
)
|
|
return Response(
|
|
{"karung": self.get_serializer(feed_sack).data, "upstream": summary}
|
|
)
|
|
|
|
|
|
class IotPanelViewSet(viewsets.ModelViewSet):
|
|
queryset = IotPanel.objects.select_related("flock", "flock__cycle").all()
|
|
serializer_class = IotPanelSerializer
|
|
|
|
def get_queryset(self):
|
|
qs = super().get_queryset()
|
|
params = self.request.query_params
|
|
if params.get("cycle_id"):
|
|
qs = qs.filter(flock__cycle_id=params["cycle_id"])
|
|
if params.get("flock_id"):
|
|
qs = qs.filter(flock_id=params["flock_id"])
|
|
if params.get("date"):
|
|
qs = qs.filter(date=params["date"])
|
|
return qs
|
|
|
|
def paginate_queryset(self, queryset):
|
|
if self.request.query_params.get("date"):
|
|
return None
|
|
return super().paginate_queryset(queryset)
|
|
|
|
@action(detail=False, methods=["get"], url_path="dates")
|
|
def dates(self, request):
|
|
dates = list(
|
|
self.get_queryset()
|
|
.order_by("date")
|
|
.values_list("date", flat=True)
|
|
.distinct()
|
|
)
|
|
return Response(dates)
|
|
|
|
|
|
class AIInsightViewSet(CycleScopedViewSet):
|
|
queryset = AIInsight.objects.select_related("cycle").all()
|
|
serializer_class = AIInsightSerializer
|
|
|
|
|
|
class HealthView(APIView):
|
|
permission_classes = [AllowAny]
|
|
authentication_classes = []
|
|
|
|
def get(self, request):
|
|
payload = {
|
|
"status": "ok",
|
|
"service": "newdashboard-api",
|
|
"karung_web_admin_base_configured": bool(settings.KARUNG_WEB_ADMIN_BASE_URL),
|
|
}
|
|
return Response(payload)
|