Spaghetti Code

This commit is contained in:
krateng 2023-03-30 14:39:44 +02:00
parent 12b5eb0b74
commit fd5d01b728
4 changed files with 51 additions and 31 deletions

View File

@ -45,6 +45,16 @@ dbstatus = {
} }
# we're running an auxiliary task that doesn't require all the random background
# nonsense to be fired up
# this is temporary
# FIX YO DAMN ARCHITECTURE ALREADY
AUX_MODE = False
def set_aux_mode():
global AUX_MODE
AUX_MODE = True
def waitfordb(func): def waitfordb(func):
def newfunc(*args,**kwargs): def newfunc(*args,**kwargs):

View File

@ -22,8 +22,10 @@ if malojaconfig['USE_GLOBAL_CACHE']:
@runhourly @runhourly
def maintenance(): def maintenance():
print_stats() from . import AUX_MODE
trim_cache() if not AUX_MODE:
print_stats()
trim_cache()
def print_stats(): def print_stats():
for name,c in (('Cache',cache),('Entity Cache',entitycache)): for name,c in (('Cache',cache),('Entity Cache',entitycache)):

View File

@ -1327,37 +1327,42 @@ def search_album(searchterm,dbconn=None):
@connection_provider @connection_provider
def clean_db(dbconn=None): def clean_db(dbconn=None):
log(f"Database Cleanup...") from . import AUX_MODE
to_delete = [ if not AUX_MODE:
# tracks with no scrobbles (trackartist entries first) with SCROBBLE_LOCK:
"from trackartists where track_id in (select id from tracks where id not in (select track_id from scrobbles))", log(f"Database Cleanup...")
"from tracks where id not in (select track_id from scrobbles)",
# artists with no tracks AND no albums
"from artists where id not in (select artist_id from trackartists) \
and id not in (select target_artist from associated_artists) \
and id not in (select artist_id from albumartists)",
# tracks with no artists (scrobbles first)
"from scrobbles where track_id in (select id from tracks where id not in (select track_id from trackartists))",
"from tracks where id not in (select track_id from trackartists)",
# albums with no tracks (albumartist entries first)
"from albumartists where album_id in (select id from albums where id not in (select album_id from tracks where album_id is not null))",
"from albums where id not in (select album_id from tracks where album_id is not null)",
# albumartist entries that are missing a reference
"from albumartists where album_id not in (select album_id from tracks where album_id is not null)",
"from albumartists where artist_id not in (select id from artists)",
# trackartist entries that mare missing a reference
"from trackartists where track_id not in (select id from tracks)",
"from trackartists where artist_id not in (select id from artists)"
]
for d in to_delete: to_delete = [
selection = dbconn.execute(sql.text(f"select * {d}")) # tracks with no scrobbles (trackartist entries first)
for row in selection.all(): "from trackartists where track_id in (select id from tracks where id not in (select track_id from scrobbles))",
log(f"Deleting {row}") "from tracks where id not in (select track_id from scrobbles)",
deletion = dbconn.execute(sql.text(f"delete {d}")) # artists with no tracks AND no albums
"from artists where id not in (select artist_id from trackartists) \
and id not in (select target_artist from associated_artists) \
and id not in (select artist_id from albumartists)",
# tracks with no artists (scrobbles first)
"from scrobbles where track_id in (select id from tracks where id not in (select track_id from trackartists))",
"from tracks where id not in (select track_id from trackartists)",
# albums with no tracks (albumartist entries first)
"from albumartists where album_id in (select id from albums where id not in (select album_id from tracks where album_id is not null))",
"from albums where id not in (select album_id from tracks where album_id is not null)",
# albumartist entries that are missing a reference
"from albumartists where album_id not in (select album_id from tracks where album_id is not null)",
"from albumartists where artist_id not in (select id from artists)",
# trackartist entries that mare missing a reference
"from trackartists where track_id not in (select id from tracks)",
"from trackartists where artist_id not in (select id from artists)"
]
log("Database Cleanup complete!") for d in to_delete:
print(d)
selection = dbconn.execute(sql.text(f"select * {d}"))
for row in selection.all():
log(f"Deleting {row}")
deletion = dbconn.execute(sql.text(f"delete {d}"))
log("Database Cleanup complete!")

View File

@ -21,6 +21,9 @@ outputs = {
def import_scrobbles(inputf): def import_scrobbles(inputf):
from ...database import set_aux_mode
set_aux_mode()
from ...database.sqldb import add_scrobbles from ...database.sqldb import add_scrobbles
result = { result = {
@ -180,7 +183,7 @@ def parse_spotify_full(inputf):
if len(inputfiles) == 0: if len(inputfiles) == 0:
print("No files found!") print("No files found!")
return return
if inputfiles != [inputf]: if inputfiles != [inputf]:
print("Spotify files should all be imported together to identify duplicates across the whole dataset.") print("Spotify files should all be imported together to identify duplicates across the whole dataset.")
if not ask("Import " + ", ".join(col['yellow'](i) for i in inputfiles) + "?",default=True): if not ask("Import " + ", ".join(col['yellow'](i) for i in inputfiles) + "?",default=True):