09e0c26c68
- Introduced Redis service in both docker-compose files for production and development. - Updated web and celery services to use Redis as the broker and result backend. - Added necessary environment variables for Celery in settings.py. - Included new tasks and soil_data apps in Django settings and updated URL routing. - Updated requirements.txt to include Celery and Redis dependencies.
121 lines
4.1 KiB
Python
121 lines
4.1 KiB
Python
from rest_framework import status
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
|
|
from .models import SoilLocation
|
|
from .serializers import (
|
|
SoilDataRequestSerializer,
|
|
SoilDataTaskResponseSerializer,
|
|
SoilLocationResponseSerializer,
|
|
)
|
|
from .tasks import fetch_soil_data_task
|
|
|
|
|
|
class SoilDataView(APIView):
|
|
"""
|
|
API خاک: مختصات جغرافیایی را میگیرد.
|
|
اگر داده در DB موجود باشد، برگردانده میشود؛ در غیر این صورت
|
|
تسک Celery صف میشود و task_id برمیگردد.
|
|
"""
|
|
|
|
def _get_request_data(self, request):
|
|
return request.data if request.method == "POST" else request.query_params
|
|
|
|
def get(self, request):
|
|
return self._process(request)
|
|
|
|
def post(self, request):
|
|
return self._process(request)
|
|
|
|
def _process(self, request):
|
|
data = self._get_request_data(request)
|
|
serializer = SoilDataRequestSerializer(data=data)
|
|
if not serializer.is_valid():
|
|
return Response(
|
|
{"code": 400, "msg": "داده نامعتبر.", "data": serializer.errors},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|
|
lat = serializer.validated_data["lat"]
|
|
lon = serializer.validated_data["lon"]
|
|
lat_rounded = round(lat, 6)
|
|
lon_rounded = round(lon, 6)
|
|
|
|
location = (
|
|
SoilLocation.objects.filter(
|
|
latitude=lat_rounded,
|
|
longitude=lon_rounded,
|
|
)
|
|
.prefetch_related("depths")
|
|
.first()
|
|
)
|
|
|
|
if location and location.is_complete:
|
|
data_serializer = SoilLocationResponseSerializer(location)
|
|
return Response(
|
|
{
|
|
"code": 200,
|
|
"msg": "success",
|
|
"data": {
|
|
"source": "database",
|
|
**data_serializer.data,
|
|
},
|
|
},
|
|
status=status.HTTP_200_OK,
|
|
)
|
|
|
|
result = fetch_soil_data_task.delay(float(lat_rounded), float(lon_rounded))
|
|
task_data = SoilDataTaskResponseSerializer(
|
|
{
|
|
"task_id": result.id,
|
|
"longitude": float(lon_rounded),
|
|
"latitude": float(lat_rounded),
|
|
"status_url": f"/api/soil-data/tasks/{result.id}/status/",
|
|
}
|
|
).data
|
|
return Response(
|
|
{
|
|
"code": 202,
|
|
"msg": "تسک در صف. وضعیت را با task_id بررسی کنید.",
|
|
"data": task_data,
|
|
},
|
|
status=status.HTTP_202_ACCEPTED,
|
|
)
|
|
|
|
|
|
class SoilDataTaskStatusView(APIView):
|
|
"""وضعیت تسک واکشی خاک. در صورت SUCCESS لیست اطلاعات هر سه عمق برگردانده میشود."""
|
|
|
|
def get(self, request, task_id):
|
|
from celery.result import AsyncResult
|
|
|
|
result = AsyncResult(task_id)
|
|
state = result.state
|
|
data = {"task_id": task_id, "status": state}
|
|
|
|
if state == "PENDING":
|
|
data["message"] = "تسک در صف یا یافت نشد."
|
|
elif state == "PROGRESS":
|
|
data["progress"] = result.info
|
|
elif state == "SUCCESS":
|
|
task_result = result.result
|
|
if isinstance(task_result, dict) and task_result.get("status") == "completed":
|
|
location_id = task_result.get("location_id")
|
|
location = (
|
|
SoilLocation.objects.filter(pk=location_id)
|
|
.prefetch_related("depths")
|
|
.first()
|
|
)
|
|
if location and location.is_complete:
|
|
data["result"] = SoilLocationResponseSerializer(location).data
|
|
else:
|
|
data["result"] = task_result
|
|
else:
|
|
data["result"] = task_result
|
|
elif state == "FAILURE":
|
|
data["error"] = str(result.result)
|
|
|
|
return Response(
|
|
{"code": 200, "msg": "success", "data": data},
|
|
status=status.HTTP_200_OK,
|
|
)
|