82 lines
2.3 KiB
Python
82 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
import json
|
|
|
|
with open('/home/mdares/.node-red/flows.json', 'r') as f:
|
|
flows = json.load(f)
|
|
|
|
# New Fetch Graph Data function that gets KPI history from global context
|
|
new_fetch_func = '''// Fetch KPI History from global context
|
|
|
|
if (msg.topic !== 'fetch-graph-data' && msg.action !== 'fetch-graph-data') {
|
|
return null;
|
|
}
|
|
|
|
// Get KPI history arrays from global context
|
|
const oeeHist = global.get("realOEE") || [];
|
|
const availHist = global.get("realAvailability") || [];
|
|
const perfHist = global.get("realPerformance") || [];
|
|
const qualHist = global.get("realQuality") || [];
|
|
|
|
node.warn(`[FETCH GRAPH] Retrieved KPI history: ${oeeHist.length} data points`);
|
|
|
|
// Filter by range if specified
|
|
const range = msg.payload?.range || '24h';
|
|
let cutoffTime;
|
|
const now = Date.now();
|
|
|
|
switch(range) {
|
|
case '1h':
|
|
cutoffTime = now - (1 * 60 * 60 * 1000);
|
|
break;
|
|
case '24h':
|
|
cutoffTime = now - (24 * 60 * 60 * 1000);
|
|
break;
|
|
case '7d':
|
|
cutoffTime = now - (7 * 24 * 60 * 60 * 1000);
|
|
break;
|
|
case '30d':
|
|
cutoffTime = now - (30 * 24 * 60 * 60 * 1000);
|
|
break;
|
|
case '90d':
|
|
cutoffTime = now - (90 * 24 * 60 * 60 * 1000);
|
|
break;
|
|
case 'all':
|
|
cutoffTime = 0; // Show all data
|
|
break;
|
|
default:
|
|
cutoffTime = now - (24 * 60 * 60 * 1000); // Default to 24h
|
|
}
|
|
|
|
// Filter arrays by cutoff time
|
|
const filterByTime = (arr) => arr.filter(point => point.timestamp >= cutoffTime);
|
|
|
|
const filteredOEE = filterByTime(oeeHist);
|
|
const filteredAvail = filterByTime(availHist);
|
|
const filteredPerf = filterByTime(perfHist);
|
|
const filteredQual = filterByTime(qualHist);
|
|
|
|
node.warn(`[FETCH GRAPH] After ${range} filter: ${filteredOEE.length} points`);
|
|
|
|
// Send to Format Graph Data
|
|
msg.topic = "kpiHistory";
|
|
msg.payload = {
|
|
oee: filteredOEE,
|
|
availability: filteredAvail,
|
|
performance: filteredPerf,
|
|
quality: filteredQual
|
|
};
|
|
|
|
return msg;'''
|
|
|
|
# Update Fetch Graph Data function
|
|
for node in flows:
|
|
if node.get('id') == 'fetch_graph_data_node_id':
|
|
node['func'] = new_fetch_func
|
|
print("✓ Updated Fetch Graph Data to use KPI history from global context")
|
|
break
|
|
|
|
with open('/home/mdares/.node-red/flows.json', 'w') as f:
|
|
json.dump(flows, f, indent=4)
|
|
|
|
print("✓ flows.json updated")
|