From 936c3892b9638e851dafe56dbc6cc38a0319f4d6 Mon Sep 17 00:00:00 2001 From: Krateng Date: Mon, 11 Mar 2019 20:04:23 +0100 Subject: [PATCH] Some testing utilities --- database.py | 52 ++++++++++++++++++++++++++++++++++++------------ server.py | 2 +- utilities.py | 11 +++++++++- website/start.py | 14 +++++++++++-- 4 files changed, 62 insertions(+), 17 deletions(-) diff --git a/database.py b/database.py index 1ae945c..5a8923f 100644 --- a/database.py +++ b/database.py @@ -66,12 +66,12 @@ def getTrackObject(o): -def createScrobble(artists,title,time): +def createScrobble(artists,title,time,volatile=False): while (time in STAMPS_SET): time += 1 STAMPS_SET.add(time) i = getTrackID(artists,title) - obj = (i,time,False) + obj = (i,time,volatile) # if volatile generated, we simply pretend we have already saved it to disk #SCROBBLES.append(obj) # immediately insert scrobble correctly so we can guarantee sorted list index = insert(SCROBBLES,obj,key=lambda x:x[1]) @@ -731,7 +731,6 @@ def build_db(): log("Database fully built!") - # Saves all cached entries to disk @@ -815,15 +814,14 @@ def db_query(artist=None,artists=None,title=None,track=None,since=None,to=None,w artist = artists.pop() - - # right now we always request everything by name, maybe we don't actually need the request by number, but i'll leave it in for now + 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 (s[0] == track or track==None) and (artist==None or artist in coa.getCreditedList(TRACKS[s[0]][0]))] + 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 (s[0] == track or track==None) and (artist==None or artist in TRACKS[s[0]][0])] + 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])] # 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 @@ -924,14 +922,42 @@ def insert(list_,item,key=lambda x:x): return i -def scrobbles_in_range(start,end): - for stamp in STAMPS: - #print("Checking " + str(stamp)) - if stamp < start: continue - if stamp > end: return - yield SCROBBLESDICT[stamp] +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] + 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 +def generateStuff(num=0,pertrack=0,mult=0): + import random + for i in range(num): + track = random.choice(TRACKS) + t = getTrackObject(track) + time = random.randint(STAMPS[0],STAMPS[-1]) + createScrobble(t["artists"],t["title"],time,volatile=True) + + for track in TRACKS: + t = getTrackObject(track) + for i in range(pertrack): + time = random.randint(STAMPS[0],STAMPS[-1]) + createScrobble(t["artists"],t["title"],time,volatile=True) + + for scrobble in SCROBBLES: + s = getScrobbleObject(scrobble) + for i in range(mult): + createScrobble(s["artists"],s["title"],s["time"] - i*500,volatile=True) diff --git a/server.py b/server.py index 05a67a2..debf113 100755 --- a/server.py +++ b/server.py @@ -140,6 +140,7 @@ def static_html(name): with open("website/common/header.html") as headerfile: headerhtml = headerfile.read() html = html.replace("",footerhtml + "").replace("",headerhtml + "") + # If a python file exists, it provides the replacement dict for the html file if os.path.exists("website/" + name + ".py"): @@ -158,7 +159,6 @@ def static_html(name): html = html.replace(k,element,1) else: html = html.replace(k,txt_keys[k]) - response.set_header("Link",",".join(linkheaders)) diff --git a/utilities.py b/utilities.py index 8bc59b3..102ca45 100644 --- a/utilities.py +++ b/utilities.py @@ -174,7 +174,16 @@ def log(msg,module=None): with open("logs/" + module + ".log","a") as logfile: logfile.write(now + " " + msg + "\n") - + +### not meant to be precise, just for a rough idea +measurement = 0 +def clock(*args): + import time + global measurement + now = time.time() + if len(args) > 0: + print(args[0] + ": " + str(now - measurement)) + measurement = now ### Media info diff --git a/website/start.py b/website/start.py index 505ce57..12729a4 100644 --- a/website/start.py +++ b/website/start.py @@ -1,6 +1,7 @@ import urllib from datetime import datetime, timedelta import database +from utilities import clock from htmlmodules import module_scrobblelist, module_pulse, module_artistcharts_tiles, module_trackcharts_tiles @@ -14,6 +15,8 @@ def instructions(keys): newdate = tod - d weekstart = [newdate.year,newdate.month,newdate.day] + clock() + # artists topartists_total = module_artistcharts_tiles() @@ -21,7 +24,8 @@ def instructions(keys): topartists_month = module_artistcharts_tiles(since="month") topartists_week = module_artistcharts_tiles(since=weekstart) - + clock("Artists") + # tracks toptracks_total = module_trackcharts_tiles() @@ -30,9 +34,13 @@ def instructions(keys): toptracks_week = module_trackcharts_tiles(since=weekstart) + clock("Tracks") + + # scrobbles html_scrobbles, _, _ = module_scrobblelist(max_=15,shortTimeDesc=True,pictures=True) + clock("Scrobbles") # stats amount = database.get_scrobbles_num(since="today") @@ -48,6 +56,8 @@ def instructions(keys): scrobbles_total = "" + str(amount) + "" + clock("Amounts") + # pulse dt = datetime.utcnow() first_month = [dt.year-1,dt.month+1] @@ -68,7 +78,7 @@ def instructions(keys): #html_pulse_month = module_pulse(max_=30,since=[dt.year,dt.month],step="day",trail=1) #html_pulse_year = module_pulse(max_=12,since=[dt.year],step="month",trail=1) - + clock("Pulse") #pushresources = [{"file":img,"type":"image"} for img in artistimages + trackimages] #can't push scrobble images as we don't get them from the module function, need to think about that pushresources = []