From fd5d01b7286573c386dac2c49c0b6ffb61fc9646 Mon Sep 17 00:00:00 2001 From: krateng Date: Thu, 30 Mar 2023 14:39:44 +0200 Subject: [PATCH] Spaghetti Code --- maloja/database/__init__.py | 10 ++++ maloja/database/dbcache.py | 6 +- maloja/database/sqldb.py | 61 +++++++++++--------- maloja/proccontrol/tasks/import_scrobbles.py | 5 +- 4 files changed, 51 insertions(+), 31 deletions(-) diff --git a/maloja/database/__init__.py b/maloja/database/__init__.py index 4711cf8..7e3f3ac 100644 --- a/maloja/database/__init__.py +++ b/maloja/database/__init__.py @@ -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 newfunc(*args,**kwargs): diff --git a/maloja/database/dbcache.py b/maloja/database/dbcache.py index 9b092a9..582a9e7 100644 --- a/maloja/database/dbcache.py +++ b/maloja/database/dbcache.py @@ -22,8 +22,10 @@ if malojaconfig['USE_GLOBAL_CACHE']: @runhourly def maintenance(): - print_stats() - trim_cache() + from . import AUX_MODE + if not AUX_MODE: + print_stats() + trim_cache() def print_stats(): for name,c in (('Cache',cache),('Entity Cache',entitycache)): diff --git a/maloja/database/sqldb.py b/maloja/database/sqldb.py index aeb9e45..f397713 100644 --- a/maloja/database/sqldb.py +++ b/maloja/database/sqldb.py @@ -1327,37 +1327,42 @@ def search_album(searchterm,dbconn=None): @connection_provider def clean_db(dbconn=None): - log(f"Database Cleanup...") + from . import AUX_MODE - to_delete = [ - # tracks with no scrobbles (trackartist entries first) - "from trackartists where track_id in (select id from tracks where id not in (select track_id from scrobbles))", - "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)" - ] + if not AUX_MODE: + with SCROBBLE_LOCK: + log(f"Database Cleanup...") - for d in to_delete: - 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}")) + to_delete = [ + # tracks with no scrobbles (trackartist entries first) + "from trackartists where track_id in (select id from tracks where id not in (select track_id from scrobbles))", + "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)" + ] - 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!") diff --git a/maloja/proccontrol/tasks/import_scrobbles.py b/maloja/proccontrol/tasks/import_scrobbles.py index b5bf620..376591d 100644 --- a/maloja/proccontrol/tasks/import_scrobbles.py +++ b/maloja/proccontrol/tasks/import_scrobbles.py @@ -21,6 +21,9 @@ outputs = { def import_scrobbles(inputf): + from ...database import set_aux_mode + set_aux_mode() + from ...database.sqldb import add_scrobbles result = { @@ -180,7 +183,7 @@ def parse_spotify_full(inputf): if len(inputfiles) == 0: print("No files found!") return - + if inputfiles != [inputf]: 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):