1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00

Complete rework of time descriptors, Return of the King

This commit is contained in:
Krateng 2019-04-11 12:07:57 +02:00
parent c9c53aa592
commit d8a977238f
6 changed files with 83 additions and 80 deletions

View File

@ -211,7 +211,7 @@ def get_scrobbles_external():
return {"list":result}
def get_scrobbles(**keys):
r = db_query(**{k:keys[k] for k in keys if k in ["artist","artists","title","since","to","within","associated","track","max_"]})
r = db_query(**{k:keys[k] for k in keys if k in ["artist","artists","title","since","to","within","timerange","associated","track","max_"]})
#if keys.get("max_") is not None:
# return r[:int(keys.get("max_"))]
#else:
@ -241,7 +241,7 @@ def get_scrobbles_num_external():
return {"amount":result}
def get_scrobbles_num(**keys):
r = db_query(**{k:keys[k] for k in keys if k in ["artist","track","artists","title","since","to","within","associated"]})
r = db_query(**{k:keys[k] for k in keys if k in ["artist","track","artists","title","since","to","within","timerange","associated"]})
return len(r)
@ -344,7 +344,7 @@ def get_charts_artists_external():
return {"list":result}
def get_charts_artists(**keys):
return db_aggregate(by="ARTIST",**{k:keys[k] for k in keys if k in ["since","to","within"]})
return db_aggregate(by="ARTIST",**{k:keys[k] for k in keys if k in ["since","to","within","timerange"]})
@ -361,7 +361,7 @@ def get_charts_tracks_external():
return {"list":result}
def get_charts_tracks(**keys):
return db_aggregate(by="TRACK",**{k:keys[k] for k in keys if k in ["since","to","within","artist"]})
return db_aggregate(by="TRACK",**{k:keys[k] for k in keys if k in ["since","to","within","timerange","artist"]})
@ -405,7 +405,7 @@ def get_performance_external():
def get_performance(**keys):
rngs = ranges(**{k:keys[k] for k in keys if k in ["since","to","within","step","stepn","trail"]})
rngs = ranges(**{k:keys[k] for k in keys if k in ["since","to","within","timerange","step","stepn","trail"]})
results = []
for rng in rngs:
@ -446,15 +446,15 @@ def get_top_artists_external():
def get_top_artists(**keys):
rngs = ranges(**{k:keys[k] for k in keys if k in ["since","to","within","step","stepn","trail"]})
rngs = ranges(**{k:keys[k] for k in keys if k in ["since","to","within","timerange","step","stepn","trail"]})
results = []
for (a,b) in rngs:
for rng in rngs:
try:
res = db_aggregate(since=a,to=b,by="ARTIST")[0]
results.append({"since":a,"to":b,"artist":res["artist"],"counting":res["counting"],"scrobbles":res["scrobbles"]})
res = db_aggregate(timerange=rng,by="ARTIST")[0]
results.append({"range":rng,"artist":res["artist"],"counting":res["counting"],"scrobbles":res["scrobbles"]})
except:
results.append({"since":a,"to":b,"artist":None,"scrobbles":0})
results.append({"range":rng,"artist":None,"scrobbles":0})
return results
@ -480,15 +480,15 @@ def get_top_tracks_external():
def get_top_tracks(**keys):
rngs = ranges(**{k:keys[k] for k in keys if k in ["since","to","within","step","stepn","trail"]})
rngs = ranges(**{k:keys[k] for k in keys if k in ["since","to","within","timerange","step","stepn","trail"]})
results = []
for (a,b) in rngs:
try:
res = db_aggregate(since=a,to=b,by="TRACK")[0]
results.append({"since":a,"to":b,"track":res["track"],"scrobbles":res["scrobbles"]})
res = db_aggregate(timerange=rng,by="TRACK")[0]
results.append({"range":rng,"track":res["track"],"scrobbles":res["scrobbles"]})
except:
results.append({"since":a,"to":b,"track":None,"scrobbles":0})
results.append({"range":rng,"track":None,"scrobbles":0})
return results

View File

