base
This commit is contained in:
44
app/main.py
44
app/main.py
@@ -37,7 +37,7 @@ class FileRecord:
|
||||
path: str
|
||||
size: int
|
||||
modified_time: float
|
||||
disk: str
|
||||
disk_label: str
|
||||
checksum: Optional[str] = None
|
||||
status: str = 'indexed' # indexed, planned, moved, verified
|
||||
|
||||
@@ -49,11 +49,11 @@ class DiskReorganizer:
|
||||
"""
|
||||
if db_config is None:
|
||||
db_config = {
|
||||
'host': '192.168.1.159',
|
||||
'port': 5432,
|
||||
'database': 'disk_reorganizer_db',
|
||||
'user': 'disk_reorg_user',
|
||||
'password': 'heel-goed-wachtwoord'
|
||||
'host': os.getenv('DB_HOST', 'localhost'),
|
||||
'port': int(os.getenv('DB_PORT', 5432)),
|
||||
'database': os.getenv('DB_NAME', 'disk_reorganizer_db'),
|
||||
'user': os.getenv('DB_USER', 'disk_reorg_user'),
|
||||
'password': os.getenv('DB_PASSWORD', 'heel-goed-wachtwoord')
|
||||
}
|
||||
self.db_config = db_config
|
||||
self.init_database()
|
||||
@@ -127,12 +127,12 @@ class DiskReorganizer:
|
||||
|
||||
# PostgreSQL INSERT ... ON CONFLICT for upsert
|
||||
cursor.execute("""
|
||||
INSERT INTO files_bak (path, size, modified_time, disk, checksum, status)
|
||||
INSERT INTO files (path, size, modified_time, disk_label, checksum, status)
|
||||
VALUES (%s, %s, %s, %s, %s, %s)
|
||||
ON CONFLICT (path) DO UPDATE SET
|
||||
size = EXCLUDED.size,
|
||||
modified_time = EXCLUDED.modified_time,
|
||||
disk = EXCLUDED.disk,
|
||||
disk_label = EXCLUDED.disk_label,
|
||||
status = EXCLUDED.status
|
||||
""", (rel_path, size, mtime, disk_name, None, 'indexed'))
|
||||
|
||||
@@ -174,9 +174,9 @@ class DiskReorganizer:
|
||||
|
||||
try:
|
||||
cursor.execute("""
|
||||
SELECT disk, SUM(size) as total_size, COUNT(*) as file_count
|
||||
FROM files_bak
|
||||
GROUP BY disk
|
||||
SELECT disk_label, SUM(size) as total_size, COUNT(*) as file_count
|
||||
FROM files
|
||||
GROUP BY disk_label
|
||||
""")
|
||||
|
||||
usage = {}
|
||||
@@ -215,7 +215,7 @@ class DiskReorganizer:
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute(
|
||||
"SELECT path, size, modified_time FROM files_bak WHERE disk = %s ORDER BY size DESC",
|
||||
"SELECT path, size, modified_time FROM files WHERE disk_label = %s ORDER BY size DESC",
|
||||
(target_disk,)
|
||||
)
|
||||
files_to_move = cursor.fetchall()
|
||||
@@ -265,15 +265,15 @@ class DiskReorganizer:
|
||||
'source_disk': target_disk,
|
||||
'source_path': rel_path,
|
||||
'dest_disk': dest_disk,
|
||||
'dest_path': rel_path, # Keep same relative path
|
||||
'target_path': rel_path, # Keep same relative path
|
||||
'size': size
|
||||
}
|
||||
plan['operations'].append(op)
|
||||
|
||||
# Store in database
|
||||
cursor.execute(
|
||||
"INSERT INTO operations_bak (source_path, dest_path, operation_type) VALUES (%s, %s, %s)",
|
||||
(f"{target_disk}:{rel_path}", f"{dest_disk}:{rel_path}", 'move')
|
||||
"INSERT INTO operations (source_path, target_path, operation_type, status) VALUES (%s, %s, %s, %s)",
|
||||
(f"{target_disk}:{rel_path}", f"{dest_disk}:{rel_path}", 'move', 'pending')
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
@@ -347,10 +347,10 @@ class DiskReorganizer:
|
||||
source_disk = op['source_disk']
|
||||
source_path = op['source_path']
|
||||
dest_disk = op['dest_disk']
|
||||
dest_path = op['dest_path']
|
||||
target_path = op['target_path']
|
||||
|
||||
source_full = Path(source_disk) / source_path
|
||||
dest_full = Path(dest_disk) / dest_path
|
||||
dest_full = Path(dest_disk) / target_path
|
||||
|
||||
# Dynamic progress display
|
||||
elapsed = time.time() - start_time
|
||||
@@ -384,7 +384,7 @@ class DiskReorganizer:
|
||||
if self.verify_operation(source_full, dest_full):
|
||||
# Update database
|
||||
cursor.execute(
|
||||
"UPDATE files_bak SET disk = %s, status = 'moved' WHERE path = %s AND disk = %s",
|
||||
"UPDATE files SET disk_label = %s, status = 'moved' WHERE path = %s AND disk_label = %s",
|
||||
(dest_disk, source_path, source_disk)
|
||||
)
|
||||
|
||||
@@ -393,7 +393,7 @@ class DiskReorganizer:
|
||||
|
||||
# Log operation as executed
|
||||
cursor.execute(
|
||||
"UPDATE operations_bak SET executed = 1, executed_at = CURRENT_TIMESTAMP WHERE source_path = %s",
|
||||
"UPDATE operations SET executed = 1, executed_at = CURRENT_TIMESTAMP WHERE source_path = %s",
|
||||
(f"{source_disk}:{source_path}",)
|
||||
)
|
||||
|
||||
@@ -407,7 +407,7 @@ class DiskReorganizer:
|
||||
except Exception as e:
|
||||
logger.error(f"\n Error processing {source_path}: {e}")
|
||||
cursor.execute(
|
||||
"UPDATE operations_bak SET error = %s WHERE source_path = %s",
|
||||
"UPDATE operations SET error = %s WHERE source_path = %s",
|
||||
(str(e), f"{source_disk}:{source_path}")
|
||||
)
|
||||
error_count += 1
|
||||
@@ -436,7 +436,7 @@ class DiskReorganizer:
|
||||
|
||||
try:
|
||||
cursor.execute("""
|
||||
SELECT status, COUNT(*), SUM(size) FROM files_bak GROUP BY status
|
||||
SELECT status, COUNT(*), SUM(size) FROM files GROUP BY status
|
||||
""")
|
||||
|
||||
print("\n=== FILE MIGRATION REPORT ===")
|
||||
@@ -445,7 +445,7 @@ class DiskReorganizer:
|
||||
print(f"{status:15}: {count:6} files, {self.format_size(size or 0)}")
|
||||
|
||||
cursor.execute("""
|
||||
SELECT operation_type, executed, verified, COUNT(*) FROM operations_bak GROUP BY operation_type, executed, verified
|
||||
SELECT operation_type, executed, verified, COUNT(*) FROM operations GROUP BY operation_type, executed, verified
|
||||
""")
|
||||
|
||||
print("\n=== OPERATIONS REPORT ===")
|
||||
|
||||
Reference in New Issue
Block a user