diff --git a/maloja/database/__init__.py b/maloja/database/__init__.py index 7a9b029..791592b 100644 --- a/maloja/database/__init__.py +++ b/maloja/database/__init__.py @@ -148,7 +148,10 @@ def get_charts_artists(**keys): @waitfordb def get_charts_tracks(**keys): (since,to) = keys.get('timerange').timestamps() - result = sqldb.count_scrobbles_by_track(since=since,to=to) + if 'artist' in keys: + result = sqldb.count_scrobbles_by_track_of_artist(since=since,to=to,artist=keys['artist']) + else: + result = sqldb.count_scrobbles_by_track(since=since,to=to) return result def get_pulse(**keys): @@ -495,7 +498,7 @@ def start_db(): dbstatus['healthy'] = True dbstatus['complete'] = True - firstscrobble = sqldb.get_scrobbles(max=1)[0] + firstscrobble = sqldb.get_scrobbles()[0] register_scrobbletime(firstscrobble['time']) diff --git a/maloja/database/sqldb.py b/maloja/database/sqldb.py index f308e1d..42ec730 100644 --- a/maloja/database/sqldb.py +++ b/maloja/database/sqldb.py @@ -302,7 +302,7 @@ def get_scrobbles_of_track(track,since=None,to=None): return result -def get_scrobbles(since=None,to=None,resolve_references=True,max=math.inf): +def get_scrobbles(since=None,to=None,resolve_references=True): if since is None: since=0 if to is None: to=now() @@ -394,7 +394,6 @@ def count_scrobbles_by_artist(since,to): def count_scrobbles_by_track(since,to): - print(since,to) with engine.begin() as conn: op = sql.select( @@ -414,6 +413,35 @@ def count_scrobbles_by_track(since,to): return result +def count_scrobbles_by_track_of_artist(since,to,artist): + + artist_id = get_artist_id(artist) + + jointable = sql.join( + DB['scrobbles'], + DB['trackartists'], + DB['scrobbles'].c.track_id == DB['trackartists'].c.track_id + ) + + with engine.begin() as conn: + op = sql.select( + sql.func.count(sql.func.distinct(DB['scrobbles'].c.timestamp)).label('count'), + DB['scrobbles'].c.track_id + ).select_from(jointable).filter( + DB['scrobbles'].c.timestamp<=to, + DB['scrobbles'].c.timestamp>=since, + DB['trackartists'].c.artist_id==artist_id + ).group_by(DB['scrobbles'].c.track_id).order_by(sql.desc('count')) + result = conn.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] + result = rank(result,key='scrobbles') + return result + + ### functions that get mappings for several entities -> rows