Some performance improvements

This commit is contained in:
Krateng 2019-03-12 12:56:53 +01:00
parent f310cf4b20
commit 4724e45ae1
5 changed files with 34 additions and 29 deletions

View File

@ -179,14 +179,12 @@ 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"]})
r.reverse()
if keys.get("max_") is not None:
return r[:int(keys.get("max_"))]
else:
return r
r = db_query(**{k:keys[k] for k in keys if k in ["artist","artists","title","since","to","within","associated","track","max_"]})
#if keys.get("max_") is not None:
# return r[:int(keys.get("max_"))]
#else:
# return r
return r
@ -826,7 +824,7 @@ def check_cache_age():
# Queries the database
def db_query_full(artist=None,artists=None,title=None,track=None,since=None,to=None,within=None,associated=False):
def db_query_full(artist=None,artists=None,title=None,track=None,since=None,to=None,within=None,associated=False,max_=None):
(since, to) = time_stamps(since,to,within)
@ -859,15 +857,20 @@ def db_query_full(artist=None,artists=None,title=None,track=None,since=None,to=N
elif artist is None and track is None and artists is not None and len(artists) != 0:
artist = artists.pop()
if associated:
#return [getScrobbleObject(s) for s in SCROBBLES if (s[0] == track or track==None) and (artist==None or artist in coa.getCreditedList(TRACKS[s[0]][0])) and (since < s[1] < to)]
return [getScrobbleObject(s) for s in scrobbles_in_range(since,to) if (track is None or s[0] == track) and (artist is None or artist in coa.getCreditedList(TRACKS[s[0]][0]))]
else:
#return [getScrobbleObject(s) for s in SCROBBLES if (s[0] == track or track==None) and (artist==None or artist in TRACKS[s[0]][0]) and (since < s[1] < to)]
return [getScrobbleObject(s) for s in scrobbles_in_range(since,to) if (track is None or s[0] == track) and (artist is None or artist in TRACKS[s[0]][0])]
# db query always reverse by default
result = []
i = 0
for s in scrobbles_in_range(since,to,reverse=True):
if i == max_: break
if (track is None or s[0] == track) and (artist is None or artist in TRACKS[s[0]][0] or associated and artist in coa.getCreditedList(TRACKS[s[0]][0])):
result.append(getScrobbleObject(s))
i += 1
return result
# pointless to check for artist when track is checked because every track has a fixed set of artists, but it's more elegant this way
@ -967,25 +970,20 @@ def insert(list_,item,key=lambda x:x):
list_.append(item)
return i
def scrobbles_in_range(start,end,reverse=False):
if reverse:
for stamp in reversed(STAMPS):
#print("Checking " + str(stamp))
if stamp < start: return
if stamp > end: continue
yield SCROBBLESDICT[stamp]
yield SCROBBLESDICT[stamp]
else:
for stamp in STAMPS:
#print("Checking " + str(stamp))
if stamp < start: continue
if stamp > end: return
yield SCROBBLESDICT[stamp]
#for stamp in range(start,end+1):
# if stamp%1000 == 0: print("testing " + str(stamp))
# if stamp in SCROBBLESDICT:
# yield SCROBBLESDICT[stamp]
# for performance testing

View File

@ -16,12 +16,16 @@ import urllib
# artist=None,track=None,since=None,to=None,within=None,associated=False,max_=None,pictures=False
def module_scrobblelist(max_=None,pictures=False,shortTimeDesc=False,**kwargs):
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")
scrobbles = database.get_scrobbles(**kwargs_time,**kwargs_filter) #we're getting all scrobbles for the number and only filtering them on site
# if earlystop, we don't care about the actual amount and only request as many from the db
# without, we request everything and filter on site
maxkey = {"max_":max_} if earlystop else {}
scrobbles = database.get_scrobbles(**kwargs_time,**kwargs_filter,**maxkey)
if pictures:
scrobbleswithpictures = scrobbles if max_ is None else scrobbles[:max_]
#scrobbleimages = [e.get("image") for e in getTracksInfo(scrobbleswithpictures)] #will still work with scrobble objects as they are a technically a subset of track objects

View File

@ -185,6 +185,9 @@ def clock(*args):
print(args[0] + ": " + str(now - measurement))
measurement = now
### Media info
def apirequest(artists=None,artist=None,title=None):

View File

@ -38,7 +38,7 @@ def instructions(keys):
# scrobbles
html_scrobbles, _, _ = module_scrobblelist(max_=15,shortTimeDesc=True,pictures=True)
html_scrobbles, _, _ = module_scrobblelist(max_=15,shortTimeDesc=True,pictures=True,earlystop=True)
clock("Scrobbles")

View File

@ -15,12 +15,12 @@ def instructions(keys):
pushresources = [{"file":imgurl,"type":"image"}] if imgurl.startswith("/") else []
data = database.trackInfo(track["artists"],track["title"])
scrobblesnum = str(data["scrobbles"])
pos = "#" + str(data["position"])
html_scrobbles, _, _ = module_scrobblelist(track=track,max_=100)
html_scrobbles, _, _ = module_scrobblelist(track=track,max_=100,earlystop=True) # we have the number already from the trackinfo
html_pulse = module_pulse(track=track,step="year",stepn=1,trail=1)