<commit_before>import os

from django.core.management import call_command
from rest_framework import serializers

from kolibri.core.auth.models import Facility
from kolibri.core.logger.csv_export import CSV_EXPORT_FILENAMES
from kolibri.core.tasks.decorators import register_task
from kolibri.core.tasks.permissions import IsAdminForJob
from kolibri.core.tasks.validation import JobValidator
from kolibri.utils import conf


def get_logs_dir_and_filepath(log_type, facility):
    logs_dir = os.path.join(conf.KOLIBRI_HOME, "log_export")
    filepath = os.path.join(
        logs_dir,
        CSV_EXPORT_FILENAMES[log_type].format(facility.name, facility.id[:4]),
    )
    return logs_dir, filepath


class ExportLogCSVValidator(JobValidator):
    facility = serializers.PrimaryKeyRelatedField(
        queryset=Facility.objects.all(), required=False
    )
    log_type = serializers.ChoiceField(choices=list(CSV_EXPORT_FILENAMES.keys()))

    def validate(self, data):
        facility = data.get("facility", None)
        if facility is None and "user" in self.context:
            facility = self.context["user"].facility
        elif facility is None:
            raise serializers.ValidationError(
                "Facility must be specified when no user is available."
            )
        logs_dir, filepath = get_logs_dir_and_filepath(data["log_type"], facility)
        if not os.path.isdir(logs_dir):
            os.mkdir(logs_dir)

        return {
            "facility_id": facility.id,
            "args": [data["log_type"], filepath, facility.id],
        }


@register_task(
    validator=ExportLogCSVValidator,
    track_progress=True,
    permission_classes=[IsAdminForJob],
)
def exportlogcsv(
    log_type,
    filepath,
    facility_id,
):
    """
    Dumps in csv format the required logs.
    By default it will be dump contentsummarylog.

    :param: logtype: Kind of log to dump, summary or session.
    :param: facility.
    """
    call_command(
        "exportlogs",
        log_type=log_type,
        output_file=filepath,
        facility=facility_id,
        overwrite=True,
    )
<commit_msg>Simplify log export tasks.<commit_after>import os

from django.core.management import call_command
from rest_framework import serializers

from kolibri.core.auth.models import Facility
from kolibri.core.logger.csv_export import CSV_EXPORT_FILENAMES
from kolibri.core.tasks.decorators import register_task
from kolibri.core.tasks.permissions import IsAdminForJob
from kolibri.core.tasks.validation import JobValidator
from kolibri.utils import conf


def get_filepath(log_type, facility_id):
    facility = Facility.objects.get(id=facility_id)
    logs_dir = os.path.join(conf.KOLIBRI_HOME, "log_export")
    if not os.path.isdir(logs_dir):
        os.mkdir(logs_dir)
    filepath = os.path.join(
        logs_dir,
        CSV_EXPORT_FILENAMES[log_type].format(facility.name, facility.id[:4]),
    )
    return filepath


class ExportLogCSVValidator(JobValidator):
    facility = serializers.PrimaryKeyRelatedField(
        queryset=Facility.objects.all(), required=False
    )

    def validate(self, data):
        facility = data.get("facility", None)
        if facility is None and "user" in self.context:
            facility = self.context["user"].facility
        elif facility is None:
            raise serializers.ValidationError(
                "Facility must be specified when no user is available."
            )

        return {
            "facility_id": facility.id,
            "args": [facility.id],
        }


def _exportlogcsv(log_type, facility_id):
    filepath = get_filepath(log_type, facility_id)
    call_command(
        "exportlogs",
        log_type=log_type,
        output_file=filepath,
        facility=facility_id,
        overwrite=True,
    )


@register_task(
    validator=ExportLogCSVValidator,
    track_progress=True,
    permission_classes=[IsAdminForJob],
)
def exportsessionlogcsv(
    facility_id,
):
    """
    Dumps in csv format the content session logs.

    :param: facility.
    """
    _exportlogcsv("session", facility_id)


@register_task(
    validator=ExportLogCSVValidator,
    track_progress=True,
    permission_classes=[IsAdminForJob],
)
def exportsummarylogcsv(
    facility_id,
):
    """
    Dumps in csv format the content summary logs.

    :param: facility.
    """
    _exportlogcsv("summary", facility_id)

{'fraction_non_alphanumeric': 0.0894844424256189, 'fraction_numerical': 0.0004542357483533954, 'mean_word_length': 3.178368121442125, 'pattern_counts': {'":': 4, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6173387', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1333, 'n_words': 291}