| 1 | from typing import List |
| 2 | |
| 3 | from fastapi import APIRouter |
| 4 | from fastapi import Depends |
| 5 | from fastapi import HTTPException |
| 6 | from fastapi import Security |
| 7 | from loguru import logger |
| 8 | |
| 9 | from app.auth.utils import AuthHandler |
| 10 | from app.connectors.cortex.schema.analyzers import AnalyzersResponse |
| 11 | from app.connectors.cortex.schema.analyzers import RunAnalyzerBody |
| 12 | from app.connectors.cortex.schema.analyzers import RunAnalyzerResponse |
| 13 | from app.connectors.cortex.services.analyzers import get_analyzers |
| 14 | from app.connectors.cortex.services.analyzers import run_analyzer |
| 15 | |
| 16 | # App specific imports |
| 17 | |
| 18 | |
| 19 | cortex_analyzer_router = APIRouter() |
| 20 | |
| 21 | |
| 22 | async def get_available_analyzers() -> List[str]: |
| 23 | """ |
| 24 | Retrieves a list of available analyzers. |
| 25 | |
| 26 | Returns: |
| 27 | A list of strings representing the available analyzers. |
| 28 | """ |
| 29 | analyzers = await get_analyzers() |
| 30 | return analyzers.analyzers |
| 31 | |
| 32 | |
| 33 | async def verify_analyzer_exists(run_analyzer_body: RunAnalyzerBody) -> RunAnalyzerBody: |
| 34 | """ |
| 35 | Verifies if the specified analyzer exists in the list of available analyzers. |
| 36 | |
| 37 | Args: |
| 38 | run_analyzer_body (RunAnalyzerBody): The body of the request containing the analyzer name. |
| 39 | |
| 40 | Returns: |
| 41 | RunAnalyzerBody: The same input body if the analyzer exists. |
| 42 | |
| 43 | Raises: |
| 44 | HTTPException: If the analyzer does not exist. |
| 45 | """ |
| 46 | available_analyzers = await get_available_analyzers() |
| 47 | if run_analyzer_body.analyzer_name not in available_analyzers: |
| 48 | raise HTTPException( |
| 49 | status_code=400, |
| 50 | detail=f"Analyzer {run_analyzer_body.analyzer_name} does not exist.", |
| 51 | ) |
| 52 | return run_analyzer_body |
| 53 | |
| 54 | |
| 55 | @cortex_analyzer_router.get( |
| 56 | "", |
| 57 | response_model=AnalyzersResponse, |
| 58 | description="Get all analyzers", |
| 59 | dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))], |
| 60 | ) |
| 61 | async def get_all_analyzers() -> AnalyzersResponse: |
| 62 | """ |
| 63 | Retrieve all analyzers. |
| 64 | |
| 65 | Returns: |
| 66 | AnalyzersResponse: The response containing the list of analyzers. |
| 67 | """ |
| 68 | logger.info("Fetching all analyzers") |
| 69 | return await get_analyzers() |
| 70 | |
| 71 | |
| 72 | @cortex_analyzer_router.post( |
| 73 | "/run", |
| 74 | response_model=RunAnalyzerResponse, |
| 75 | description="Run an analyzer", |
| 76 | dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))], |
| 77 | ) |
| 78 | async def run_analyzer_route( |
| 79 | run_analyzer_body: RunAnalyzerBody = Depends(verify_analyzer_exists), |
| 80 | ) -> RunAnalyzerResponse: |
| 81 | """ |
| 82 | Run an analyzer. |
| 83 | |
| 84 | Args: |
| 85 | run_analyzer_body (RunAnalyzerBody): The request body containing the analyzer name and data. |
| 86 | |
| 87 | Returns: |
| 88 | RunAnalyzerResponse: The response containing the result of running the analyzer. |
| 89 | """ |
| 90 | is_valid, data_type = RunAnalyzerBody.is_valid_datatype( |
| 91 | run_analyzer_body.analyzer_data, |
| 92 | ) |
| 93 | if not is_valid: |
| 94 | raise HTTPException(status_code=400, detail=f"Invalid data type: {data_type}") |
| 95 | |
| 96 | logger.info( |
| 97 | f"Running analyzer {run_analyzer_body.analyzer_name} with data {run_analyzer_body.analyzer_data} of type {data_type}", |
| 98 | ) |
| 99 | return await run_analyzer(run_analyzer_body, data_type) |