<commit_before># -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function

from django.conf import settings
from django.http import Http404
from rest_framework import status
from rest_framework.generics import CreateAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from libs.utils import to_bool
from libs.views import ProtectedView
from plugins.serializers import TensorboardJobSerializer, NotebookJobSerializer
from projects.models import Project
from projects.permissions import IsProjectOwnerOrPublicReadOnly, get_permissible_project
from projects.tasks import start_tensorboard, stop_tensorboard, build_notebook, stop_notebook
from repos import git
from schedulers import notebook_scheduler, tensorboard_scheduler


class StartTensorboardView(CreateAPIView):
    queryset = Project.objects.all()
    serializer_class = TensorboardJobSerializer
    permission_classes = (IsAuthenticated, IsProjectOwnerOrPublicReadOnly)
    lookup_field = 'name'

    def filter_queryset(self, queryset):
        username = self.kwargs['username']
        return queryset.filter(user__username=username)

    @staticmethod
    def _get_default_tensorboard_config(project):
        return {
            'config': {
                'version': 1,
                'project': {'name': project.name},
                'run': {'image': settings.TENSORBOARD_DOCKER_IMAGE}
            }
        }

    def _should_create_tensorboard_job(self, project):
        # If the project already has a tensorboard specification
        # and no data is provided to update
        # then we do not need to create a TensorboardJob
        if project.tensorboard and not self.request.data:
            return False
        return True

    def _create_tensorboard(self, project):
        if not self._should_create_tensorboard_job(project):
            return
        config = self.request.data or self._get_default_tensorboard_config(project)
        serializer = self.get_serializer(instance=project.tensorboard, data=config)
        serializer.is_valid(raise_exception=True)
        project.tensorboard = serializer.save(user=self.request.user, project=project)
        project.save()

    def post(self, request, *args, **kwargs):
        obj = self.get_object()
        if not obj.has_tensorboard:
            self._create_tensorboard(obj)
            if not obj.tensorboard.is_running:
                start_tensorboard.delay(project_id=obj.id)
        return Response(status=status.HTTP_200_OK)


class StopTensorboardView(CreateAPIView):
    queryset = Project.objects.all()
    permission_classes = (IsAuthenticated, IsProjectOwnerOrPublicReadOnly)
    lookup_field = 'name'

    def filter_queryset(self, queryset):
        username = self.kwargs['username']
        return queryset.filter(user__username=username)

    def post(self, request, *args, **kwargs):
        obj = self.get_object()
        if obj.has_tensorboard:
            stop_tensorboard.delay(project_id=obj.id)
        return Response(status=status.HTTP_200_OK)


class StartNotebookView(CreateAPIView):
    queryset = Project.objects.all()
    serializer_class = NotebookJobSerializer
    permission_classes = (IsAuthenticated, IsProjectOwnerOrPublicReadOnly)
    lookup_field = 'name'

    def filter_queryset(self, queryset):
        username = self.kwargs['username']
        return queryset.filter(user__username=username)

    def _should_create_notebook_job(self, project):
        # If the project already has a notebook specification
        # and no data is provided to update
        # then we do not need to create a TensorboardJob
        if project.notebook and not self.request.data:
            return False
        return True

    def _create_notebook(self, project):
        if not self._should_create_notebook_job(project):
            return
        serializer = self.get_serializer(instance=project.notebook, data=self.request.data)
        serializer.is_valid(raise_exception=True)
        project.notebook = serializer.save(user=self.request.user, project=project)
        project.save()

    def post(self, request, *args, **kwargs):
        obj = self.get_object()
        if not obj.has_notebook:
            self._create_notebook(obj)
            if not obj.notebook.is_running:
                build_notebook.delay(project_id=obj.id)
        return Response(status=status.HTTP_200_OK)


class StopNotebookView(CreateAPIView):
    queryset = Project.objects.all()
    permission_classes = (IsAuthenticated, IsProjectOwnerOrPublicReadOnly)
    lookup_field = 'name'

    def filter_queryset(self, queryset):
        username = self.kwargs['username']
        return queryset.filter(user__username=username)

    def post(self, request, *args, **kwargs):
        obj = self.get_object()
        if obj.has_notebook:
            commit = request.data.get('commit')
            commit = to_bool(commit) if commit is not None else True
            if commit:
                # Commit changes
                git.commit(obj.repo.path, request.user.email, request.user.username)
            else:
                # Reset changes
                git.undo(obj.repo.path)
            stop_notebook.delay(project_id=obj.id)
        return Response(status=status.HTTP_200_OK)


class PluginJobView(ProtectedView):
    @staticmethod
    def get_base_path(project):
        return ''
<commit_msg>Update notebook status if it should stop and still in the build process<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function

