Improve AI validate revert visuals, fix some regressions
This commit is contained in:
@@ -111,23 +111,44 @@ router.post("/debug", async (req, res) => {
|
||||
}
|
||||
|
||||
try {
|
||||
const avgTimeResults = await pool.query(
|
||||
`SELECT AVG(duration_seconds) as avg_duration,
|
||||
COUNT(*) as sample_count
|
||||
FROM ai_validation_performance
|
||||
WHERE prompt_length BETWEEN $1 * 0.8 AND $1 * 1.2`,
|
||||
[debugResponse.promptLength]
|
||||
// Instead of looking for similar prompt lengths, calculate an average processing rate
|
||||
const rateResults = await pool.query(
|
||||
`SELECT
|
||||
AVG(duration_seconds / prompt_length) as avg_rate_per_char,
|
||||
COUNT(*) as sample_count,
|
||||
AVG(duration_seconds) as avg_duration
|
||||
FROM ai_validation_performance`
|
||||
);
|
||||
|
||||
// Add estimated time to the response
|
||||
if (avgTimeResults.rows && avgTimeResults.rows[0]) {
|
||||
if (rateResults.rows && rateResults.rows[0] && rateResults.rows[0].avg_rate_per_char) {
|
||||
// Calculate estimated time based on the rate and current prompt length
|
||||
const rate = rateResults.rows[0].avg_rate_per_char;
|
||||
const estimatedSeconds = Math.max(15, Math.round(rate * debugResponse.promptLength));
|
||||
|
||||
debugResponse.estimatedProcessingTime = {
|
||||
seconds: avgTimeResults.rows[0].avg_duration || null,
|
||||
sampleCount: avgTimeResults.rows[0].sample_count || 0
|
||||
seconds: estimatedSeconds,
|
||||
sampleCount: rateResults.rows[0].sample_count || 0,
|
||||
avgRate: rate,
|
||||
calculationMethod: "rate-based"
|
||||
};
|
||||
console.log("📊 Retrieved processing time estimate:", debugResponse.estimatedProcessingTime);
|
||||
console.log("📊 Calculated time estimate using rate-based method:", {
|
||||
rate: rate,
|
||||
promptLength: debugResponse.promptLength,
|
||||
estimatedSeconds: estimatedSeconds,
|
||||
sampleCount: rateResults.rows[0].sample_count
|
||||
});
|
||||
} else {
|
||||
console.log("📊 No processing time estimates available for prompt length:", debugResponse.promptLength);
|
||||
// Fallback: Calculate a simple estimate based on prompt length (1 second per 1000 characters)
|
||||
const estimatedSeconds = Math.max(15, Math.round(debugResponse.promptLength / 1000));
|
||||
console.log("📊 No rate data available, using fallback calculation");
|
||||
debugResponse.estimatedProcessingTime = {
|
||||
seconds: estimatedSeconds,
|
||||
sampleCount: 0,
|
||||
isEstimate: true,
|
||||
calculationMethod: "fallback"
|
||||
};
|
||||
console.log("📊 Fallback time estimate:", debugResponse.estimatedProcessingTime);
|
||||
}
|
||||
} catch (queryError) {
|
||||
console.error("⚠️ Failed to query performance metrics:", queryError);
|
||||
@@ -812,29 +833,38 @@ router.post("/validate", async (req, res) => {
|
||||
// Insert performance data into the local PostgreSQL database
|
||||
await pool.query(
|
||||
`INSERT INTO ai_validation_performance
|
||||
(prompt_length, product_count, start_time, end_time)
|
||||
VALUES ($1, $2, $3, $4)`,
|
||||
[promptLength, products.length, startTime, endTime]
|
||||
(prompt_length, product_count, start_time, end_time, duration_seconds)
|
||||
VALUES ($1, $2, $3, $4, $5)`,
|
||||
[
|
||||
promptLength,
|
||||
products.length,
|
||||
startTime,
|
||||
endTime,
|
||||
(endTime - startTime) / 1000
|
||||
]
|
||||
);
|
||||
|
||||
console.log("📊 Performance metrics inserted into database");
|
||||
|
||||
// Query for average processing time based on similar prompt lengths
|
||||
try {
|
||||
const avgTimeResults = await pool.query(
|
||||
`SELECT AVG(duration_seconds) as avg_duration,
|
||||
COUNT(*) as sample_count
|
||||
FROM ai_validation_performance
|
||||
WHERE prompt_length BETWEEN $1 * 0.8 AND $1 * 1.2`,
|
||||
[promptLength]
|
||||
const rateResults = await pool.query(
|
||||
`SELECT
|
||||
AVG(duration_seconds / prompt_length) as avg_rate_per_char,
|
||||
COUNT(*) as sample_count,
|
||||
AVG(duration_seconds) as avg_duration
|
||||
FROM ai_validation_performance`
|
||||
);
|
||||
|
||||
if (avgTimeResults.rows && avgTimeResults.rows[0]) {
|
||||
performanceMetrics.avgDuration = avgTimeResults.rows[0].avg_duration;
|
||||
performanceMetrics.sampleCount = avgTimeResults.rows[0].sample_count;
|
||||
if (rateResults.rows && rateResults.rows[0] && rateResults.rows[0].avg_rate_per_char) {
|
||||
const rate = rateResults.rows[0].avg_rate_per_char;
|
||||
performanceMetrics.avgRate = rate;
|
||||
performanceMetrics.estimatedSeconds = Math.round(rate * promptLength);
|
||||
performanceMetrics.sampleCount = rateResults.rows[0].sample_count;
|
||||
performanceMetrics.calculationMethod = "rate-based";
|
||||
}
|
||||
|
||||
console.log("📊 Performance metrics retrieved:", performanceMetrics);
|
||||
console.log("📊 Performance metrics with rate calculation:", performanceMetrics);
|
||||
} catch (queryError) {
|
||||
console.error("⚠️ Failed to query performance metrics:", queryError);
|
||||
}
|
||||
@@ -854,7 +884,13 @@ router.post("/validate", async (req, res) => {
|
||||
res.json({
|
||||
success: true,
|
||||
changeDetails: changeDetails,
|
||||
performanceMetrics,
|
||||
performanceMetrics: performanceMetrics || {
|
||||
// Fallback: calculate a simple estimate
|
||||
promptLength: promptLength,
|
||||
processingTimeSeconds: Math.max(15, Math.round(promptLength / 1000)),
|
||||
isEstimate: true,
|
||||
productCount: products.length
|
||||
},
|
||||
...aiResponse,
|
||||
});
|
||||
} catch (parseError) {
|
||||
|
||||
Reference in New Issue
Block a user