# Implementation Complete ## Changes Applied ### ✅ Code Updates (Automated) 1. **Graphs Template** - Updated with: - `msg.ui_control.tab` detection (not `msg.tab`) - 300ms debounce to prevent reconnect floods - Chart.js instance cleanup (`scope._charts` with `.destroy()`) - Filter buttons trigger data reload - No `_graphsBooted` flag - reloads every time 2. **Machine Cycles** - Updated with: - 4 outputs (was 3) - Fixed `moldActive.cavities` object access - Guarded `produced = Math.max(0, totalProduced - scrapTotal)` - 4th output persists `cycle_count` to database - Uses `msg.payload` for MariaDB parameters 3. **Work Order Buttons** - Updated with: - KPI timestamp initialization on START - Consistent `work_order_id` in all SQL (not `id`) - All timestamps set to `Date.now()` on start 4. **New Nodes Created**: - `Fetch Graph Data` - Converts range to SQL INTERVAL - `Format Graph Data` - Formats DB results for Chart.js + cleans msg - `DB Guard (Graphs)` - Switch node filters SQL strings - `DB Guard (Cycles)` - Switch node filters SQL strings ### ⚠️ Manual Steps Required #### 1. Wire Nodes in Node-RED UI Open Node-RED editor and create these connections: ``` Graphs Template output ↓ Fetch Graph Data input ↓ DB Guard (Graphs) input ↓ mariaDB input ↓ Format Graph Data input ↓ Graphs Template input (closes the loop) ``` ``` Machine Cycles output 4 (new!) ↓ DB Guard (Cycles) input ↓ mariaDB input ``` #### 2. Run Database Migration Execute this SQL on your MariaDB database: ```sql ALTER TABLE work_orders ADD COLUMN cycle_count INT DEFAULT 0; CREATE INDEX idx_work_orders_updated_at ON work_orders(updated_at); ``` **How to run:** - Option A: Node-RED Inject node → function with SQL → mariaDB - Option B: Direct MariaDB command line - Option C: phpMyAdmin or similar tool --- ## Key Fixes Applied | Issue | Fix | |-------|-----| | moldActive.cavities returning NaN | Changed to `Number((global.get("moldActive") || {}).cavities) \|\| 1` | | Negative produced values | Added `Math.max(0, totalProduced - scrapTotal)` | | Inconsistent column names | Using `work_order_id` everywhere (not `id`) | | MariaDB parameter syntax | Using `msg.payload` array (not `msg.params`) | | "Query not defined" errors | Added Switch nodes with `typeof msg.topic === 'string'` guards | | Graphs don't auto-reload | Added `msg.ui_control.tab` watcher with debounce | | Filter buttons don't reload | `selectRange()` sends `fetch-graph-data` message | | Chart stacking on reload | `scope._charts[x].destroy()` before creating new charts | | SQL loops to UI | `delete msg.topic; delete msg.payload;` in Format node | --- ## Testing Checklist After wiring and database migration: - [ ] Navigate to Graphs tab → should auto-load charts - [ ] Click filter button (24h → 7d) → should reload charts - [ ] Refresh browser → Graphs tab should still auto-load - [ ] Start work order → produce parts → check cycle_count in DB - [ ] Check Node-RED debug for "query is not defined" errors (should be gone) - [ ] Verify KPIs show values on first START (not 0) - [ ] Produce parts → verify graphs update with real data (not mock) --- ## Rollback If issues occur: ```bash cd /home/mdares/.node-red/projects/Plastico cp flows.json flows.json.broken cp flows.json.backup-$(ls -t flows.json.backup-* | head -1 | cut -d'-' -f2-) flows.json ``` Then restart Node-RED. --- ## What's NOT Implemented Yet These were in the original plan but require additional UI work: 1. **Work Order Resume Prompt** - User confirmation to continue vs restart - Needs modal dialog in Home Template - Needs `case "continue-work-order"` and `case "restart-work-order"` handlers - Requires checking DB status before starting This can be added as a follow-up if needed. --- ## Files Modified - `flows.json` - Main Node-RED flow configuration - `flows.json.backup-*` - Automatic backup created before changes ## Files Created - `/tmp/update_flows_corrected.py` - Update script with all recommendations - `IMPLEMENTATION_COMPLETE.md` - This file --- Generated: $(date)