<commit_before>from tempfile import NamedTemporaryFile
from typing import IO, TYPE_CHECKING, Dict, List, Union

import petl as etl
from django.utils import timezone

from ...celeryconf import app
from ...core import JobStatus
from ...product.models import Product
from .. import ExportEvents, events
from ..emails import send_email_with_link_to_download_csv
from ..models import ExportFile
from .products_data import get_products_data

if TYPE_CHECKING:
    # flake8: noqa
    from django.db.models import QuerySet


def on_task_failure(self, exc, task_id, args, kwargs, einfo):
    export_file_id = args[0]
    export_file = ExportFile.objects.get(pk=export_file_id)
    update_export_file_when_task_finished(export_file, JobStatus.FAILED)
    events.data_export_failed_event(
        export_file=export_file, user=export_file.created_by, message=str(exc)
    )


def on_task_success(self, retval, task_id, args, kwargs):
    export_file_id = args[0]
    export_file = ExportFile.objects.get(pk=export_file_id)
    update_export_file_when_task_finished(export_file, JobStatus.SUCCESS)
    events.data_export_success_event(
        export_file=export_file, user=export_file.created_by
    )


def update_export_file_when_task_finished(export_file: ExportFile, status: JobStatus):
    export_file.status = status  # type: ignore
    export_file.save(update_fields=["status", "updated_at"])<commit_msg>Drop unused import<commit_after>from tempfile import NamedTemporaryFile
from typing import IO, TYPE_CHECKING, Dict, List, Union

import petl as etl
from django.utils import timezone

from ...celeryconf import app
from ...core import JobStatus
from ...product.models import Product
from .. import events
from ..emails import send_email_with_link_to_download_csv
from ..models import ExportFile
from .products_data import get_products_data

if TYPE_CHECKING:
    # flake8: noqa
    from django.db.models import QuerySet


def on_task_failure(self, exc, task_id, args, kwargs, einfo):
    export_file_id = args[0]
    export_file = ExportFile.objects.get(pk=export_file_id)
    update_export_file_when_task_finished(export_file, JobStatus.FAILED)
    events.data_export_failed_event(
        export_file=export_file, user=export_file.created_by, message=str(exc)
    )


def on_task_success(self, retval, task_id, args, kwargs):
    export_file_id = args[0]
    export_file = ExportFile.objects.get(pk=export_file_id)
    update_export_file_when_task_finished(export_file, JobStatus.SUCCESS)
    events.data_export_success_event(
        export_file=export_file, user=export_file.created_by
    )


def update_export_file_when_task_finished(export_file: ExportFile, status: JobStatus):
    export_file.status = status  # type: ignore
    export_file.save(update_fields=["status", "updated_at"])
{'fraction_non_alphanumeric': 0.07918308849874597, 'fraction_numerical': 0.002149767108563239, 'mean_word_length': 5.260089686098655, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '90642', 'n_tokens_mistral': 897, 'n_tokens_neox': 889, 'n_words': 198}