@ -22,7 +22,7 @@ import math
def module_scrobblelist(max_=None,pictures=False,shortTimeDesc=False,earlystop=False,**kwargs):
kwargs_filter = pickKeys(kwargs,"artist","track","associated")
kwargs_time = pickKeys(kwargs,"since","to","within")
kwargs_time = pickKeys(kwargs,"timerange","since","to","within")
# if earlystop, we don't care about the actual amount and only request as many from the db
@ -89,8 +89,8 @@ def module_pulse(max_=None,**kwargs):
range = t["range"]
html += "<tr>"
html += "<td>" + range.desc() + "</td>"
html += "<td class='amount'>" + scrobblesLink({"since":range.fromstr(),"to":range.tostr()},amount=t["scrobbles"],**kwargs_filter) + "</td>"
html += "<td class='bar'>" + scrobblesLink({"since":range.fromstr(),"to":range.tostr()},percent=t["scrobbles"]*100/maxbar,**kwargs_filter) + "</td>"
html += "<td class='amount'>" + scrobblesLink(range.urikeys(),amount=t["scrobbles"],**kwargs_filter) + "</td>"
html += "<td class='bar'>" + scrobblesLink(range.urikeys(),percent=t["scrobbles"]*100/maxbar,**kwargs_filter) + "</td>"
html += "</tr>"
html += "</table>"
@ -102,7 +102,7 @@ def module_pulse(max_=None,**kwargs):
def module_performance(max_=None,**kwargs):
kwargs_filter = pickKeys(kwargs,"artist","track")
kwargs_time = pickKeys(kwargs,"since","to","within","step","stepn","trail")
kwargs_time = pickKeys(kwargs,"since","to","within","timerange","step","stepn","trail")
ranges = database.get_performance(**kwargs_time,**kwargs_filter)
@ -123,10 +123,10 @@ def module_performance(max_=None,**kwargs):
for t in ranges:
range = t["range"]
html += "<tr>"
html += "<td>" + range_desc(t["from"],t["to"],short=True) + "</td>"
html += "<td>" + range.desc() + "</td>"
html += "<td class='rank'>" + ("#" + str(t["rank"]) if t["rank"] is not None else "No scrobbles") + "</td>"
prct = (minrank+1-t["rank"])*100/minrank if t["rank"] is not None else 0
html += "<td class='chart'>" + rankLink({"since":range.fromstr(),"to":range.tostr()},percent=prct,**kwargs_filter,medal=t["rank"]) + "</td>"
html += "<td class='chart'>" + rankLink(range.urikeys(),percent=prct,**kwargs_filter,medal=t["rank"]) + "</td>"
html += "</tr>"
html += "</table>"
@ -138,14 +138,13 @@ def module_performance(max_=None,**kwargs):
def module_trackcharts(max_=None,**kwargs):
kwargs_filter = pickKeys(kwargs,"artist","associated")
kwargs_time = pickKeys(kwargs,"since","to","within")
kwargs_time = pickKeys(kwargs,"timerange","since","to","within")
tracks = database.get_charts_tracks(**kwargs_filter,**kwargs_time)
# last time range (to compare)
if "within" in kwargs_time:
from malojatime import _get_next
trackslast = database.get_charts_tracks(**kwargs_filter,within=_get_next(kwargs_time["within"],step=-1))
try:
trackslast = database.get_charts_tracks(**kwargs_filter,timerange=kwargs_time["timerange"].next(step=-1))
# create rank association
lastrank = {}
for tl in trackslast:
@ -154,7 +153,9 @@ def module_trackcharts(max_=None,**kwargs):
try:
t["delta"] = lastrank[(*t["track"]["artists"],t["track"]["title"])] - t["rank"]
except:
t["delta"] = None
t["delta"] = math.inf
except:
pass
if tracks != []:
maxbar = tracks[0]["scrobbles"]
@ -176,8 +177,9 @@ def module_trackcharts(max_=None,**kwargs):
else:
html += "<td class='rank'></td>"
# rank change
if "within" not in kwargs_time: pass
elif e["delta"] is None:
if e.get("delta") is None:
pass
elif e["delta"] is math.inf:
html += "<td class='rankup' title='New'>🆕</td>"
elif e["delta"] > 0:
html += "<td class='rankup' title='up from #" + str(e["rank"]+e["delta"]) + "'>↗</td>"
@ -200,14 +202,14 @@ def module_trackcharts(max_=None,**kwargs):
def module_artistcharts(max_=None,**kwargs):
kwargs_filter = pickKeys(kwargs,"associated") #not used right now
kwargs_time = pickKeys(kwargs,"since","to","within")
kwargs_time = pickKeys(kwargs,"timerange","since","to","within")
artists = database.get_charts_artists(**kwargs_filter,**kwargs_time)
# last time range (to compare)
if "within" in kwargs_time:
from malojatime import _get_next
artistslast = database.get_charts_artists(**kwargs_filter,within=_get_next(kwargs_time["within"],step=-1))
try:
#from malojatime import _get_next
artistslast = database.get_charts_artists(**kwargs_filter,timerange=kwargs_time["timerange"].next(step=-1))
# create rank association
lastrank = {}
for al in artistslast:
@ -216,7 +218,9 @@ def module_artistcharts(max_=None,**kwargs):
try:
a["delta"] = lastrank[a["artist"]] - a["rank"]
except:
a["delta"] = None
a["delta"] = math.inf
except:
pass
if artists != []:
maxbar = artists[0]["scrobbles"]
@ -237,8 +241,10 @@ def module_artistcharts(max_=None,**kwargs):
else:
html += "<td class='rank'></td>"
# rank change
if "within" not in kwargs_time: pass
elif e["delta"] is None:
#if "within" not in kwargs_time: pass
if e.get("delta") is None:
pass
elif e["delta"] is math.inf:
html += "<td class='rankup' title='New'>🆕</td>"
elif e["delta"] > 0:
html += "<td class='rankup' title='up from #" + str(e["rank"]+e["delta"]) + "'>↗</td>"
@ -263,7 +269,7 @@ def module_artistcharts(max_=None,**kwargs):
def module_toptracks(pictures=True,**kwargs):
kwargs_filter = pickKeys(kwargs,"artist","associated")
kwargs_time = pickKeys(kwargs,"since","to","within","step","stepn","trail")
kwargs_time = pickKeys(kwargs,"timerange","since","to","within","step","stepn","trail")
tracks = database.get_top_tracks(**kwargs_filter,**kwargs_time)
@ -310,8 +316,8 @@ def module_toptracks(pictures=True,**kwargs):
img = getTrackImage(e["track"]["artists"],e["track"]["title"],fast=True)
else: img = None
html += entity_column(e["track"],image=img)
html += "<td class='amount'>" + scrobblesTrackLink(e["track"],{"since":range.fromstr(),"to":range.tostr()},amount=e["scrobbles"]) + "</td>"
html += "<td class='bar'>" + scrobblesTrackLink(e["track"],{"since":range.fromstr(),"to":range.tostr()},percent=e["scrobbles"]*100/maxbar) + "</td>"
html += "<td class='amount'>" + scrobblesTrackLink(e["track"],range.urikeys(),amount=e["scrobbles"]) + "</td>"
html += "<td class='bar'>" + scrobblesTrackLink(e["track"],range.urikeys(),percent=e["scrobbles"]*100/maxbar) + "</td>"
html += "</tr>"
prev = e
html += "</table>"
@ -320,7 +326,7 @@ def module_toptracks(pictures=True,**kwargs):
def module_topartists(pictures=True,**kwargs):
kwargs_time = pickKeys(kwargs,"since","to","within","step","stepn","trail")
kwargs_time = pickKeys(kwargs,"timerange","since","to","within","step","stepn","trail")
artists = database.get_top_artists(**kwargs_time)
@ -366,8 +372,8 @@ def module_topartists(pictures=True,**kwargs):
img = getArtistImage(e["artist"],fast=True)
else: img = None
html += entity_column(e["artist"],image=img)
html += "<td class='amount'>" + scrobblesArtistLink(e["artist"],{"since":range.fromstr(),"to":range.tostr()},amount=e["scrobbles"],associated=True) + "</td>"
html += "<td class='bar'>" + scrobblesArtistLink(e["artist"],{"since":range.fromstr(),"to":range.tostr()},percent=e["scrobbles"]*100/maxbar,associated=True) + "</td>"
html += "<td class='amount'>" + scrobblesArtistLink(e["artist"],range.urikeys(),amount=e["scrobbles"],associated=True) + "</td>"
html += "<td class='bar'>" + scrobblesArtistLink(e["artist"],range.urikeys(),percent=e["scrobbles"]*100/maxbar,associated=True) + "</td>"
html += "</tr>"
prev = e
html += "</table>"
@ -378,7 +384,7 @@ def module_topartists(pictures=True,**kwargs):
def module_artistcharts_tiles(**kwargs):
kwargs_filter = pickKeys(kwargs,"associated") #not used right now
kwargs_time = pickKeys(kwargs,"since","to","within")
kwargs_time = pickKeys(kwargs,"timerange","since","to","within")
artists = database.get_charts_artists(**kwargs_filter,**kwargs_time)[:14]
while len(artists)<14: artists.append(None)
@ -426,7 +432,7 @@ def module_artistcharts_tiles(**kwargs):
def module_trackcharts_tiles(**kwargs):
kwargs_filter = pickKeys(kwargs,"artist","associated")
kwargs_time = pickKeys(kwargs,"since","to","within")
kwargs_time = pickKeys(kwargs,"timerange","since","to","within")
tracks = database.get_charts_tracks(**kwargs_filter,**kwargs_time)[:14]
while len(tracks)<14: tracks.append(None) #{"track":{"title":"","artists":[]}}

