Updates. Got statistics calculated and stored.
Signed-off-by: Cliff Hill <xlorep@darkhelm.org>
This commit is contained in:
@@ -238,3 +238,58 @@ async def update_average_track_length(new_average: float) -> None:
|
||||
except Exception as e:
|
||||
print(f"Failed to update the average track length: {e}")
|
||||
await session.rollback()
|
||||
|
||||
|
||||
async def update_statistics(
|
||||
new_average_duration: float,
|
||||
new_podcast_length: float,
|
||||
new_max_playtime: float,
|
||||
new_active_holidays: int,
|
||||
new_max_tracks: int,
|
||||
new_max_regular: int,
|
||||
new_max_holiday: int,
|
||||
) -> None:
|
||||
"""Update the statistics model.
|
||||
|
||||
Args:
|
||||
new_average_duration (float): The new average duration of a track by geometric mean
|
||||
of all track durations as a float.
|
||||
new_podcast_length (float): The new total podcast length of all current episodes as a float.
|
||||
new_max_playtime (float): The new max playtime as a float.
|
||||
new_active_holidays (int): The new active holidays as an int.
|
||||
new_max_tracks (int): The new max tracks per day as an int.
|
||||
new_max_regular (int): The new max regular tracks as an int.
|
||||
new_max_holiday (int): The new max holiday tracks as an int.
|
||||
"""
|
||||
async with async_session_maker() as session:
|
||||
try:
|
||||
async with session.begin():
|
||||
# Assuming there's only one statistics record, or you might need to handle this differently
|
||||
stats = await session.get(
|
||||
models.Statistics, 1
|
||||
) # Adjust based on how you access the statistics record
|
||||
if stats:
|
||||
stats.average_track_length = new_average_duration
|
||||
stats.total_podcast_length = new_podcast_length
|
||||
stats.max_playtime = new_max_playtime
|
||||
stats.active_holiday_count = new_active_holidays
|
||||
stats.max_tracks_per_day = new_max_tracks
|
||||
stats.max_regular_tracks = new_max_regular
|
||||
stats.max_holiday_tracks = new_max_holiday
|
||||
await session.commit()
|
||||
else:
|
||||
# If no statistics entry exists, create one
|
||||
new_stats = models.Statistics(
|
||||
average_track_length=new_average_duration,
|
||||
total_podcast_length=new_podcast_length,
|
||||
max_playtime=new_max_playtime,
|
||||
active_holiday_count=new_active_holidays,
|
||||
max_tracks_per_day=new_max_tracks,
|
||||
max_regular_tracks=new_max_regular,
|
||||
max_holiday_tracks=new_max_holiday,
|
||||
)
|
||||
session.add(new_stats)
|
||||
await session.commit()
|
||||
except Exception as e:
|
||||
print(f"Failed to update the statistics model: {e}")
|
||||
await session.rollback()
|
||||
|
||||
Reference in New Issue
Block a user