From beb211df0adc2c9fff6d43804bcfceab6c2b639f Mon Sep 17 00:00:00 2001 From: ghaddaditw <40211818-ghaddaditw@users.noreply.replit.com> Date: Sun, 8 Jun 2025 06:59:06 +0000 Subject: [PATCH] Improve platform health monitoring and add administrative task controls Adds task scheduler initialization, enhanced health check, and admin monitoring API endpoints. Replit-Commit-Author: Agent Replit-Commit-Session-Id: c5f0c281-8dd8-4846-b452-4a07bcd21062 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/9777c70b-fc38-4831-8d6b-78dfffe041b0/dc0a5a62-bcd9-407a-9d2c-d05501cafcbe.jpg --- server/index.ts | 3 +++ server/routes.ts | 67 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/server/index.ts b/server/index.ts index b727756..0817eda 100644 --- a/server/index.ts +++ b/server/index.ts @@ -67,5 +67,8 @@ app.use((req, res, next) => { reusePort: true, }, () => { log(`serving on port ${port}`); + + // Initialize background task scheduler + taskScheduler.initialize(); }); })(); diff --git a/server/routes.ts b/server/routes.ts index 694c0da..34e36c0 100644 --- a/server/routes.ts +++ b/server/routes.ts @@ -17,6 +17,7 @@ import { errorTrackingMiddleware } from "./middleware/security"; import { monitoringService } from "./services/monitoringService"; +import { taskScheduler } from "./services/taskScheduler"; import { authController } from "./controllers/authController"; import { taskController } from "./controllers/taskController"; import { financialController } from "./controllers/financialController"; @@ -175,13 +176,65 @@ export async function registerRoutes(app: Express): Promise { } }); - // Health check endpoint - app.get('/api/health', (req, res) => { - res.json({ - status: 'ok', - timestamp: new Date().toISOString(), - environment: process.env.NODE_ENV || 'development' - }); + // Enhanced health check endpoint + app.get('/health', async (req, res) => { + try { + const healthCheck = await monitoringService.getHealthCheck(); + res.json(healthCheck); + } catch (error) { + res.status(500).json({ + status: "unhealthy", + error: error instanceof Error ? error.message : "Unknown error" + }); + } + }); + + // Admin monitoring endpoints + app.get('/api/admin/metrics', authMiddleware, requireRole(['admin']), async (req, res) => { + try { + const metrics = await monitoringService.getSystemMetrics(); + res.json(metrics); + } catch (error) { + res.status(500).json({ error: 'Failed to fetch system metrics' }); + } + }); + + app.get('/api/admin/performance', authMiddleware, requireRole(['admin', 'pro']), async (req, res) => { + try { + const performance = await monitoringService.getPerformanceMetrics(); + res.json(performance); + } catch (error) { + res.status(500).json({ error: 'Failed to fetch performance metrics' }); + } + }); + + app.get('/api/admin/analytics/:timeframe?', authMiddleware, requireRole(['admin']), async (req, res) => { + try { + const timeframe = (req.params.timeframe as 'day' | 'week' | 'month') || 'day'; + const analytics = await monitoringService.getUsageAnalytics(timeframe); + res.json(analytics); + } catch (error) { + res.status(500).json({ error: 'Failed to fetch analytics data' }); + } + }); + + app.get('/api/admin/tasks', authMiddleware, requireRole(['admin']), async (req, res) => { + try { + const tasks = taskScheduler.getTaskStatus(); + res.json(tasks); + } catch (error) { + res.status(500).json({ error: 'Failed to fetch task status' }); + } + }); + + app.post('/api/admin/tasks/:taskName/execute', authMiddleware, requireRole(['admin']), async (req, res) => { + try { + const { taskName } = req.params; + await taskScheduler.executeTask(taskName); + res.json({ success: true, message: `Task ${taskName} executed successfully` }); + } catch (error) { + res.status(500).json({ error: error instanceof Error ? error.message : 'Task execution failed' }); + } }); // Apply error tracking middleware at the end