Spaces:
Sleeping
Sleeping
LapStore
commited on
Commit
·
446a461
1
Parent(s):
b234310
modified routes
Browse files- routers/traffic_data.py +50 -0
routers/traffic_data.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException, Depends
|
| 2 |
+
import pymysql
|
| 3 |
+
from utils.database import get_db_connection, DatabaseConfig
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
|
| 6 |
+
router = APIRouter(
|
| 7 |
+
prefix="/traffic-data",
|
| 8 |
+
tags=["traffic-data"]
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
class TrafficDataRequest(BaseModel):
|
| 12 |
+
speed: float
|
| 13 |
+
var_speed: float
|
| 14 |
+
waiting_time: float
|
| 15 |
+
var_waiting: float
|
| 16 |
+
avg_throughput: float
|
| 17 |
+
avg_queue_length: float
|
| 18 |
+
avg_occupancy: float
|
| 19 |
+
reward: float
|
| 20 |
+
last_action: int
|
| 21 |
+
action: int
|
| 22 |
+
|
| 23 |
+
@router.post("/add")
|
| 24 |
+
def add_traffic_data(data: TrafficDataRequest):
|
| 25 |
+
connection = get_db_connection(DatabaseConfig.TRAFFIC_MANAGER)
|
| 26 |
+
try:
|
| 27 |
+
with connection.cursor() as cursor:
|
| 28 |
+
sql = """
|
| 29 |
+
INSERT INTO traffic_data (
|
| 30 |
+
speed, var_speed, waiting_time, var_waiting, avg_throughput, avg_queue_length, avg_occupancy, reward, last_action, action
|
| 31 |
+
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
| 32 |
+
"""
|
| 33 |
+
cursor.execute(sql, (
|
| 34 |
+
data.speed,
|
| 35 |
+
data.var_speed,
|
| 36 |
+
data.waiting_time,
|
| 37 |
+
data.var_waiting,
|
| 38 |
+
data.avg_throughput,
|
| 39 |
+
data.avg_queue_length,
|
| 40 |
+
data.avg_occupancy,
|
| 41 |
+
data.reward,
|
| 42 |
+
data.last_action,
|
| 43 |
+
data.action
|
| 44 |
+
))
|
| 45 |
+
connection.commit()
|
| 46 |
+
return {"message": "Traffic data added successfully"}
|
| 47 |
+
except pymysql.Error as e:
|
| 48 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 49 |
+
finally:
|
| 50 |
+
connection.close()
|