File size: 22,429 Bytes
57cf043
 
 
 
 
 
 
308de05
57cf043
 
 
744a170
86c402d
 
57cf043
 
 
 
 
 
86c402d
57cf043
 
 
 
 
 
86c402d
57cf043
 
 
 
 
 
 
86c402d
57cf043
744a170
86c402d
744a170
86c402d
57cf043
86c402d
 
 
 
 
 
 
 
57cf043
 
 
86c402d
 
57cf043
86c402d
57cf043
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86c402d
57cf043
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86c402d
 
 
 
57cf043
 
 
 
 
 
 
 
 
 
 
 
744a170
 
57cf043
 
 
 
 
 
 
744a170
57cf043
 
86c402d
 
 
57cf043
86c402d
 
 
 
 
 
 
 
 
 
57cf043
 
 
 
86c402d
57cf043
744a170
57cf043
 
 
86c402d
 
 
 
57cf043
 
 
86c402d
57cf043
 
 
 
86c402d
57cf043
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9027f19
 
57cf043
9027f19
57cf043
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
744a170
57cf043
 
 
 
 
 
 
 
 
 
86c402d
57cf043
 
 
 
 
 
 
 
 
 
 
 
 
308de05
86c402d
 
 
57cf043
 
 
 
 
 
 
 
 
9027f19
57cf043
 
 
 
 
 
 
 
86c402d
57cf043
 
 
 
 
 
 
 
 
 
 
 
744a170
 
 
 
308de05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
744a170
308de05
 
 
 
 
 
 
 
 
57cf043
 
 
 
 
 
 
 
 
 
 
 
 
 
86c402d
57cf043
 
 
 
 
 
 
 
 
 
 
 
 
86c402d
57cf043
 
 
 
 
 
 
 
 
744a170
57cf043
 
 
 
86c402d
57cf043
86c402d
57cf043
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86c402d
 
57cf043
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86c402d
57cf043
 
86c402d
57cf043
 
 
 
 
 
 
 
 
 
 
 
308de05
 
57cf043
 
 
 
 
 
 
 
 
308de05
86c402d
308de05
57cf043
308de05
 
 
57cf043
86c402d
57cf043
 
 
 
 
86c402d
 
 
57cf043
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
import json
import logging
import os
import shutil
import zipfile
from datetime import datetime
from pathlib import Path
import asyncio

import torch
from fastapi import BackgroundTasks, HTTPException, UploadFile
from components.dbo.models.entity import EntityModel
from ntr_fileparser import ParsedDocument, UniversalParser
from sqlalchemy.orm import Session

from common.common import get_source_format
from common.configuration import Configuration
from components.dbo.models.dataset import Dataset
from components.dbo.models.dataset_document import DatasetDocument
from components.dbo.models.document import Document
from components.services.entity import EntityService
from schemas.dataset import Dataset as DatasetSchema
from schemas.dataset import DatasetExpanded as DatasetExpandedSchema
from schemas.dataset import DatasetProcessing
from schemas.dataset import DocumentsPage as DocumentsPageSchema
from schemas.dataset import SortQueryList
from schemas.document import Document as DocumentSchema

logger = logging.getLogger(__name__)


