-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:Projecte-UrbanTree/UrbanTree
- Loading branch information
Showing
28 changed files
with
590 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
apscheduler==3.11.0 | ||
databases[mysql] | ||
fastapi[standard]==0.115.6 | ||
pydantic_core==2.27.1 | ||
pydantic-settings==2.6.1 | ||
pydantic==2.10.3 | ||
sentry-sdk[fastapi]==2.19.2 | ||
sqlmodel==0.0.22 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from sqlmodel import Session, SQLModel, create_engine | ||
|
||
from .config import settings | ||
|
||
engine = create_engine(str(settings.SQLALCHEMY_DATABASE_URI)) | ||
|
||
|
||
def create_db_and_tables(engine=engine): | ||
SQLModel.metadata.create_all(engine) | ||
|
||
|
||
def drop_db_and_tables(engine=engine): | ||
SQLModel.metadata.drop_all(engine) | ||
|
||
|
||
def get_session(): | ||
with Session(engine) as session: | ||
return session |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from datetime import datetime | ||
|
||
from sqlmodel import Field, Relationship, SQLModel | ||
|
||
|
||
class Sensor(SQLModel, table=True): | ||
__tablename__ = "sensors" | ||
|
||
id: int | None = Field(default=None, primary_key=True) | ||
|
||
contract_id: int = Field(index=True) | ||
zone_id: int = Field(index=True) | ||
point_id: int = Field(index=True) | ||
model: str | None = Field(default=None, index=True) | ||
is_active: bool | None = Field(default=None, index=True) | ||
|
||
histories: list["SensorHistory"] = Relationship(back_populates="sensor") | ||
|
||
|
||
class SensorHistoryBase(SQLModel): | ||
temperature: float | ||
humidity: float | ||
inclination: float | ||
created_at: datetime | ||
|
||
sensor_id: int = Field(foreign_key="sensors.id") | ||
|
||
|
||
class SensorHistory(SensorHistoryBase, table=True): | ||
__tablename__ = "sensor_history" | ||
|
||
id: int | None = Field(default=None, primary_key=True) | ||
|
||
sensor: Sensor = Relationship(back_populates="histories") | ||
|
||
|
||
class SensorHistoryCreate(SensorHistoryBase): | ||
pass |
Empty file.
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from datetime import datetime | ||
|
||
from apscheduler.schedulers.background import BackgroundScheduler | ||
from apscheduler.triggers.interval import IntervalTrigger | ||
|
||
from .sensor_service import check_sensor_task | ||
|
||
trigger8h = IntervalTrigger(hours=8) | ||
|
||
now = datetime.now() | ||
|
||
scheduler = BackgroundScheduler() | ||
scheduler.add_job(check_sensor_task, trigger8h, next_run_time=now) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,37 @@ | ||
from typing import List | ||
from sentry_sdk.crons import monitor | ||
from sqlmodel import select | ||
|
||
from sqlalchemy.orm import Session | ||
from src.models.sensor_model import Sensor, SensorHistory | ||
from src.schemas.sensor import ModelItem | ||
from src.database import get_session | ||
from src.models import SensorHistory | ||
from src.utils.file_loader import load_json_file | ||
|
||
|
||
def insert_data(sensor_data: List[ModelItem], session: Session): | ||
# Define a cron job to check the sensors | ||
@monitor(monitor_slug="check-sensors") | ||
def check_sensor_task(): | ||
try: | ||
for sensor in sensor_data: | ||
# Search existent sensor | ||
db_sensor = session.query(Sensor).filter_by(id=sensor.sensor_id).first() | ||
sensor_file_data = load_json_file("sensors.json") | ||
session = get_session() | ||
|
||
if db_sensor: | ||
db_history = SensorHistory( | ||
sensor_id=sensor.sensor_id, | ||
temperature=sensor.temperature, | ||
humidity=sensor.humidity, | ||
inclination=sensor.inclination, | ||
created_at=sensor.created_at, | ||
) | ||
session.add(db_history) | ||
if not sensor_file_data: | ||
raise ValueError("No data to insert") | ||
|
||
for sensor_history_entry in sensor_file_data: | ||
sensor_history = SensorHistory.model_validate(sensor_history_entry) | ||
statement = select(SensorHistory).where( | ||
SensorHistory.sensor_id == sensor_history.sensor_id, | ||
SensorHistory.created_at == sensor_history.created_at, | ||
) | ||
sensor_db = session.exec(statement).first() | ||
if sensor_db: | ||
sensor_db.temperature = sensor_history.temperature | ||
sensor_db.humidity = sensor_history.humidity | ||
sensor_db.inclination = sensor_history.inclination | ||
session.add(sensor_db) | ||
else: | ||
session.add(sensor_history) | ||
session.commit() | ||
|
||
session.commit() | ||
print("Datos insertados correctamente 👍🏼") | ||
except Exception as e: | ||
session.rollback() | ||
print(f"Error al insertar los datos: {e}") | ||
raise e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,9 @@ | ||
import json | ||
from typing import List | ||
|
||
from src.schemas.sensor import ModelItem | ||
|
||
|
||
def load_sensor_data(file_path: str) -> List[ModelItem]: | ||
def load_json_file(file_path: str) -> json: | ||
try: | ||
with open(file_path, "r") as f: | ||
raw_data = json.load(f) | ||
# raw_data: List[dict] | ||
|
||
return [ModelItem(**sensor) for sensor in raw_data] | ||
return json.load(f) | ||
except Exception as e: | ||
print("Error", e) | ||
return [] | ||
raise e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"test": "invalid_test_file_loader" | ||
"description": "This is a test file with invalid JSON format" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,15 @@ def test_settings_with_custom_settings(): | |
assert custom.SENTRY_RELEASE == "[email protected]" | ||
|
||
|
||
def test_v_prefixed_image_version(): | ||
custom = Settings( | ||
APP_ENV="production", | ||
IMAGE_VERSION="v1.0.0", | ||
) | ||
assert custom.IMAGE_VERSION == "1.0.0" | ||
assert custom.SENTRY_RELEASE == "[email protected]" | ||
|
||
|
||
def test_settings_missing_password(): | ||
with pytest.raises(ValidationError): | ||
Settings( | ||
|
Oops, something went wrong.