-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
110 lines (83 loc) · 2.85 KB
/
main.py
File metadata and controls
110 lines (83 loc) · 2.85 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
import uvicorn
from fastapi import FastAPI, HTTPException
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
import config
from models import CodeRequest, Results
from measures import AnalysisHandler
from utils import get_logger
logger = get_logger(__name__)
app = FastAPI()
# Static Files
app.mount("/static", StaticFiles(directory=config.STATIC_DIR), name="static")
# Routes
## Pages
@app.get("/")
def code_tester():
"""
Endpoint for serving the index file.
Returns:
FileResponse: The index file response.
Raises:
HTTPException: If there is an error serving the index file.
"""
try:
return FileResponse(config.INDEX_FILE)
except Exception as e:
logger.error("Error while serving index file: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/endpoint")
def endpoint_tester():
"""
Endpoint for serving the endpoint file.
Returns:
FileResponse: The endpoint file response.
Raises:
HTTPException: If there is an error serving the endpoint file.
"""
try:
return FileResponse(config.ENDPOINT_FILE)
except Exception as e:
logger.error("Error while serving endpoint file: {e}")
raise HTTPException(status_code=500, detail=str(e))
## Helpful frontend APIs
@app.get("/languages", response_model=list[str])
def get_languages() -> list[str]:
"""
Retrieve a list of languages available for analysis.
Returns:
list[str]: A list of language names.
"""
return list(AnalysisHandler.languages.keys())
@app.get("/strategies", response_model=list[str])
def get_strategies(language: str) -> list[str]:
"""
Retrieve a list of strategies available in the PerformanceAnalyzer.
Returns:
list[str]: A list of strategy names.
"""
analyzer = AnalysisHandler.languages.get(language)
if analyzer is None:
raise HTTPException(status_code=404, detail=f"Language '{language}' not supported.")
return list(analyzer.strategies.keys())
## the magic
@app.post("/analyze", response_model=Results)
async def process_code(request: CodeRequest) -> Results:
"""
Process the given code request with performance measures and return the results.
Args:
request (CodeRequest): The code request object containing the code to be analyzed.
Returns:
Results: The results of the code analysis.
Raises:
HTTPException: If there is an error during the code analysis.
"""
try:
return AnalysisHandler.measure(request)
# TODO: Improve error handling
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except RuntimeError as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
uvicorn.run(app, host=config.HOST, port=config.PORT)