fix: Add proper analysis timer functionality to automation status

FIXES:
- Add nextAnalysisIn and analysisInterval fields to AutomationStatus interface
- Calculate countdown timer based on nextScheduled time from database
- Update nextScheduled time in database at start of each automation cycle
- Frontend timer will now show proper countdown instead of 'Analyzing now...'

- Analysis Timer will show: '4:32' (next analysis in 4 min 32 sec)
- Progress bar will display proper countdown visualization
- Timer updates every second showing time until next analysis cycle
This commit is contained in:
mindesbunister
2025-07-24 11:54:40 +02:00
parent a5e124c556
commit 2db492dd54
2 changed files with 37 additions and 1 deletions

View File

@@ -37,6 +37,9 @@ export interface AutomationStatus {
nextScheduled?: Date
errorCount: number
lastError?: string
nextAnalysisIn?: number // Seconds until next analysis
analysisInterval?: number // Analysis interval in seconds
currentCycle?: number // Current automation cycle
}
export class AutomationService {
@@ -184,6 +187,26 @@ export class AutomationService {
try {
console.log(`🔍 Running automation cycle for ${this.config.symbol} ${this.config.timeframe}`)
// Update next scheduled time in database for timer display
const intervalMs = this.getIntervalFromTimeframe(this.config.timeframe)
const nextScheduled = new Date(Date.now() + intervalMs)
try {
await prisma.automationSession.updateMany({
where: {
userId: this.config.userId,
status: 'ACTIVE'
},
data: {
nextScheduled: nextScheduled,
lastAnalysis: new Date()
}
})
console.log(`⏰ Next analysis scheduled for: ${nextScheduled.toLocaleTimeString()}`)
} catch (dbError) {
console.error('Failed to update next scheduled time:', dbError)
}
// Step 1: Check daily trade limit
const todayTrades = await this.getTodayTradeCount(this.config.userId)
if (todayTrades >= this.config.maxDailyTrades) {
@@ -1133,6 +1156,16 @@ ${validResults.map(r => `• ${r.timeframe}: ${r.analysis?.recommendation} (${r.
await this.autoRestartFromSession(session)
}
// Calculate next analysis timing
const analysisInterval = 300 // 5 minutes in seconds
let nextAnalysisIn = 0
if (this.isRunning && session.nextScheduled) {
const nextScheduledTime = new Date(session.nextScheduled).getTime()
const currentTime = Date.now()
nextAnalysisIn = Math.max(0, Math.floor((nextScheduledTime - currentTime) / 1000))
}
return {
isActive: this.isRunning && this.config !== null,
mode: session.mode as 'SIMULATION' | 'LIVE',
@@ -1146,7 +1179,10 @@ ${validResults.map(r => `• ${r.timeframe}: ${r.analysis?.recommendation} (${r.
lastError: session.lastError || undefined,
lastAnalysis: session.lastAnalysis || undefined,
lastTrade: session.lastTrade || undefined,
nextScheduled: session.nextScheduled || undefined
nextScheduled: session.nextScheduled || undefined,
nextAnalysisIn: nextAnalysisIn,
analysisInterval: analysisInterval,
currentCycle: session.totalTrades || 0
}
} catch (error) {
console.error('Failed to get automation status:', error)

Binary file not shown.