ai insight rework #1
This commit is contained in:
1 parent
0d960c8153
commit
9d76f54d33
13 files changed
+730
-299
No files matched your search
@@ -323,6 +323,25 @@ class AIInsightViewSet(CycleScopedViewSet):
|
||||
{"detail": "context.kandangId must match kandang_id."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
# Phase 4: async mode returns 202 + job_id (client polls jobs/{id}/).
|
||||
if bool(data.get("async")):
|
||||
from apps.operations.services import insight_jobs
|
||||
|
||||
job_id = insight_jobs.create_job(
|
||||
{
|
||||
"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,
|
||||
}
|
||||
)
|
||||
return Response(
|
||||
{"job_id": job_id, "status": "queued"},
|
||||
status=status.HTTP_202_ACCEPTED,
|
||||
)
|
||||
try:
|
||||
result = generate_insight(
|
||||
cycle_id=int(cycle_id),
|
||||
@@ -342,6 +361,15 @@ class AIInsightViewSet(CycleScopedViewSet):
|
||||
)
|
||||
return Response(result)
|
||||
|
||||
@action(detail=False, methods=["get"], url_path=r"jobs/(?P<job_id>[^/.]+)")
|
||||
def job_status(self, request, job_id: str | None = None):
|
||||
from apps.operations.services import insight_jobs
|
||||
|
||||
job = insight_jobs.get_job(job_id or "")
|
||||
if job is None:
|
||||
return Response({"detail": "Job not found."}, status=status.HTTP_404_NOT_FOUND)
|
||||
return Response(job)
|
||||
|
||||
@action(detail=False, methods=["get"], url_path="cached")
|
||||
def cached(self, request):
|
||||
from apps.operations.services.insight_service import get_cached_insight
|
||||
|
||||
Reference in new issue
Block a user