Skip to content

Commit

Permalink
fix: stream results on stats endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dreulavelle committed Oct 9, 2024
1 parent a8a6aa9 commit ff14f85
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions src/controllers/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,22 @@ async def trakt_oauth_callback(code: str, request: Request):
async def get_stats(_: Request):
payload = {}
with db.Session() as session:
# Ensure the connection is open for the entire duration of the session
with session.connection().execution_options(stream_results=True) as conn:
movies_symlinks = conn.execute(select(func.count(Movie._id)).where(Movie.symlinked == True)).scalar_one()
episodes_symlinks = conn.execute(select(func.count(Episode._id)).where(Episode.symlinked == True)).scalar_one()
total_symlinks = movies_symlinks + episodes_symlinks

movies_symlinks = session.execute(select(func.count(Movie._id)).where(Movie.symlinked == True)).scalar_one()
episodes_symlinks = session.execute(select(func.count(Episode._id)).where(Episode.symlinked == True)).scalar_one()
total_symlinks = movies_symlinks + episodes_symlinks

total_movies = session.execute(select(func.count(Movie._id))).scalar_one()
total_shows = session.execute(select(func.count(Show._id))).scalar_one()
total_seasons = session.execute(select(func.count(Season._id))).scalar_one()
total_episodes = session.execute(select(func.count(Episode._id))).scalar_one()
total_items = session.execute(select(func.count(MediaItem._id))).scalar_one()
total_movies = conn.execute(select(func.count(Movie._id))).scalar_one()
total_shows = conn.execute(select(func.count(Show._id))).scalar_one()
total_seasons = conn.execute(select(func.count(Season._id))).scalar_one()
total_episodes = conn.execute(select(func.count(Episode._id))).scalar_one()
total_items = conn.execute(select(func.count(MediaItem._id))).scalar_one()

# Use a server-side cursor for batch processing
incomplete_retries = {}
batch_size = 1000
# Use a server-side cursor for batch processing
incomplete_retries = {}
batch_size = 1000

with session.connection().execution_options(stream_results=True) as conn:
result = conn.execute(
select(MediaItem._id, MediaItem.scraped_times)
.where(MediaItem.last_state != States.Completed)
Expand All @@ -131,21 +131,21 @@ async def get_stats(_: Request):
for media_item_id, scraped_times in batch:
incomplete_retries[media_item_id] = scraped_times

states = {}
for state in States:
states[state] = session.execute(select(func.count(MediaItem._id)).where(MediaItem.last_state == state)).scalar_one()

payload["total_items"] = total_items
payload["total_movies"] = total_movies
payload["total_shows"] = total_shows
payload["total_seasons"] = total_seasons
payload["total_episodes"] = total_episodes
payload["total_symlinks"] = total_symlinks
payload["incomplete_items"] = len(incomplete_retries)
payload["incomplete_retries"] = incomplete_retries
payload["states"] = states

return {"success": True, "data": payload}
states = {}
for state in States:
states[state] = conn.execute(select(func.count(MediaItem._id)).where(MediaItem.last_state == state)).scalar_one()

payload["total_items"] = total_items
payload["total_movies"] = total_movies
payload["total_shows"] = total_shows
payload["total_seasons"] = total_seasons
payload["total_episodes"] = total_episodes
payload["total_symlinks"] = total_symlinks
payload["incomplete_items"] = len(incomplete_retries)
payload["incomplete_retries"] = incomplete_retries
payload["states"] = states

return {"success": True, "data": payload}

@router.get("/logs", operation_id="logs")
async def get_logs():
Expand Down

0 comments on commit ff14f85

Please sign in to comment.