FastAPI app that pulls interval usage from Sungrow iSolarCloud (OAuth2), caches AER Consumer Data Right electricity plan data, and compares actual metered usage against every available plan to estimate real annual cost (TOU, tiered feed-in, and flat tariffs). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
28 lines
680 B
Python
28 lines
680 B
Python
from collections.abc import Iterator
|
|
from contextlib import contextmanager
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import Session, sessionmaker
|
|
|
|
from app.config import settings
|
|
from app.db.models import Base
|
|
|
|
engine = create_engine(settings.db_url, connect_args={"check_same_thread": False})
|
|
SessionLocal = sessionmaker(bind=engine, expire_on_commit=False)
|
|
|
|
|
|
def init_db() -> None:
|
|
Base.metadata.create_all(engine)
|
|
|
|
|
|
@contextmanager
|
|
def get_session() -> Iterator[Session]:
|
|
session = SessionLocal()
|
|
try:
|
|
yield session
|
|
session.commit()
|
|
except Exception:
|
|
session.rollback()
|
|
raise
|
|
finally:
|
|
session.close()
|