Add database cleanup buttons
This commit is contained in:
Binary file not shown.
177
routes/admin.py
177
routes/admin.py
@@ -65,3 +65,180 @@ def sync_files():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
db.session.rollback()
|
db.session.rollback()
|
||||||
return jsonify({'error': str(e)}), 500
|
return jsonify({'error': str(e)}), 500
|
||||||
|
|
||||||
|
@admin.route('/api/admin/verify-db-state', methods=['GET'])
|
||||||
|
@login_required
|
||||||
|
def verify_db_state():
|
||||||
|
if not current_user.is_admin:
|
||||||
|
return jsonify({'error': 'Unauthorized'}), 403
|
||||||
|
|
||||||
|
try:
|
||||||
|
DATA_ROOT = '/data/rooms'
|
||||||
|
verification_results = {
|
||||||
|
'rooms_checked': 0,
|
||||||
|
'files_in_db_not_fs': [],
|
||||||
|
'files_in_fs_not_db': [],
|
||||||
|
'permission_mismatches': [],
|
||||||
|
'size_mismatches': [],
|
||||||
|
'modified_time_mismatches': [],
|
||||||
|
'total_files_checked': 0,
|
||||||
|
'total_folders_checked': 0
|
||||||
|
}
|
||||||
|
|
||||||
|
rooms = Room.query.all()
|
||||||
|
for room in rooms:
|
||||||
|
verification_results['rooms_checked'] += 1
|
||||||
|
room_dir = os.path.join(DATA_ROOT, str(room.id))
|
||||||
|
|
||||||
|
# Get all files and folders from database for this room
|
||||||
|
db_files = RoomFile.query.filter_by(room_id=room.id, deleted=False).all()
|
||||||
|
db_paths = {(f.path, f.name): f for f in db_files}
|
||||||
|
|
||||||
|
# Check filesystem if directory exists
|
||||||
|
if os.path.exists(room_dir):
|
||||||
|
for root, dirs, files in os.walk(room_dir):
|
||||||
|
rel_root = os.path.relpath(root, room_dir)
|
||||||
|
rel_path = '' if rel_root == '.' else rel_root.replace('\\', '/')
|
||||||
|
|
||||||
|
# Check folders
|
||||||
|
for d in dirs:
|
||||||
|
verification_results['total_folders_checked'] += 1
|
||||||
|
folder_path = os.path.join(root, d)
|
||||||
|
stat = os.stat(folder_path)
|
||||||
|
|
||||||
|
# Check if folder exists in database
|
||||||
|
db_file = db_paths.get((rel_path, d))
|
||||||
|
if not db_file:
|
||||||
|
verification_results['files_in_fs_not_db'].append({
|
||||||
|
'room_id': room.id,
|
||||||
|
'room_name': room.name,
|
||||||
|
'path': rel_path,
|
||||||
|
'name': d,
|
||||||
|
'type': 'folder'
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
# Verify folder metadata
|
||||||
|
if abs(stat.st_mtime - db_file.modified) > 1: # Allow 1 second difference
|
||||||
|
verification_results['modified_time_mismatches'].append({
|
||||||
|
'room_id': room.id,
|
||||||
|
'room_name': room.name,
|
||||||
|
'path': rel_path,
|
||||||
|
'name': d,
|
||||||
|
'type': 'folder',
|
||||||
|
'fs_modified': stat.st_mtime,
|
||||||
|
'db_modified': db_file.modified
|
||||||
|
})
|
||||||
|
# Remove from db_paths as we've checked it
|
||||||
|
db_paths.pop((rel_path, d), None)
|
||||||
|
|
||||||
|
# Check files
|
||||||
|
for f in files:
|
||||||
|
verification_results['total_files_checked'] += 1
|
||||||
|
file_path = os.path.join(root, f)
|
||||||
|
stat = os.stat(file_path)
|
||||||
|
|
||||||
|
# Check if file exists in database
|
||||||
|
db_file = db_paths.get((rel_path, f))
|
||||||
|
if not db_file:
|
||||||
|
verification_results['files_in_fs_not_db'].append({
|
||||||
|
'room_id': room.id,
|
||||||
|
'room_name': room.name,
|
||||||
|
'path': rel_path,
|
||||||
|
'name': f,
|
||||||
|
'type': 'file'
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
# Verify file metadata
|
||||||
|
if abs(stat.st_mtime - db_file.modified) > 1: # Allow 1 second difference
|
||||||
|
verification_results['modified_time_mismatches'].append({
|
||||||
|
'room_id': room.id,
|
||||||
|
'room_name': room.name,
|
||||||
|
'path': rel_path,
|
||||||
|
'name': f,
|
||||||
|
'type': 'file',
|
||||||
|
'fs_modified': stat.st_mtime,
|
||||||
|
'db_modified': db_file.modified
|
||||||
|
})
|
||||||
|
if stat.st_size != db_file.size:
|
||||||
|
verification_results['size_mismatches'].append({
|
||||||
|
'room_id': room.id,
|
||||||
|
'room_name': room.name,
|
||||||
|
'path': rel_path,
|
||||||
|
'name': f,
|
||||||
|
'type': 'file',
|
||||||
|
'fs_size': stat.st_size,
|
||||||
|
'db_size': db_file.size
|
||||||
|
})
|
||||||
|
# Remove from db_paths as we've checked it
|
||||||
|
db_paths.pop((rel_path, f), None)
|
||||||
|
|
||||||
|
# Any remaining items in db_paths are in DB but not in filesystem
|
||||||
|
for (path, name), db_file in db_paths.items():
|
||||||
|
verification_results['files_in_db_not_fs'].append({
|
||||||
|
'room_id': room.id,
|
||||||
|
'room_name': room.name,
|
||||||
|
'path': path,
|
||||||
|
'name': name,
|
||||||
|
'type': db_file.type
|
||||||
|
})
|
||||||
|
|
||||||
|
# Calculate total issues
|
||||||
|
total_issues = (
|
||||||
|
len(verification_results['files_in_db_not_fs']) +
|
||||||
|
len(verification_results['files_in_fs_not_db']) +
|
||||||
|
len(verification_results['permission_mismatches']) +
|
||||||
|
len(verification_results['size_mismatches']) +
|
||||||
|
len(verification_results['modified_time_mismatches'])
|
||||||
|
)
|
||||||
|
|
||||||
|
# Add summary statistics
|
||||||
|
verification_results['summary'] = {
|
||||||
|
'total_issues': total_issues,
|
||||||
|
'status': 'healthy' if total_issues == 0 else 'issues_found'
|
||||||
|
}
|
||||||
|
|
||||||
|
return jsonify(verification_results)
|
||||||
|
except Exception as e:
|
||||||
|
return jsonify({'error': str(e)}), 500
|
||||||
|
|
||||||
|
@admin.route('/api/admin/cleanup-orphaned-records', methods=['POST'])
|
||||||
|
@login_required
|
||||||
|
def cleanup_orphaned_records():
|
||||||
|
if not current_user.is_admin:
|
||||||
|
return jsonify({'error': 'Unauthorized'}), 403
|
||||||
|
|
||||||
|
try:
|
||||||
|
DATA_ROOT = '/data/rooms'
|
||||||
|
rooms = Room.query.all()
|
||||||
|
cleaned_records = []
|
||||||
|
|
||||||
|
for room in rooms:
|
||||||
|
room_dir = os.path.join(DATA_ROOT, str(room.id))
|
||||||
|
|
||||||
|
# Get all files and folders from database for this room
|
||||||
|
db_files = RoomFile.query.filter_by(room_id=room.id, deleted=False).all()
|
||||||
|
|
||||||
|
for db_file in db_files:
|
||||||
|
file_path = os.path.join(room_dir, db_file.path, db_file.name) if db_file.path else os.path.join(room_dir, db_file.name)
|
||||||
|
|
||||||
|
# If file doesn't exist in filesystem, mark it as deleted in database
|
||||||
|
if not os.path.exists(file_path):
|
||||||
|
db_file.deleted = True
|
||||||
|
cleaned_records.append({
|
||||||
|
'room_id': room.id,
|
||||||
|
'room_name': room.name,
|
||||||
|
'path': db_file.path,
|
||||||
|
'name': db_file.name,
|
||||||
|
'type': db_file.type
|
||||||
|
})
|
||||||
|
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return jsonify({
|
||||||
|
'success': True,
|
||||||
|
'message': f'Cleaned up {len(cleaned_records)} orphaned records',
|
||||||
|
'cleaned_records': cleaned_records
|
||||||
|
})
|
||||||
|
except Exception as e:
|
||||||
|
db.session.rollback()
|
||||||
|
return jsonify({'error': str(e)}), 500
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
<div class="debugging-tab">
|
<div class="debugging-tab">
|
||||||
<div class="card shadow-sm mb-4">
|
<div class="card shadow-sm mb-4">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
|
<h5 class="card-title mb-3">File system</h5>
|
||||||
<div class="d-flex align-items-center gap-3 mb-3">
|
<div class="d-flex align-items-center gap-3 mb-3">
|
||||||
<button type="button" id="syncFilesBtn" class="btn btn-primary d-flex align-items-center gap-2" style="background-color:var(--primary-color); border:1px solid var(--primary-color);" onmouseover="this.style.backgroundColor='var(--primary-light)'" onmouseout="this.style.backgroundColor='var(--primary-color)'">
|
<button type="button" id="syncFilesBtn" class="btn btn-primary d-flex align-items-center gap-2" style="background-color:var(--primary-color); border:1px solid var(--primary-color);" onmouseover="this.style.backgroundColor='var(--primary-light)'" onmouseout="this.style.backgroundColor='var(--primary-color)'">
|
||||||
<i class="fas fa-sync-alt"></i> Sync File System
|
<i class="fas fa-sync-alt"></i> Sync File System
|
||||||
@@ -15,6 +16,99 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="card shadow-sm mb-4">
|
||||||
|
<div class="card-body">
|
||||||
|
<h5 class="card-title mb-3">Database Verification</h5>
|
||||||
|
<div class="d-flex align-items-center gap-3 mb-3">
|
||||||
|
<button type="button" id="verifyDbBtn" class="btn btn-primary d-flex align-items-center gap-2" style="background-color:var(--primary-color); border:1px solid var(--primary-color);" onmouseover="this.style.backgroundColor='var(--primary-light)'" onmouseout="this.style.backgroundColor='var(--primary-color)'">
|
||||||
|
<i class="fas fa-database"></i> Verify Database State
|
||||||
|
</button>
|
||||||
|
<button type="button" id="cleanupOrphanedBtn" class="btn btn-outline-warning d-flex align-items-center gap-2" style="border-color:var(--warning-color); color:var(--warning-color);" onmouseover="this.style.backgroundColor='var(--warning-color)'; this.style.color='white'" onmouseout="this.style.backgroundColor='transparent'; this.style.color='var(--warning-color)'">
|
||||||
|
<i class="fas fa-broom"></i> Cleanup Orphaned Records
|
||||||
|
</button>
|
||||||
|
<div id="verifyStatus" class="text-muted small"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="verificationResults" style="display: none;">
|
||||||
|
<div class="alert alert-info mb-3">
|
||||||
|
<div class="d-flex align-items-center gap-2">
|
||||||
|
<i class="fas fa-info-circle"></i>
|
||||||
|
<div>
|
||||||
|
<strong>Summary:</strong>
|
||||||
|
<span id="verificationSummary"></span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="accordion" id="verificationAccordion">
|
||||||
|
<!-- Files in DB but not in FS -->
|
||||||
|
<div class="accordion-item">
|
||||||
|
<h2 class="accordion-header">
|
||||||
|
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#dbNotFsCollapse">
|
||||||
|
<i class="fas fa-exclamation-triangle text-warning me-2"></i>
|
||||||
|
Files in Database but not in Filesystem
|
||||||
|
<span id="dbNotFsCount" class="badge bg-warning ms-2">0</span>
|
||||||
|
</button>
|
||||||
|
</h2>
|
||||||
|
<div id="dbNotFsCollapse" class="accordion-collapse collapse" data-bs-parent="#verificationAccordion">
|
||||||
|
<div class="accordion-body">
|
||||||
|
<div id="dbNotFsList" class="list-group"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Files in FS but not in DB -->
|
||||||
|
<div class="accordion-item">
|
||||||
|
<h2 class="accordion-header">
|
||||||
|
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#fsNotDbCollapse">
|
||||||
|
<i class="fas fa-exclamation-triangle text-warning me-2"></i>
|
||||||
|
Files in Filesystem but not in Database
|
||||||
|
<span id="fsNotDbCount" class="badge bg-warning ms-2">0</span>
|
||||||
|
</button>
|
||||||
|
</h2>
|
||||||
|
<div id="fsNotDbCollapse" class="accordion-collapse collapse" data-bs-parent="#verificationAccordion">
|
||||||
|
<div class="accordion-body">
|
||||||
|
<div id="fsNotDbList" class="list-group"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Size Mismatches -->
|
||||||
|
<div class="accordion-item">
|
||||||
|
<h2 class="accordion-header">
|
||||||
|
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#sizeMismatchCollapse">
|
||||||
|
<i class="fas fa-exclamation-triangle text-warning me-2"></i>
|
||||||
|
Size Mismatches
|
||||||
|
<span id="sizeMismatchCount" class="badge bg-warning ms-2">0</span>
|
||||||
|
</button>
|
||||||
|
</h2>
|
||||||
|
<div id="sizeMismatchCollapse" class="accordion-collapse collapse" data-bs-parent="#verificationAccordion">
|
||||||
|
<div class="accordion-body">
|
||||||
|
<div id="sizeMismatchList" class="list-group"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Modified Time Mismatches -->
|
||||||
|
<div class="accordion-item">
|
||||||
|
<h2 class="accordion-header">
|
||||||
|
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#timeMismatchCollapse">
|
||||||
|
<i class="fas fa-exclamation-triangle text-warning me-2"></i>
|
||||||
|
Modified Time Mismatches
|
||||||
|
<span id="timeMismatchCount" class="badge bg-warning ms-2">0</span>
|
||||||
|
</button>
|
||||||
|
</h2>
|
||||||
|
<div id="timeMismatchCollapse" class="accordion-collapse collapse" data-bs-parent="#verificationAccordion">
|
||||||
|
<div class="accordion-body">
|
||||||
|
<div id="timeMismatchList" class="list-group"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
@@ -59,5 +153,166 @@ document.getElementById('syncFilesBtn').addEventListener('click', async function
|
|||||||
}, 5000);
|
}, 5000);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
document.getElementById('verifyDbBtn').addEventListener('click', async function() {
|
||||||
|
const btn = this;
|
||||||
|
const status = document.getElementById('verifyStatus');
|
||||||
|
const results = document.getElementById('verificationResults');
|
||||||
|
|
||||||
|
// Disable button and show loading state
|
||||||
|
btn.disabled = true;
|
||||||
|
btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Verifying...';
|
||||||
|
status.textContent = 'Verifying database state...';
|
||||||
|
results.style.display = 'none';
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/admin/verify-db-state', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-CSRFToken': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
console.log('Verification response:', data); // Debug log
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(data.error || 'Verification failed');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update summary
|
||||||
|
const summary = document.getElementById('verificationSummary');
|
||||||
|
const totalIssues = (data.files_in_db_not_fs?.length || 0) +
|
||||||
|
(data.files_in_fs_not_db?.length || 0) +
|
||||||
|
(data.size_mismatches?.length || 0) +
|
||||||
|
(data.modified_time_mismatches?.length || 0);
|
||||||
|
|
||||||
|
summary.textContent = `Found ${totalIssues} issues across ${data.rooms_checked || 0} rooms.`;
|
||||||
|
|
||||||
|
// Update counts and lists
|
||||||
|
updateMismatchSection('dbNotFs', data.files_in_db_not_fs || []);
|
||||||
|
updateMismatchSection('fsNotDb', data.files_in_fs_not_db || []);
|
||||||
|
updateMismatchSection('sizeMismatch', data.size_mismatches || []);
|
||||||
|
updateMismatchSection('timeMismatch', data.modified_time_mismatches || []);
|
||||||
|
|
||||||
|
// Show results
|
||||||
|
results.style.display = 'block';
|
||||||
|
status.textContent = 'Verification completed!';
|
||||||
|
status.className = 'text-success small';
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Verification error:', error); // Debug log
|
||||||
|
status.textContent = error.message;
|
||||||
|
status.className = 'text-danger small';
|
||||||
|
} finally {
|
||||||
|
// Reset button state
|
||||||
|
btn.disabled = false;
|
||||||
|
btn.innerHTML = '<i class="fas fa-database"></i> Verify Database State';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById('cleanupOrphanedBtn').addEventListener('click', async function() {
|
||||||
|
const btn = this;
|
||||||
|
const status = document.getElementById('verifyStatus');
|
||||||
|
|
||||||
|
// Disable button and show loading state
|
||||||
|
btn.disabled = true;
|
||||||
|
btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Cleaning up...';
|
||||||
|
status.textContent = 'Cleaning up orphaned records...';
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/admin/cleanup-orphaned-records', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-CSRFToken': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(data.error || 'Cleanup failed');
|
||||||
|
}
|
||||||
|
|
||||||
|
status.textContent = data.message;
|
||||||
|
status.className = 'text-success small';
|
||||||
|
|
||||||
|
// If there were cleaned records, trigger a verification
|
||||||
|
if (data.cleaned_records && data.cleaned_records.length > 0) {
|
||||||
|
document.getElementById('verifyDbBtn').click();
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Cleanup error:', error);
|
||||||
|
status.textContent = error.message;
|
||||||
|
status.className = 'text-danger small';
|
||||||
|
} finally {
|
||||||
|
// Reset button state
|
||||||
|
btn.disabled = false;
|
||||||
|
btn.innerHTML = '<i class="fas fa-broom"></i> Cleanup Orphaned Records';
|
||||||
|
|
||||||
|
// Clear status after 5 seconds
|
||||||
|
setTimeout(() => {
|
||||||
|
status.textContent = '';
|
||||||
|
status.className = 'text-muted small';
|
||||||
|
}, 5000);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function updateMismatchSection(sectionId, items) {
|
||||||
|
const count = document.getElementById(`${sectionId}Count`);
|
||||||
|
const list = document.getElementById(`${sectionId}List`);
|
||||||
|
|
||||||
|
if (!count || !list) {
|
||||||
|
console.error(`Missing elements for section ${sectionId}`); // Debug log
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
count.textContent = items.length;
|
||||||
|
|
||||||
|
if (items.length === 0) {
|
||||||
|
list.innerHTML = '<div class="text-muted p-3">No issues found</div>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
list.innerHTML = items.map(item => `
|
||||||
|
<div class="list-group-item">
|
||||||
|
<div class="d-flex justify-content-between align-items-start">
|
||||||
|
<div>
|
||||||
|
<strong>${item.name || 'Unknown'}</strong>
|
||||||
|
<div class="text-muted small">
|
||||||
|
Room: ${item.room_name || 'Unknown'}
|
||||||
|
${item.path ? `<br>Path: ${item.path}` : ''}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="text-muted small">
|
||||||
|
${item.type === 'file' ? 'File' : 'Folder'}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
${item.fs_size !== undefined ? `
|
||||||
|
<div class="mt-2 small">
|
||||||
|
<span class="text-danger">Size mismatch:</span>
|
||||||
|
<br>Filesystem: ${formatSize(item.fs_size)}
|
||||||
|
<br>Database: ${formatSize(item.db_size)}
|
||||||
|
</div>
|
||||||
|
` : ''}
|
||||||
|
${item.fs_modified !== undefined ? `
|
||||||
|
<div class="mt-2 small">
|
||||||
|
<span class="text-danger">Time mismatch:</span>
|
||||||
|
<br>Filesystem: ${new Date(item.fs_modified * 1000).toLocaleString()}
|
||||||
|
<br>Database: ${new Date(item.db_modified * 1000).toLocaleString()}
|
||||||
|
</div>
|
||||||
|
` : ''}
|
||||||
|
</div>
|
||||||
|
`).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatSize(bytes) {
|
||||||
|
if (bytes === 0) return '0 B';
|
||||||
|
const k = 1024;
|
||||||
|
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||||
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||||
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
Reference in New Issue
Block a user