# backend/server/apps/endpoints/views.py file
from rest_framework import viewsets
from rest_framework import mixins
from rest_framework.views import APIView
from rest_framework.response import Response

from endpoints.models import Endpoint, MLAlgorithm , MLAlgorithmStatus , MLRequest
from endpoints.api.serializers import EndpointSerializer,MLAlgorithmSerializer, MLAlgorithmStatusSerializer,MLRequestSerializer



class EndpointViewSet(
    mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet
):
    serializer_class = EndpointSerializer
    queryset = Endpoint.objects.all()


class MLAlgorithmViewSet(
    mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet
):
    serializer_class = MLAlgorithmSerializer
    queryset = MLAlgorithm.objects.all()


def deactivate_other_statuses(instance):
    old_statuses = MLAlgorithmStatus.objects.filter(parent_mlalgorithm = instance.parent_mlalgorithm,
                                                        created_at__lt=instance.created_at,
                                                        active=True)
    for i in range(len(old_statuses)):
        old_statuses[i].active = False
    MLAlgorithmStatus.objects.bulk_update(old_statuses, ["active"])

class MLAlgorithmStatusViewSet(
    mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet,
    mixins.CreateModelMixin
):
    serializer_class = MLAlgorithmStatusSerializer
    queryset = MLAlgorithmStatus.objects.all()
    def perform_create(self, serializer):
        try:
            with transaction.atomic():
                instance = serializer.save(active=True)
                # set active=False for other statuses
                deactivate_other_statuses(instance)



        except Exception as e:
            raise APIException(str(e))

class MLRequestViewSet(
    mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet,
    mixins.UpdateModelMixin
):
    serializer_class = MLRequestSerializer
    queryset = MLRequest.objects.all()




class LastFiveMLRequestView(viewsets.ViewSet):
    serializer_class = MLRequestSerializer

    queryset = MLRequest.objects.all().order_by('-created_at')[:5]

    def list(self, request):
        queryset = MLRequest.objects.all().order_by('-created_at')[:5]
        serializer = MLRequestSerializer(queryset, many=True)
        return Response(serializer.data)
37851943fd0f3e47009f1583b7c3f01665a4aab4
<mask token>


class MLRequestViewSet(mixins.RetrieveModelMixin, mixins.ListModelMixin,
    viewsets.GenericViewSet, mixins.UpdateModelMixin):
    <mask token>
    <mask token>


class LastFiveMLRequestView(viewsets.ViewSet):
    serializer_class = MLRequestSerializer
    queryset = MLRequest.objects.all().order_by('-created_at')[:5]

    def list(self, request):
        queryset = MLRequest.objects.all().order_by('-created_at')[:5]
        serializer = MLRequestSerializer(queryset, many=True)
        return Response(serializer.data)

<mask token>


class MLAlgorithmStatusViewSet(mixins.RetrieveModelMixin, mixins.
    ListModelMixin, viewsets.GenericViewSet, mixins.CreateModelMixin):
    <mask token>
    <mask token>
    <mask token>


class MLRequestViewSet(mixins.RetrieveModelMixin, mixins.ListModelMixin,
    viewsets.GenericViewSet, mixins.UpdateModelMixin):
    serializer_class = MLRequestSerializer
    queryset = MLRequest.objects.all()


class LastFiveMLRequestView(viewsets.ViewSet):
    serializer_class = MLRequestSerializer
    queryset = MLRequest.objects.all().order_by('-created_at')[:5]

    def list(self, request):
        queryset = MLRequest.objects.all().order_by('-created_at')[:5]
        serializer = MLRequestSerializer(queryset, many=True)
        return Response(serializer.data)

<mask token>


class MLAlgorithmStatusViewSet(mixins.RetrieveModelMixin, mixins.
    ListModelMixin, viewsets.GenericViewSet, mixins.CreateModelMixin):
    serializer_class = MLAlgorithmStatusSerializer
    queryset = MLAlgorithmStatus.objects.all()

    def perform_create(self, serializer):
        try:
            with transaction.atomic():
                instance = serializer.save(active=True)
                deactivate_other_statuses(instance)
        except Exception as e:
            raise APIException(str(e))


