fix: Update request logging to use request_tracking table instead of dashboard_request_log

This commit is contained in:
Rene Fichtmueller 2026-04-26 00:42:58 +02:00
parent 2814fb50b9
commit ff090de82b

View File

@ -8,7 +8,7 @@ export class RequestLogger {
constructor(private db: Pool) {} constructor(private db: Pool) {}
/** /**
* Log a completion request to dashboard_request_log table * Log a completion request to request_tracking table
* Also emits event for real-time SSE subscribers * Also emits event for real-time SSE subscribers
*/ */
async logRequest( async logRequest(
@ -32,9 +32,9 @@ export class RequestLogger {
// Write to database // Write to database
await this.db.query( await this.db.query(
` `
INSERT INTO dashboard_request_log ( INSERT INTO request_tracking (
request_id, request_id,
caller, caller_id,
task_type, task_type,
model, model,
status, status,
@ -45,9 +45,8 @@ export class RequestLogger {
latency_ms, latency_ms,
fallback_used, fallback_used,
error_message, error_message,
created_at, created_at
created_at_epoch ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
`, `,
[ [
requestId, requestId,
@ -62,8 +61,7 @@ export class RequestLogger {
latencyMs, latencyMs,
fallbackUsed || false, fallbackUsed || false,
errorMessage || null, errorMessage || null,
now, now
epochSeconds
] ]
); );
@ -92,7 +90,7 @@ export class RequestLogger {
} }
/** /**
* Get recent requests from dashboard_request_log * Get recent requests from request_tracking
* Used by /api/dashboard/requests endpoint * Used by /api/dashboard/requests endpoint
*/ */
async getRecentRequests( async getRecentRequests(
@ -119,7 +117,7 @@ export class RequestLogger {
` `
SELECT SELECT
request_id, request_id,
caller, caller_id as caller,
task_type, task_type,
model, model,
status, status,
@ -131,8 +129,8 @@ export class RequestLogger {
fallback_used, fallback_used,
error_message, error_message,
created_at created_at
FROM dashboard_request_log FROM request_tracking
WHERE created_at > NOW() - INTERVAL $1 HOUR WHERE created_at > NOW() - MAKE_INTERVAL(hours => $1)
ORDER BY created_at DESC ORDER BY created_at DESC
LIMIT $2 LIMIT $2
`, `,
@ -184,18 +182,18 @@ export class RequestLogger {
SUM(CASE WHEN status = 'approved' THEN 1 ELSE 0 END)::FLOAT / COUNT(*) as success_rate, SUM(CASE WHEN status = 'approved' THEN 1 ELSE 0 END)::FLOAT / COUNT(*) as success_rate,
AVG(confidence_score) as avg_confidence, AVG(confidence_score) as avg_confidence,
SUM(CASE WHEN fallback_used = true THEN 1 ELSE 0 END)::FLOAT / COUNT(*) as fallback_percentage SUM(CASE WHEN fallback_used = true THEN 1 ELSE 0 END)::FLOAT / COUNT(*) as fallback_percentage
FROM dashboard_request_log FROM request_tracking
WHERE created_at > NOW() - INTERVAL $1 MINUTE WHERE created_at > NOW() - MAKE_INTERVAL(mins => $1)
`, `,
[bucketMinutes] [bucketMinutes]
); );
const topCallersResult = await this.db.query( const topCallersResult = await this.db.query(
` `
SELECT caller, COUNT(*) as count SELECT caller_id as caller, COUNT(*) as count
FROM dashboard_request_log FROM request_tracking
WHERE created_at > NOW() - INTERVAL $1 MINUTE WHERE created_at > NOW() - MAKE_INTERVAL(mins => $1)
GROUP BY caller GROUP BY caller_id
ORDER BY count DESC ORDER BY count DESC
LIMIT 5 LIMIT 5
`, `,
@ -205,8 +203,8 @@ export class RequestLogger {
const topModelsResult = await this.db.query( const topModelsResult = await this.db.query(
` `
SELECT model, COUNT(*) as count SELECT model, COUNT(*) as count
FROM dashboard_request_log FROM request_tracking
WHERE created_at > NOW() - INTERVAL $1 MINUTE WHERE created_at > NOW() - MAKE_INTERVAL(mins => $1)
GROUP BY model GROUP BY model
ORDER BY count DESC ORDER BY count DESC
LIMIT 5 LIMIT 5
@ -216,10 +214,10 @@ export class RequestLogger {
const recentErrorsResult = await this.db.query( const recentErrorsResult = await this.db.query(
` `
SELECT request_id, caller, error_message, created_at SELECT request_id, caller_id as caller, error_message, created_at
FROM dashboard_request_log FROM request_tracking
WHERE status IN ('rejected', 'error') WHERE status IN ('rejected', 'error')
AND created_at > NOW() - INTERVAL $1 MINUTE AND created_at > NOW() - ($1 * INTERVAL '1 minute')
ORDER BY created_at DESC ORDER BY created_at DESC
LIMIT 10 LIMIT 10
`, `,