fix: replace in-memory storage with database persistence for AI decisions
This commit is contained in:
@@ -1,12 +1,57 @@
|
|||||||
import { NextResponse } from 'next/server'
|
import { NextResponse } from 'next/server'
|
||||||
|
import { PrismaClient } from '@prisma/client'
|
||||||
|
|
||||||
// In-memory store for live trading decisions and risk management blocks
|
const prisma = new PrismaClient()
|
||||||
let liveDecisions = []
|
|
||||||
let maxDecisions = 10 // Keep last 10 decisions
|
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
try {
|
||||||
// Return the most recent decisions with full context
|
// Get recent AI decisions from database (last 10)
|
||||||
|
const decisions = await prisma.ai_analysis_sessions.findMany({
|
||||||
|
where: {
|
||||||
|
// Get sessions with actual analysis data
|
||||||
|
lastAnalysisData: {
|
||||||
|
not: null
|
||||||
|
}
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
createdAt: 'desc'
|
||||||
|
},
|
||||||
|
take: 10,
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
symbol: true,
|
||||||
|
confidence: true,
|
||||||
|
lastAnalysisData: true,
|
||||||
|
createdAt: true,
|
||||||
|
sessionId: true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Transform database records to match expected format
|
||||||
|
const liveDecisions = decisions.map(session => {
|
||||||
|
let analysisData = {}
|
||||||
|
try {
|
||||||
|
analysisData = JSON.parse(session.lastAnalysisData || '{}')
|
||||||
|
} 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')),
|
||||||
|
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,
|
||||||
|
reasoning: analysisData.reasoning || analysisData.summary || 'AI market analysis',
|
||||||
|
timestamp: session.createdAt.toISOString(),
|
||||||
|
sessionId: session.sessionId
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
const response = {
|
const response = {
|
||||||
success: true,
|
success: true,
|
||||||
decisions: liveDecisions,
|
decisions: liveDecisions,
|
||||||
@@ -26,31 +71,27 @@ export async function GET() {
|
|||||||
|
|
||||||
export async function POST(request) {
|
export async function POST(request) {
|
||||||
try {
|
try {
|
||||||
const decision = await request.json()
|
const data = await request.json()
|
||||||
|
|
||||||
// Add timestamp if not provided
|
// Store AI decision in database for persistence
|
||||||
if (!decision.timestamp) {
|
await prisma.ai_analysis_sessions.create({
|
||||||
decision.timestamp = new Date().toISOString()
|
data: {
|
||||||
}
|
id: `live_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
||||||
|
symbol: data.symbol || 'SOLUSD',
|
||||||
// Add to the beginning of the array (most recent first)
|
confidence: data.confidence || 0,
|
||||||
liveDecisions.unshift(decision)
|
lastAnalysisData: JSON.stringify(data),
|
||||||
|
sessionId: data.sessionId || `session_${Date.now()}`,
|
||||||
// Keep only the last maxDecisions
|
createdAt: new Date()
|
||||||
if (liveDecisions.length > maxDecisions) {
|
}
|
||||||
liveDecisions = liveDecisions.slice(0, maxDecisions)
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('📊 Live decision recorded:', {
|
|
||||||
type: decision.type,
|
|
||||||
action: decision.action,
|
|
||||||
blocked: decision.blocked,
|
|
||||||
confidence: decision.confidence
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return NextResponse.json({ success: true, total: liveDecisions.length })
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
message: 'Decision stored persistently',
|
||||||
|
timestamp: new Date().toISOString()
|
||||||
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ Live decisions POST error:', error)
|
console.error('❌ Error storing live decision:', error)
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ success: false, error: error.message },
|
{ success: false, error: error.message },
|
||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user