Big refactoring
This commit is contained in:
@@ -9,10 +9,11 @@ import logging
|
||||
from dataclasses import dataclass
|
||||
from datetime import date, timedelta
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.exceptions import NotFoundError, ProviderError, RateLimitError
|
||||
from app.models.ohlcv import OHLCVRecord
|
||||
from app.models.settings import IngestionProgress
|
||||
from app.models.ticker import Ticker
|
||||
from app.providers.protocol import MarketDataProvider
|
||||
@@ -50,6 +51,13 @@ async def _get_progress(db: AsyncSession, ticker_id: int) -> IngestionProgress |
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
async def _get_ohlcv_bar_count(db: AsyncSession, ticker_id: int) -> int:
|
||||
result = await db.execute(
|
||||
select(func.count()).select_from(OHLCVRecord).where(OHLCVRecord.ticker_id == ticker_id)
|
||||
)
|
||||
return int(result.scalar() or 0)
|
||||
|
||||
|
||||
async def _update_progress(
|
||||
db: AsyncSession, ticker_id: int, last_date: date
|
||||
) -> None:
|
||||
@@ -84,10 +92,17 @@ async def fetch_and_ingest(
|
||||
if end_date is None:
|
||||
end_date = date.today()
|
||||
|
||||
# Resolve start_date: use progress resume or default to 1 year ago
|
||||
# Resolve start_date: use progress resume or default to 1 year ago.
|
||||
# If we have too little history, force a one-year backfill even if
|
||||
# ingestion progress exists (upsert makes this safe and idempotent).
|
||||
if start_date is None:
|
||||
progress = await _get_progress(db, ticker.id)
|
||||
if progress is not None:
|
||||
bar_count = await _get_ohlcv_bar_count(db, ticker.id)
|
||||
minimum_backfill_bars = 200
|
||||
|
||||
if bar_count < minimum_backfill_bars:
|
||||
start_date = end_date - timedelta(days=365)
|
||||
elif progress is not None:
|
||||
start_date = progress.last_ingested_date + timedelta(days=1)
|
||||
else:
|
||||
start_date = end_date - timedelta(days=365)
|
||||
|
||||
Reference in New Issue
Block a user