From 2db492dd54c4e9444854d27e7ab7153798bf87e3 Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Thu, 24 Jul 2025 11:54:40 +0200 Subject: [PATCH] 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 --- lib/automation-service-simple.ts | 38 ++++++++++++++++++++++++++++++- prisma/prisma/dev.db | Bin 987136 -> 1003520 bytes 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/automation-service-simple.ts b/lib/automation-service-simple.ts index 7c46269..cc4146b 100644 --- a/lib/automation-service-simple.ts +++ b/lib/automation-service-simple.ts @@ -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) { @@ -1132,6 +1155,16 @@ ${validResults.map(r => `• ${r.timeframe}: ${r.analysis?.recommendation} (${r. console.log('🔄 Found active session but automation not running, attempting auto-restart...') 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, @@ -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) diff --git a/prisma/prisma/dev.db b/prisma/prisma/dev.db index 94c9b8a8919e442aeb72f69846a563ef01cb2f76..6f5c64cf94824eab94b82ed8352d9cea24ed9e96 100644 GIT binary patch delta 2460 zcmai#3v5&8702(r_I0lB^}UWE0aEgCLqfz#o$J@Nouo@b;t(YvPdhJBnwMicj^hV* zTxabPr<<}CvL+SFp@6_^i#k=B2(*jESOb&BRIP(%B^g`Lpp2^9#0p`ho9L+6XXl}9 zM4P2^B>V6Co$vdf-*@gvv}h#y%*0kQ6T`4CCzZu`W~AdM9r(%T6m(P~e_ghjE25+9 zD5GE$?<)8vGUl}58^xy4Q6-gLoST-Krl3|nI3C$XVi*;1k|fsc?r|7ggCRZ7^SvG? zKcwq6svUmY^YB9(o(X||JQtWgJO#e(!Z(Qv7x+VPuOIi)h4bMcei(iKK`h#bUnZ(I zKUSm;ix{Y_kNu2llN7dEI%x8mP-$thhVd7uE6hP;81`jMHi93=DdsqHn0bvkh%C}mu|KupC-E&vz8|rm z{6(Y*<-bK5QN9;xh`n_H@5rI^7!M}DCBG6#UG}woB?}+c- z8+1BM{f@!BvGH>_3Ic@)g%|~iLV_Y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