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