fix charts

This commit is contained in:
Dobromir Popov
2025-12-10 14:14:17 +02:00
parent bfaba556ea
commit 83bb688158
5 changed files with 4013 additions and 9 deletions

View File

@@ -41,6 +41,9 @@ class ChartManager {
const date = new Date(timestamp);
return date.toISOString(); // Always returns UTC with Z suffix
};
// Throttle pivot recalculation to prevent excessive API calls
this.pivotRecalcThrottle = {}; // {timeframe: lastCallTime}
console.log('ChartManager initialized with timeframes:', timeframes);
}
@@ -2542,9 +2545,21 @@ class ChartManager {
async recalculatePivots(timeframe, data) {
try {
// Don't recalculate if we don't have enough data
if (data.timestamps.length < 50) return;
if (data.timestamps.length < 50) {
console.log(`[${timeframe}] Skipping pivot recalculation: only ${data.timestamps.length} candles (need 50+)`);
return;
}
console.log(` Recalculating pivots for ${timeframe} with ${data.timestamps.length} candles...`);
// Throttle pivot recalculation (max once every 5 seconds per timeframe)
const now = Date.now();
const lastCall = this.pivotRecalcThrottle[timeframe] || 0;
if (now - lastCall < 5000) {
console.log(`[${timeframe}] Throttling pivot recalculation (last call ${Math.round((now - lastCall)/1000)}s ago)`);
return;
}
this.pivotRecalcThrottle[timeframe] = now;
console.log(`[${timeframe}] Recalculating pivots with ${data.timestamps.length} candles...`);
// Optimized: Only send symbol and timeframe, backend uses its own data
const response = await fetch('/api/recalculate-pivots', {
@@ -2562,14 +2577,23 @@ class ChartManager {
// Update pivot markers in chart data
const chart = this.charts[timeframe];
if (chart && chart.data) {
const oldPivotCount = chart.data.pivot_markers ? Object.keys(chart.data.pivot_markers).length : 0;
chart.data.pivot_markers = result.pivot_markers;
console.log(` Pivots recalculated: ${Object.keys(result.pivot_markers).length} pivot candles`);
const newPivotCount = Object.keys(result.pivot_markers).length;
console.log(`[${timeframe}] Pivots updated: ${oldPivotCount}${newPivotCount} pivot candles`);
// Redraw the chart with updated pivots
this.redrawChartWithPivots(timeframe, chart.data);
// Ensure pivots are visible (in case they were toggled off)
if (this.displayToggles.pivots) {
console.log(`[${timeframe}] Ensuring pivots are visible after recalculation`);
}
} else {
console.warn(`[${timeframe}] Chart not found for pivot update`);
}
} else {
console.warn('Failed to recalculate pivots:', result.error);
console.warn(`[${timeframe}] Failed to recalculate pivots:`, result.error || 'Unknown error');
}
} catch (error) {
@@ -2582,7 +2606,12 @@ class ChartManager {
*/
redrawChartWithPivots(timeframe, data) {
const chart = this.charts[timeframe];
if (!chart) return;
if (!chart) {
console.warn(`[${timeframe}] Cannot redraw pivots: chart not found`);
return;
}
console.log(`[${timeframe}] Redrawing chart with pivots...`);
// Build pivot shapes and annotations
const shapes = [];
@@ -4365,3 +4394,34 @@ class ChartManager {
this.liveMetricsOverlay = null;
}
}
// Debug method to manually trigger pivot recalculation
debugRecalculatePivots(timeframe) {
console.log(`[DEBUG] Manually triggering pivot recalculation for ${timeframe}`);
const chart = this.charts[timeframe];
if (chart && chart.data) {
// Bypass throttling for debug
delete this.pivotRecalcThrottle[timeframe];
this.recalculatePivots(timeframe, chart.data);
} else {
console.log(`[DEBUG] No chart data available for ${timeframe}`);
}
}
// Debug method to check pivot status
debugPivotStatus() {
console.log('=== PIVOT DEBUG STATUS ===');
console.log('Display toggles:', this.displayToggles);
console.log('Pivot recalc throttle:', this.pivotRecalcThrottle);
this.timeframes.forEach(tf => {
const chart = this.charts[tf];
if (chart && chart.data) {
const pivotCount = chart.data.pivot_markers ? Object.keys(chart.data.pivot_markers).length : 0;
console.log(`${tf}: ${chart.data.timestamps.length} candles, ${pivotCount} pivot markers`);
} else {
console.log(`${tf}: No chart data`);
}
});
console.log('========================');
}