View File

@ -201,14 +201,24 @@ class MTimeWeek(MRangeDescriptor):
self.year = year
self.week = week
# assume the first day of the first week of this year is 1/1
firstday = date(year,1,1)
y,w,d = firstday.chrcalendar()
if y == self.year:
firstday -= datetime.timedelta(days=(d-1))
else:
firstday += datetime.timedelta(days=8-d)
self.firstday = firstday + datetime.timedelta(days=7*(week-1))
self.lastday = self.firstday + datetime.timedelta(days=6)
# now we know the real first day, add the weeks we need
firstday = firstday + datetime.timedelta(days=7*(week-1))
lastday = firstday + datetime.timedelta(days=6)
# turn them into local overwritten date objects
self.firstday = date(firstday.year,firstday.month,firstday.day)
self.lastday = date(lastday.year,lastday.month,lastday.day)
# now check if we're still in the same year
y,w,_ = self.firstday.chrcalendar()
self.year,self.week = y,w
# firstday and lastday are already correct
def __str__(self):
return str(self.year) + "/W" + str(self.week)
@ -255,11 +265,7 @@ class MTimeWeek(MRangeDescriptor):
def next(self,step=1):
if abs(step) == math.inf: return None
try:
return MTimeWeek(self.year,self.week + step)
except:
pass
# TODO
# a range that is defined by separate start and end
class MRange(MRangeDescriptor):