class DatasetService:
    """
    Сервис для работы с датасетами.
    """

    def __init__(
        self,
        entity_service: EntityService,
        config: Configuration,
        db: Session,
    ) -> None:
        """
        Инициализация сервиса.

        Args:
            entity_service: Сервис для работы с сущностями
            config: Конфигурация приложения
            db: SQLAlchemy сессия
        """
        logger.info("DatasetService initializing")
        self.db = db
        self.config = config
        self.parser = UniversalParser()
        self.entity_service = entity_service
        self.documents_path = Path(config.db_config.files.documents_path)
        self.tmp_path = Path(os.environ.get("APP_TMP_PATH", '.'))
        logger.info("DatasetService initialized")

    def get_dataset(
        self,
        dataset_id: int,
        page: int = 1,
        page_size: int = 20,
        search: str = '',
        sort: SortQueryList = [],
    ) -> DatasetExpandedSchema:
        """
        Получить пагинированную информацию о датасете и его документах.
        """
        logger.info(
            f"Getting dataset {dataset_id} (page={page}, size={page_size}, search='{search}')"
        )
        self.raise_if_processing()

        with self.db() as session:
            dataset: Dataset = (
                session.query(Dataset).filter(Dataset.id == dataset_id).first()
            )
            if not dataset:
                raise HTTPException(status_code=404, detail='Dataset not found')

            query = (
                session.query(Document)
                .join(DatasetDocument, DatasetDocument.document_id == Document.id)
                .filter(DatasetDocument.dataset_id == dataset_id)
                .filter(Document.title.like(f'%{search}%'))
            )

            query = self.sort_documents(query, sort)

            documents = query.offset((page - 1) * page_size).limit(page_size).all()

            total_documents = (
                session.query(Document)
                .join(DatasetDocument, DatasetDocument.document_id == Document.id)
                .filter(DatasetDocument.dataset_id == dataset_id)
                .filter(Document.title.like(f'%{search}%'))
                .count()
            )

            dataset_expanded = DatasetExpandedSchema(
                id=dataset.id,
                name=dataset.name,
                isDraft=dataset.is_draft,
                isActive=dataset.is_active,
                dateCreated=dataset.date_created,
                data=DocumentsPageSchema(
                    page=[
                        DocumentSchema(
                            id=document.id,
                            name=document.title,
                            owner=document.owner,
                            status=document.status,
                        )
                        for document in documents
                    ],
                    total=total_documents,
                    pageNumber=page,
                    pageSize=page_size,
                ),
            )

        return dataset_expanded

    def get_datasets(self) -> list[DatasetSchema]:
        """
        Получить список всех датасетов.
        """
        self.raise_if_processing()

        with self.db() as session:
            datasets: list[Dataset] = session.query(Dataset).all()
        return [
            DatasetSchema(
                id=dataset.id,
                name=dataset.name,
                isDraft=dataset.is_draft,
                isActive=dataset.is_active,
                dateCreated=dataset.date_created,
            )
            for dataset in datasets
        ]

    def create_draft(self, parent_id: int) -> DatasetSchema:
        """
        Создать черновик датасета на основе родительского датасета.
        """
        logger.info(f"Creating draft dataset from parent {parent_id}")
        self.raise_if_processing()

        with self.db() as session:
            parent = session.query(Dataset).filter(Dataset.id == parent_id).first()
            if not parent:
                raise HTTPException(status_code=404, detail='Parent dataset not found')

            if parent.is_draft:
                raise HTTPException(status_code=400, detail='Parent dataset is draft')

            date = datetime.now()
            dataset = Dataset(
                name=f"{date.strftime('%Y-%m-%d %H:%M:%S')}",
                is_draft=True,
                is_active=False,
            )

            parent_documents = (
                session.query(DatasetDocument)
                .filter(DatasetDocument.dataset_id == parent_id)
                .all()
            )
            new_dataset_documents = [
                DatasetDocument(
                    dataset_id=dataset.id,
                    document_id=document.id,
                )
                for document in parent_documents
            ]

            dataset.documents = new_dataset_documents

            session.add(dataset)
            session.commit()
            session.refresh(dataset)

        return self.get_dataset(dataset.id)

    def delete_dataset(self, dataset_id: int) -> None:
        """
        Удалить черновик датасета.
        """
        logger.info(f"Deleting dataset {dataset_id}")
        self.raise_if_processing()

        with self.db() as session:
            dataset: Dataset = (
                session.query(Dataset).filter(Dataset.id == dataset_id).first()
            )

            if not dataset:
                raise HTTPException(status_code=404, detail='Dataset not found')

            if dataset.name == 'default':
                raise HTTPException(
                    status_code=400, detail='Default dataset cannot be deleted'
                )

            if dataset.is_active:
                raise HTTPException(
                    status_code=403, detail='Active dataset cannot be deleted'
                )
            
            session.query(EntityModel).filter(EntityModel.dataset_id == dataset_id).delete()
            session.delete(dataset)
            session.commit()

    def apply_draft_task(self, dataset_id: int):
        """
        Метод для выполнения в отдельном процессе.
        """
        logger.info(f"apply_draft_task started")
        try:
            with self.db() as session:
                dataset = (
                    session.query(Dataset).filter(Dataset.id == dataset_id).first()
                )
                if not dataset:
                    raise HTTPException(
                        status_code=404,
                        detail=f"Dataset with id {dataset_id} not found",
                    )

                active_dataset = (
                    session.query(Dataset).filter(Dataset.is_active == True).first()
                )

                self.apply_draft(dataset)
                dataset.is_draft = False
                dataset.is_active = True
                if active_dataset:
                    active_dataset.is_active = False

                session.commit()
            logger.info(f"apply_draft_task finished")
        except Exception as e:
            logger.error(f"Error applying draft: {e}")
            raise

    def activate_dataset(
        self, dataset_id: int, background_tasks: BackgroundTasks
    ) -> DatasetExpandedSchema:
        """
        Активировать датасет в фоновой задаче.
        """

        logger.info(f"Activating dataset {dataset_id}")
        self.raise_if_processing()

        with self.db() as session:
            dataset = session.query(Dataset).filter(Dataset.id == dataset_id).first()
            active_dataset = session.query(Dataset).filter(Dataset.is_active).first()
            if not dataset:
                raise HTTPException(status_code=404, detail='Dataset not found')

            if dataset.is_active:
                raise HTTPException(status_code=400, detail='Dataset is already active')

            if dataset.is_draft:
                background_tasks.add_task(self.apply_draft_task, dataset_id)
            else:
                dataset.is_active = True

                if active_dataset:
                    active_dataset.is_active = False

                session.commit()

            return self.get_dataset(dataset_id)

    def get_processing(self) -> DatasetProcessing:
        """
        Получить информацию о процессе обработки датасета.
        """
        tmp_file = Path(self.tmp_path / 'tmp.json')
        if tmp_file.exists():
            try:
                with open(tmp_file, 'r', encoding='utf-8') as f:
                    info = json.load(f)
            except Exception as e:
                logger.warning(f"Error loading processing info: {e}")
                return DatasetProcessing(
                    status='in_progress',
                    total=None,
                    current=None,
                    datasetName=None,
                )

            with self.db() as session:
                dataset_name = (
                    session.query(Dataset)
                    .filter(Dataset.id == info['dataset_id'])
                    .first()
                    .name
                )

            return DatasetProcessing(
                status='in_progress',
                total=info['total'],
                current=info['current'],
                datasetName=dataset_name,
            )

        return DatasetProcessing(
            status='ready',
            total=None,
            current=None,
            datasetName=None,
        )

    def upload_zip(self, file: UploadFile) -> DatasetExpandedSchema:
        """
        Загрузить архив с датасетом.
        """
        logger.info(f"Uploading ZIP file {file.filename}")
        self.raise_if_processing()

        file_location = Path(self.tmp_path / 'tmp' / 'tmp.zip')
        logger.debug(f"Saving uploaded file to {file_location}")
        file_location.parent.mkdir(parents=True, exist_ok=True)
        with open(file_location, 'wb') as f:
            f.write(file.file.read())

        with zipfile.ZipFile(file_location, 'r') as zip_ref:
            zip_ref.extractall(file_location.parent)

        dataset = self.create_dataset_from_directory(
            is_default=False,
            directory_with_documents=file_location.parent,
        )

        file_location.unlink()
        shutil.rmtree(file_location.parent)

        return self.get_dataset(dataset.id)

    def apply_draft(
        self,
        dataset: Dataset,
    ) -> None:
        """
        Сохранить черновик как полноценный датасет.
        Вызывает асинхронную обработку документов.

        Args:
            dataset: Датасет для применения
        """
        torch.set_num_threads(1)
        logger.info(f"Applying draft dataset {dataset.id}")
        if not dataset.is_draft:
            logger.error(f"Dataset {dataset.id} is not a draft")
            raise HTTPException(
                status_code=400, detail='Dataset is not draft but trying to apply it'
            )

        TMP_PATH = Path(self.tmp_path / 'tmp.json')

        def progress_callback(current: int, total: int) -> None:
            log_step = total // 100
            if log_step == 0:
                log_step = 1
            if current % log_step != 0:
                return
            if (total > 10) and (current % (total // 10) == 0):
                logger.info(f"Processing dataset {dataset.id}: {current}/{total}")
            with open(TMP_PATH, 'w', encoding='utf-8') as f:
                json.dump(
                    {
                        'total': total,
                        'current': current,
                        'dataset_id': dataset.id,
                    },
                    f,
                )

        TMP_PATH.touch()

        documents: list[Document] = [
            doc_dataset_link.document for doc_dataset_link in dataset.documents
        ]

        async def process_single_document(document: Document):
            path = self.documents_path / f'{document.id}.{document.source_format}'
            try:
                parsed = self.parser.parse_by_path(str(path))
                if parsed is None:
                    logger.warning(
                        f"Failed to parse document {document.id} at path {path}"
                    )
                    return
                parsed.name = document.title
                await self.entity_service.process_document(
                    parsed,
                    dataset.id,
                    progress_callback=progress_callback,  # Callback остается синхронным
                )
            except Exception as e:
                logger.error(
                    f"Error processing document {document.id} in apply_draft: {e}",
                    exc_info=True,
                )

        async def main_processing():
            tasks = [process_single_document(doc) for doc in documents]
            await asyncio.gather(*tasks)

        try:
            asyncio.run(main_processing())
        finally:
            if TMP_PATH.exists():
                TMP_PATH.unlink()

    def raise_if_processing(self) -> None:
        """
        Поднять ошибку, если процесс обработки датасета еще не завершен.
        """
        if self.get_processing().status == 'in_progress':
            logger.error("Dataset processing is already in progress")
            raise HTTPException(
                status_code=409, detail='Dataset processing is in progress'
            )

    def create_dataset_from_directory(
        self,
        is_default: bool,
        directory_with_documents: Path,
    ) -> Dataset:
        """
        Создать датасет из директории с xml-документами.

        Args:
            is_default: Создать ли датасет по умолчанию.
            directory_with_xmls: Путь к директории с xml-документами.
            directory_with_processed_dataset: Путь к директории с обработанным датасетом - если не передан, будет произведена полная обработка (например, при создании датасета из скриптов).

        Returns:
            Dataset: Созданный датасет.
        """
        logger.info(
            f"Creating {'default' if is_default else 'new'} dataset from directory {directory_with_documents}"
        )
        with self.db() as session:
            documents = []

            date = datetime.now()
            name = 'default' if is_default else f'{date.strftime("%Y-%m-%d %H:%M:%S")}'

            dataset = Dataset(
                name=name,
                is_draft=True,
                is_active=True if is_default else False,
            )
            session.add(dataset)

            for subpath in self._get_recursive_dirlist(directory_with_documents):
                document, relation = self._create_document(
                    directory_with_documents, subpath, dataset
                )
                if document is None:
                    continue
                documents.append(document)
                session.add(document)
                session.add(relation)

            logger.info(f"Created {len(documents)} documents")

            session.flush()

            self.documents_path.mkdir(parents=True, exist_ok=True)

            for document in documents:
                session.refresh(document)
                old_filename = document.filename
                new_filename = '{}.{}'.format(document.id, document.source_format)
                shutil.copy(
                    directory_with_documents / old_filename,
                    self.documents_path / new_filename,
                )
                document.filename = new_filename

            logger.info(f"Documents renamed with ids")

            session.commit()
            session.refresh(dataset)

            dataset_id = dataset.id

        logger.info(f"Dataset {dataset_id} created")

        return dataset

    def create_empty_dataset(self, is_default: bool) -> Dataset:
        """
        Создать пустой датасет.
        """
        with self.db() as session:
            name = (
                'default'
                if is_default
                else f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}'
            )
            dataset = Dataset(
                name=name,
                is_active=True if is_default else False,
                is_draft=False,
            )
            session.add(dataset)
            session.commit()
            session.refresh(dataset)

        return dataset

    @staticmethod
    def _get_recursive_dirlist(path: Path) -> list[Path]:
        """
        Возвращает список всех xml и docx файлов на всех уровнях вложенности.

        Args:
            path: Путь к директории.

        Returns:
            list[Path]: Список путей к xml-файлам относительно path.
        """
        xml_files = set()  # set для отбрасывания неуникальных путей
        for ext in ('*.xml', '*.XML', '*.docx', '*.DOCX'):
            xml_files.update(path.glob(f'**/{ext}'))

        return [p.relative_to(path) for p in xml_files]

    def _create_document(
        self,
        documents_path: Path,
        subpath: os.PathLike,
        dataset: Dataset,
    ) -> tuple[Document | None, DatasetDocument | None]:
        """
        Создаёт документ в базе данных.

        Args:
            documents_path: Путь к директории с документами.
            subpath: Путь к документу относительно documents_path.
            dataset: Датасет, к которому относится документ.

        Returns:
            tuple[Document, DatasetDocument]: Кортеж из документа и его связи с датасетом.
        """
        logger.debug(f"Creating document from {subpath}")

        try:
            source_format = get_source_format(str(subpath))
            path = documents_path / subpath
            parsed: ParsedDocument | None = self.parser.parse_by_path(
                str(path)
            )
            
            if 'Приложение' in parsed.name:
                parsed.name = path.parent.name + ' ' + parsed.name

            if not parsed:
                logger.warning(f"Failed to parse file: {subpath}")
                return None, None

            document = Document(
                filename=str(subpath),
                title=parsed.name,
                status=parsed.meta.status,
                owner=parsed.meta.owner,
                source_format=source_format,
            )
            relation = DatasetDocument(
                document=document,
                dataset=dataset,
            )

            return document, relation

        except Exception as e:
            logger.error(f"Error creating document from {subpath}: {e}")
            return None, None

    def get_current_dataset(self) -> Dataset | None:
        with self.db() as session:
            result = session.query(Dataset).filter(Dataset.is_active == True).first()
        return result

    def get_default_dataset(self) -> Dataset | None:
        with self.db() as session:
            result = session.query(Dataset).filter(Dataset.name == 'default').first()
        return result

    def sort_documents(
        self,
        query: "Query",  # type: ignore
        sort: SortQueryList,
    ) -> "Query":  # type: ignore
        """
        Сортирует документы по заданным полям и направлениям сортировки.
        """
        if sort and (len(sort.sorts) > 0):
            for sort_query in sort.sorts:
                field = sort_query.field
                direction = sort_query.direction

                if field == 'name':
                    column = Document.title
                elif field == 'status':
                    column = Document.status
                elif field == 'owner':
                    column = Document.owner
                elif field == 'id':
                    column = Document.id
                else:
                    raise HTTPException(
                        status_code=400, detail=f'Invalid sort field: {field}'
                    )

                query = query.order_by(
                    column.desc() if direction.lower() == 'desc' else column
                )
        else:
            query = query.order_by(Document.id.desc())  # Default sorting

        return query