feat: Add base HTML template and implement dashboard, logs, and service views

- Created a base HTML template for consistent layout across pages.
- Developed a dashboard page to display backup service metrics and statuses.
- Implemented a log viewer for detailed log file inspection.
- Added error handling page for better user experience during failures.
- Introduced service detail page to show specific service metrics and actions.
- Enhanced log filtering and viewing capabilities.
- Integrated auto-refresh functionality for real-time updates on metrics.
- Created integration and unit test scripts for backup metrics functionality.
This commit is contained in:
Peter Wood
2025-06-18 08:06:08 -04:00
parent d066f32b10
commit 6d726cb015
34 changed files with 6006 additions and 26 deletions

View File

@@ -2,6 +2,18 @@
set -e
# Load the unified backup metrics library
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
LIB_DIR="$SCRIPT_DIR/lib"
if [[ -f "$LIB_DIR/unified-backup-metrics.sh" ]]; then
# shellcheck source=lib/unified-backup-metrics.sh
source "$LIB_DIR/unified-backup-metrics.sh"
METRICS_ENABLED=true
else
echo "Warning: Unified backup metrics library not found at $LIB_DIR/unified-backup-metrics.sh"
METRICS_ENABLED=false
fi
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
@@ -465,6 +477,20 @@ backup_service() {
if $docker_cmd 2>&1 | tee -a "$LOG_FILE"; then
log_success "Backup completed for $service"
# File-level metrics tracking (success)
if [[ "$METRICS_ENABLED" == "true" ]]; then
local file_size checksum
if [ -f "$dest_path" ]; then
file_size=$(stat -c%s "$dest_path" 2>/dev/null || echo "0")
checksum=$(md5sum "$dest_path" 2>/dev/null | cut -d' ' -f1 || echo "")
metrics_add_file "$dest_path" "success" "$file_size" "$checksum"
elif [ -d "$dest_path" ]; then
# For directories, sum file sizes and add one entry for the directory
file_size=$(find "$dest_path" -type f -exec stat -c%s {} + 2>/dev/null | awk '{s+=$1} END {print s}' || echo "0")
metrics_add_file "$dest_path" "success" "$file_size"
fi
fi
# Verify the backup
if verify_backup "$container" "$src_path" "$dest_path"; then
log_file_details "$service" "$container:$src_path" "$dest_path" "SUCCESS"
@@ -472,11 +498,33 @@ backup_service() {
return 0
else
log_file_details "$service" "$container:$src_path" "$dest_path" "VERIFICATION_FAILED"
# File-level metrics tracking (verification failed)
if [[ "$METRICS_ENABLED" == "true" ]]; then
local file_size
if [ -f "$dest_path" ]; then
file_size=$(stat -c%s "$dest_path" 2>/dev/null || echo "0")
metrics_add_file "$dest_path" "failed" "$file_size" "" "Verification failed"
elif [ -d "$dest_path" ]; then
file_size=$(find "$dest_path" -type f -exec stat -c%s {} + 2>/dev/null | awk '{s+=$1} END {print s}' || echo "0")
metrics_add_file "$dest_path" "failed" "$file_size" "" "Verification failed"
fi
fi
return 1
fi
else
log_error "Backup failed for $service"
log_file_details "$service" "$container:$src_path" "$dest_path" "FAILED"
# File-level metrics tracking (backup failed)
if [[ "$METRICS_ENABLED" == "true" ]]; then
local file_size
if [ -f "$dest_path" ]; then
file_size=$(stat -c%s "$dest_path" 2>/dev/null || echo "0")
metrics_add_file "$dest_path" "failed" "$file_size" "" "Backup failed"
elif [ -d "$dest_path" ]; then
file_size=$(find "$dest_path" -type f -exec stat -c%s {} + 2>/dev/null | awk '{s+=$1} END {print s}' || echo "0")
metrics_add_file "$dest_path" "failed" "$file_size" "" "Backup failed"
fi
fi
return 1
fi
}
@@ -618,6 +666,12 @@ main() {
log_message "Parallel Mode: $PARALLEL_BACKUPS"
log_message "Verify Backups: $VERIFY_BACKUPS"
# Initialize metrics if enabled
if [[ "$METRICS_ENABLED" == "true" ]]; then
metrics_backup_start "media-services" "Media services backup (Sonarr, Radarr, etc.)" "$BACKUP_ROOT"
metrics_status_update "initializing" "Preparing media services backup"
fi
# Initialize logging
initialize_json_log
@@ -629,8 +683,16 @@ main() {
echo ""
} > "$MARKDOWN_LOG"
# Update metrics for pre-flight checks
if [[ "$METRICS_ENABLED" == "true" ]]; then
metrics_status_update "checking" "Running pre-flight checks"
fi
# Pre-flight checks
if ! check_disk_space; then
if [[ "$METRICS_ENABLED" == "true" ]]; then
metrics_backup_complete "failed" "Insufficient disk space"
fi
send_notification "Media Backup Failed" "Insufficient disk space" "error" 0 1
exit 1
fi
@@ -638,6 +700,9 @@ main() {
# Check if Docker is running
if ! docker info >/dev/null 2>&1; then
log_error "Docker is not running or accessible"
if [[ "$METRICS_ENABLED" == "true" ]]; then
metrics_backup_complete "failed" "Docker is not accessible"
fi
send_notification "Media Backup Failed" "Docker is not accessible" "error" 0 1
exit 1
fi
@@ -649,6 +714,11 @@ main() {
if [ "$PARALLEL_BACKUPS" == true ]; then
log_message "Running backups in parallel mode"
# Update metrics for parallel backup phase
if [[ "$METRICS_ENABLED" == "true" ]]; then
metrics_status_update "backing_up" "Running media service backups in parallel"
fi
# Create temporary file for collecting results
local temp_results
temp_results=$(mktemp)
@@ -683,6 +753,11 @@ main() {
else
log_message "Running backups in sequential mode"
# Update metrics for sequential backup phase
if [[ "$METRICS_ENABLED" == "true" ]]; then
metrics_status_update "backing_up" "Running media service backups sequentially"
fi
# Run backups sequentially
for service in "${!MEDIA_SERVICES[@]}"; do
if backup_service "$service"; then
@@ -703,6 +778,15 @@ main() {
# Track overall performance
track_performance "full_media_backup" "$script_start_time" "$script_end_time"
# Update metrics for cleanup phase
if [[ "$METRICS_ENABLED" == "true" ]]; then
if [ "$DRY_RUN" != true ]; then
metrics_status_update "cleaning_up" "Cleaning up old backup files"
else
metrics_status_update "completed" "Dry run completed successfully"
fi
fi
# Clean up old backups (only if not dry run)
if [ "$DRY_RUN" != true ]; then
cleanup_old_backups
@@ -738,6 +822,17 @@ main() {
send_notification "Media Backup Complete" "$message" "$status" "$success_count" "$failed_count"
# Finalize metrics
if [[ "$METRICS_ENABLED" == "true" ]]; then
if [ "$failed_count" -gt 0 ]; then
metrics_backup_complete "completed_with_errors" "Media backup completed with $failed_count failures"
elif [ "$DRY_RUN" == true ]; then
metrics_backup_complete "success" "Media backup dry run completed successfully"
else
metrics_backup_complete "success" "Media backup completed successfully"
fi
fi
# Exit with error code if any backups failed
if [ "$failed_count" -gt 0 ]; then
exit 1