2026-03-22 01:09:09 +03:30
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
|
|
from celery.result import AsyncResult
|
|
|
|
|
from drf_spectacular.utils import OpenApiResponse, extend_schema, inline_serializer
|
|
|
|
|
from rest_framework import serializers as drf_serializers
|
|
|
|
|
from rest_framework import status
|
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
|
2026-03-25 01:56:41 +03:30
|
|
|
from config.openapi import (
|
|
|
|
|
build_envelope_serializer,
|
|
|
|
|
build_response,
|
|
|
|
|
build_task_queue_data_serializer,
|
|
|
|
|
build_task_status_data_serializer,
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-22 01:09:09 +03:30
|
|
|
from .tasks import generate_dashboard_data_task
|
|
|
|
|
|
|
|
|
|
|
2026-03-25 01:56:41 +03:30
|
|
|
DashboardDataGenerateDataSerializer = build_task_queue_data_serializer(
|
|
|
|
|
"DashboardDataGenerateDataSerializer"
|
|
|
|
|
)
|
|
|
|
|
DashboardDataGenerateResponseSerializer = build_envelope_serializer(
|
|
|
|
|
"DashboardDataGenerateResponseSerializer",
|
|
|
|
|
DashboardDataGenerateDataSerializer,
|
|
|
|
|
)
|
|
|
|
|
DashboardDataErrorResponseSerializer = build_envelope_serializer(
|
|
|
|
|
"DashboardDataErrorResponseSerializer",
|
|
|
|
|
data_required=False,
|
|
|
|
|
allow_null=True,
|
|
|
|
|
)
|
|
|
|
|
DashboardDataStatusResponseSerializer = build_envelope_serializer(
|
|
|
|
|
"DashboardDataStatusResponseSerializer",
|
|
|
|
|
build_task_status_data_serializer("DashboardDataStatusDataSerializer"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-03-22 01:09:09 +03:30
|
|
|
class DashboardDataGenerateView(APIView):
|
|
|
|
|
@extend_schema(
|
|
|
|
|
tags=["Dashboard Data"],
|
|
|
|
|
summary="Generate dashboard data",
|
|
|
|
|
request=inline_serializer(
|
|
|
|
|
name="DashboardDataGenerateRequest",
|
|
|
|
|
fields={
|
|
|
|
|
"sensor_id": drf_serializers.UUIDField(required=False),
|
|
|
|
|
"snesor_id": drf_serializers.UUIDField(required=False),
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
responses={
|
2026-03-25 01:56:41 +03:30
|
|
|
202: build_response(
|
|
|
|
|
DashboardDataGenerateResponseSerializer,
|
|
|
|
|
"تسک ساخت داده داشبورد در صف قرار گرفت.",
|
|
|
|
|
),
|
|
|
|
|
400: build_response(
|
|
|
|
|
DashboardDataErrorResponseSerializer,
|
|
|
|
|
"پارامتر ورودی نامعتبر است.",
|
2026-03-22 01:09:09 +03:30
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
def post(self, request):
|
|
|
|
|
sensor_id = request.data.get("sensor_id") or request.data.get("snesor_id")
|
|
|
|
|
if not sensor_id:
|
|
|
|
|
return Response(
|
|
|
|
|
{"code": 400, "msg": "پارامتر sensor_id الزامی است.", "data": None},
|
|
|
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
|
|
|
)
|
|
|
|
|
try:
|
|
|
|
|
sensor_id = str(UUID(str(sensor_id)))
|
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
|
return Response(
|
|
|
|
|
{"code": 400, "msg": "sensor_id باید UUID معتبر باشد.", "data": None},
|
|
|
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
task = generate_dashboard_data_task.delay(sensor_id)
|
|
|
|
|
return Response(
|
|
|
|
|
{
|
|
|
|
|
"code": 202,
|
|
|
|
|
"msg": "dashboard task queued",
|
|
|
|
|
"data": {
|
|
|
|
|
"task_id": task.id,
|
|
|
|
|
"status_url": f"/api/dashboard-data/{task.id}/status/",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
status=status.HTTP_202_ACCEPTED,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DashboardDataStatusView(APIView):
|
|
|
|
|
@extend_schema(
|
|
|
|
|
tags=["Dashboard Data"],
|
|
|
|
|
summary="Dashboard task status",
|
2026-03-25 01:56:41 +03:30
|
|
|
responses={
|
|
|
|
|
200: build_response(
|
|
|
|
|
DashboardDataStatusResponseSerializer,
|
|
|
|
|
"وضعیت فعلی تسک داده داشبورد.",
|
|
|
|
|
),
|
|
|
|
|
},
|
2026-03-22 01:09:09 +03:30
|
|
|
)
|
|
|
|
|
def get(self, request, task_id):
|
|
|
|
|
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},
|
|
|
|
|
status=status.HTTP_200_OK,
|
|
|
|
|
)
|