Fix database persistence and add analytics

- Fixed Prisma client not being available in Docker container
- Added isTestTrade flag to exclude test trades from analytics
- Created analytics views for net positions (matches Drift UI netting)
- Added API endpoints: /api/analytics/positions and /api/analytics/stats
- Added test trade endpoint: /api/trading/test-db
- Updated Dockerfile to properly copy Prisma client from builder stage
- Database now successfully stores all trades with full details
- Supports position netting calculations to match Drift perpetuals behavior
This commit is contained in:
mindesbunister
2025-10-27 09:35:01 +01:00
parent 1da5db5e75
commit 8e5c592cac
9 changed files with 589 additions and 8 deletions

View File

@@ -0,0 +1,40 @@
/**
* Position Analytics API
*
* Shows net positions (what Drift displays) vs individual trades (what DB stores)
* GET /api/analytics/positions
*/
import { NextResponse } from 'next/server'
import { getPositionSummary, getNetPositions, getTradesWithNetContext } from '@/lib/database/views'
export async function GET() {
try {
const [summary, netPositions, tradesWithContext] = await Promise.all([
getPositionSummary(),
getNetPositions(),
getTradesWithNetContext(),
])
return NextResponse.json({
success: true,
summary,
netPositions,
individualTrades: tradesWithContext,
explanation: {
drift: 'Drift UI shows NET positions - opposite directions in same market cancel out',
database: 'Database stores individual trades to track entry/exit of each position',
example: 'If you have 0.3 SOL LONG and 0.5 SOL SHORT, Drift shows 0.2 SOL SHORT (net)',
},
})
} catch (error) {
console.error('❌ Error getting position analytics:', error)
return NextResponse.json(
{
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
},
{ status: 500 }
)
}
}

View File

@@ -0,0 +1,32 @@
/**
* Trading Statistics API
*
* Performance analytics excluding test trades
* GET /api/analytics/stats?days=30
*/
import { NextRequest, NextResponse } from 'next/server'
import { getTradingStats } from '@/lib/database/views'
export async function GET(request: NextRequest) {
try {
const searchParams = request.nextUrl.searchParams
const days = parseInt(searchParams.get('days') || '30')
const stats = await getTradingStats(days)
return NextResponse.json({
success: true,
stats,
})
} catch (error) {
console.error('❌ Error getting trading stats:', error)
return NextResponse.json(
{
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
},
{ status: 500 }
)
}
}