UPDATE
This commit is contained in:
+29
-64
@@ -6,8 +6,6 @@ from rest_framework.views import APIView
|
||||
from config.openapi import (
|
||||
build_envelope_serializer,
|
||||
build_response,
|
||||
build_task_queue_data_serializer,
|
||||
build_task_status_data_serializer,
|
||||
)
|
||||
|
||||
from .models import IrrigationMethod
|
||||
@@ -31,13 +29,9 @@ IrrigationValidationErrorSerializer = build_envelope_serializer(
|
||||
data_required=False,
|
||||
allow_null=True,
|
||||
)
|
||||
IrrigationQueueResponseSerializer = build_envelope_serializer(
|
||||
"IrrigationQueueResponseSerializer",
|
||||
build_task_queue_data_serializer("IrrigationQueueDataSerializer"),
|
||||
)
|
||||
IrrigationStatusResponseSerializer = build_envelope_serializer(
|
||||
"IrrigationStatusResponseSerializer",
|
||||
build_task_status_data_serializer("IrrigationStatusDataSerializer"),
|
||||
IrrigationRecommendResponseSerializer = build_envelope_serializer(
|
||||
"IrrigationRecommendResponseSerializer",
|
||||
data_schema=None,
|
||||
)
|
||||
|
||||
|
||||
@@ -112,7 +106,7 @@ class IrrigationMethodListCreateView(APIView):
|
||||
|
||||
class IrrigationRecommendView(APIView):
|
||||
"""
|
||||
توصیه آبیاری با Celery.
|
||||
توصیه آبیاری به صورت مستقیم.
|
||||
POST با sensor_uuid، plant_name، growth_stage، irrigation_method_name.
|
||||
اطلاعات گیاه از plant app و روش آبیاری از irrigation app دریافت میشود.
|
||||
"""
|
||||
@@ -121,21 +115,25 @@ class IrrigationRecommendView(APIView):
|
||||
tags=["Irrigation Recommendation"],
|
||||
summary="درخواست توصیه آبیاری",
|
||||
description=(
|
||||
"دادههای سنسور، گیاه و روش آبیاری را دریافت کرده و یک تسک Celery "
|
||||
"برای تولید توصیه آبیاری در صف قرار میدهد. "
|
||||
"دادههای سنسور، گیاه و روش آبیاری را دریافت کرده و "
|
||||
"توصیه آبیاری را مستقیم برمیگرداند. "
|
||||
"اطلاعات گیاه از جدول Plant و روش آبیاری از جدول IrrigationMethod بارگذاری میشود. "
|
||||
"محاسبات ET₀ و ETc با مدل FAO-56 در بکاند انجام میشود و مدل زبانی فقط توضیح برنامه آبیاری را تولید میکند."
|
||||
),
|
||||
request=IrrigationRecommendRequestSerializer,
|
||||
responses={
|
||||
202: build_response(
|
||||
IrrigationQueueResponseSerializer,
|
||||
"تسک توصیه آبیاری در صف قرار گرفت.",
|
||||
200: build_response(
|
||||
IrrigationRecommendResponseSerializer,
|
||||
"توصیه آبیاری با موفقیت تولید شد.",
|
||||
),
|
||||
400: build_response(
|
||||
IrrigationValidationErrorSerializer,
|
||||
"پارامتر ورودی نامعتبر است.",
|
||||
),
|
||||
500: build_response(
|
||||
IrrigationValidationErrorSerializer,
|
||||
"خطا در تولید توصیه آبیاری.",
|
||||
),
|
||||
},
|
||||
examples=[
|
||||
OpenApiExample(
|
||||
@@ -151,7 +149,7 @@ class IrrigationRecommendView(APIView):
|
||||
],
|
||||
)
|
||||
def post(self, request):
|
||||
from rag.tasks import irrigation_recommendation_task
|
||||
from rag.services.irrigation import get_irrigation_recommendation
|
||||
|
||||
serializer = IrrigationRecommendRequestSerializer(data=request.data)
|
||||
if not serializer.is_valid():
|
||||
@@ -167,55 +165,22 @@ class IrrigationRecommendView(APIView):
|
||||
irrigation_method_name = validated.get("irrigation_method_name")
|
||||
query = validated.get("query")
|
||||
|
||||
task = irrigation_recommendation_task.delay(
|
||||
sensor_uuid=sensor_uuid,
|
||||
plant_name=plant_name,
|
||||
growth_stage=growth_stage,
|
||||
irrigation_method_name=irrigation_method_name,
|
||||
query=query,
|
||||
)
|
||||
try:
|
||||
result = get_irrigation_recommendation(
|
||||
sensor_uuid=sensor_uuid,
|
||||
plant_name=plant_name,
|
||||
growth_stage=growth_stage,
|
||||
irrigation_method_name=irrigation_method_name,
|
||||
query=query,
|
||||
)
|
||||
except Exception as exc:
|
||||
return Response(
|
||||
{"code": 500, "msg": f"خطا در تولید توصیه آبیاری: {exc}", "data": None},
|
||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
)
|
||||
|
||||
return Response(
|
||||
{
|
||||
"code": 202,
|
||||
"msg": "تسک توصیه آبیاری در صف قرار گرفت.",
|
||||
"data": {
|
||||
"task_id": task.id,
|
||||
"status_url": f"/api/irrigation/recommend/{task.id}/status/",
|
||||
},
|
||||
},
|
||||
status=status.HTTP_202_ACCEPTED,
|
||||
)
|
||||
|
||||
|
||||
class IrrigationRecommendStatusView(APIView):
|
||||
"""وضعیت تسک توصیه آبیاری."""
|
||||
|
||||
@extend_schema(
|
||||
tags=["Irrigation Recommendation"],
|
||||
summary="وضعیت تسک توصیه آبیاری",
|
||||
description="وضعیت تسک Celery توصیه آبیاری را برمیگرداند.",
|
||||
responses={
|
||||
200: build_response(
|
||||
IrrigationStatusResponseSerializer,
|
||||
"وضعیت فعلی تسک توصیه آبیاری.",
|
||||
),
|
||||
},
|
||||
)
|
||||
def get(self, request, task_id):
|
||||
from celery.result import AsyncResult
|
||||
|
||||
result = AsyncResult(task_id)
|
||||
data = {"task_id": task_id, "status": result.state}
|
||||
if result.state == "PENDING":
|
||||
data["message"] = "تسک در صف یا یافت نشد."
|
||||
elif result.state == "PROGRESS":
|
||||
data["progress"] = result.info
|
||||
elif result.state == "SUCCESS":
|
||||
data["result"] = result.result
|
||||
elif result.state == "FAILURE":
|
||||
data["error"] = str(result.result)
|
||||
return Response(
|
||||
{"code": 200, "msg": "success", "data": data},
|
||||
{"code": 200, "msg": "success", "data": result},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user