Some performance tweaks

This commit is contained in:
krateng 2023-10-24 16:42:02 +02:00
parent 91e51a0167
commit f8e65cd611
5 changed files with 40 additions and 17 deletions

View File

@ -321,17 +321,30 @@ def associate_tracks_to_album(target_id,source_ids):
@waitfordb
def get_scrobbles(dbconn=None,**keys):
(since,to) = keys.get('timerange').timestamps()
reverse = keys.get('reverse',True) # comaptibility with old calls
if 'perpage' in keys:
limit = (keys.get('page',0)+1) * keys.get('perpage',100)
behead = keys.get('page',0) * keys.get('perpage',100)
else:
limit = None
behead = 0
associated = keys.get('associated',False)
if 'artist' in keys:
result = sqldb.get_scrobbles_of_artist(artist=keys['artist'],since=since,to=to,associated=associated,dbconn=dbconn)
result = sqldb.get_scrobbles_of_artist(artist=keys['artist'],since=since,to=to,associated=associated,limit=limit,reverse=reverse,dbconn=dbconn)
elif 'track' in keys:
result = sqldb.get_scrobbles_of_track(track=keys['track'],since=since,to=to,dbconn=dbconn)
result = sqldb.get_scrobbles_of_track(track=keys['track'],since=since,to=to,limit=limit,reverse=reverse,dbconn=dbconn)
elif 'album' in keys:
result = sqldb.get_scrobbles_of_album(album=keys['album'],since=since,to=to,dbconn=dbconn)
result = sqldb.get_scrobbles_of_album(album=keys['album'],since=since,to=to,limit=limit,reverse=reverse,dbconn=dbconn)
else:
result = sqldb.get_scrobbles(since=since,to=to,dbconn=dbconn)
result = sqldb.get_scrobbles(since=since,to=to,limit=limit,reverse=reverse,dbconn=dbconn)
#return result[keys['page']*keys['perpage']:(keys['page']+1)*keys['perpage']]
return list(reversed(result))
#print(result)
return list(result[behead:])
@waitfordb

View File

@ -898,20 +898,30 @@ def get_scrobbles_of_album(album,since=None,to=None,resolve_references=True,dbco
@cached_wrapper
@connection_provider
def get_scrobbles(since=None,to=None,resolve_references=True,dbconn=None):
def get_scrobbles(since=None,to=None,resolve_references=True,limit=None,reverse=False,dbconn=None):
if since is None: since=0
if to is None: to=now()
op = DB['scrobbles'].select().where(
DB['scrobbles'].c.timestamp<=to,
DB['scrobbles'].c.timestamp>=since,
).order_by(sql.asc('timestamp'))
DB['scrobbles'].c.timestamp.between(since,to)
)
if reverse:
op = op.order_by(sql.desc('timestamp'))
else:
op = op.order_by(sql.asc('timestamp'))
if limit:
op = op.limit(limit)
result = dbconn.execute(op).all()
if resolve_references:
result = scrobbles_db_to_dict(result,dbconn=dbconn)
#result = [scrobble_db_to_dict(row,resolve_references=resolve_references) for i,row in enumerate(result) if i<max]
return result
@ -924,8 +934,7 @@ def get_scrobbles_num(since=None,to=None,dbconn=None):
if to is None: to=now()
op = sql.select(sql.func.count()).select_from(DB['scrobbles']).where(
DB['scrobbles'].c.timestamp<=to,
DB['scrobbles'].c.timestamp>=since,
DB['scrobbles'].c.timestamp.between(since,to)
)
result = dbconn.execute(op).all()

View File

@ -34,10 +34,12 @@ def profile(func):
realfunc = realfunc.__innerfunc__
log(f"Executed {realfunc.__name__} ({args}, {kwargs}) in {seconds:.2f}s",module="debug_performance")
if FULL_PROFILE:
targetfilename = os.path.join(benchmarkfolder,f"{realfunc.__name__}.stats")
try:
pstats.Stats(profiler).dump_stats(os.path.join(benchmarkfolder,f"{realfunc.__name__}.stats"))
pstats.Stats(profiler).dump_stats(targetfilename)
log(f"Saved benchmark as {targetfilename}")
except Exception:
pass
log(f"Failed to save benchmark as {targetfilename}")
return result

View File

@ -8,7 +8,6 @@
<table class='list'>
{% for s in scrobbles -%}
{%- if loop.index0 >= firstindex and loop.index0 < lastindex -%}
<tr>
<td class='time'>{{ malojatime.timestamp_desc(s["time"],short=shortTimeDesc) }}</td>
{{ entityrow.row(s.track) }}
@ -41,6 +40,5 @@
</td>
{% endif %}
</tr>
{%- endif -%}
{% endfor %}
</table>

View File

@ -4,8 +4,9 @@
{% import 'snippets/filterdescription.jinja' as filterdesc %}
{% import 'snippets/pagination.jinja' as pagination %}
{% set totalscrobbles = dbc.get_scrobbles_num(filterkeys,limitkeys) %}
{% set scrobbles = dbc.get_scrobbles(filterkeys,limitkeys,amountkeys) %}
{% set pages = math.ceil(scrobbles.__len__() / amountkeys.perpage) %}
{% set pages = math.ceil(totalscrobbles / amountkeys.perpage) %}
{% if filterkeys.get('track') is not none %}
{% set img = images.get_track_image(filterkeys.track) %}
@ -29,7 +30,7 @@
<h1>Scrobbles</h1><br/>
{{ filterdesc.desc(filterkeys,limitkeys) }}
<br/>
<p class="stats">{{ scrobbles.__len__() }} Scrobbles</p>
<p class="stats">{{ totalscrobbles }} Scrobbles</p>
<br/>
{% with delimitkeys = {} %}
{% include 'snippets/timeselection.jinja' %}