class MLRequestViewSet(mixins.RetrieveModelMixin, mixins.ListModelMixin,
    viewsets.GenericViewSet, mixins.UpdateModelMixin):
    serializer_class = MLRequestSerializer
    queryset = MLRequest.objects.all()


class LastFiveMLRequestView(viewsets.ViewSet):
    serializer_class = MLRequestSerializer
    queryset = MLRequest.objects.all().order_by('-created_at')[:5]

    def list(self, request):
        queryset = MLRequest.objects.all().order_by('-created_at')[:5]
        serializer = MLRequestSerializer(queryset, many=True)
        return Response(serializer.data)

<mask token>


class MLAlgorithmViewSet(mixins.RetrieveModelMixin, mixins.ListModelMixin,
    viewsets.GenericViewSet):
    serializer_class = MLAlgorithmSerializer
    queryset = MLAlgorithm.objects.all()


<mask token>


class MLAlgorithmStatusViewSet(mixins.RetrieveModelMixin, mixins.
    ListModelMixin, viewsets.GenericViewSet, mixins.CreateModelMixin):
    serializer_class = MLAlgorithmStatusSerializer
    queryset = MLAlgorithmStatus.objects.all()

    def perform_create(self, serializer):
        try:
            with transaction.atomic():
                instance = serializer.save(active=True)
                deactivate_other_statuses(instance)
        except Exception as e:
            raise APIException(str(e))


class MLRequestViewSet(mixins.RetrieveModelMixin, mixins.ListModelMixin,
    viewsets.GenericViewSet, mixins.UpdateModelMixin):
    serializer_class = MLRequestSerializer
    queryset = MLRequest.objects.all()


class LastFiveMLRequestView(viewsets.ViewSet):
    serializer_class = MLRequestSerializer
    queryset = MLRequest.objects.all().order_by('-created_at')[:5]

    def list(self, request):
        queryset = MLRequest.objects.all().order_by('-created_at')[:5]
        serializer = MLRequestSerializer(queryset, many=True)
        return Response(serializer.data)

# backend/server/apps/endpoints/views.py file
from rest_framework import viewsets
from rest_framework import mixins
from rest_framework.views import APIView
from rest_framework.response import Response

from endpoints.models import Endpoint, MLAlgorithm , MLAlgorithmStatus , MLRequest
from endpoints.api.serializers import EndpointSerializer,MLAlgorithmSerializer, MLAlgorithmStatusSerializer,MLRequestSerializer



class EndpointViewSet(
    mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet
):
    serializer_class = EndpointSerializer
    queryset = Endpoint.objects.all()


class MLAlgorithmViewSet(
    mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet
):
    serializer_class = MLAlgorithmSerializer
    queryset = MLAlgorithm.objects.all()


def deactivate_other_statuses(instance):
    old_statuses = MLAlgorithmStatus.objects.filter(parent_mlalgorithm = instance.parent_mlalgorithm,
                                                        created_at__lt=instance.created_at,
                                                        active=True)
    for i in range(len(old_statuses)):
        old_statuses[i].active = False
    MLAlgorithmStatus.objects.bulk_update(old_statuses, ["active"])

class MLAlgorithmStatusViewSet(
    mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet,
    mixins.CreateModelMixin
):
    serializer_class = MLAlgorithmStatusSerializer
    queryset = MLAlgorithmStatus.objects.all()
    def perform_create(self, serializer):
        try:
            with transaction.atomic():
                instance = serializer.save(active=True)
                # set active=False for other statuses
                deactivate_other_statuses(instance)



        except Exception as e:
            raise APIException(str(e))

class MLRequestViewSet(
    mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet,
    mixins.UpdateModelMixin
):
    serializer_class = MLRequestSerializer
    queryset = MLRequest.objects.all()




class LastFiveMLRequestView(viewsets.ViewSet):
    serializer_class = MLRequestSerializer

    queryset = MLRequest.objects.all().order_by('-created_at')[:5]

    def list(self, request):
        queryset = MLRequest.objects.all().order_by('-created_at')[:5]
        serializer = MLRequestSerializer(queryset, many=True)
        return Response(serializer.data)