Skip to content

Commit 1506052

Browse files
pyline issue resolved
1 parent cdc8bd3 commit 1506052

2 files changed

Lines changed: 73 additions & 35 deletions

File tree

src/backend/api/api_routes.py

Lines changed: 68 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@
88
import zipfile
99
from typing import Optional
1010

11+
# Local application
12+
from api.auth.auth_utils import get_authenticated_user
13+
from api.event_utils import track_event_if_configured
14+
from api.status_updates import app_connection_manager, close_connection
15+
1116
# Third-party
1217
from azure.monitor.opentelemetry import configure_azure_monitor
13-
18+
from common.logger.app_logger import AppLogger
19+
from common.services.batch_service import BatchService
1420
from fastapi import (
1521
APIRouter,
1622
File,
@@ -22,16 +28,8 @@
2228
WebSocketDisconnect,
2329
)
2430
from fastapi.responses import Response
25-
2631
from opentelemetry import trace
2732
from opentelemetry.trace import Status, StatusCode
28-
29-
# Local application
30-
from api.auth.auth_utils import get_authenticated_user
31-
from api.event_utils import track_event_if_configured
32-
from api.status_updates import app_connection_manager, close_connection
33-
from common.logger.app_logger import AppLogger
34-
from common.services.batch_service import BatchService
3533
from sql_agents.process_batch import process_batch_async
3634

3735
router = APIRouter()
@@ -42,10 +40,14 @@
4240
if instrumentation_key:
4341
# Configure Application Insights if the Instrumentation Key is found
4442
configure_azure_monitor(connection_string=instrumentation_key)
45-
logging.info("Application Insights configured with the provided Instrumentation Key")
43+
logging.info(
44+
"Application Insights configured with the provided Instrumentation Key"
45+
)
4646
else:
4747
# Log a warning if the Instrumentation Key is not found
48-
logging.warning("No Application Insights Instrumentation Key found. Skipping configuration")
48+
logging.warning(
49+
"No Application Insights Instrumentation Key found. Skipping configuration"
50+
)
4951

5052
# Configure logging
5153
logging.basicConfig(level=logging.INFO)
@@ -109,11 +111,10 @@ async def start_processing(request: Request):
109111
translate_from = payload.get("translate_from")
110112
translate_to = payload.get("translate_to")
111113

112-
track_event_if_configured("ProcessingStart", {
113-
"batch_id": batch_id,
114-
"from": translate_from,
115-
"to": translate_to
116-
})
114+
track_event_if_configured(
115+
"ProcessingStart",
116+
{"batch_id": batch_id, "from": translate_from, "to": translate_to},
117+
)
117118

118119
await process_batch_async(
119120
batch_id=batch_id, convert_from=translate_from, convert_to=translate_to
@@ -213,7 +214,9 @@ async def download_files(batch_id: str):
213214
}
214215

