Compare commits

...

8 Commits

Author SHA1 Message Date
ІМ-24 Владислав Коваленко
92c91c2594 refactor: use try block for error handling and session rollback
All checks were successful
Component testing / Hub testing (push) Successful in 20s
Component testing / Store testing (push) Successful in 35s
Component testing / Integration smoke testing (push) Successful in 2m38s
2026-03-26 13:38:04 +00:00
ІМ-24 Владислав Коваленко
2e623c3a93 fix: remove visibility update from PUT endpoint 2026-03-26 13:21:37 +00:00
ІМ-24 Владислав Коваленко
edd2360d88 feat: update visibility field with websocket 2026-03-26 13:16:13 +00:00
ІМ-24 Владислав Коваленко
8e9dcb50c1 feat: add schema for receiving data from websocker 2026-03-26 10:57:10 +00:00
ІМ-24 Владислав Коваленко
e2c3cda458 fix: remove visible from schemas, so only store knows about it 2026-03-25 17:15:28 +00:00
ІМ-24 Владислав Коваленко
780855c93f fix: add visible columnt to .sql init script 2026-03-25 16:50:19 +00:00
ІМ-24 Владислав Коваленко
86d7b1aaf8 feat: add visibility field to database table 2026-03-25 14:12:09 +00:00
ІМ-24 Владислав Коваленко
d627836e47 feat: add visibility field 2026-03-25 14:00:47 +00:00
3 changed files with 29 additions and 4 deletions

View File

@@ -7,5 +7,6 @@ CREATE TABLE processed_agent_data (
z FLOAT, z FLOAT,
latitude FLOAT, latitude FLOAT,
longitude FLOAT, longitude FLOAT,
timestamp TIMESTAMP timestamp TIMESTAMP,
visible BOOLEAN
); );

View File

@@ -8,12 +8,13 @@ from sqlalchemy import (
Integer, Integer,
String, String,
Float, Float,
Boolean,
DateTime, DateTime,
) )
from sqlalchemy.sql import select from sqlalchemy.sql import select
from database import metadata, SessionLocal from database import metadata, SessionLocal
from schemas import ProcessedAgentData, ProcessedAgentDataInDB from schemas import ProcessedAgentData, ProcessedAgentDataInDB, WebSocketData
# FastAPI app setup # FastAPI app setup
app = FastAPI() app = FastAPI()
@@ -30,6 +31,7 @@ processed_agent_data = Table(
Column("latitude", Float), Column("latitude", Float),
Column("longitude", Float), Column("longitude", Float),
Column("timestamp", DateTime), Column("timestamp", DateTime),
Column("visible", Boolean),
) )
# WebSocket subscriptions # WebSocket subscriptions
@@ -57,7 +59,24 @@ async def websocket_endpoint(websocket: WebSocket, user_id: int):
# receive forever # receive forever
while True: while True:
await websocket.receive_text() data = await websocket.receive_text()
try:
if (data):
ws_data = WebSocketData.model_validate(json.loads(data))
session = SessionLocal()
update_query = (
processed_agent_data.update()
.where(processed_agent_data.c.id == ws_data.id)
.values(visible=False)
).returning(processed_agent_data)
res = session.execute(update_query).fetchone()
if (not res):
session.rollback()
raise Exception("Error while websocket PUT")
session.commit()
finally:
session.close()
except WebSocketDisconnect: except WebSocketDisconnect:
subscriptions.remove(websocket) subscriptions.remove(websocket)
@@ -81,6 +100,7 @@ def ProcessedAgentData_to_td(data: List[ProcessedAgentData]):
"latitude": item.agent_data.gps.latitude, "latitude": item.agent_data.gps.latitude,
"longitude": item.agent_data.gps.longitude, "longitude": item.agent_data.gps.longitude,
"timestamp": item.agent_data.timestamp, "timestamp": item.agent_data.timestamp,
"visible": True,
} }
for item in data for item in data
] ]

View File

@@ -49,3 +49,7 @@ class AgentData(BaseModel):
class ProcessedAgentData(BaseModel): class ProcessedAgentData(BaseModel):
road_state: str road_state: str
agent_data: AgentData agent_data: AgentData
class WebSocketData(BaseModel):
id: int