From c8e6a54a678dce43e70c5e2e01161d3b51a41ac7 Mon Sep 17 00:00:00 2001 From: alim4r <7687869+alim4r@users.noreply.github.com> Date: Sat, 4 Feb 2023 23:08:36 +0100 Subject: [PATCH] Add first implementation of the rediscover feature --- maloja/database/__init__.py | 6 +++ maloja/database/sqldb.py | 28 ++++++++++ .../jinja/partials/rediscover_scrobbles.jinja | 27 ++++++++++ maloja/web/jinja/rediscover.jinja | 54 +++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 maloja/web/jinja/partials/rediscover_scrobbles.jinja create mode 100644 maloja/web/jinja/rediscover.jinja diff --git a/maloja/database/__init__.py b/maloja/database/__init__.py index 473cb7b..e40beb5 100644 --- a/maloja/database/__init__.py +++ b/maloja/database/__init__.py @@ -336,6 +336,12 @@ def get_top_tracks(dbconn=None,**keys): return results +@waitfordb +def get_rediscover(dbconn=None,**keys): + (since,to) = keys.get('timerange').timestamps() + result = sqldb.get_scrobbles_rediscover(since=since,to=to,dbconn=dbconn) + return result + @waitfordb def artist_info(dbconn=None,**keys): diff --git a/maloja/database/sqldb.py b/maloja/database/sqldb.py index 957243b..11a60b3 100644 --- a/maloja/database/sqldb.py +++ b/maloja/database/sqldb.py @@ -579,6 +579,34 @@ def get_scrobbles_num(since=None,to=None,dbconn=None): return result[0][0] + +@cached_wrapper +@connection_provider +def get_scrobbles_rediscover(since,to,resolve_ids=True,dbconn=None): + op = sql.select( + sql.func.count(DB['scrobbles'].c.track_id).label('count'), + DB['scrobbles'].c.track_id, + sql.func.max(DB['scrobbles'].c.timestamp).label('timestamp') + ).select_from(DB['scrobbles']).where( + #DB['scrobbles'].c.timestamp<=to, + #DB['scrobbles'].c.timestamp>=since + ).group_by(DB['scrobbles'].c.track_id).having( + DB['scrobbles'].c.timestamp<=to + ).having( + sql.text('count >= 3') + ).order_by(sql.desc('timestamp'), sql.desc('count')) + result = dbconn.execute(op).all() + + if resolve_ids: + counts = [row.count for row in result] + tracks = get_tracks_map([row.track_id for row in result],dbconn=dbconn) + result = [{'scrobbles':row.count,'track':tracks[row.track_id],'time':row.timestamp} for row in result] + else: + result = [{'scrobbles':row.count,'track_id':row.track_id,'time':row.timestamp} for row in result] + result = rank(result,key='scrobbles') + return result + + @cached_wrapper @connection_provider def get_artists_of_track(track_id,resolve_references=True,dbconn=None): diff --git a/maloja/web/jinja/partials/rediscover_scrobbles.jinja b/maloja/web/jinja/partials/rediscover_scrobbles.jinja new file mode 100644 index 0000000..d677432 --- /dev/null +++ b/maloja/web/jinja/partials/rediscover_scrobbles.jinja @@ -0,0 +1,27 @@ +{% import 'snippets/links.jinja' as links %} +{% import 'snippets/entityrow.jinja' as entityrow %} + +{% if scrobbles is undefined %} + {% set scrobbles = dbc.get_rediscover(filterkeys,limitkeys) %} +{% endif %} + +{% set firstindex = amountkeys.page * amountkeys.perpage %} +{% set lastindex = firstindex + amountkeys.perpage %} + +{% set maxbar = scrobbles[0]['scrobbles'] if scrobbles != [] else 0 %} +
{{ malojatime.timestamp_desc(e["time"],short=shortTimeDesc) }} | + +{{ links.link_scrobbles([{'track':e.track,'timerange':limitkeys.timerange}],amount=e['scrobbles']) }} | +{{ links.link_scrobbles([{'track':e.track,'timerange':limitkeys.timerange}],percent=e['scrobbles']*100/maxbar) }} | + + + {{ entityrow.row(e['track']) }} +
+ + | +
+ Rediscover+ {% if filterkeys.get('artist') is not none %}by {{ links.link(filterkeys.get('artist')) }}{% endif %} + {{ limitkeys.timerange.desc(prefix=True) }} ++ {{ scrobbles.__len__() }} Scrobbles ++ {% with delimitkeys = {} %} + {% include 'snippets/timeselection.jinja' %} + {% endwith %} + + |
+