-
Notifications
You must be signed in to change notification settings - Fork 420
Expand file tree
/
Copy pathapi_routes.py
More file actions
986 lines (919 loc) · 33.7 KB
/
api_routes.py
File metadata and controls
986 lines (919 loc) · 33.7 KB
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
"""FastAPI API routes for file processing and conversion."""
# Standard library
import asyncio
import io
import logging
import os
import zipfile
from typing import Optional
# Local application
from api.auth.auth_utils import get_authenticated_user
from api.event_utils import track_event_if_configured
from api.status_updates import app_connection_manager, close_connection
# Third-party
# Azure Monitor OpenTelemetry integration is currently causing issues with OpenAI calls in process_batch_async, needs further investigation, commenting out for now
from azure.monitor.opentelemetry import configure_azure_monitor
from common.logger.app_logger import AppLogger
from common.services.batch_service import BatchService
from fastapi import (
APIRouter,
File,
Form,
HTTPException,
Request,
UploadFile,
WebSocket,
WebSocketDisconnect,
)
from fastapi.responses import Response
from opentelemetry import trace
from opentelemetry.trace import Status, StatusCode
from sql_agents.process_batch import process_batch_async
router = APIRouter()
logger = AppLogger("APIRoutes")
# Check if the Application Insights Instrumentation Key is set in the environment variables
instrumentation_key = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING")
if instrumentation_key:
# Configure Application Insights if the Instrumentation Key is found
configure_azure_monitor(
connection_string=instrumentation_key,
instrumentation_options={
"azure_sdk": {"enabled": False},
}
)
logging.info(
"Application Insights configured with the provided Instrumentation Key"
)
else:
# Log a warning if the Instrumentation Key is not found
logging.warning(
"No Application Insights Instrumentation Key found. Skipping configuration"
)
def record_exception_to_trace(e):
"""Record exception to the current OpenTelemetry trace span."""
span = trace.get_current_span()
if span is not None:
span.record_exception(e)
span.set_status(Status(StatusCode.ERROR, str(e)))
# start processing the batch
@router.post("/start-processing")
async def start_processing(request: Request):
"""
Start processing files for a given batch.
---
tags:
- File Processing
parameters:
- in: body
name: processing_config
schema:
type: object
properties:
batch_id:
type: string
format: uuid
translate_from:
type: string
translate_to:
type: string
responses:
200:
description: Processing initiated successfully
content:
application/json:
schema:
type: object
properties:
status:
type: string
message:
type: string
400:
description: Invalid processing request
500:
description: Internal server error
"""
try:
payload = await request.json()
batch_id = payload.get("batch_id")
translate_from = payload.get("translate_from")
translate_to = payload.get("translate_to")
track_event_if_configured(
"ProcessingStart",
{"batch_id": batch_id, "from": translate_from, "to": translate_to},
)
await process_batch_async(
batch_id=batch_id, convert_from=translate_from, convert_to=translate_to
)
await close_connection(batch_id)
return {
"batch_id": batch_id,
"status": "Processing completed",
"message": "Files processed",
}
except Exception as e:
record_exception_to_trace(e)
raise HTTPException(status_code=500, detail=str(e)) from e
@router.get(
"/download/{upload_id}",
summary="Download files as ZIP",
description="Download all files associated with an upload ID as a ZIP archive",
response_description="ZIP file containing all processed files",
)
async def download_files(batch_id: str):
"""
Download files as ZIP.
---
tags:
- File Download
consumes:
- application/json
parameters:
- in: path
name: upload_id
required: true
description: The ID of the upload to download files for
schema:
type: string
responses:
200:
description: ZIP file containing all processed files
schema:
type: string
format: binary
404:
description: Batch not found or error creating ZIP file
schema:
type: object
properties:
detail:
type: string
example: Batch not found
"""
# call batch_service get_batch_for_zip to get all files for batch_id
batch_service = BatchService()
await batch_service.initialize_database()
file_data = await batch_service.get_batch_for_zip(batch_id)
if not file_data:
track_event_if_configured("DownloadBatchNotFound", {"batch_id": batch_id})
raise HTTPException(status_code=404, detail="Batch not found")
# Create an in-memory bytes buffer for the zip file
zip_stream = io.BytesIO()
try:
# Create a new zip file in memory
with zipfile.ZipFile(
zip_stream, mode="w", compression=zipfile.ZIP_DEFLATED
) as zf:
for file_name, content in file_data:
# Make sure the file has content
if not content:
continue
# Ensure the file name ends with '.sql'
if not file_name.endswith(".sql"):
file_name += ".sql"
# Ensure the file name is safe for zip
file_name = file_name.replace("/", "_").replace("\\", "_")
# Write the file into the zip archive with its content
zf.writestr(file_name, content)
# Reset the stream's position to the beginning
zip_stream.seek(0)
zip_data = zip_stream.getvalue()
# Get the size of the zip file for the Content-Length header
zip_size = len(zip_data)
# Prepare headers for file download
headers = {
"Content-Disposition": "attachment; filename=tsql_relts.zip",
"Content-Length": str(zip_size),
}
# Return the zip file as a streaming response
track_event_if_configured(
"DownloadZipSuccess", {"batch_id": batch_id, "file_count": len(file_data)}
)
return Response(zip_data, media_type="application/zip", headers=headers)
except Exception as e:
record_exception_to_trace(e)
raise HTTPException(
status_code=404, detail=f"Error creating ZIP file: {str(e)}"
) from e
@router.websocket("/socket/{batch_id}")
async def batch_status_updates(
websocket: WebSocket, batch_id: str
): # , request: Request):
"""
Web-Socket endpoint for real-time batch status updates.
---
tags:
- Batch Status
parameters:
- in: path
name: batch_id
required: true
schema:
type: string
format: uuid
description: Unique identifier for the batch
responses:
101:
description: WebSocket connection established for batch updates
content:
application/json:
schema:
type: object
properties:
batch_id:
type: string
format: uuid
description: Unique identifier for the batch
file_id:
type: string
format: uuid
description: Unique identifier for the file
process_status:
type: string
description: Current processing status of the file
400:
description: Invalid batch_id format
401:
description: User authentication failed
404:
description: Batch not found
500:
description: Internal server error
"""
try:
batch_service = BatchService()
await batch_service.initialize_database()
# Validate batch_id format
if not batch_service.is_valid_uuid(batch_id):
track_event_if_configured("WebSocketInvalidBatchId", {"batch_id": batch_id})
await websocket.close(code=4002, reason="Invalid batch_id format")
return
# Accept WebSocket connection
await websocket.accept()
# Add to the connection manager for backend updates
app_connection_manager.add_connection(batch_id, websocket)
track_event_if_configured("WebSocketConnectionAccepted", {"batch_id": batch_id})
# Keep the connection open - FastAPI will close the connection if this returns
while True:
# no expectation that we will receive anything from the client but this keeps
# the connection open and does not take cpu cycle
try:
await websocket.receive_text()
except asyncio.TimeoutError:
# TimeoutError is ignored to keep the WebSocket connection open without receiving data
pass
except WebSocketDisconnect:
track_event_if_configured("WebSocketDisconnect", {"batch_id": batch_id})
logger.info(f"Client disconnected from batch {batch_id}")
await close_connection(batch_id)
except Exception as e:
record_exception_to_trace(e)
logger.error("Error in WebSocket connection", error=str(e))
await close_connection(batch_id)
@router.get("/batch-story/{batch_id}")
async def get_batch_status(request: Request, batch_id: str):
"""
Retrieve batch history and file statuses.
---
tags:
- Batch History
parameters:
- in: path
name: batch_id
required: true
schema:
type: string
format: uuid
description: Unique identifier for the batch
responses:
200:
description: Batch history retrieved successfully
content:
application/json:
schema:
type: object
properties:
batch:
type: object
properties:
batch_id:
type: string
format: uuid
description: Unique identifier for the batch
user_id:
type: string
description: ID of the user who owns the batch
files:
type: integer
description: Number of files in the batch
created_at:
type: string
format: date-time
description: Timestamp when the batch was created
updated_at:
type: string
format: date-time
description: Timestamp of last batch update
status:
type: string
description: Current processing status of the batch
files:
type: array
description: List of files associated with the batch
items:
type: object
properties:
file_id:
type: string
format: uuid
description: Unique identifier for the file
batch_id:
type: string
format: uuid
description: ID of the batch the file belongs to
original_name:
type: string
description: Original name of the uploaded file
blob_path:
type: string
description: Path where file is stored
translated_path:
type: string
description: Path of the translated file (if available)
status:
type: string
description: Processing status of the file
error_count:
type: integer
description: Number of errors encountered
created_at:
type: string
format: date-time
description: Timestamp when the file was uploaded
updated_at:
type: string
format: date-time
description: Timestamp of last file status update
400:
description: Invalid batch_id format
401:
description: User authentication failed
404:
description: Batch not found
500:
description: Internal server error
"""
try:
batch_service = BatchService()
await batch_service.initialize_database()
# Authenticate user
authenticated_user = get_authenticated_user(request)
user_id = authenticated_user.user_principal_id
if not user_id:
track_event_if_configured(
"UserIdNotFound", {"status_code": 400, "detail": "no user"}
)
raise HTTPException(status_code=401, detail="User not authenticated")
# Validate batch_id format
if not batch_service.is_valid_uuid(batch_id):
track_event_if_configured("InvalidBatchId", {"batch_id": batch_id})
raise HTTPException(status_code=400, detail="Invalid batch_id format")
# Fetch batch details
batch_data = await batch_service.get_batch(batch_id, user_id)
if not batch_data:
track_event_if_configured(
"BatchNotFound", {"batch_id": batch_id, "user_id": user_id}
)
raise HTTPException(status_code=404, detail="Batch not found")
track_event_if_configured(
"BatchStoryRetrieved", {"batch_id": batch_id, "user_id": user_id}
)
return batch_data
except HTTPException as e:
record_exception_to_trace(e)
raise e
except Exception as e:
logger.error("Error retrieving batch history", error=str(e))
record_exception_to_trace(e)
error_message = str(e)
if "403" in error_message:
raise HTTPException(status_code=403, detail="Incorrect user_id") from e
else:
raise HTTPException(status_code=500, detail="Internal server error") from e
@router.get("/batch-summary/{batch_id}")
async def get_batch_summary(request: Request, batch_id: str):
"""Retrieve batch summary for a given batch ID."""
try:
batch_service = BatchService()
await batch_service.initialize_database()
# Authenticate user
authenticated_user = get_authenticated_user(request)
user_id = authenticated_user.user_principal_id
if not user_id:
track_event_if_configured(
"UserIdNotFound", {"status_code": 400, "detail": "no user"}
)
raise HTTPException(status_code=401, detail="User not authenticated")
# Retrieve batch summary
batch_summary = await batch_service.get_batch_summary(batch_id, user_id)
if not batch_summary:
track_event_if_configured(
"BatchSummaryNotFound", {"batch_id": batch_id, "user_id": user_id}
)
raise HTTPException(status_code=404, detail="No batch summary found.")
track_event_if_configured(
"BatchSummaryRetrieved", {"batch_summary": batch_summary}
)
return batch_summary
except HTTPException as e:
logger.error("Error fetching batch summary", error=str(e))
record_exception_to_trace(e)
raise e
except Exception as e:
logger.error("Error fetching batch summary", error=str(e))
record_exception_to_trace(e)
raise HTTPException(status_code=404, detail="Batch not found") from e
@router.post("/upload")
async def upload_file(
request: Request, file: UploadFile = File(...), batch_id: str = Form(...)
):
"""
Upload file for conversion.
---
tags:
- File Conversion
consumes:
- multipart/form-data
parameters:
- in: formData
name: file
type: file
required: true
- in: formData
name: batch_id
type: string
format: uuid
required: true
description: The batch ID to associate the file with.
responses:
200:
description: File uploaded successfully
schema:
type: object
properties:
batch_info:
type: object
properties:
batch_id:
type: string
format: uuid
description: Unique identifier for the conversion batch
user_id:
type: string
description: ID of the authenticated user
created_at:
type: string
format: date-time
description: Timestamp when the batch was created
updated_at:
type: string
format: date-time
description: Timestamp of last update
status:
type: string
description: Overall status of the conversion batch
file:
type: object
properties:
file_id:
type: string
format: uuid
description: Unique identifier for the file
original_name:
type: string
description: Original filename
blob_path:
type: string
description: Path where file is stored
translated_path:
type: string
description: Path of the translated file (if available)
status:
type: string
description: Status of file processing
error_count:
type: integer
description: Number of errors encountered
created_at:
type: string
format: date-time
description: When file was uploaded
updated_at:
type: string
format: date-time
description: Last status update
400:
description: Invalid file type or missing authentication
401:
description: User not authenticated
500:
description: Internal server error during processing
"""
try:
batch_service = BatchService()
await batch_service.initialize_database()
# Authenticate user
authenticated_user = get_authenticated_user(request)
user_id = authenticated_user.user_principal_id
if not user_id:
track_event_if_configured(
"UserIdNotFound", {"status_code": 400, "detail": "no user"}
)
raise HTTPException(status_code=401, detail="User not authenticated")
# Validate batch_id format
logger.info(f"batch_id: {batch_id}")
if not batch_service.is_valid_uuid(batch_id):
track_event_if_configured("InvalidBatchId", {"batch_id": batch_id})
raise HTTPException(status_code=400, detail="Invalid batch_id format")
# Upload file via BatchService
upload_result = await batch_service.upload_file_to_batch(
batch_id, user_id, file
)
track_event_if_configured("FileUploaded", upload_result)
return upload_result
except HTTPException as e:
record_exception_to_trace(e)
raise e
except Exception as e:
record_exception_to_trace(e)
raise HTTPException(status_code=500, detail="Internal server error") from e
@router.get("/file/{file_id}")
async def get_file_details(request: Request, file_id: str):
"""
Retrieve file details and processing logs.
---
tags:
- File Management
parameters:
- in: path
name: file_id
required: true
schema:
type: string
format: uuid
description: Unique identifier for the file.
responses:
200:
description: File details retrieved successfully.
content:
application/json:
schema:
type: object
properties:
file:
type: object
properties:
file_id:
type: string
format: uuid
description: Unique identifier for the file.
batch_id:
type: string
format: uuid
description: ID of the batch the file belongs to.
original_name:
type: string
description: Original name of the uploaded file.
blob_path:
type: string
description: Path where file is stored.
translated_path:
type: string
description: Path of the translated file (if available).
status:
type: string
description: Processing status of the file.
error_count:
type: integer
description: Number of errors encountered.
created_at:
type: string
format: date-time
description: Timestamp when the file was uploaded.
updated_at:
type: string
format: date-time
description: Timestamp of last file status update.
logs:
type: array
description: List of logs associated with the file.
items:
type: object
properties:
file_id:
type: string
format: uuid
description: Unique identifier for the file.
status:
type: string
description: Status of the file at the time of log entry.
description:
type: string
description: Description of the log event.
log_type:
type: string
description: Type of log event (success or error).
timestamp:
type: string
format: date-time
description: Timestamp of the log entry.
400:
description: Invalid file_id format.
401:
description: User authentication failed.
404:
description: File not found.
500:
description: Internal server error.
"""
try:
batch_service = BatchService()
await batch_service.initialize_database()
# Authenticate user
authenticated_user = get_authenticated_user(request)
user_id = authenticated_user.user_principal_id
if not user_id:
track_event_if_configured(
"UserIdNotFound", {"endpoint": "get_file_details"}
)
raise HTTPException(status_code=401, detail="User not authenticated")
# Validate file_id format
if not batch_service.is_valid_uuid(file_id):
track_event_if_configured("InvalidFileId", {"file_id": file_id})
raise HTTPException(status_code=400, detail="Invalid file_id format")
# Fetch file details
file_data = await batch_service.get_file_report(file_id)
if not file_data:
track_event_if_configured("FileNotFound", {"file_id": file_id})
raise HTTPException(status_code=404, detail="File not found")
track_event_if_configured("FileDetailsRetrieved", {"file_data": file_data})
return file_data
except HTTPException as e:
record_exception_to_trace(e)
raise e
except Exception as e:
logger.error("Error retrieving file details", error=str(e))
record_exception_to_trace(e)
raise HTTPException(status_code=500, detail="Internal server error") from e
@router.delete("/delete-batch/{batch_id}")
async def delete_batch_details(request: Request, batch_id: str):
"""
Delete batch history using batch_id.
---
tags:
- Batch Delete
parameters:
- in: path
name: batch_id
required: true
schema:
type: string
format: uuid
description: Unique identifier for the batch
responses:
200:
description: Batch deleted successfully
400:
description: Invalid batch_id format
401:
description: User authentication failed
404:
description: Batch not found
500:
description: Internal server error
"""
try:
batch_service = BatchService()
await batch_service.initialize_database()
# Authenticate user
authenticated_user = get_authenticated_user(request)
user_id = authenticated_user.user_principal_id
if not user_id:
track_event_if_configured(
"UserIdNotFound", {"endpoint": "delete_batch_details"}
)
raise HTTPException(status_code=401, detail="User not authenticated")
# Validate file_id format
if not batch_service.is_valid_uuid(batch_id):
track_event_if_configured("InvalidBatchId", {"batch_id": batch_id})
raise HTTPException(
status_code=400, detail=f"Invalid batch_id format: {batch_id}"
)
await batch_service.delete_batch_and_files(batch_id, user_id)
track_event_if_configured(
"BatchDeleted", {"batch_id": batch_id, "user_id": user_id}
)
logger.info(f"Batch deleted successfully: {batch_id}")
return {"message": "Batch deleted successfully"}
except HTTPException as e:
record_exception_to_trace(e)
raise e
except Exception as e:
logger.error("Failed to delete batch from database", error=str(e))
record_exception_to_trace(e)
raise HTTPException(status_code=500, detail="Database connection error") from e
@router.delete("/delete-file/{file_id}")
async def delete_file_details(request: Request, file_id: str):
"""
Delete file history using batch_id.
---
tags:
- File Delete
parameters:
- in: path
name: file_id
required: true
schema:
type: string
format: uuid
description: Unique identifier for the batch
responses:
200:
description: File deleted successfully
400:
description: Invalid file_id format
401:
description: User authentication failed
404:
description: File not found
500:
description: Internal server error
"""
try:
batch_service = BatchService()
await batch_service.initialize_database()
# Authenticate user
authenticated_user = get_authenticated_user(request)
user_id = authenticated_user.user_principal_id
if not user_id:
track_event_if_configured(
"UserIdNotFound", {"endpoint": "delete_file_details"}
)
raise HTTPException(status_code=401, detail="User not authenticated")
# Validate file_id format
if not batch_service.is_valid_uuid(file_id):
track_event_if_configured("InvalidFileId", {"file_id": file_id})
raise HTTPException(
status_code=400, detail=f"Invalid file_id format: {file_id}"
)
# Delete file
file_delete = await batch_service.delete_file(file_id, user_id)
if file_delete is None:
track_event_if_configured("FileDeleteNotFound", {"file_id": file_id})
raise HTTPException(status_code=404, detail=f"File not found: {file_id}")
logger.info(f"File deleted successfully: {file_delete}")
track_event_if_configured("FileDeleted", {"file_id": file_id})
return {"message": "File deleted successfully"}
except HTTPException as e:
record_exception_to_trace(e)
raise e
except Exception as e:
logger.error("Failed to delete file from database", error=str(e))
record_exception_to_trace(e)
raise HTTPException(status_code=500, detail="Database connection error") from e
@router.delete("/delete_all")
async def delete_all_details(request: Request):
"""
Delete all the history of batches, files and logs.
---
tags:
- Delete All
responses:
200:
description: All user data deleted successfully
401:
description: User authentication failed
404:
description: Delete operation failed
500:
description: Internal server error
"""
try:
batch_service = BatchService()
await batch_service.initialize_database()
# Authenticate user
authenticated_user = get_authenticated_user(request)
user_id = authenticated_user.user_principal_id
if not user_id:
track_event_if_configured(
"UserIdNotFound", {"endpoint": "delete_all_details"}
)
raise HTTPException(status_code=401, detail="User not authenticated")
# Validate file_id format
if not batch_service.is_valid_uuid(user_id):
track_event_if_configured("InvalidUserId", {"user_id": user_id})
raise HTTPException(
status_code=400, detail=f"Invalid user_id format: {user_id}"
)
# Delete all the files from storage and cosmosDB
delete_all = await batch_service.delete_all_from_storage_cosmos(user_id)
if delete_all is None:
track_event_if_configured("DeleteAllNotFound", {"user_id": user_id})
logger.error("File/Batch not found")
raise HTTPException(status_code=404, detail="File/Batch not found")
logger.info(f"All user data deleted successfully: {user_id}")
track_event_if_configured("DeleteAllSuccess", {"user_id": user_id})
return {"message": "All user data deleted successfully"}
except HTTPException as e:
record_exception_to_trace(e)
raise e
except Exception as e:
logger.error("Failed to delete user data from database", error=str(e))
record_exception_to_trace(e)
raise HTTPException(status_code=500, detail="Database connection error") from e
@router.get("/batch-history")
async def list_batch_history(request: Request, offset: int = 0, limit: Optional[int] = None):
"""
Retrieve batch processing history for the authenticated user.
---
tags:
- Batch History
parameters:
- in: query
name: offset
required: false
schema:
type: integer
description: Pagination offset for batch history retrieval.
- in: query
name: limit
required: false
schema:
type: integer
description: Number of batch history records to fetch.
responses:
200:
description: Successfully retrieved batch history.
content:
application/json:
schema:
type: array
items:
type: object
properties:
batch_id:
type: string
format: uuid
description: Unique identifier for the batch.
user_id:
type: string
description: User ID associated with the batch.
created_at:
type: string
format: date-time
description: Timestamp when the batch was created.
updated_at:
type: string
format: date-time
description: Timestamp of the last update.
status:
type: string
description: Processing status of the batch.
400:
description: Invalid request parameters.
401:
description: Unauthorized request.
500:
description: Internal server error.
"""
try:
batch_service = BatchService()
await batch_service.initialize_database()
# Authenticate user
authenticated_user = get_authenticated_user(request)
user_id = authenticated_user.user_principal_id
if not user_id:
track_event_if_configured(
"UserIdNotFound", {"endpoint": "list_batch_history"}
)
raise HTTPException(status_code=401, detail="User not authenticated")
# Retrieve batch history
batch_history = await batch_service.get_batch_history(
user_id, limit=limit, offset=offset
)
if not batch_history:
track_event_if_configured("BatchHistoryEmpty", {"user_id": user_id})
return HTTPException(status_code=404, detail="No batch history found.")
track_event_if_configured(
"BatchHistoryRetrieved", {"user_id": user_id, "count": len(batch_history)}
)
return batch_history
except HTTPException as e:
record_exception_to_trace(e)
raise e
except Exception as e:
logger.error("Error fetching batch history", error=str(e))
record_exception_to_trace(e)
raise HTTPException(
status_code=500, detail="Error retrieving batch history"
) from e