-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
114 lines (101 loc) · 4.5 KB
/
app.py
File metadata and controls
114 lines (101 loc) · 4.5 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
import httpx
import uvicorn
from pydantic import BaseModel
from fastapi import FastAPI, Request, HTTPException
from contextlib import asynccontextmanager
orchestrator_url = "http://localhost:8081/"
class LoadTestRequest(BaseModel):
test_type: str
test_server: str = "http://localhost:8080/ping"
test_message_delay: int
message_count_per_driver: int
class TestConfig(BaseModel):
TestType: str
@asynccontextmanager
async def lifespan(app: FastAPI):
app.requests_client = httpx.AsyncClient()
yield
await app.requests_client.aclose()
app = FastAPI(lifespan=lifespan)
@app.post("/trigger-load-test")
async def trigger_load_test(request_data: LoadTestRequest):
# Convert pydantic model to dict to send as JSON payload
payload = request_data.model_dump()
try:
async with httpx.AsyncClient() as client:
response = await client.post(f"{orchestrator_url}trigger-load-test", json=payload)
response.raise_for_status() # Raise an exception for 4xx or 5xx responses
return response.json()
except httpx.HTTPError as exc:
# Handle HTTP errors
raise HTTPException(status_code=exc.response.status_code, detail="HTTP Error occurred")
except httpx.RequestError:
# Handle other request errors
raise HTTPException(status_code=500, detail="Request Error occurred")
@app.get("/test-config")
async def get_test_config():
try:
async with httpx.AsyncClient() as client:
response = await client.get(f"{orchestrator_url}test-config")
response.raise_for_status() # Raise an exception for 4xx or 5xx responses
return response.json()
except httpx.HTTPError as exc:
# Handle HTTP errors
raise HTTPException(status_code=exc.response.status_code, detail="HTTP Error occurred")
except httpx.RequestError:
# Handle other request errors
raise HTTPException(status_code=500, detail="Request Error occurred")
@app.get("/metrics/{nodeid}")
async def retrieve_metrics_for_node(nodeid: str):
try:
async with httpx.AsyncClient() as client:
response = await client.get(f"{orchestrator_url}metrics/{nodeid}")
response.raise_for_status() # Raise an exception for 4xx or 5xx responses
return response.json()
except httpx.HTTPError as exc:
# Handle HTTP errors
raise HTTPException(status_code=exc.response.status_code, detail="HTTP Error occurred")
except httpx.RequestError:
# Handle other request errors
raise HTTPException(status_code=500, detail="Request Error occurred")
@app.get("/all-metrics")
async def retrieve_all_metrics():
try:
async with httpx.AsyncClient() as client:
response = await client.get(f"{orchestrator_url}all-metrics")
response.raise_for_status() # Raise an exception for 4xx or 5xx responses
return response.json()
except httpx.HTTPError as exc:
# Handle HTTP errors
raise HTTPException(status_code=exc.response.status_code, detail="HTTP Error occurred")
except httpx.RequestError:
# Handle other request errors
raise HTTPException(status_code=500, detail="Request Error occurred")
@app.get("/heartbeat/{nodeid}")
async def retrieve_heartbeat(nodeid: str):
try:
async with httpx.AsyncClient() as client:
response = await client.get(f"{orchestrator_url}heartbeat/{nodeid}")
response.raise_for_status() # Raise an exception for 4xx or 5xx responses
return response.json()
except httpx.HTTPError as exc:
# Handle HTTP errors
raise HTTPException(status_code=exc.response.status_code, detail="HTTP Error occurred")
except httpx.RequestError:
# Handle other request errors
raise HTTPException(status_code=500, detail="Request Error occurred")
@app.get("/all-nodes")
async def retrieve_all_nodes():
try:
async with httpx.AsyncClient() as client:
response = await client.get(f"{orchestrator_url}all-nodes")
response.raise_for_status() # Raise an exception for 4xx or 5xx responses
return response.json()
except httpx.HTTPError as exc:
# Handle HTTP errors
raise HTTPException(status_code=exc.response.status_code, detail="HTTP Error occurred")
except httpx.RequestError:
# Handle other request errors
raise HTTPException(status_code=500, detail="Request Error occurred")
if __name__ == "__main__":
uvicorn.run(app, port=8000)