File size: 1,619 Bytes
3a93fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

const savePrediction = async (sessionId, text, result) => {
  try {
    return await prisma.prediction.create({
      data: {
        sessionId: sessionId || 'anonymous',
        text: text,
        lrResult: JSON.stringify(result.models.lr || {}),
        lstmResult: JSON.stringify(result.models.lstm || {}),
        bertResult: JSON.stringify(result.models.bert || {}),
        majorityLabel: result.overall.label,
        agreement: result.overall.agreement,
        totalLatencyMs: result.timing.total_ms
      }
    });
  } catch (error) {
    console.error('Failed to save prediction to DB:', error);
    // Non-blocking error - we don't want to fail the request if DB fails
    return null;
  }
};

const getSessionHistory = async (sessionId) => {
  try {
    const history = await prisma.prediction.findMany({
      where: { sessionId },
      orderBy: { createdAt: 'desc' },
      take: 20 // Return last 20 predictions
    });
    return history.map(item => ({
      ...item,
      lrResult: JSON.parse(item.lrResult),
      lstmResult: JSON.parse(item.lstmResult),
      bertResult: JSON.parse(item.bertResult)
    }));
  } catch (error) {
    console.error('Failed to fetch history from DB:', error);
    return [];
  }
};

const getModelMetrics = async () => {
  try {
    return await prisma.modelMetric.findMany();
  } catch (error) {
    console.error('Failed to fetch model metrics from DB:', error);
    return [];
  }
};

module.exports = {
  prisma,
  savePrediction,
  getSessionHistory,
  getModelMetrics
};