from django.conf import settings
from django.http import Http404
from rest_framework import status
from rest_framework.generics import CreateAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from libs.utils import to_bool
from libs.views import ProtectedView
from plugins.serializers import TensorboardJobSerializer, NotebookJobSerializer
from projects.models import Project
from projects.permissions import IsProjectOwnerOrPublicReadOnly, get_permissible_project
from projects.tasks import start_tensorboard, stop_tensorboard, build_notebook, stop_notebook
from repos import git
from schedulers import notebook_scheduler, tensorboard_scheduler
from spawners.utils.constants import ExperimentLifeCycle


class StartTensorboardView(CreateAPIView):
    queryset = Project.objects.all()
    serializer_class = TensorboardJobSerializer
    permission_classes = (IsAuthenticated, IsProjectOwnerOrPublicReadOnly)
    lookup_field = 'name'

    def filter_queryset(self, queryset):
        username = self.kwargs['username']
        return queryset.filter(user__username=username)

    @staticmethod
    def _get_default_tensorboard_config(project):
        return {
            'config': {
                'version': 1,
                'project': {'name': project.name},
                'run': {'image': settings.TENSORBOARD_DOCKER_IMAGE}
            }
        }

    def _should_create_tensorboard_job(self, project):
        # If the project already has a tensorboard specification
        # and no data is provided to update
        # then we do not need to create a TensorboardJob
        if project.tensorboard and not self.request.data:
            return False
        return True

    def _create_tensorboard(self, project):
        if not self._should_create_tensorboard_job(project):
            return
        config = self.request.data or self._get_default_tensorboard_config(project)
        serializer = self.get_serializer(instance=project.tensorboard, data=config)
        serializer.is_valid(raise_exception=True)
        project.tensorboard = serializer.save(user=self.request.user, project=project)
        project.save()

    def post(self, request, *args, **kwargs):
        obj = self.get_object()
        if not obj.has_tensorboard:
            self._create_tensorboard(obj)
            if not obj.tensorboard.is_running:
                start_tensorboard.delay(project_id=obj.id)
        return Response(status=status.HTTP_200_OK)


class StopTensorboardView(CreateAPIView):
    queryset = Project.objects.all()
    permission_classes = (IsAuthenticated, IsProjectOwnerOrPublicReadOnly)
    lookup_field = 'name'

    def filter_queryset(self, queryset):
        username = self.kwargs['username']
        return queryset.filter(user__username=username)

    def post(self, request, *args, **kwargs):
        obj = self.get_object()
        if obj.has_tensorboard:
            stop_tensorboard.delay(project_id=obj.id)
        return Response(status=status.HTTP_200_OK)


class StartNotebookView(CreateAPIView):
    queryset = Project.objects.all()
    serializer_class = NotebookJobSerializer
    permission_classes = (IsAuthenticated, IsProjectOwnerOrPublicReadOnly)
    lookup_field = 'name'

    def filter_queryset(self, queryset):
        username = self.kwargs['username']
        return queryset.filter(user__username=username)

    def _should_create_notebook_job(self, project):
        # If the project already has a notebook specification
        # and no data is provided to update
        # then we do not need to create a TensorboardJob
        if project.notebook and not self.request.data:
            return False
        return True

    def _create_notebook(self, project):
        if not self._should_create_notebook_job(project):
            return
        serializer = self.get_serializer(instance=project.notebook, data=self.request.data)
        serializer.is_valid(raise_exception=True)
        project.notebook = serializer.save(user=self.request.user, project=project)
        project.save()

    def post(self, request, *args, **kwargs):
        obj = self.get_object()
        if not obj.has_notebook:
            self._create_notebook(obj)
            if not obj.notebook.is_running:
                build_notebook.delay(project_id=obj.id)
        return Response(status=status.HTTP_200_OK)


class StopNotebookView(CreateAPIView):
    queryset = Project.objects.all()
    permission_classes = (IsAuthenticated, IsProjectOwnerOrPublicReadOnly)
    lookup_field = 'name'

    def filter_queryset(self, queryset):
        username = self.kwargs['username']
        return queryset.filter(user__username=username)

    def post(self, request, *args, **kwargs):
        obj = self.get_object()
        if obj.has_notebook:
            commit = request.data.get('commit')
            commit = to_bool(commit) if commit is not None else True
            if commit:
                # Commit changes
                git.commit(obj.repo.path, request.user.email, request.user.username)
            else:
                # Reset changes
                git.undo(obj.repo.path)
            stop_notebook.delay(project_id=obj.id)
        elif obj.notebook and obj.notebook.is_running:
            obj.notebook.set_status(status=ExperimentLifeCycle.STOPPED,
                                    message='Notebook was stopped')
        return Response(status=status.HTTP_200_OK)


class PluginJobView(ProtectedView):
    @staticmethod
    def get_base_path(project):
        return ''

{'fraction_non_alphanumeric': 0.07752768846016435, 'fraction_numerical': 0.003036798856734548, 'mean_word_length': 3.096963044273692, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3796482', 'n_tokens_mistral': 3088, 'n_tokens_neox': 3013, 'n_words': 712}