update api configuration for panel iot and adjust ai insight
This commit is contained in:
1 parent
a2b5b10977
commit
20d04acb3e
20 files changed
+814
-177
No files matched your search
@@ -8,7 +8,7 @@ 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.farms.models import Cycle, Flock
|
||||
from apps.operations.models import (
|
||||
AIInsight,
|
||||
ChickenCounting,
|
||||
@@ -33,6 +33,7 @@ from apps.operations.services.chicken_counting_edge import (
|
||||
sync_chicken_counting_for_cycle,
|
||||
sync_chicken_weight_for_cycle,
|
||||
)
|
||||
from apps.operations.services.iot_api import IotApiError, sync_iot_panels
|
||||
from apps.operations.services.visibility import dashboard_publish_time, visible_through_date
|
||||
|
||||
READ_ACTIONS = frozenset({"list", "retrieve", "latest_average", "latest", "dates", "cached"})
|
||||
@@ -250,6 +251,39 @@ class IotPanelViewSet(viewsets.ModelViewSet):
|
||||
)
|
||||
return Response(dates)
|
||||
|
||||
@action(detail=False, methods=["post"], url_path="sync")
|
||||
def sync_from_api(self, request):
|
||||
flock_id = request.data.get("flock_id")
|
||||
if not flock_id:
|
||||
return Response(
|
||||
{"detail": "flock_id is required."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
try:
|
||||
flock = Flock.objects.get(pk=int(flock_id))
|
||||
except (Flock.DoesNotExist, TypeError, ValueError):
|
||||
return Response({"detail": "Flock not found."}, status=status.HTTP_404_NOT_FOUND)
|
||||
if not (flock.iot_api_url or "").strip():
|
||||
return Response(
|
||||
{"detail": "Flock has no IoT API URL configured."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
try:
|
||||
summary = sync_iot_panels(flock_id=flock.pk)
|
||||
except IotApiError 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(
|
||||
{
|
||||
"upserted": summary.upserted,
|
||||
"skipped": summary.skipped,
|
||||
"pages": summary.pages,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class AIInsightViewSet(CycleScopedViewSet):
|
||||
queryset = AIInsight.objects.select_related("cycle", "kandang").all()
|
||||
|
||||
Reference in new issue
Block a user