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

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 newfunc(*args,**kwargs):

View File

@ -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)):

View File

@ -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!")

View File

@ -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):