Skip to content

Commit

Permalink
Add health endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
vladsavelyev committed Mar 26, 2024
1 parent 56b34aa commit 55ff7ec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
7 changes: 4 additions & 3 deletions app/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ def get_visit_stats(
statement = select(VisitStats)
if start:
# Ignore type because Visit.called_at can be None for default value
statement.where(VisitStats.start >= start) # type: ignore
statement = statement.where(VisitStats.start >= start) # type: ignore
if end:
statement.where(VisitStats.end <= end) # type: ignore
statement = statement.where(VisitStats.end <= end) # type: ignore
if limit:
statement.limit(limit)
statement = statement.limit(limit)
statement.order_by(VisitStats.start.desc()) # type: ignore
return session.exec(statement).all()

Expand Down Expand Up @@ -129,3 +129,4 @@ def insert_download_stats(df: pd.DataFrame) -> pd.DataFrame:
session.add(new_entry)
session.commit()
return df

16 changes: 15 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
from logzio.handler import LogzioHandler

from typing import List, Dict, Optional

Expand Down Expand Up @@ -126,6 +125,21 @@ async def version(
return models.VersionResponse(latest_release=app.latest_release)


@app.get("/health")
async def health():
"""
Health check endpoint. Checks if the visits table contains records
in the past 10 minutes.
"""
try:
visits = db.get_visit_stats(start=datetime.datetime.now() - datetime.timedelta(minutes=10))
except Exception as e:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))
if not visits:
raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="No recent visits found")
return PlainTextResponse(content=str(len(visits)))


def _log_visit(
timestamp: str,
version_multiqc: str = "",
Expand Down

0 comments on commit 55ff7ec

Please sign in to comment.