feat: add price history GET and POST endpoints
This commit is contained in:
parent
b077c93351
commit
e3b047f782
1 changed files with 72 additions and 1 deletions
|
|
@ -10,16 +10,25 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from auth import require_permission
|
from auth import require_permission
|
||||||
from database import get_db
|
from database import get_db
|
||||||
from models import CounterRecord, Permission, RandomNumberOutcome, SumRecord, User
|
from models import (
|
||||||
|
CounterRecord,
|
||||||
|
Permission,
|
||||||
|
PriceHistory,
|
||||||
|
RandomNumberOutcome,
|
||||||
|
SumRecord,
|
||||||
|
User,
|
||||||
|
)
|
||||||
from pagination import (
|
from pagination import (
|
||||||
calculate_offset,
|
calculate_offset,
|
||||||
calculate_total_pages,
|
calculate_total_pages,
|
||||||
create_paginated_response,
|
create_paginated_response,
|
||||||
)
|
)
|
||||||
|
from price_fetcher import fetch_btc_eur_price
|
||||||
from schemas import (
|
from schemas import (
|
||||||
CounterRecordResponse,
|
CounterRecordResponse,
|
||||||
PaginatedCounterRecords,
|
PaginatedCounterRecords,
|
||||||
PaginatedSumRecords,
|
PaginatedSumRecords,
|
||||||
|
PriceHistoryResponse,
|
||||||
RandomNumberOutcomeResponse,
|
RandomNumberOutcomeResponse,
|
||||||
SumRecordResponse,
|
SumRecordResponse,
|
||||||
)
|
)
|
||||||
|
|
@ -140,3 +149,65 @@ async def get_random_job_outcomes(
|
||||||
)
|
)
|
||||||
for outcome, email in rows
|
for outcome, email in rows
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Price History Endpoints
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
PRICE_HISTORY_LIMIT = 20
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/price-history", response_model=list[PriceHistoryResponse])
|
||||||
|
async def get_price_history(
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
_current_user: User = Depends(require_permission(Permission.VIEW_AUDIT)),
|
||||||
|
) -> list[PriceHistoryResponse]:
|
||||||
|
"""Get the 20 most recent price history records."""
|
||||||
|
query = (
|
||||||
|
select(PriceHistory)
|
||||||
|
.order_by(desc(PriceHistory.timestamp))
|
||||||
|
.limit(PRICE_HISTORY_LIMIT)
|
||||||
|
)
|
||||||
|
result = await db.execute(query)
|
||||||
|
records = result.scalars().all()
|
||||||
|
|
||||||
|
return [
|
||||||
|
PriceHistoryResponse(
|
||||||
|
id=record.id,
|
||||||
|
source=record.source,
|
||||||
|
pair=record.pair,
|
||||||
|
price=record.price,
|
||||||
|
timestamp=record.timestamp,
|
||||||
|
created_at=record.created_at,
|
||||||
|
)
|
||||||
|
for record in records
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/price-history/fetch", response_model=PriceHistoryResponse)
|
||||||
|
async def fetch_price_now(
|
||||||
|
db: AsyncSession = Depends(get_db),
|
||||||
|
_current_user: User = Depends(require_permission(Permission.VIEW_AUDIT)),
|
||||||
|
) -> PriceHistoryResponse:
|
||||||
|
"""Manually trigger a price fetch from Bitfinex."""
|
||||||
|
price, timestamp = await fetch_btc_eur_price()
|
||||||
|
|
||||||
|
record = PriceHistory(
|
||||||
|
source="bitfinex",
|
||||||
|
pair="BTC/EUR",
|
||||||
|
price=price,
|
||||||
|
timestamp=timestamp,
|
||||||
|
)
|
||||||
|
db.add(record)
|
||||||
|
await db.commit()
|
||||||
|
await db.refresh(record)
|
||||||
|
|
||||||
|
return PriceHistoryResponse(
|
||||||
|
id=record.id,
|
||||||
|
source=record.source,
|
||||||
|
pair=record.pair,
|
||||||
|
price=record.price,
|
||||||
|
timestamp=record.timestamp,
|
||||||
|
created_at=record.created_at,
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue