update applying current ai insight from dashboard-cpsp

This commit is contained in:
Alberto-Audrix committed 2026-10-05 14:25:22 +07:00
1 parent 31272035e9
commit b87f20e3b3
27 files changed
+5300 -343

No files matched your search

+110 -1
View File
@@ -236,9 +236,118 @@ 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=["get"], url_path="cached")
def cached(self, request):
import json
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,
)
report_type = params.get("report_type") or "page"
report_period = params.get("report_period") or "current"
row = (
AIInsight.objects.filter(
cycle_id=cycle_id,
kandang_id=kandang_id,
topic=topic,
report_type=report_type,
report_period=report_period,
)
.order_by("-updated_at")
.first()
)
if row is None:
return Response({"detail": "No cached insight."}, status=status.HTTP_404_NOT_FOUND)
raw_cites = row.citations
if isinstance(raw_cites, list):
cites = raw_cites
elif isinstance(raw_cites, str) and raw_cites.strip():
try:
cites = json.loads(raw_cites)
except Exception:
cites = []
else:
cites = []
return Response({
"success": True,
"insight_text": row.insight_text,
"summary": row.summary,
"alert": row.alert,
"citations": cites,
"topic": row.topic,
"report_type": row.report_type,
"report_period": row.report_period,
})
@action(detail=False, methods=["post"], url_path="generate")
def generate(self, request):
params = request.data or {}
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,
)
report_type = params.get("report_type") or "page"
report_period = params.get("report_period") or "current"
row = (
AIInsight.objects.filter(
cycle_id=cycle_id,
kandang_id=kandang_id,
topic=topic,
report_type=report_type,
report_period=report_period,
)
.order_by("-updated_at")
.first()
)
if row is not None:
import json
raw_cites = row.citations
try:
cites = json.loads(raw_cites) if isinstance(raw_cites, str) else (raw_cites or [])
except Exception:
cites = []
return Response({
"success": True,
"insight_text": row.insight_text,
"summary": row.summary,
"alert": row.alert,
"citations": cites,
"topic": row.topic,
"report_type": row.report_type,
"report_period": row.report_period,
})
return Response(
{"detail": "Insight belum tersedia pada mirror HQ. Pastikan insight telah di-generate pada site dashboard dan disinkronkan."},
status=status.HTTP_404_NOT_FOUND,
)
class HealthView(APIView):
permission_classes = [AllowAny]