Fix SLA queries to use parsed daysNum instead of string days

This commit is contained in:
nmemmert committed 2025-12-15 12:45:24 -05:00
1 parent ecfe725f6f
commit c8e8d54aa8
1 file changed
+9 -4
+9 -4
View File
@@ -550,14 +550,18 @@ app.get('/api/sla', (req, res) => {
const currentPage = Math.max(1, parseInt(page)); const currentPage = Math.max(1, parseInt(page));
const offset = (currentPage - 1) * pageLimit; const offset = (currentPage - 1) * pageLimit;
// Create cache key // Create cache key - ensure days is treated as a number
const cacheKey = `sla:days=${days}:page=${currentPage}:limit=${pageLimit}`; const daysNum = parseInt(days);
const cacheKey = `sla:days=${daysNum}:page=${currentPage}:limit=${pageLimit}`;
console.log('SLA request:', { days: daysNum, page: currentPage, limit: pageLimit, cacheKey });
// Check cache first // Check cache first
const cachedResult = cache.get(cacheKey); const cachedResult = cache.get(cacheKey);
if (cachedResult) { if (cachedResult) {
console.log('SLA: Cache hit');
return res.json(cachedResult); return res.json(cachedResult);
} }
console.log('SLA: Cache miss, querying database');
// Get total count // Get total count
const totalCount = db.prepare('SELECT COUNT(*) as count FROM resources WHERE enabled = 1').get(); const totalCount = db.prepare('SELECT COUNT(*) as count FROM resources WHERE enabled = 1').get();
@@ -572,7 +576,7 @@ app.get('/api/sla', (req, res) => {
SELECT status SELECT status
FROM checks FROM checks
WHERE resource_id = ? AND checked_at > datetime('now', ?) WHERE resource_id = ? AND checked_at > datetime('now', ?)
`).all(resource.id, `-${days} days`); `).all(resource.id, `-${daysNum} days`);
const upCount = checks.filter(c => c.status === 'up').length; const upCount = checks.filter(c => c.status === 'up').length;
const actualUptime = checks.length > 0 ? (upCount / checks.length * 100) : 0; const actualUptime = checks.length > 0 ? (upCount / checks.length * 100) : 0;
@@ -584,7 +588,8 @@ app.get('/api/sla', (req, res) => {
SUM(julianday(COALESCE(resolved_at, datetime('now'))) - julianday(started_at)) * 24 * 60 as downtime_minutes SUM(julianday(COALESCE(resolved_at, datetime('now'))) - julianday(started_at)) * 24 * 60 as downtime_minutes
FROM incidents FROM incidents
WHERE resource_id = ? AND started_at > datetime('now', ?) WHERE resource_id = ? AND started_at > datetime('now', ?)
`).get(resource.id, `-${days} days`); `).get(resource.id, `-${daysNum} days`);
console.log(`SLA ${resource.name}: ${checks.length} checks in ${daysNum} days`);
return { return {
resource_id: resource.id, resource_id: resource.id,