File size: 2,811 Bytes
4847e7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from drf_yasg.utils import swagger_auto_schema
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

from solar_api.serializers import (
    BillOptimizationRequestSerializer,
    BillOptimizationResponseSerializer,
)
from solar_api.services.bill_optimization_service import BillOptimizationService

# Stateless service β€” safe to instantiate once at module level
_service = BillOptimizationService()


class BillOptimizationView(APIView):
    """

    POST /api/solar/bill-optimization-slab/



    Calculates the recommended solar capacity to reduce a monthly electricity

    bill from a current amount to a target amount, using Indian slab-based

    tariff calculations.

    """

    @swagger_auto_schema(

        operation_summary="Solar bill optimisation (slab tariff)",

        operation_description=(

            "Accepts the user's current electricity bill and a desired target bill, "

            "then calculates the required solar capacity (kW) and number of panels "

            "needed to bridge the gap using Indian slab-based tariff rates.\n\n"

            "**Tariff slabs (β‚Ή/unit)**\n"

            "| Slab | Rate |\n"

            "|------|------|\n"

            "| 0 – 50 units | β‚Ή3.00 |\n"

            "| 51 – 100 units | β‚Ή3.50 |\n"

            "| 101 – 200 units | β‚Ή5.00 |\n"

            "| 201+ units | β‚Ή7.00 |\n\n"

            "**Assumptions**: 1 kW solar β†’ 120 units/month Β· panel size = 540 W"

        ),

        request_body=BillOptimizationRequestSerializer,

        responses={

            200: BillOptimizationResponseSerializer,

            400: "Validation error β€” see error details in response body.",

            500: "Internal server error.",

        },

        tags=["Solar Optimisation"],

    )
    def post(self, request):
        # ── 1. Validate & deserialize request ────────────────────────
        req_serializer = BillOptimizationRequestSerializer(data=request.data)
        if not req_serializer.is_valid():
            return Response(req_serializer.errors, status=status.HTTP_400_BAD_REQUEST)

        # ── 2. Run pure-calculation service ───────────────────────────
        result, status_code = _service.optimize(req_serializer.validated_data)

        if status_code != 200:
            return Response(result, status=status_code)

        # ── 3. Serialize & return response ────────────────────────────
        resp_serializer = BillOptimizationResponseSerializer(result)
        return Response(resp_serializer.data, status=status.HTTP_200_OK)