-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics-server.js
More file actions
151 lines (132 loc) · 4.84 KB
/
Copy pathmetrics-server.js
File metadata and controls
151 lines (132 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env node
/**
* Standalone Metrics Server for QA Watcher
*
* Starts a web server to view QA metrics without running Ralph mode.
* Usage: npm run qa-metrics
*/
const path = require('path');
const fs = require('fs');
const http = require('http');
const { exec } = require('child_process');
// Optional dependencies
let express;
try {
express = require('express');
} catch (e) {
console.error('❌ Express not installed. Run: npm install express');
process.exit(1);
}
// ============================================================================
// CONFIGURATION
// ============================================================================
const DEFAULT_PORT = 3000;
const MAX_PORT_RETRIES = 10;
// Load config for port
let config = { dashboard: { port: DEFAULT_PORT, autoOpen: true } };
const configPath = path.join(process.cwd(), '.qawatch.json');
if (fs.existsSync(configPath)) {
try {
config = { ...config, ...JSON.parse(fs.readFileSync(configPath, 'utf8')) };
} catch (e) {
// Use defaults
}
}
// ============================================================================
// SERVER
// ============================================================================
function startMetricsServer(port, attempt = 1) {
const app = express();
// Serve static files including the dashboard
app.use(express.static(path.join(__dirname, 'public')));
// Metrics API endpoint
app.get('/api/metrics', (req, res) => {
const metricsPath = path.join(process.cwd(), 'qa-reviews', 'metrics.json');
try {
if (fs.existsSync(metricsPath)) {
const metrics = JSON.parse(fs.readFileSync(metricsPath, 'utf8'));
res.json(metrics);
} else {
res.json({
sessions: [],
aggregates: {
totalReviews: 0,
totalIssuesFound: 0,
totalIssuesFixed: 0,
totalCost: 0,
totalDuration: 0,
estimatedTimeSaved: 0
},
topFiles: {},
issueTypeBreakdown: {}
});
}
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Serve metrics page
app.get('/metrics', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'metrics.html'));
});
// Health check
app.get('/health', (req, res) => {
res.json({ status: 'ok', mode: 'metrics-only' });
});
const server = http.createServer(app);
server.on('error', (error) => {
if (error.code === 'EADDRINUSE') {
if (attempt < MAX_PORT_RETRIES) {
console.log(`⚠️ Port ${port} in use, trying ${port + 1}...`);
startMetricsServer(port + 1, attempt + 1);
} else {
console.error(`❌ Could not find available port after ${MAX_PORT_RETRIES} attempts`);
process.exit(1);
}
} else {
console.error('❌ Server error:', error.message);
process.exit(1);
}
});
server.listen(port, () => {
const url = `http://localhost:${port}/metrics`;
console.log('');
console.log('╔════════════════════════════════════════════════════════════╗');
console.log('║ 📊 QA Watcher Metrics Server ║');
console.log('╠════════════════════════════════════════════════════════════╣');
console.log(`║ Server running at: ${url.padEnd(38)}║`);
console.log('║ ║');
console.log('║ Press Ctrl+C to stop ║');
console.log('╚════════════════════════════════════════════════════════════╝');
console.log('');
// Auto-open browser if configured
if (config.dashboard?.autoOpen !== false) {
const platform = process.platform;
let openCmd;
if (platform === 'win32') {
openCmd = `start "" "${url}"`;
} else if (platform === 'darwin') {
openCmd = `open "${url}"`;
} else {
openCmd = `xdg-open "${url}"`;
}
exec(openCmd, (err) => {
if (err) {
console.log(`📋 Open manually: ${url}`);
}
});
}
});
// Graceful shutdown
process.on('SIGINT', () => {
console.log('\n👋 Shutting down metrics server...');
server.close(() => {
process.exit(0);
});
});
}
// ============================================================================
// MAIN
// ============================================================================
console.log('🚀 Starting QA Watcher Metrics Server...');
startMetricsServer(config.dashboard?.port || DEFAULT_PORT);