================================================================================ DATA PERSISTENCE IMPLEMENTATION CHECKLIST Issue #1: Database Storage for Crash Recovery ================================================================================ PHASE 1: DATABASE SETUP ──────────────────────────────────────────────────────────────────────────── [ ] Connect to MySQL/MariaDB database [ ] Execute create_session_state_table.sql [ ] Verify table exists: SQL: SHOW TABLES LIKE 'session_state'; [ ] Verify table structure: SQL: DESCRIBE session_state; [ ] Test database permissions (INSERT, UPDATE) PHASE 2: MODIFY MACHINE CYCLES FUNCTION ──────────────────────────────────────────────────────────────────────────── [ ] Open Node-RED editor [ ] Locate "Machine cycles" function node [ ] Double-click to edit [ ] Change "Outputs" from 2 to 3 [ ] Replace function code with contents from: modified_machine_cycles.js [ ] Click "Done" [ ] Add new MySQL node named "Session State DB" [ ] Configure MySQL node with database credentials [ ] Wire Machine Cycles output 3 → Session State DB [ ] Deploy changes PHASE 3: MODIFY WORK ORDER BUTTONS FUNCTION ──────────────────────────────────────────────────────────────────────────── [ ] Locate "Work Order buttons" function node [ ] Double-click to edit [ ] Change "Outputs" from 4 to 5 [ ] Replace function code with contents from: modified_work_order_buttons.js [ ] Click "Done" [ ] Wire Work Order Buttons output 5 → Session State DB (same node as above) [ ] Deploy changes PHASE 4: CREATE STARTUP RECOVERY FLOW ──────────────────────────────────────────────────────────────────────────── 4.1 - Startup Trigger [ ] Add Inject node [ ] Configure: - Name: "Startup Trigger" - Inject once after: 0.1 seconds - Repeat: none [ ] Click "Done" 4.2 - Query Function [ ] Add Function node [ ] Configure: - Name: "Query Previous Session" - Outputs: 1 - Code: Copy from startup_recovery_query.js [ ] Wire: Startup Trigger → Query Previous Session [ ] Click "Done" 4.3 - Database Query [ ] Wire: Query Previous Session → Session State DB 4.4 - Process Response [ ] Add Function node [ ] Configure: - Name: "Process Recovery Response" - Outputs: 2 - Code: Copy from process_recovery_response.js [ ] Wire: Session State DB → Process Recovery Response [ ] Click "Done" 4.5 - Deactivate Stale Sessions [ ] Wire: Process Recovery Response output 1 → Session State DB 4.6 - User Prompt UI [ ] Option A: Add ui_toast node - Name: "Recovery Prompt" - Position: top right - Duration: 5 seconds [ ] Option B: Add ui_template node for modal (see NODE_CONFIGURATION.md) [ ] Wire: Process Recovery Response output 2 → Prompt UI [ ] Click "Done" 4.7 - Restore Session Function [ ] Add Function node [ ] Configure: - Name: "Restore Session" - Outputs: 2 - Code: Copy from restore_session.js [ ] Wire: User "Restore" action → Restore Session [ ] Wire: Restore Session output 1 → Session State DB [ ] Wire: Restore Session output 2 → Success notification UI [ ] Click "Done" 4.8 - Start Fresh Function [ ] Add Function node [ ] Configure: - Name: "Start Fresh" - Outputs: 2 - Code: Copy from start_fresh.js [ ] Wire: User "Start Fresh" action → Start Fresh [ ] Wire: Start Fresh output 1 → Session State DB [ ] Wire: Start Fresh output 2 → Success notification UI [ ] Click "Done" 4.9 - Deploy [ ] Deploy all changes [ ] Check debug panel for errors PHASE 5: ADD DEBUG NODES (RECOMMENDED) ──────────────────────────────────────────────────────────────────────────── [ ] Add debug node after Session State DB Name: "Debug Session Sync" [ ] Add debug node after Process Recovery Response output 2 Name: "Debug Recovery Data" [ ] Add debug node after Restore Session output 2 Name: "Debug Restore Status" [ ] Deploy changes PHASE 6: TESTING ──────────────────────────────────────────────────────────────────────────── Test 1: Normal Operation [ ] Start a work order [ ] Click START button [ ] Run several machine cycles (5-10) [ ] Check debug panel for session-sync messages [ ] Query database: SQL: SELECT * FROM session_state WHERE is_active = 1; [ ] Verify data updates every ~5 seconds [ ] Verify cycle_count increments [ ] Verify operating_time increases Test 2: Crash Recovery (Simulated) [ ] Start work order and run 20+ cycles [ ] Note current cycle count [ ] Stop Node-RED: Command: sudo systemctl stop nodered [ ] Wait 10 seconds [ ] Verify session in database: SQL: SELECT * FROM session_state WHERE is_active = 1; [ ] Restart Node-RED: Command: sudo systemctl start nodered [ ] Wait for recovery prompt (should appear within 5 seconds) [ ] Verify prompt shows correct data: - Work order ID - Cycle count - Operating time - Last update time [ ] Click "Restore Session" [ ] Verify success message [ ] Check active work order in dashboard [ ] Run more cycles [ ] Verify cycle count continues from previous value Test 3: Start Fresh Option [ ] Start work order and run cycles [ ] Stop and restart Node-RED [ ] When recovery prompt appears, click "Start Fresh" [ ] Verify success message [ ] Check database - old session should be deactivated: SQL: SELECT * FROM session_state WHERE session_id = 'OLD_SESSION_ID'; (is_active should be 0) [ ] Verify all counters reset to 0 [ ] Start new work order [ ] Verify normal operation Test 4: Stale Session Detection [ ] Insert old session (25 hours ago): SQL: UPDATE session_state SET updated_at = updated_at - (25 * 60 * 60 * 1000) WHERE is_active = 1; [ ] Restart Node-RED [ ] Verify system detects stale session [ ] Verify automatic start fresh (no prompt) [ ] Check logs for stale session warning Test 5: Multiple Restarts [ ] Start work order, run cycles [ ] Restart Node-RED 3 times in a row [ ] Each time, restore the session [ ] Verify cycle count accumulates correctly [ ] Verify no data loss across restarts PHASE 7: VERIFICATION ──────────────────────────────────────────────────────────────────────────── [ ] Database table created successfully [ ] Modified functions deployed [ ] Recovery flow deployed [ ] All wiring correct [ ] All tests passed [ ] No errors in Node-RED log: Command: journalctl -u nodered -n 50 [ ] No errors in MySQL log [ ] Debug output shows expected behavior [ ] Dashboard shows recovery prompts correctly PHASE 8: CLEANUP (OPTIONAL) ──────────────────────────────────────────────────────────────────────────── [ ] Remove debug nodes (keep for troubleshooting) [ ] Add comments to function nodes [ ] Document custom UI templates [ ] Create backup of flows.json: Command: cp flows.json flows.json.backup_persistence [ ] Test backup restoration procedure PHASE 9: MONITORING SETUP ──────────────────────────────────────────────────────────────────────────── [ ] Set up database monitoring query (optional): SQL: SELECT COUNT(*) as active_sessions FROM session_state WHERE is_active = 1; [ ] Add dashboard tile showing last session sync time [ ] Configure alerts for stale sessions (optional) [ ] Document recovery procedures for operators TROUBLESHOOTING CHECKLIST ──────────────────────────────────────────────────────────────────────────── If sync not working: [ ] Check MySQL node configuration [ ] Check database user permissions [ ] Check for SQL syntax errors in debug panel [ ] Verify msg.topic contains valid SQL [ ] Check network connectivity to database If recovery not working: [ ] Check inject node fires on startup [ ] Verify session_state table has data [ ] Check SQL query in startup_recovery_query.js [ ] Verify process_recovery_response.js outputs [ ] Check UI notification configuration If data incorrect after restore: [ ] Verify JSON serialization in database [ ] Check single quote escaping in SQL [ ] Verify timestamp format (milliseconds) [ ] Check global.get/set calls in restore function PERFORMANCE VERIFICATION ──────────────────────────────────────────────────────────────────────────── [ ] Database writes ~every 5 seconds during production [ ] No lag or delays in UI [ ] Node-RED CPU usage < 50% [ ] Database connection stable [ ] No connection pool exhaustion Expected database write frequency: Normal operation: ~12 writes/minute Active production: ~12-15 writes/minute Idle (no cycles): 0 writes/minute DOCUMENTATION ──────────────────────────────────────────────────────────────────────────── [ ] Read IMPLEMENTATION_GUIDE.md [ ] Read IMPLEMENTATION_SUMMARY.md [ ] Read NODE_CONFIGURATION.md [ ] Bookmark optimization_prompt.txt [ ] Document any custom changes made [ ] Share recovery procedures with operators [ ] Train operators on recovery prompts SIGN-OFF ──────────────────────────────────────────────────────────────────────────── Implementation Date: ___________________________ Implemented By: _________________________________ Tests Passed: [ ] All [ ] Some (document failures below) Production Ready: [ ] Yes [ ] No Notes: _________________________________________________________________________ _________________________________________________________________________ _________________________________________________________________________ NEXT STEPS ──────────────────────────────────────────────────────────────────────────── After successful implementation and testing: [ ] Proceed to Issue #4: Intelligent Downtime Categorization [ ] Proceed to Issue #5: Session Management and Pattern Tracking [ ] Proceed to Issue #2: Cycle Count Capping [ ] Proceed to Issue #3: Hardware Irregularity Tracking ================================================================================ END OF CHECKLIST ================================================================================ Reference Files: Database Schema: create_session_state_table.sql Modified Functions: modified_machine_cycles.js modified_work_order_buttons.js Recovery Functions: startup_recovery_query.js process_recovery_response.js restore_session.js start_fresh.js Documentation: IMPLEMENTATION_GUIDE.md IMPLEMENTATION_SUMMARY.md NODE_CONFIGURATION.md For support, check Node-RED logs: journalctl -u nodered -f