Skip to content

Commit

Permalink
fix: removed unused functions relating to resolving duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
dreulavelle committed Nov 5, 2024
1 parent 0c8057a commit 5aec8fb
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 98 deletions.
56 changes: 0 additions & 56 deletions src/program/db/db_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,56 +437,6 @@ def hard_reset_database():
logger.error(f"Error verifying database state: {str(e)}")
raise

def resolve_duplicates(batch_size: int = 100):
"""Resolve duplicates in the database without loading all items into memory."""
from program.media.item import MediaItem
with db.Session() as session:
try:
# Find all duplicate ids
duplicates = session.query(
MediaItem.id,
func.count(MediaItem.id).label("dupe_count")
).group_by(MediaItem.id).having(func.count(MediaItem.id) > 1)

# Loop through the duplicates and resolve them in batches
for id, _ in duplicates.yield_per(batch_size):
offset = 0
while True:
# Fetch a batch of duplicate items
duplicate_items = session.query(MediaItem.id)\
.filter(MediaItem.id == id)\
.order_by(desc(MediaItem.indexed_at))\
.offset(offset)\
.limit(batch_size)\
.all()

if not duplicate_items:
break

# Keep the first item (most recent) and delete the others
for item_id in [item.id for item in duplicate_items[1:]]:
try:
delete_media_item_by_id(item_id)
logger.debug(f"Deleted duplicate item with id {id} and ID {item_id}")
except Exception as e:
logger.error(f"Error deleting duplicate item with id {id} and ID {item_id}: {str(e)}")

session.commit()
offset += batch_size

# Recreate the unique index
session.execute(text("CREATE UNIQUE INDEX IF NOT EXISTS uix_id ON MediaItem (id)"))
session.commit()

logger.success("Duplicate entries resolved in batches successfully and unique index recreated.")

except SQLAlchemyError as e:
logger.error(f"Error resolving duplicates: {str(e)}")
session.rollback()

finally:
session.close()

def hard_reset_database_pre_migration():
"""Resets the database to a fresh state."""
logger.log("DATABASE", "Starting Hard Reset of Database")
Expand Down Expand Up @@ -555,9 +505,3 @@ def hard_reset_database_pre_migration():
if os.getenv("REPAIR_SYMLINKS", None) is not None and os.getenv("REPAIR_SYMLINKS").lower() in ["true","1"]:
fix_broken_symlinks(settings_manager.settings.symlink.library_path, settings_manager.settings.symlink.rclone_path)
exit(0)

# Resolve Duplicates
if os.getenv("RESOLVE_DUPLICATES", None) is not None and os.getenv("RESOLVE_DUPLICATES").lower() in ["true","1"]:
resolve_duplicates()
exit(0)

42 changes: 0 additions & 42 deletions src/program/utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,12 @@ def handle_args():
action="store_true",
help="Fix broken symlinks.",
)
parser.add_argument(
"--plex_listener",
action="store_true",
help="Start a Plex listener.",
)
parser.add_argument(
"-p", "--port",
type=int,
default=8080,
help="Port to run the server on (default: 8000)"
)
parser.add_argument(
"--resolve_duplicates",
action="store_true",
help="Resolve duplicate items.",
)

args = parser.parse_args()

Expand All @@ -71,36 +61,4 @@ def handle_args():
fix_broken_symlinks(settings_manager.settings.symlink.library_path, settings_manager.settings.symlink.rclone_path)
exit(0)

if args.resolve_duplicates:
resolve_duplicates()
exit(0)

if args.plex_listener:
plex_listener()
exit(0)

return args


def plex_listener():
"""Start a Plex listener."""
# NOTE: This isn't staying, just merely testing
import time

from plexapi.server import PlexServer

def plex_event(event):
logger.debug(f"Event: {event}")

try:
settings = settings_manager.settings.updaters.plex
plex = PlexServer(settings.url, settings.token)
plex.startAlertListener(plex_event)

logger.debug("Started Plex listener")
logger.debug("Press Ctrl+C to stop")

while True:
time.sleep(1)
except KeyboardInterrupt:
exit(0)

0 comments on commit 5aec8fb

Please sign in to comment.