diff --git a/maloja/database/__init__.py b/maloja/database/__init__.py index cc8aeb9..24419d4 100644 --- a/maloja/database/__init__.py +++ b/maloja/database/__init__.py @@ -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 diff --git a/maloja/database/sqldb.py b/maloja/database/sqldb.py index e94246d..cd730cb 100644 --- a/maloja/database/sqldb.py +++ b/maloja/database/sqldb.py @@ -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=since, + DB['scrobbles'].c.timestamp.between(since,to) ) result = dbconn.execute(op).all() diff --git a/maloja/dev/profiler.py b/maloja/dev/profiler.py index 209ffb2..29bd231 100644 --- a/maloja/dev/profiler.py +++ b/maloja/dev/profiler.py @@ -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 diff --git a/maloja/web/jinja/partials/scrobbles.jinja b/maloja/web/jinja/partials/scrobbles.jinja index 32d839b..e54bd72 100644 --- a/maloja/web/jinja/partials/scrobbles.jinja +++ b/maloja/web/jinja/partials/scrobbles.jinja @@ -8,7 +8,6 @@ {% for s in scrobbles -%} - {%- if loop.index0 >= firstindex and loop.index0 < lastindex -%} {{ entityrow.row(s.track) }} @@ -41,6 +40,5 @@ {% endif %} - {%- endif -%} {% endfor %}
{{ malojatime.timestamp_desc(s["time"],short=shortTimeDesc) }}
diff --git a/maloja/web/jinja/scrobbles.jinja b/maloja/web/jinja/scrobbles.jinja index 0331bb9..a3cdbcb 100644 --- a/maloja/web/jinja/scrobbles.jinja +++ b/maloja/web/jinja/scrobbles.jinja @@ -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 @@

Scrobbles


{{ filterdesc.desc(filterkeys,limitkeys) }}
-

{{ scrobbles.__len__() }} Scrobbles

+

{{ totalscrobbles }} Scrobbles


{% with delimitkeys = {} %} {% include 'snippets/timeselection.jinja' %}