Add preview of changes before applying optimizations to Option 5

- Display all changes that will be made with per-domain breakdown
- Show memory impact per domain and total impact
- Calculate memory freed/allocated for each change
- Require final confirmation before actually applying changes
- Provides safety check to prevent accidental bad configurations

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
cschantz
2026-02-17 22:59:37 -05:00
parent 95429dc192
commit 7c960c4870
+104
View File
@@ -926,6 +926,110 @@ optimize_all_domains() {
;;
esac
# ========================================================================
# STEP 3: PREVIEW CHANGES BEFORE APPLYING
# ========================================================================
show_banner
cecho "${WHITE}${BOLD}PREVIEW: Changes that will be applied${NC}"
cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}"
echo ""
local preview_count=0
local preview_total_impact=0
# Get all users for preview
local preview_users
preview_users=$(list_all_users)
while IFS= read -r username; do
[ -z "$username" ] && continue
local user_domains
user_domains=$(get_user_domains "$username")
while IFS= read -r domain; do
[ -z "$domain" ] && continue
# Check if this domain/user was selected
local should_preview=false
if [ "$SYS_CONTROL_PANEL" = "cpanel" ]; then
[ "${domains_to_apply[$domain]}" = "true" ] && should_preview=true
else
[ "${domains_to_apply[$username]}" = "true" ] && should_preview=true
fi
if [ "$should_preview" = "false" ]; then
continue
fi
preview_count=$((preview_count + 1))
# Get current pool config
local pool_config
pool_config=$(find_fpm_pool_config "$username" "$domain" 2>/dev/null)
if [ -z "$pool_config" ] || [ ! -f "$pool_config" ]; then
continue
fi
# Get current and recommended values
local current_max recommended_max
current_max=$(grep "^pm.max_children" "$pool_config" 2>/dev/null | awk -F'=' '{print $2}' | tr -d ' ')
if [ "$SYS_CONTROL_PANEL" = "cpanel" ]; then
recommended_max="${recommended_values[$domain]}"
else
recommended_max="${recommended_values[$username]}"
fi
if [ -z "$current_max" ] || [ -z "$recommended_max" ]; then
continue
fi
# Calculate memory impact (assume 20MB per process on average)
local memory_freed=$((( current_max - recommended_max ) * 20))
if [ "$current_max" -ne "$recommended_max" ]; then
cecho " ${CYAN}[$preview_count]${NC} $domain"
cecho " Current: pm.max_children = ${YELLOW}$current_max${NC}"
cecho " Change to: pm.max_children = ${GREEN}$recommended_max${NC}"
if [ "$memory_freed" -gt 0 ]; then
cecho " Memory freed: ${GREEN}+${memory_freed}MB${NC}"
preview_total_impact=$((preview_total_impact + memory_freed))
elif [ "$memory_freed" -lt 0 ]; then
cecho " Memory allocated: ${YELLOW}$((-memory_freed))MB${NC} (growth)"
fi
echo ""
fi
done <<< "$user_domains"
done <<< "$preview_users"
if [ "$preview_count" -eq 0 ]; then
cecho "${YELLOW}No changes to preview${NC}"
echo ""
read -p "Press Enter to continue..."
return
fi
echo ""
cecho "${CYAN}═══════════════════════════════════════════════════════════════════${NC}"
cecho "${WHITE}${BOLD}PREVIEW SUMMARY${NC}"
cecho " Total changes: ${WHITE}$preview_count${NC}"
if [ "$preview_total_impact" -gt 0 ]; then
cecho " Total memory that could be freed: ${GREEN}+${preview_total_impact}MB${NC}"
fi
echo ""
cecho "${CYAN}─────────────────────────────────────────────────────────────────────${NC}"
# Ask for final confirmation before applying
if ! confirm "Apply these changes?"; then
cecho "${YELLOW}Optimization cancelled${NC}"
read -p "Press Enter to continue..."
return
fi
show_banner
cecho "${WHITE}${BOLD}Applying optimizations...${NC}"
cecho "${CYAN}Selected: ${#domains_to_apply[@]} domain(s)/user(s)${NC}"