215216
# Return the zip file as a streaming response
216-
track_event_if_configured("DownloadZipSuccess", {"batch_id": batch_id, "file_count": len(file_data)})
217+
track_event_if_configured(
218+
"DownloadZipSuccess", {"batch_id": batch_id, "file_count": len(file_data)}
219+
)
217220
return Response(zip_data, media_type="application/zip", headers=headers)
218221
except Exception as e:
219222
span = trace.get_current_span()
@@ -306,7 +309,9 @@ async def batch_status_updates(
306309
if span is not None:
307310
span.record_exception(e)
308311
span.set_status(Status(StatusCode.ERROR, str(e)))
309-
track_event_if_configured("WebSocketError", {"batch_id": batch_id, "error": str(e)})
312+
track_event_if_configured(
313+
"WebSocketError", {"batch_id": batch_id, "error": str(e)}
314+
)
310315
await close_connection(batch_id)
311316

312317

@@ -411,7 +416,9 @@ async def get_batch_status(request: Request, batch_id: str):
411416
authenticated_user = get_authenticated_user(request)
412417
user_id = authenticated_user.user_principal_id
413418
if not user_id:
414-
track_event_if_configured("UserIdNotFound", {"status_code": 400, "detail": "no user"})
419+
track_event_if_configured(
420+
"UserIdNotFound", {"status_code": 400, "detail": "no user"}
421+
)
415422
raise HTTPException(status_code=401, detail="User not authenticated")
416423

417424
# Validate batch_id format
@@ -422,10 +429,14 @@ async def get_batch_status(request: Request, batch_id: str):
422429
# Fetch batch details
423430
batch_data = await batch_service.get_batch(batch_id, user_id)
424431
if not batch_data:
425-
track_event_if_configured("BatchNotFound", {"batch_id": batch_id, "user_id": user_id})
432+
track_event_if_configured(
433+
"BatchNotFound", {"batch_id": batch_id, "user_id": user_id}
434+
)
426435
raise HTTPException(status_code=404, detail="Batch not found")
427436

428-
track_event_if_configured("BatchStoryRetrieved", {"batch_id": batch_id, "user_id": user_id})
437+
track_event_if_configured(
438+
"BatchStoryRetrieved", {"batch_id": batch_id, "user_id": user_id}
439+
)
429440
return batch_data
430441
except HTTPException as e:
431442
span = trace.get_current_span()
@@ -457,14 +468,20 @@ async def get_batch_summary(request: Request, batch_id: str):
457468
authenticated_user = get_authenticated_user(request)
458469
user_id = authenticated_user.user_principal_id
459470
if not user_id:
460-
track_event_if_configured("UserIdNotFound", {"status_code": 400, "detail": "no user"})
471+
track_event_if_configured(
472+
"UserIdNotFound", {"status_code": 400, "detail": "no user"}
473+
)
461474
raise HTTPException(status_code=401, detail="User not authenticated")
462475
# Retrieve batch summary
463476
batch_summary = await batch_service.get_batch_summary(batch_id, user_id)
464477
if not batch_summary:
465-
track_event_if_configured("BatchSummaryNotFound", {"batch_id": batch_id, "user_id": user_id})
478+
track_event_if_configured(
479+
"BatchSummaryNotFound", {"batch_id": batch_id, "user_id": user_id}
480+
)
466481
raise HTTPException(status_code=404, detail="No batch summary found.")
467-
track_event_if_configured("BatchSummaryRetrieved", {"batch_id": batch_id, "user_id": user_id})
482+
track_event_if_configured(
483+
"BatchSummaryRetrieved", {"batch_id": batch_id, "user_id": user_id}
484+
)
468485
return batch_summary
469486

470487
except HTTPException as e:
@@ -578,7 +595,9 @@ async def upload_file(
578595
user_id = authenticated_user.user_principal_id
579596

580597
if not user_id:
581-
track_event_if_configured("UserIdNotFound", {"status_code": 400, "detail": "no user"})
598+
track_event_if_configured(
599+
"UserIdNotFound", {"status_code": 400, "detail": "no user"}
600+
)
582601
raise HTTPException(status_code=401, detail="User not authenticated")
583602

584603
# Validate batch_id format
@@ -713,7 +732,9 @@ async def get_file_details(request: Request, file_id: str):
713732
authenticated_user = get_authenticated_user(request)
714733
user_id = authenticated_user.user_principal_id
715734
if not user_id:
716-
track_event_if_configured("UserIdNotFound", {"endpoint": "get_file_details"})
735+
track_event_if_configured(
736+
"UserIdNotFound", {"endpoint": "get_file_details"}
737+
)
717738
raise HTTPException(status_code=401, detail="User not authenticated")
718739

719740
# Validate file_id format
@@ -780,7 +801,9 @@ async def delete_batch_details(request: Request, batch_id: str):
780801
authenticated_user = get_authenticated_user(request)
781802
user_id = authenticated_user.user_principal_id
782803
if not user_id:
783-
track_event_if_configured("UserIdNotFound", {"endpoint": "delete_batch_details"})
804+
track_event_if_configured(
805+
"UserIdNotFound", {"endpoint": "delete_batch_details"}
806+
)
784807
raise HTTPException(status_code=401, detail="User not authenticated")
785808

786809
# Validate file_id format
@@ -791,7 +814,9 @@ async def delete_batch_details(request: Request, batch_id: str):
791814
)
792815

793816
await batch_service.delete_batch_and_files(batch_id, user_id)
794-
track_event_if_configured("BatchDeleted", {"batch_id": batch_id, "user_id": user_id})
817+
track_event_if_configured(
818+
"BatchDeleted", {"batch_id": batch_id, "user_id": user_id}
819+
)
795820
logger.info(f"Batch deleted successfully: {batch_id}")
796821
return {"message": "Batch deleted successfully"}
797822

@@ -845,7 +870,9 @@ async def delete_file_details(request: Request, file_id: str):
845870
authenticated_user = get_authenticated_user(request)
846871
user_id = authenticated_user.user_principal_id
847872
if not user_id:
848-
track_event_if_configured("UserIdNotFound", {"endpoint": "delete_file_details"})
873+
track_event_if_configured(
874+
"UserIdNotFound", {"endpoint": "delete_file_details"}
875+
)
849876
raise HTTPException(status_code=401, detail="User not authenticated")
850877

851878
# Validate file_id format
@@ -905,7 +932,9 @@ async def delete_all_details(request: Request):
905932
authenticated_user = get_authenticated_user(request)
906933
user_id = authenticated_user.user_principal_id
907934
if not user_id:
908-
track_event_if_configured("UserIdNotFound", {"endpoint": "delete_all_details"})
935+
track_event_if_configured(
936+
"UserIdNotFound", {"endpoint": "delete_all_details"}
937+
)
909938
raise HTTPException(status_code=401, detail="User not authenticated")
910939

911940
# Validate file_id format
@@ -942,7 +971,9 @@ async def delete_all_details(request: Request):
942971

943972

944973
@router.get("/batch-history")
945-
async def list_batch_history(request: Request, offset: int = 0, limit: Optional[int] = None):
974+
async def list_batch_history(
975+
request: Request, offset: int = 0, limit: Optional[int] = None
976+
):
946977
"""
947978
Retrieve batch processing history for the authenticated user.
948979
@@ -1005,7 +1036,9 @@ async def list_batch_history(request: Request, offset: int = 0, limit: Optional[
10051036
authenticated_user = get_authenticated_user(request)
10061037
user_id = authenticated_user.user_principal_id
10071038
if not user_id:
1008-
track_event_if_configured("UserIdNotFound", {"endpoint": "list_batch_history"})
1039+
track_event_if_configured(
1040+
"UserIdNotFound", {"endpoint": "list_batch_history"}
1041+
)
10091042
raise HTTPException(status_code=401, detail="User not authenticated")
10101043

10111044
# Retrieve batch history
@@ -1016,7 +1049,9 @@ async def list_batch_history(request: Request, offset: int = 0, limit: Optional[
10161049
track_event_if_configured("BatchHistoryEmpty", {"user_id": user_id})
10171050
return HTTPException(status_code=404, detail="No batch history found.")
10181051

1019-
track_event_if_configured("BatchHistoryRetrieved", {"user_id": user_id, "count": len(batch_history)})
1052+
track_event_if_configured(
1053+
"BatchHistoryRetrieved", {"user_id": user_id, "count": len(batch_history)}
1054+
)
10201055
return batch_history
10211056

10221057
except HTTPException as e:

src/backend/api/event_utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Standard library
2-
import os
32
import logging
3+
import os
4+
45
# Third-party
56
from azure.monitor.events.extension import track_event
67
from dotenv import load_dotenv
@@ -13,4 +14,6 @@ def track_event_if_configured(event_name: str, event_data: dict):
1314
if instrumentation_key:
1415
track_event(event_name, event_data)
1516
else:
16-
logging.warning(f"Skipping track_event for {event_name} as Application Insights is not configured")
17+
logging.warning(
18+
f"Skipping track_event for {event_name} as Application Insights is not configured"
19+
)

0 commit comments

Comments
 (0)