refactors

This commit is contained in:
counterweight 2025-12-25 18:27:59 +01:00
parent f46d2ae8b3
commit 168b67acee
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
12 changed files with 471 additions and 126 deletions

View file

@ -1,5 +1,7 @@
"""Price repository for database queries."""
from datetime import datetime
from sqlalchemy import desc, select
from sqlalchemy.ext.asyncio import AsyncSession
@ -25,3 +27,32 @@ class PriceRepository:
)
result = await self.db.execute(query)
return result.scalar_one_or_none()
async def get_recent(self, limit: int = 20) -> list[PriceHistory]:
"""Get the most recent price history records."""
query = select(PriceHistory).order_by(desc(PriceHistory.timestamp)).limit(limit)
result = await self.db.execute(query)
return list(result.scalars().all())
async def get_by_timestamp(
self,
timestamp: str | datetime,
source: str = SOURCE_BITFINEX,
pair: str = PAIR_BTC_EUR,
) -> PriceHistory | None:
"""Get a price record by timestamp."""
# Convert string timestamp to datetime if needed
timestamp_dt: datetime
if isinstance(timestamp, str):
timestamp_dt = datetime.fromisoformat(timestamp.replace("Z", "+00:00"))
else:
timestamp_dt = timestamp
result = await self.db.execute(
select(PriceHistory).where(
PriceHistory.source == source,
PriceHistory.pair == pair,
PriceHistory.timestamp == timestamp_dt,
)
)
return result.scalar_one_or_none()