feat: add hashing utility functions and logging for format and profile names

This commit is contained in:
Sam Chau
2025-01-08 17:49:03 +10:30
parent 3310a6e01a
commit 58d4f677ce
3 changed files with 85 additions and 0 deletions

View File

@@ -16,6 +16,7 @@ class Config:
LOG_DIR = os.path.join(CONFIG_DIR, 'log')
GENERAL_LOG_FILE = os.path.join(LOG_DIR, 'profilarr.log')
IMPORTARR_LOG_FILE = os.path.join(LOG_DIR, 'importarr.log')
HASH_LOG_FILE = os.path.join(LOG_DIR, 'hash.log')
# Flask Configuration
FLASK_ENV = os.getenv('FLASK_ENV', 'production')

View File

@@ -44,6 +44,16 @@ def setup_logging():
'filename': config.IMPORTARR_LOG_FILE,
'maxBytes': 1048576,
'backupCount': 20
},
# hash_file handler
'hash_file': {
'class': 'logging.handlers.RotatingFileHandler',
'level': 'INFO',
'formatter': 'detailed',
'filename': config.HASH_LOG_FILE,
'maxBytes': 1048576, # 1MB
'backupCount': 20
}
},
'root': {

74
backend/app/utils/hash.py Normal file
View File

@@ -0,0 +1,74 @@
# app/utils/hash.py
import hashlib
import logging
from typing import Dict, Any
logger = logging.getLogger('hash')
def generate_format_hash(format_name: str, profile_name: str,
arr_config: Dict[str, Any]) -> str:
"""
Generate a unique hash for a format based on its name, parent profile, and arr config.
"""
arr_identifier = f"{arr_config['name']}-{arr_config['type']}"
hash_input = f"{format_name}:{profile_name}:{arr_identifier}".encode(
'utf-8')
hash_value = hashlib.sha256(hash_input).hexdigest()[:8]
logger.info(
f"Generated hash for format '{format_name}' in profile '{profile_name}' for {arr_identifier}"
)
return hash_value
def process_format_name(format_name: str, profile_name: str,
arr_config: Dict[str, Any]) -> str:
"""
Process a format name and generate a unique hashed version if needed.
"""
logger.info(f"Processing format name: {format_name}")
if not arr_config.get('import_as_unique', False):
logger.info(
f"Unique import disabled, keeping original name: {format_name}")
return format_name
hash_value = generate_format_hash(format_name, profile_name, arr_config)
new_name = f"{format_name} [{hash_value}]"
logger.info(f"Format name changed: {format_name} -> {new_name}")
return new_name
def generate_profile_hash(profile_data: Dict[str, Any],
arr_config: Dict[str, Any]) -> str:
"""
Generate a unique hash for a profile based on profile name and arr name.
"""
profile_name = profile_data.get('name', '')
arr_name = arr_config['name']
logger.info(f"Generating hash for profile '{profile_name}' for {arr_name}")
hash_input = f"{profile_name}:{arr_name}".encode('utf-8')
hash_value = hashlib.sha256(hash_input).hexdigest()[:8]
logger.info(f"Generated profile hash: {hash_value}")
return hash_value
def process_profile_name(profile_data: Dict[str, Any],
arr_config: Dict[str, Any]) -> str:
"""
Process a profile name and generate a unique hashed version if needed.
"""
profile_name = profile_data['name']
logger.info(f"Processing profile name: {profile_name}")
if not arr_config.get('import_as_unique', False):
logger.info(
f"Unique import disabled, keeping original name: {profile_name}")
return profile_name
hash_value = generate_profile_hash(profile_data, arr_config)
new_name = f"{profile_name} [{hash_value}]"
logger.info(f"Profile name changed: {profile_name} -> {new_name}")
return new_name