File size: 1,248 Bytes
da6ee5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""API response serializers."""

from __future__ import annotations

from app.models import Batch, Document
from app.schemas import BatchOut, DocumentOut


def document_out(doc: Document) -> DocumentOut:
    """Map ORM document to API schema."""
    return DocumentOut(
        id=doc.id,
        batch_id=doc.batch_id,
        filename=doc.filename,
        doc_type=doc.doc_type,
        status=doc.status,
        language=doc.language or "",
        extracted=doc.extracted,
        confidence=doc.confidence,
        issues=doc.issues or [],
        is_duplicate=bool(doc.is_duplicate),
        duplicate_of=doc.duplicate_of,
        error=doc.error,
        avg_confidence=doc.avg_confidence,
        created_at=doc.created_at,
        updated_at=doc.updated_at,
    )


def batch_out(batch: Batch) -> BatchOut:
    """Serialize batch with progress percent."""
    progress = 0.0
    if batch.total > 0:
        progress = round(100.0 * batch.processed / batch.total, 1)
    return BatchOut(
        id=batch.id,
        status=batch.status,
        total=batch.total,
        processed=batch.processed,
        failed=batch.failed,
        progress=progress,
        created_at=batch.created_at,
        finished_at=batch.finished_at,
    )