File size: 8,682 Bytes
8ae78b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d14811
8ae78b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import APIRouter, Depends, BackgroundTasks
from sqlalchemy.orm import Session

from app.db.base import get_db
from app.models.processing import ProcessingRequest, ProcessingStatus
from app.services.processing.processing_service import ProcessingService
from app.utils.logging_utils import setup_logger, log_success
from app.utils.auth import get_current_active_user, get_api_key_user
from app.db.models import User

"""
Video Processing API Routes
==========================

This module provides API endpoints for video processing operations, including:
- Initiating video processing
- Checking processing status
- Retrieving processing results

All endpoints are prefixed with '/processing' and include appropriate logging
with endpoint identification in square brackets.
"""

# Setup logger for this module
logger = setup_logger("processing_router")

router = APIRouter(
    prefix="/processing",
    tags=["processing"],
    responses={404: {"description": "Not found"}},
)

@router.post("", response_model=ProcessingStatus)
async def process_video(
    request: ProcessingRequest,
    background_tasks: BackgroundTasks,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user)
):
    """
    Initiate processing of a video.
    
    This endpoint accepts a video ID and initiates the processing pipeline
    as a background task. It returns a processing status object with the
    video ID that can be used to check the status later.
    
    Args:
        request (ProcessingRequest): Request object containing the video ID and processing options
        background_tasks (BackgroundTasks): FastAPI background tasks manager
        db (Session): Database session dependency
        current_user (User): Current active user dependency
        
    Returns:
        ProcessingStatus: Object containing the video ID and initial processing status
        
    Example:
        POST /processing
        {
            "video_id": "vid-12345",
            "frame_rate": 5,
            "language": "en",
            "generate_annotated_video": true
        }
    """
    logger.info(f"[process_video] Received request to process video: {request.video_id}")
    processing_service = ProcessingService(db)
    result = await processing_service.process_video(request, background_tasks)
    logger.info(f"     : {result.video_id}")
    return result

@router.get("/status/{video_id}", response_model=ProcessingStatus)
async def get_processing_status(
    video_id: str,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user)
):
    """
    Get the current processing status of a video.
    
    This endpoint retrieves the current status of a video processing job
    using the video ID returned from the process_video endpoint.
    
    Args:
        video_id (str): Unique identifier for the video processing job
        db (Session): Database session dependency
        current_user (User): Current active user dependency
        
    Returns:
        ProcessingStatus: Object containing the video ID and current processing status
        
    Example:
        GET /processing/status/vid-12345
    """
    logger.info(f"[get_processing_status] Checking status for video ID: {video_id}")
    processing_service = ProcessingService(db)
    status = processing_service.get_processing_status(video_id)
    logger.info(f"[get_processing_status] Status for video ID {video_id}: {status.status}")
    return status

@router.get("/results/{video_id}")
async def get_processing_results(
    video_id: str,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user)
):
    """
    Get the results of a completed video processing job.
    
    This endpoint retrieves the full results of a video processing job
    after it has completed. It should only be called after the status
    endpoint indicates that processing is complete.
    
    Args:
        video_id (str): Unique identifier for the video processing job
        db (Session): Database session dependency
        current_user (User): Current active user dependency
        
    Returns:
        dict: Processing results including behavior analytics data
        
    Example:
        GET /processing/results/vid-12345
    """
    logger.info(f"[get_processing_results] Retrieving results for video ID: {video_id}")
    processing_service = ProcessingService(db)
    results = processing_service.get_processing_results(video_id)
    log_success(logger, f"[get_processing_results] Successfully retrieved results for video ID: {video_id}")
    return results

@router.post("/direct", response_model=ProcessingStatus)
@router.post("/direct/", response_model=ProcessingStatus)
async def process_video_direct(
    request: ProcessingRequest,
    background_tasks: BackgroundTasks,
    db: Session = Depends(get_db),
    api_key_valid: bool = Depends(get_api_key_user)
):
    """
    Initiate processing of a video using API key authentication.
    
    This endpoint accepts a video ID and initiates the processing pipeline
    as a background task. It is secured with API key authentication.
    
    Args:
        request (ProcessingRequest): Request object containing the video ID and processing options
        background_tasks (BackgroundTasks): FastAPI background tasks manager
        db (Session): Database session dependency
        api_key_valid (bool): API key validation dependency
        
    Returns:
        ProcessingStatus: Object containing the video ID and initial processing status
        
    Example:
        POST /processing/direct
        X-API-Key: your-api-key
        {
            "video_id": "vid-12345",
            "frame_rate": 5,
            "language": "en",
            "generate_annotated_video": true
        }
    """
    logger.info(f"[process_video_direct] Received request to process video: {request.video_id}")
    processing_service = ProcessingService(db)
    result = await processing_service.process_video(request, background_tasks)
    logger.info(f"[process_video_direct] Started processing for video ID: {result.video_id}")
    return result

@router.get("/direct/status/{video_id}", response_model=ProcessingStatus)
async def get_processing_status_direct(
    video_id: str,
    db: Session = Depends(get_db),
    api_key_valid: bool = Depends(get_api_key_user)
):
    """
    Get the current processing status of a video using API key authentication.
    
    This endpoint retrieves the current status of a video processing job.
    It is secured with API key authentication.
    
    Args:
        video_id (str): Unique identifier for the video processing job
        db (Session): Database session dependency
        api_key_valid (bool): API key validation dependency
        
    Returns:
        ProcessingStatus: Object containing the video ID and current processing status
        
    Example:
        GET /processing/direct/status/vid-12345
        X-API-Key: your-api-key
    """
    logger.info(f"[get_processing_status_direct] Checking status for video ID: {video_id}")
    processing_service = ProcessingService(db)
    try:
        status = processing_service.get_processing_status(video_id)
        logger.info(f"[get_processing_status_direct] Status for video ID {video_id}: {status.status}, Progress: {status.progress}")
        return status
    except Exception as e:
        logger.error(f"[get_processing_status_direct] Error getting status: {str(e)}")
        raise

@router.get("/direct/results/{video_id}")
async def get_processing_results_direct(
    video_id: str,
    db: Session = Depends(get_db),
    api_key_valid: bool = Depends(get_api_key_user)
):
    """
    Get the results of a completed video processing job using API key authentication.
    
    This endpoint retrieves the full results of a video processing job
    after it has completed. It is secured with API key authentication.
    
    Args:
        video_id (str): Unique identifier for the video processing job
        db (Session): Database session dependency
        api_key_valid (bool): API key validation dependency
        
    Returns:
        dict: Processing results including behavior analytics data
        
    Example:
        GET /processing/direct/results/vid-12345
        X-API-Key: your-api-key
    """
    logger.info(f"[get_processing_results_direct] Retrieving results for video ID: {video_id}")
    processing_service = ProcessingService(db)
    results = processing_service.get_processing_results(video_id)
    log_success(logger, f"[get_processing_results_direct] Successfully retrieved results for video ID: {video_id}")
    return results