fix: use correct ai_learning_data table for persistent AI decisions storage
This commit is contained in:
@@ -6,10 +6,10 @@ const prisma = new PrismaClient()
|
||||
export async function GET() {
|
||||
try {
|
||||
// Get recent AI decisions from database (last 10)
|
||||
const decisions = await prisma.ai_analysis_sessions.findMany({
|
||||
const decisions = await prisma.ai_learning_data.findMany({
|
||||
where: {
|
||||
// Get sessions with actual analysis data
|
||||
lastAnalysisData: {
|
||||
// Get learning data with actual analysis
|
||||
analysisData: {
|
||||
not: null
|
||||
}
|
||||
},
|
||||
@@ -20,35 +20,41 @@ export async function GET() {
|
||||
select: {
|
||||
id: true,
|
||||
symbol: true,
|
||||
confidence: true,
|
||||
lastAnalysisData: true,
|
||||
confidenceScore: true,
|
||||
analysisData: true,
|
||||
createdAt: true,
|
||||
sessionId: true
|
||||
sessionId: true,
|
||||
tradeId: true
|
||||
}
|
||||
})
|
||||
|
||||
// Transform database records to match expected format
|
||||
const liveDecisions = decisions.map(session => {
|
||||
const liveDecisions = decisions.map(record => {
|
||||
let analysisData = {}
|
||||
try {
|
||||
analysisData = JSON.parse(session.lastAnalysisData || '{}')
|
||||
analysisData = typeof record.analysisData === 'string'
|
||||
? JSON.parse(record.analysisData)
|
||||
: record.analysisData || {}
|
||||
} catch (e) {
|
||||
analysisData = {}
|
||||
}
|
||||
|
||||
return {
|
||||
type: 'AI_DECISION',
|
||||
action: analysisData.recommendation?.toUpperCase() || 'HOLD',
|
||||
symbol: session.symbol,
|
||||
blocked: !(analysisData.recommendation?.toLowerCase().includes('buy') || analysisData.recommendation?.toLowerCase().includes('sell')),
|
||||
action: analysisData.recommendation?.toUpperCase() || analysisData.decision?.toUpperCase() || 'HOLD',
|
||||
symbol: record.symbol,
|
||||
blocked: !(analysisData.recommendation?.toLowerCase().includes('buy') ||
|
||||
analysisData.recommendation?.toLowerCase().includes('sell') ||
|
||||
analysisData.decision?.toLowerCase().includes('execute')),
|
||||
executed: false, // Always false for analysis decisions
|
||||
confidence: session.confidence || analysisData.confidence || 0,
|
||||
entryPrice: analysisData.entry?.price || analysisData.currentPrice || 0,
|
||||
stopLoss: analysisData.stopLoss?.price || analysisData.stopLoss || null,
|
||||
takeProfit: analysisData.takeProfits?.tp1?.price || analysisData.takeProfit || null,
|
||||
confidence: record.confidenceScore || analysisData.confidence || 0,
|
||||
entryPrice: analysisData.aiLevels?.entry || analysisData.entry?.price || analysisData.currentPrice || 0,
|
||||
stopLoss: analysisData.aiLevels?.stopLoss || analysisData.stopLoss?.price || analysisData.stopLoss || null,
|
||||
takeProfit: analysisData.aiLevels?.takeProfit || analysisData.takeProfits?.tp1?.price || analysisData.takeProfit || null,
|
||||
reasoning: analysisData.reasoning || analysisData.summary || 'AI market analysis',
|
||||
timestamp: session.createdAt.toISOString(),
|
||||
sessionId: session.sessionId
|
||||
timestamp: record.createdAt.toISOString(),
|
||||
sessionId: record.sessionId,
|
||||
tradeId: record.tradeId
|
||||
}
|
||||
})
|
||||
|
||||
@@ -73,21 +79,29 @@ export async function POST(request) {
|
||||
try {
|
||||
const data = await request.json()
|
||||
|
||||
// Store AI decision in database for persistence
|
||||
await prisma.ai_analysis_sessions.create({
|
||||
// Store AI decision in database for persistence using ai_learning_data table
|
||||
await prisma.ai_learning_data.create({
|
||||
data: {
|
||||
id: `live_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
||||
userId: 'system', // Use system user for automated decisions
|
||||
symbol: data.symbol || 'SOLUSD',
|
||||
confidence: data.confidence || 0,
|
||||
lastAnalysisData: JSON.stringify(data),
|
||||
confidenceScore: data.confidence || 0,
|
||||
analysisData: data, // Store full decision data as JSON
|
||||
marketConditions: {
|
||||
timeframes: data.timeframes || ['1h', '4h'],
|
||||
strategy: 'automation',
|
||||
timestamp: new Date().toISOString()
|
||||
},
|
||||
sessionId: data.sessionId || `session_${Date.now()}`,
|
||||
tradeId: data.learningDecisionId || data.tradeId || `trade_${Date.now()}`,
|
||||
timeframe: (data.timeframes && data.timeframes[0]) || '1h',
|
||||
createdAt: new Date()
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Decision stored persistently',
|
||||
message: 'Decision stored persistently in database',
|
||||
timestamp: new Date().toISOString()
|
||||
})
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user