add: hub template
This commit is contained in:
24
hub/app/adapters/store_api_adapter.py
Normal file
24
hub/app/adapters/store_api_adapter.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import json
|
||||
import logging
|
||||
from typing import List
|
||||
|
||||
import pydantic_core
|
||||
import requests
|
||||
|
||||
from app.entities.processed_agent_data import ProcessedAgentData
|
||||
from app.interfaces.store_gateway import StoreGateway
|
||||
|
||||
|
||||
class StoreApiAdapter(StoreGateway):
|
||||
def __init__(self, api_base_url):
|
||||
self.api_base_url = api_base_url
|
||||
|
||||
def save_data(self, processed_agent_data_batch: List[ProcessedAgentData]):
|
||||
"""
|
||||
Save the processed road data to the Store API.
|
||||
Parameters:
|
||||
processed_agent_data_batch (dict): Processed road data to be saved.
|
||||
Returns:
|
||||
bool: True if the data is successfully saved, False otherwise.
|
||||
"""
|
||||
# Implement it
|
||||
33
hub/app/entities/agent_data.py
Normal file
33
hub/app/entities/agent_data.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from datetime import datetime
|
||||
from pydantic import BaseModel, field_validator
|
||||
|
||||
|
||||
class AccelerometerData(BaseModel):
|
||||
x: float
|
||||
y: float
|
||||
z: float
|
||||
|
||||
|
||||
class GpsData(BaseModel):
|
||||
latitude: float
|
||||
longitude: float
|
||||
|
||||
|
||||
class AgentData(BaseModel):
|
||||
user_id: int
|
||||
accelerometer: AccelerometerData
|
||||
gps: GpsData
|
||||
timestamp: datetime
|
||||
|
||||
@classmethod
|
||||
@field_validator('timestamp', mode='before')
|
||||
def parse_timestamp(cls, value):
|
||||
# Convert the timestamp to a datetime object
|
||||
if isinstance(value, datetime):
|
||||
return value
|
||||
try:
|
||||
return datetime.fromisoformat(value)
|
||||
except (TypeError, ValueError):
|
||||
raise ValueError(
|
||||
"Invalid timestamp format. Expected ISO 8601 format (YYYY-MM-DDTHH:MM:SSZ)."
|
||||
)
|
||||
7
hub/app/entities/processed_agent_data.py
Normal file
7
hub/app/entities/processed_agent_data.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from pydantic import BaseModel
|
||||
from app.entities.agent_data import AgentData
|
||||
|
||||
|
||||
class ProcessedAgentData(BaseModel):
|
||||
road_state: str
|
||||
agent_data: AgentData
|
||||
21
hub/app/interfaces/store_gateway.py
Normal file
21
hub/app/interfaces/store_gateway.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import List
|
||||
from app.entities.processed_agent_data import ProcessedAgentData
|
||||
|
||||
|
||||
class StoreGateway(ABC):
|
||||
"""
|
||||
Abstract class representing the Store Gateway interface.
|
||||
All store gateway adapters must implement these methods.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def save_data(self, processed_agent_data_batch: List[ProcessedAgentData]) -> bool:
|
||||
"""
|
||||
Method to save the processed agent data in the database.
|
||||
Parameters:
|
||||
processed_agent_data_batch (ProcessedAgentData): The processed agent data to be saved.
|
||||
Returns:
|
||||
bool: True if the data is successfully saved, False otherwise.
|
||||
"""
|
||||
pass
|
||||
Reference in New Issue
Block a user