update ai insight migration for all page
This commit is contained in:
1 parent
07d36081ce
commit
18790ab8f6
69 files changed
+9457
-399
No files matched your search
@@ -35,7 +35,7 @@ from apps.operations.services.chicken_counting_edge import (
|
||||
)
|
||||
from apps.operations.services.visibility import dashboard_publish_time, visible_through_date
|
||||
|
||||
READ_ACTIONS = frozenset({"list", "retrieve", "latest_average", "latest", "dates"})
|
||||
READ_ACTIONS = frozenset({"list", "retrieve", "latest_average", "latest", "dates", "cached"})
|
||||
|
||||
|
||||
class VisibilityFilteredMixin:
|
||||
@@ -252,9 +252,89 @@ class IotPanelViewSet(viewsets.ModelViewSet):
|
||||
|
||||
|
||||
class AIInsightViewSet(CycleScopedViewSet):
|
||||
queryset = AIInsight.objects.select_related("cycle").all()
|
||||
queryset = AIInsight.objects.select_related("cycle", "kandang").all()
|
||||
serializer_class = AIInsightSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
qs = super().get_queryset()
|
||||
params = self.request.query_params
|
||||
if params.get("kandang_id"):
|
||||
qs = qs.filter(kandang_id=params["kandang_id"])
|
||||
if params.get("topic"):
|
||||
qs = qs.filter(topic=params["topic"])
|
||||
if params.get("report_type"):
|
||||
qs = qs.filter(report_type=params["report_type"])
|
||||
if params.get("report_period"):
|
||||
qs = qs.filter(report_period=params["report_period"])
|
||||
return qs
|
||||
|
||||
@action(detail=False, methods=["post"], url_path="generate")
|
||||
def generate(self, request):
|
||||
from apps.operations.services.insight_service import generate_insight
|
||||
|
||||
data = request.data or {}
|
||||
cycle_id = data.get("cycle_id")
|
||||
kandang_id = data.get("kandang_id")
|
||||
topic = data.get("topic")
|
||||
context = data.get("context") or {}
|
||||
report_type = data.get("report_type") or "page"
|
||||
report_period = data.get("report_period") or "current"
|
||||
force_refresh = bool(data.get("force_refresh"))
|
||||
if not cycle_id or not kandang_id or not topic:
|
||||
return Response(
|
||||
{"detail": "cycle_id, kandang_id, and topic are required."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
# Reject mixed-farm prompts: context must match selected kandang.
|
||||
ctx_kid = context.get("kandangId") or context.get("kandang_id")
|
||||
if ctx_kid is not None and int(ctx_kid) != int(kandang_id):
|
||||
return Response(
|
||||
{"detail": "context.kandangId must match kandang_id."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
try:
|
||||
result = generate_insight(
|
||||
cycle_id=int(cycle_id),
|
||||
kandang_id=int(kandang_id),
|
||||
topic=str(topic),
|
||||
context=context if isinstance(context, dict) else {},
|
||||
report_type=str(report_type),
|
||||
report_period=str(report_period),
|
||||
force_refresh=force_refresh,
|
||||
)
|
||||
except ValueError as exc:
|
||||
return Response({"detail": str(exc)}, status=status.HTTP_400_BAD_REQUEST)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
return Response(
|
||||
{"detail": f"Insight generate failed: {exc}"},
|
||||
status=status.HTTP_502_BAD_GATEWAY,
|
||||
)
|
||||
return Response(result)
|
||||
|
||||
@action(detail=False, methods=["get"], url_path="cached")
|
||||
def cached(self, request):
|
||||
from apps.operations.services.insight_service import get_cached_insight
|
||||
|
||||
params = request.query_params
|
||||
cycle_id = params.get("cycle_id")
|
||||
kandang_id = params.get("kandang_id")
|
||||
topic = params.get("topic")
|
||||
if not cycle_id or not kandang_id or not topic:
|
||||
return Response(
|
||||
{"detail": "cycle_id, kandang_id, and topic are required."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
result = get_cached_insight(
|
||||
cycle_id=int(cycle_id),
|
||||
kandang_id=int(kandang_id),
|
||||
topic=str(topic),
|
||||
report_type=params.get("report_type") or "page",
|
||||
report_period=params.get("report_period") or "current",
|
||||
)
|
||||
if result is None:
|
||||
return Response({"detail": "No cached insight."}, status=status.HTTP_404_NOT_FOUND)
|
||||
return Response(result)
|
||||
|
||||
|
||||
class HealthView(APIView):
|
||||
permission_classes = [AllowAny]
|
||||
|
||||
Reference in new issue
Block a user