From b806be6e025d9aa39f22913a896c2995688d4381 Mon Sep 17 00:00:00 2001 From: krateng Date: Sat, 16 Apr 2022 03:10:51 +0200 Subject: [PATCH] Cached stats now use IDs to survive renames --- maloja/database/__init__.py | 16 ++++++++-------- maloja/database/cached.py | 28 ++++++++++++++++------------ maloja/database/sqldb.py | 24 ++++++++++++++---------- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/maloja/database/__init__.py b/maloja/database/__init__.py index adf5be3..71fd883 100644 --- a/maloja/database/__init__.py +++ b/maloja/database/__init__.py @@ -302,11 +302,11 @@ def artist_info(dbconn=None,**keys): "position":position, "associated":others, "medals":{ - "gold": [year for year in cached.medals_artists if artist in cached.medals_artists[year]['gold']], - "silver": [year for year in cached.medals_artists if artist in cached.medals_artists[year]['silver']], - "bronze": [year for year in cached.medals_artists if artist in cached.medals_artists[year]['bronze']], + "gold": [year for year in cached.medals_artists if artist_id in cached.medals_artists[year]['gold']], + "silver": [year for year in cached.medals_artists if artist_id in cached.medals_artists[year]['silver']], + "bronze": [year for year in cached.medals_artists if artist_id in cached.medals_artists[year]['bronze']], }, - "topweeks":len([e for e in cached.weekly_topartists if e == artist]), + "topweeks":len([e for e in cached.weekly_topartists if e == artist_id]), "id":artist_id } except: @@ -345,12 +345,12 @@ def track_info(dbconn=None,**keys): "scrobbles":scrobbles, "position":position, "medals":{ - "gold": [year for year in cached.medals_tracks if track in cached.medals_tracks[year]['gold']], - "silver": [year for year in cached.medals_tracks if track in cached.medals_tracks[year]['silver']], - "bronze": [year for year in cached.medals_tracks if track in cached.medals_tracks[year]['bronze']], + "gold": [year for year in cached.medals_tracks if track_id in cached.medals_tracks[year]['gold']], + "silver": [year for year in cached.medals_tracks if track_id in cached.medals_tracks[year]['silver']], + "bronze": [year for year in cached.medals_tracks if track_id in cached.medals_tracks[year]['bronze']], }, "certification":cert, - "topweeks":len([e for e in cached.weekly_toptracks if e == track]), + "topweeks":len([e for e in cached.weekly_toptracks if e == track_id]), "id":track_id } diff --git a/maloja/database/cached.py b/maloja/database/cached.py index 7967665..43a719a 100644 --- a/maloja/database/cached.py +++ b/maloja/database/cached.py @@ -3,6 +3,7 @@ from doreah.regular import runyearly, rundaily from .. import database +from . import sqldb from .. import malojatime as mjt @@ -27,8 +28,9 @@ def update_medals(): for year in mjt.ranges(step="year"): if year == mjt.thisyear(): break - charts_artists = database.get_charts_artists(timerange=year) - charts_tracks = database.get_charts_tracks(timerange=year) + with sqldb.engine.begin() as conn: + charts_artists = sqldb.count_scrobbles_by_artist(since=year.first_stamp(),to=year.last_stamp(),resolve_ids=False,dbconn=conn) + charts_tracks = sqldb.count_scrobbles_by_track(since=year.first_stamp(),to=year.last_stamp(),resolve_ids=False,dbconn=conn) entry_artists = {'gold':[],'silver':[],'bronze':[]} entry_tracks = {'gold':[],'silver':[],'bronze':[]} @@ -36,18 +38,19 @@ def update_medals(): medals_tracks[year.desc()] = entry_tracks for entry in charts_artists: - if entry['rank'] == 1: entry_artists['gold'].append(entry['artist']) - elif entry['rank'] == 2: entry_artists['silver'].append(entry['artist']) - elif entry['rank'] == 3: entry_artists['bronze'].append(entry['artist']) + if entry['rank'] == 1: entry_artists['gold'].append(entry['artist_id']) + elif entry['rank'] == 2: entry_artists['silver'].append(entry['artist_id']) + elif entry['rank'] == 3: entry_artists['bronze'].append(entry['artist_id']) else: break for entry in charts_tracks: - if entry['rank'] == 1: entry_tracks['gold'].append(entry['track']) - elif entry['rank'] == 2: entry_tracks['silver'].append(entry['track']) - elif entry['rank'] == 3: entry_tracks['bronze'].append(entry['track']) + if entry['rank'] == 1: entry_tracks['gold'].append(entry['track_id']) + elif entry['rank'] == 2: entry_tracks['silver'].append(entry['track_id']) + elif entry['rank'] == 3: entry_tracks['bronze'].append(entry['track_id']) else: break + @rundaily def update_weekly(): @@ -58,12 +61,13 @@ def update_weekly(): for week in mjt.ranges(step="week"): if week == mjt.thisweek(): break - charts_artists = database.get_charts_artists(timerange=week) - charts_tracks = database.get_charts_tracks(timerange=week) + with sqldb.engine.begin() as conn: + charts_artists = sqldb.count_scrobbles_by_artist(since=week.first_stamp(),to=week.last_stamp(),resolve_ids=False,dbconn=conn) + charts_tracks = sqldb.count_scrobbles_by_track(since=week.first_stamp(),to=week.last_stamp(),resolve_ids=False,dbconn=conn) for entry in charts_artists: - if entry['rank'] == 1: weekly_topartists.append(entry['artist']) + if entry['rank'] == 1: weekly_topartists.append(entry['artist_id']) else: break for entry in charts_tracks: - if entry['rank'] == 1: weekly_toptracks.append(entry['track']) + if entry['rank'] == 1: weekly_toptracks.append(entry['track_id']) else: break diff --git a/maloja/database/sqldb.py b/maloja/database/sqldb.py index b560d8f..5423952 100644 --- a/maloja/database/sqldb.py +++ b/maloja/database/sqldb.py @@ -508,7 +508,7 @@ def get_tracks(dbconn=None): @cached_wrapper @connection_provider -def count_scrobbles_by_artist(since,to,dbconn=None): +def count_scrobbles_by_artist(since,to,resolve_ids=True,dbconn=None): jointable = sql.join( DB['scrobbles'], DB['trackartists'], @@ -536,16 +536,18 @@ def count_scrobbles_by_artist(since,to,dbconn=None): ).order_by(sql.desc('count')) result = dbconn.execute(op).all() - - counts = [row.count for row in result] - artists = get_artists_map([row.artist_id for row in result]) - result = [{'scrobbles':row.count,'artist':artists[row.artist_id]} for row in result] + if resolve_ids: + counts = [row.count for row in result] + artists = get_artists_map([row.artist_id for row in result]) + result = [{'scrobbles':row.count,'artist':artists[row.artist_id]} for row in result] + else: + result = [{'scrobbles':row.count,'artist_id':row.artist_id} for row in result] result = rank(result,key='scrobbles') return result @cached_wrapper @connection_provider -def count_scrobbles_by_track(since,to,dbconn=None): +def count_scrobbles_by_track(since,to,resolve_ids=True,dbconn=None): op = sql.select( @@ -557,10 +559,12 @@ def count_scrobbles_by_track(since,to,dbconn=None): ).group_by(DB['scrobbles'].c.track_id).order_by(sql.desc('count')) result = dbconn.execute(op).all() - - counts = [row.count for row in result] - tracks = get_tracks_map([row.track_id for row in result]) - result = [{'scrobbles':row.count,'track':tracks[row.track_id]} for row in result] + if resolve_ids: + counts = [row.count for row in result] + tracks = get_tracks_map([row.track_id for row in result]) + result = [{'scrobbles':row.count,'track':tracks[row.track_id]} for row in result] + else: + result = [{'scrobbles':row.count,'track_id':row.track_id} for row in result] result = rank(result,key='scrobbles') return result