File size: 4,809 Bytes
fbb917f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging
from typing import Optional, List
from fastapi import APIRouter, Depends, HTTPException, BackgroundTasks

from auth.models import User
from auth.service import get_current_user
from models.statements import ProcessingState, ProcessingStage
from core.state import statements
from core.extraction import run_coordinate_extraction_task
from core.statement_io import _persist_swap_to_xlsx

logger = logging.getLogger("befree.processing")

router = APIRouter(prefix="/api/v1/statements", tags=["processing"])


@router.post("/{statement_id}/process")
async def start_processing(
    statement_id: str,
    background_tasks: BackgroundTasks,
    current_user: User = Depends(get_current_user)
):
    """Start processing: detect layout then immediately begin extraction (no HITL gate)."""
    if statement_id not in statements:
        raise HTTPException(status_code=404, detail="Statement not found")

    stmt = statements[statement_id]
    stmt.status = "detecting_layout"
    stmt.layout_detected_count = 0
    stmt.extracted_count = 0

    background_tasks.add_task(run_coordinate_extraction_task, statement_id)

    return {"statementId": statement_id, "status": "detecting_layout"}


@router.get("/{statement_id}/processing", response_model=ProcessingState)
async def get_processing_state(statement_id: str, current_user: User = Depends(get_current_user)):
    """Get processing progress and coordinate layout detection status."""
    if statement_id not in statements:
        raise HTTPException(status_code=404, detail="Statement not found")
    
    stmt = statements[statement_id]
    
    stages = [
        ProcessingStage(key="read", label="Reading PDF", status="completed", progress=100),
        ProcessingStage(key="detect", label="Detecting Layout", status="pending", progress=0),
        ProcessingStage(key="extract", label="Extracting Transactions", status="pending", progress=0),
        ProcessingStage(key="audit", label="Running Audits", status="pending", progress=0),
        ProcessingStage(key="generate", label="Generating Workbook", status="pending", progress=0),
    ]
    
    overall = 0
    estimated = None
    
    total_files = len(getattr(stmt, "pdf_paths", [1])) if getattr(stmt, "pdf_paths", None) else 1
    
    if stmt.status == "uploaded":
        stages[0].status = "active"
        stages[0].progress = 50
        overall = 10
    elif stmt.status == "detecting_layout":
        stages[0].status = "completed"
        stages[0].progress = 100
        stages[1].status = "active"
        detected = getattr(stmt, "layout_detected_count", 0)
        stages[1].progress = int((detected / max(total_files, 1)) * 100)
        overall = 10 + int((stages[1].progress / 100) * 15)
        estimated = max(10 - detected * 2, 2)
    elif stmt.status in ("awaiting_layout_review", "extracting"):
        stages[0].status = "completed"
        stages[0].progress = 100
        stages[1].status = "completed"
        stages[1].progress = 100
        stages[2].status = "active"
        extracted = getattr(stmt, "extracted_count", 0)
        stages[2].progress = int((extracted / max(total_files, 1)) * 100)
        overall = 25 + int((stages[2].progress / 100) * 35)
        estimated = max(20 - extracted * 4, 3)
    elif stmt.status in ("ready", "completed"):
        for s in stages:
            s.status = "completed"
            s.progress = 100
        overall = 100
    elif stmt.status == "failed":
        stages[0].status = "failed"
        overall = 0
        
    return ProcessingState(
        statementId=statement_id,
        overallProgress=overall,
        estimatedSecondsRemaining=estimated,
        stages=stages,
        ocrEnabled=True,
        multiEngine=True,
        confidenceScoring=True,
        validationChecks=True,
        status=stmt.status,
    )


@router.post("/{statement_id}/processing/cancel")
async def cancel_processing(statement_id: str, current_user: User = Depends(get_current_user)):
    """Cancel processing."""
    if statement_id not in statements:
        raise HTTPException(status_code=404, detail="Statement not found")
    
    statements[statement_id].status = "cancelled"
    return None


@router.post("/{statement_id}/save-progress")
async def save_progress(statement_id: str, current_user: User = Depends(get_current_user)):
    """Save progress."""
    if statement_id not in statements:
        raise HTTPException(status_code=404, detail="Statement not found")
        
    try:
        _persist_swap_to_xlsx(statement_id)
    except Exception as e:
        logger.error(f"Failed to save progress for statement {statement_id}: {e}")
        raise HTTPException(status_code=500, detail=f"Failed to save progress: {e}")
        
    return {"status": "success", "message": "Progress saved successfully"}