Skip to content

Commit

Permalink
Prefix table names
Browse files Browse the repository at this point in the history
  • Loading branch information
vladsavelyev committed Feb 23, 2024
1 parent 10739f0 commit 2ce0f17
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions app/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class VisitStats(SQLModel, table=True):
coming from each source.
"""

__tablename__ = "multiqc_api_visits_stats"

id: int | None = Field(default=None, primary_key=True)
start: datetime.datetime
end: datetime.datetime
Expand All @@ -35,11 +37,13 @@ class VisitStats(SQLModel, table=True):
count: int


class DownloadStatsDaily(SQLModel, table=True):
class DownloadStats(SQLModel, table=True):
"""
Daily download statistics.
"""

__tablename__ = "multiqc_api_downloads_stats"

date: datetime.datetime = Field(primary_key=True)
pip_new: Optional[int] = None
pip_total: Optional[int] = None
Expand Down Expand Up @@ -87,17 +91,17 @@ def get_download_stats(
start: datetime.datetime | None = None,
end: datetime.datetime | None = None,
limit: int | None = None,
) -> Sequence[DownloadStatsDaily]:
) -> Sequence[DownloadStats]:
"""Return list of daily download statistics from the DB."""
with Session(engine) as session:
statement = select(DownloadStatsDaily)
statement = select(DownloadStats)
if start:
statement.where(DownloadStatsDaily.date >= start) # type: ignore
statement.where(DownloadStats.date >= start) # type: ignore
if end:
statement.where(DownloadStatsDaily.date <= end) # type: ignore
statement.where(DownloadStats.date <= end) # type: ignore
if limit:
statement.limit(limit)
statement.order_by(DownloadStatsDaily.date.desc()) # type: ignore
statement.order_by(DownloadStats.date.desc()) # type: ignore
return session.exec(statement).all()


Expand All @@ -116,14 +120,12 @@ def insert_download_stats(df: pd.DataFrame) -> pd.DataFrame:
with Session(engine) as session:
for index, row in df.iterrows():
row = row.where(pd.notna(row), None)
existing_entry = session.exec(
select(DownloadStatsDaily).where(DownloadStatsDaily.date == row["date"])
).first()
existing_entry = session.exec(select(DownloadStats).where(DownloadStats.date == row["date"])).first()
if existing_entry:
for key, value in row.items():
setattr(existing_entry, key, value)
else:
new_entry = DownloadStatsDaily(**row)
new_entry = DownloadStats(**row)
session.add(new_entry)
session.commit()
return df

0 comments on commit 2ce0f17

Please sign in to comment.