const { Pool } = require('pg'); const logger = require('./logger'); /** * Health Monitoring Module * Tracks application health metrics including uptime and database connectivity */ const startTime = Date.now(); /** * Get application health status * @returns {Object} Health status object with status, uptime, and timestamp */ async function getHealthStatus(pool) { try { // Check database connectivity const dbHealthStart = Date.now(); const dbResult = await pool.query('SELECT NOW()'); const dbHealthDuration = Date.now() - dbHealthStart; const dbHealthy = dbResult.rows.length > 0; return { status: dbHealthy ? 'healthy' : 'degraded', uptime: Math.floor((Date.now() - startTime) / 1000), // uptime in seconds timestamp: new Date().toISOString(), database: { connected: dbHealthy, responseTime: `${dbHealthDuration}ms` } }; } catch (err) { logger.error('Health check failed', { error: err.message }); return { status: 'unhealthy', uptime: Math.floor((Date.now() - startTime) / 1000), timestamp: new Date().toISOString(), database: { connected: false, error: err.message } }; } } /** * Get uptime in seconds since application start * @returns {number} Uptime in seconds */ function getUptime() { return Math.floor((Date.now() - startTime) / 1000); } module.exports = { getHealthStatus, getUptime };