Potential performance breakthrough?

This commit is contained in:
krateng 2023-10-28 01:36:53 +02:00
parent 936f4094eb
commit ad9cad9e94
4 changed files with 70 additions and 10 deletions

View File

@ -421,14 +421,12 @@ def get_charts_tracks(dbconn=None,resolve_ids=True,**keys):
return result return result
@waitfordb @waitfordb
def get_charts_albums(dbconn=None,resolve_ids=True,**keys): def get_charts_albums(dbconn=None,resolve_ids=True,only_own_albums=False,**keys):
(since,to) = keys.get('timerange').timestamps() (since,to) = keys.get('timerange').timestamps()
appearing = keys.get('appearing',False)
if 'artist' in keys: if 'artist' in keys:
if appearing: result = sqldb.count_scrobbles_by_album_combined(since=since,to=to,artist=keys['artist'],associated=keys.get('associated',False),resolve_ids=resolve_ids,dbconn=dbconn)
result = sqldb.count_scrobbles_of_artist_by_album(since=since,to=to,artist=keys['artist'],associated=keys.get('associated',False),resolve_ids=resolve_ids,dbconn=dbconn) result = [e for e in result if (not only_own_albums) or (keys['artist'] in (e['album']['artists'] or []))]
else:
result = sqldb.count_scrobbles_by_album_of_artist(since=since,to=to,artist=keys['artist'],associated=keys.get('associated',False),resolve_ids=resolve_ids,dbconn=dbconn)
else: else:
result = sqldb.count_scrobbles_by_album(since=since,to=to,resolve_ids=resolve_ids,dbconn=dbconn) result = sqldb.count_scrobbles_by_album(since=since,to=to,resolve_ids=resolve_ids,dbconn=dbconn)
return result return result

View File

@ -1111,6 +1111,69 @@ def count_scrobbles_by_album(since,to,resolve_ids=True,dbconn=None):
result = rank(result,key='scrobbles') result = rank(result,key='scrobbles')
return result return result
# get ALL albums the artist is in any way related to and rank them by TBD
@cached_wrapper
@connection_provider
def count_scrobbles_by_album_combined(since,to,artist,associated=False,resolve_ids=True,dbconn=None):
if associated:
artist_ids = get_associated_artists(artist,resolve_ids=False,dbconn=dbconn) + [get_artist_id(artist,dbconn=dbconn)]
else:
artist_ids = [get_artist_id(artist,dbconn=dbconn)]
# get all tracks that either have a relevant trackartist
# or are on an album with a relevant albumartist
op1 = sql.select(DB['tracks'].c.id).select_from(
sql.join(
sql.join(
DB['tracks'],
DB['trackartists'],
DB['tracks'].c.id == DB['trackartists'].c.track_id
),
DB['albumartists'],
DB['tracks'].c.album_id == DB['albumartists'].c.album_id,
isouter=True
)
).where(
DB['tracks'].c.album_id.is_not(None), # tracks without albums don't matter
sql.or_(
DB['trackartists'].c.artist_id.in_(artist_ids),
DB['albumartists'].c.artist_id.in_(artist_ids)
)
)
relevant_tracks = dbconn.execute(op1).all()
relevant_track_ids = set(row.id for row in relevant_tracks)
#for row in relevant_tracks:
# print(get_track(row.id))
op2 = sql.select(
sql.func.count(sql.func.distinct(DB['scrobbles'].c.timestamp)).label('count'),
DB['tracks'].c.album_id
).select_from(
sql.join(
DB['scrobbles'],
DB['tracks'],
DB['scrobbles'].c.track_id == DB['tracks'].c.id
)
).where(
DB['scrobbles'].c.timestamp.between(since,to),
DB['scrobbles'].c.track_id.in_(relevant_track_ids)
).group_by(DB['tracks'].c.album_id).order_by(sql.desc('count'))
result = dbconn.execute(op2).all()
if resolve_ids:
albums = get_albums_map([row.album_id for row in result],dbconn=dbconn)
result = [{'scrobbles':row.count,'album':albums[row.album_id],'album_id':row.album_id} for row in result]
else:
result = [{'scrobbles':row.count,'album_id':row.album_id} for row in result]
result = rank(result,key='scrobbles')
#from pprint import pprint
#pprint(result)
return result
@cached_wrapper @cached_wrapper
@connection_provider @connection_provider
# this ranks the albums of that artist, not albums the artist appears on - even scrobbles # this ranks the albums of that artist, not albums the artist appears on - even scrobbles

View File

@ -4,7 +4,7 @@
<div id="showcase_container"> <div id="showcase_container">
{% for entry in dbc.get_charts_albums(filterkeys,limitkeys) %} {% for entry in dbc.get_charts_albums(filterkeys,limitkeys,{'only_own_albums':True}) %}
{%- set cert = None -%} {%- set cert = None -%}
{%- if entry.scrobbles >= settings.scrobbles_gold_album -%}{% set cert = 'gold' %}{%- endif -%} {%- if entry.scrobbles >= settings.scrobbles_gold_album -%}{% set cert = 'gold' %}{%- endif -%}
@ -26,7 +26,7 @@
</table> </table>
{% endfor %} {% endfor %}
{% for entry in dbc.get_charts_albums(filterkeys,limitkeys,{'appearing':True}) %} {% for entry in dbc.get_charts_albums(filterkeys,limitkeys,{'only_own_albums':False}) %}
{% if artist not in (entry.album.artists or []) %} {% if artist not in (entry.album.artists or []) %}

View File

@ -57,8 +57,7 @@
<!-- SUBCERTS --> <!-- SUBCERTS -->
{% set albumcharts = dbc.get_charts_albums({'artist':artist,'timerange':malojatime.alltime(),'resolve_ids':True}) %} {% set albumcharts = dbc.get_charts_albums({'artist':artist,'timerange':malojatime.alltime(),'resolve_ids':True,'only_own_albums':True}) %}
{# TODO: find better solution, we just resolve ids here because we have that in the cache anyway #}
{% for e in albumcharts -%} {% for e in albumcharts -%}
{%- if e.scrobbles >= settings.scrobbles_gold_album -%}{% set cert = 'gold' %}{%- endif -%} {%- if e.scrobbles >= settings.scrobbles_gold_album -%}{% set cert = 'gold' %}{%- endif -%}
{%- if e.scrobbles >= settings.scrobbles_platinum_album -%}{% set cert = 'platinum' %}{%- endif -%} {%- if e.scrobbles >= settings.scrobbles_platinum_album -%}{% set cert = 'platinum' %}{%- endif -%}