View File

@ -103,7 +103,7 @@ def uri_to_internal(keys,forceTrack=False,forceArtist=False):
#4
resultkeys4 = {}
resultkeys4 = {"max_":300}
if "max" in keys: resultkeys4["max_"] = int(keys["max"])
@ -129,9 +129,9 @@ def internal_to_uri(keys):
elif "within" in keys:
urikeys.append("in",time_str(keys["within"]))
else:
if "since" in keys:
if "since" in keys and keys["since"] is not None:
urikeys.append("since",time_str(keys["since"]))
if "to" in keys:
if "to" in keys and keys["to"] is not None:
urikeys.append("to",time_str(keys["to"]))
# delimit

View File

@ -77,6 +77,7 @@
<div class="sidelist">
<h1><a href="/scrobbles?max=100">Last Scrobbles</a></h1>
<span class="stats">Today</span> KEY_SCROBBLE_NUM_TODAY
<span class="stats">This Week</span> KEY_SCROBBLE_NUM_WEEK
<span class="stats">This month</span> KEY_SCROBBLE_NUM_MONTH
<span class="stats">This year</span> KEY_SCROBBLE_NUM_YEAR
<span class="stats">All Time</span> KEY_SCROBBLE_NUM_TOTAL

View File

@ -14,30 +14,26 @@ def instructions(keys):
js_command += "showRange('toptracks','" + get_settings("DEFAULT_RANGE_CHARTS_TRACKS") + "');"
js_command += "showRange('pulse','" + get_settings("DEFAULT_RANGE_PULSE") + "');"
# get start of week
tod = datetime.utcnow()
change = (tod.weekday() + 1) % 7
d = timedelta(days=change)
newdate = tod - d
weekstart = [newdate.year,newdate.month,newdate.day]
clock()
from malojatime import today,thisweek,thismonth,thisyear
# artists
topartists_total = module_artistcharts_tiles()
topartists_year = module_artistcharts_tiles(since="year")
topartists_month = module_artistcharts_tiles(since="month")
topartists_week = module_artistcharts_tiles(since=weekstart)
topartists_year = module_artistcharts_tiles(timerange=thisyear())
topartists_month = module_artistcharts_tiles(timerange=thismonth())
topartists_week = module_artistcharts_tiles(timerange=thisweek())
clockp("Artists")
# tracks
toptracks_total = module_trackcharts_tiles()
toptracks_year = module_trackcharts_tiles(since="year")
toptracks_month = module_trackcharts_tiles(since="month")
toptracks_week = module_trackcharts_tiles(since=weekstart)
toptracks_year = module_trackcharts_tiles(timerange=thisyear())
toptracks_month = module_trackcharts_tiles(timerange=thismonth())
toptracks_week = module_trackcharts_tiles(timerange=thisweek())
clockp("Tracks")
@ -52,19 +48,17 @@ def instructions(keys):
# stats
#(amount_day,amount_month,amount_year,amount_total) = database.get_scrobbles_num_multiple(("today","month","year",None))
#amount_month += amount_day
#amount_year += amount_month
#amount_total += amount_year
amount_day = database.get_scrobbles_num(timerange=today())
scrobbles_today = "<a href='/scrobbles?in=today'>" + str(amount_day) + "</a>"
amount_day = database.get_scrobbles_num(since="today")
scrobbles_today = "<a href='/scrobbles?since=today'>" + str(amount_day) + "</a>"
amount_week = database.get_scrobbles_num(timerange=thisweek())
scrobbles_week = "<a href='/scrobbles?in=week'>" + str(amount_week) + "</a>"
amount_month = database.get_scrobbles_num(since="month")
scrobbles_month = "<a href='/scrobbles?since=month'>" + str(amount_month) + "</a>"
amount_month = database.get_scrobbles_num(timerange=thismonth())
scrobbles_month = "<a href='/scrobbles?in=month'>" + str(amount_month) + "</a>"
amount_year = database.get_scrobbles_num(since="year")
scrobbles_year = "<a href='/scrobbles?since=year'>" + str(amount_year) + "</a>"
amount_year = database.get_scrobbles_num(timerange=thisyear())
scrobbles_year = "<a href='/scrobbles?in=year'>" + str(amount_year) + "</a>"
amount_total = database.get_scrobbles_num()
scrobbles_total = "<a href='/scrobbles'>" + str(amount_total) + "</a>"
@ -72,7 +66,7 @@ def instructions(keys):
clockp("Amounts")
# pulse
from malojatime import today,thisweek,thismonth,thisyear
html_pulse_days = module_pulse(max_=7,since=today().next(-6),step="day",trail=1)
html_pulse_weeks = module_pulse(max_=12,since=thisweek().next(-11),step="week",trail=1)
@ -90,14 +84,10 @@ def instructions(keys):
pushresources = []
replace = {
# "KEY_ARTISTIMAGE":artistimages,"KEY_ARTISTNAME":artisttitles,"KEY_ARTISTLINK":artistlinks,"KEY_POSITION_ARTIST":posrange,
# "KEY_TRACKIMAGE":trackimages,"KEY_TRACKNAME":tracktitles,"KEY_TRACKLINK":tracklinks,"KEY_POSITION_TRACK":posrange,
# "KEY_SCROBBLE_TIME":scrobbletimes,"KEY_SCROBBLE_ARTISTS":scrobbleartists,"KEY_SCROBBLE_TITLE":scrobbletracklinks,"KEY_SCROBBLE_IMAGE":scrobbleimages,
# "KEY_PULSE_TERM":pulse_rangedescs,"KEY_PULSE_AMOUNT":pulse_amounts,"KEY_PULSE_BAR":pulse_bars
"KEY_TOPARTISTS_TOTAL":topartists_total,"KEY_TOPARTISTS_YEAR":topartists_year,"KEY_TOPARTISTS_MONTH":topartists_month,"KEY_TOPARTISTS_WEEK":topartists_week,
"KEY_TOPTRACKS_TOTAL":toptracks_total,"KEY_TOPTRACKS_YEAR":toptracks_year,"KEY_TOPTRACKS_MONTH":toptracks_month,"KEY_TOPTRACKS_WEEK":toptracks_week,
"KEY_JS_INIT_RANGES":js_command,
"KEY_SCROBBLE_NUM_TODAY":scrobbles_today,"KEY_SCROBBLE_NUM_MONTH":scrobbles_month,"KEY_SCROBBLE_NUM_YEAR":scrobbles_year,"KEY_SCROBBLE_NUM_TOTAL":scrobbles_total,
"KEY_SCROBBLE_NUM_TODAY":scrobbles_today,"KEY_SCROBBLE_NUM_WEEK":scrobbles_week,"KEY_SCROBBLE_NUM_MONTH":scrobbles_month,"KEY_SCROBBLE_NUM_YEAR":scrobbles_year,"KEY_SCROBBLE_NUM_TOTAL":scrobbles_total,
"KEY_SCROBBLES":html_scrobbles,
"KEY_PULSE_MONTHS":html_pulse_months,"KEY_PULSE_YEARS":html_pulse_years,"KEY_PULSE_DAYS":html_pulse_days,"KEY_PULSE_WEEKS":html_pulse_weeks,
#"KEY_PULSE_YEAR":html_pulse_year,"KEY_PULSE_MONTH":html_pulse_month,"KEY_PULSE_WEEK":html_pulse_week