From da7fc7460bddd2aa8e1e6b4ad40a394ba9c11940 Mon Sep 17 00:00:00 2001 From: Cliff Hill Date: Wed, 24 Apr 2024 14:34:11 -0400 Subject: [PATCH] Fixing some small things. Signed-off-by: Cliff Hill --- backend/src/playlist/models.py | 30 +++++++++++++++++++++--------- backend/src/playlist/sql.py | 14 ++++++++++---- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/backend/src/playlist/models.py b/backend/src/playlist/models.py index 7d0e885..5c03883 100644 --- a/backend/src/playlist/models.py +++ b/backend/src/playlist/models.py @@ -14,7 +14,6 @@ from sqlalchemy import String from sqlalchemy import Table from sqlalchemy import UniqueConstraint from sqlalchemy import func -from sqlalchemy import text from sqlalchemy.orm import Mapped from sqlalchemy.orm import mapped_column from sqlalchemy.orm import registry @@ -152,12 +151,12 @@ class Track: date_added (DateTime): Date the track was added to the database. rating (float): Rating of the track on a 1-5 scale. genre_id (int): ForeignKey linking to the genre. + category_id (int): Foreign key linking to the category. + server_id (str): Identifier for the server where the track is hosted, indexed. genre (Genre): The genre for this track. playlist_tracks (list[PlaylistTrack]): List of playlist tracks for this track. - category_id (int): Foreign key linking to the category. category (Category): The category for this track. holidays (list[Holiday]): The list of holidays this track is assigned to. - server_id (str): Identifier for the server where the track is hosted, indexed. """ __tablename__ = "tracks" @@ -299,6 +298,7 @@ class Episode: name (Mapped[str]): The title of the episode. filename (Mapped[str]): The filename of the episode's media file. podcast_id (Mapped[int]): Foreign key linking to the associated podcast. + server_id (str): Identifier for the server where the episode is hosted, indexed. podcast (Mapped[Podcast]): The podcast this episode belongs to. """ @@ -308,6 +308,10 @@ class Episode: name: Mapped[str] = mapped_column(String(255), nullable=False) filename: Mapped[str] = mapped_column(String(255), nullable=False) podcast_id: Mapped[int] = mapped_column(ForeignKey("podcasts.id")) + server_id: Mapped[str] = mapped_column( + String(255), nullable=False, index=True, unique=True + ) + podcast: Mapped[Podcast] = relationship("Podcast", back_populates="episodes") @@ -337,8 +341,10 @@ class Statistics: for holidays. max_holiday_general_base (Mapped[int]): The maximum number of general tracks in one sublist for holidays. - track_update_start_timestamp (Mapped[DateTime]): The timestamp when track updates begin. - track_update_end_timestamp (Mapped[DateTime]): The timestamp when track updates end. + start_update (Mapped[DateTime]): The timestamp when track updates begin. + end_update (Mapped[DateTime]): The timestamp when track updates end. + start_playlist_gen (Mapped[DateTime]): The timestamp when the playlist generation begins. + end_playlist_gen (Mapped[DateTime]): The timestamp when the playlist generation ends. """ __tablename__ = "statistics" @@ -360,9 +366,15 @@ class Statistics: max_regular_general_base: Mapped[int] = mapped_column(Integer) max_holiday_favorite_base: Mapped[int] = mapped_column(Integer) max_holiday_general_base: Mapped[int] = mapped_column(Integer) - track_update_start_timestamp: Mapped[DateTime] = mapped_column( - DateTime, server_default=text("CURRENT_TIMESTAMP") + start_update: Mapped[DateTime | None] = mapped_column( + DateTime, nullable=True, server_default=None ) - track_update_end_timestamp: Mapped[DateTime] = mapped_column( - DateTime, server_default=text("CURRENT_TIMESTAMP") + end_update: Mapped[DateTime | None] = mapped_column( + DateTime, nullable=True, server_default=None + ) + start_playlist_gen: Mapped[DateTime | None] = mapped_column( + DateTime, nullable=True, server_default=None + ) + end_playlist_gen: Mapped[DateTime | None] = mapped_column( + DateTime, nullable=True, server_default=None ) diff --git a/backend/src/playlist/sql.py b/backend/src/playlist/sql.py index 1b00eb1..840d83c 100644 --- a/backend/src/playlist/sql.py +++ b/backend/src/playlist/sql.py @@ -106,12 +106,17 @@ async def cleanup_unused_genres(session: AsyncSession) -> None: await session.rollback() -async def update_statistics_timestamps(session: AsyncSession, operation: str) -> None: +async def update_statistics_timestamps( + session: AsyncSession, operation: str +) -> datetime: """Update the start or end timestamps in the statistics model based on the specified operation. Args: session (AsyncSession): The database session. operation (str): A string to specify whether to update 'start' or 'end' timestamp. + + Returns: + datetime: The datetime of the last update. """ try: async with session.begin(): @@ -119,11 +124,10 @@ async def update_statistics_timestamps(session: AsyncSession, operation: str) -> stats_obj = stats.scalars().first() if operation == "start": - stats_obj.track_update_start_timestamp = datetime.now() - stats_obj.track_update_end_timestamp = None + stats_obj.start_update = datetime.now() log_message = "Statistics start timestamp updated." elif operation == "end": - stats_obj.track_update_end_timestamp = datetime.now() + stats_obj.end_update = datetime.now() log_message = "Statistics end timestamp updated." else: raise ValueError( @@ -133,6 +137,8 @@ async def update_statistics_timestamps(session: AsyncSession, operation: str) -> await session.commit() await logger.info(log_message) + return stats_obj.end_update + except SQLAlchemyError as e: await logger.error(f"Database error during statistics timestamp update: {e}") await session.rollback()