-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard-server.js
More file actions
437 lines (384 loc) · 11.6 KB
/
Copy pathdashboard-server.js
File metadata and controls
437 lines (384 loc) · 11.6 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/**
* Real-Time Dashboard Server for Ralph Mode
*
* Provides a web-based dashboard with live updates via WebSocket
* to visualize Ralph mode's progress through detection → fixing → completed states.
*/
const path = require('path');
const fs = require('fs');
const http = require('http');
const { exec } = require('child_process');
// Optional dependencies - dashboard is optional feature
let express, WebSocket;
try {
express = require('express');
WebSocket = require('ws');
} catch (e) {
// Dependencies not installed - dashboard won't work
}
// ============================================================================
// STATE
// ============================================================================
let app = null;
let server = null;
let wss = null;
let clients = new Set();
let isRunning = false;
let currentPort = 3000;
// Ralph state - synced to clients on connect
let ralphState = {
started: false,
cycle: 0,
maxCycles: 10,
budgetHard: 20,
totalCost: 0,
startTime: null,
issues: new Map(), // id -> issue object
fixing: new Set(), // issue ids currently being fixed
completed: new Set(), // issue ids that are done
logs: [] // recent log entries
};
// ============================================================================
// SERVER SETUP
// ============================================================================
/**
* Start the dashboard server
* @param {Object} options - Configuration options
* @param {number} options.port - Port to listen on (default: 3000)
* @returns {Promise<number>} - The port the server is running on
*/
async function startDashboard(options = {}) {
if (!express || !WebSocket) {
console.warn('[Dashboard] Dependencies not installed. Run: npm install express ws');
return null;
}
if (isRunning) {
return currentPort;
}
const startPort = options.port || 3000;
const maxRetries = 5;
// Helper to try starting on a specific port
function tryPort(port, attempt) {
return new Promise((resolve, reject) => {
try {
app = express();
// Serve static files from public directory
app.use(express.static(path.join(__dirname, 'public')));
// Health check endpoint
app.get('/health', (req, res) => {
res.json({ status: 'ok', clients: clients.size });
});
// 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 {
// Return empty metrics structure if no data yet
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'));
});
// Create HTTP server
server = http.createServer(app);
// Handle server errors BEFORE listening
server.on('error', (error) => {
if (error.code === 'EADDRINUSE') {
if (attempt < maxRetries) {
console.log(`[Dashboard] Port ${port} in use, trying ${port + 1}`);
// Close this server instance and try next port
server.close();
resolve(tryPort(port + 1, attempt + 1));
} else {
reject(new Error(`All ports ${startPort}-${port} in use`));
}
} else {
reject(error);
}
});
server.listen(port, () => {
currentPort = port;
// Create WebSocket server only after HTTP server is listening
wss = new WebSocket.Server({ server });
wss.on('connection', (ws) => {
clients.add(ws);
console.log(`[Dashboard] Client connected (${clients.size} total)`);
// Send connected acknowledgment
ws.send(JSON.stringify({
type: 'connected',
timestamp: new Date().toISOString()
}));
// Send current Ralph state so client can catch up
if (ralphState.started) {
ws.send(JSON.stringify({
type: 'state_sync',
data: getSerializableState(),
timestamp: new Date().toISOString()
}));
console.log(`[Dashboard] Sent state_sync: ${ralphState.issues.size} issues, ${ralphState.completed.size} completed`);
}
ws.on('close', () => {
clients.delete(ws);
console.log(`[Dashboard] Client disconnected (${clients.size} total)`);
});
ws.on('error', (error) => {
console.error('[Dashboard] WebSocket error:', error.message);
clients.delete(ws);
});
});
isRunning = true;
console.log(`[Dashboard] Server running at http://127.0.0.1:${port}`);
resolve(port);
});
} catch (error) {
reject(error);
}
});
}
return tryPort(startPort, 1);
}
/**
* Stop the dashboard server gracefully
*/
async function stopDashboard() {
if (!isRunning) return;
return new Promise((resolve) => {
// Close all WebSocket connections
clients.forEach(ws => {
try {
ws.send(JSON.stringify({ type: 'server_shutdown' }));
ws.close();
} catch (e) {
// Ignore errors on close
}
});
clients.clear();
// Close WebSocket server
if (wss) {
wss.close(() => {
console.log('[Dashboard] WebSocket server closed');
});
}
// Close HTTP server
if (server) {
server.close(() => {
console.log('[Dashboard] HTTP server closed');
isRunning = false;
resolve();
});
// Force close after timeout
setTimeout(() => {
isRunning = false;
resolve();
}, 2000);
} else {
isRunning = false;
resolve();
}
});
}
/**
* Update internal state based on event
* @param {string} eventName - Name of the event
* @param {Object} data - Event data
*/
function updateRalphState(eventName, data) {
switch (eventName) {
case 'ralph_started':
ralphState.started = true;
ralphState.maxCycles = data.maxCycles || 10;
ralphState.budgetHard = data.budgetHard || 20;
ralphState.startTime = Date.now();
ralphState.cycle = 0;
ralphState.totalCost = 0;
ralphState.issues.clear();
ralphState.fixing.clear();
ralphState.completed.clear();
ralphState.logs = [];
break;
case 'cycle_started':
ralphState.cycle = data.cycle;
ralphState.maxCycles = data.maxCycles;
break;
case 'detection_complete':
// Clear previous issues, add new ones
ralphState.issues.clear();
ralphState.fixing.clear();
if (data.issues) {
data.issues.forEach(issue => {
const id = issue.id || `${issue.file}:${issue.line}`;
if (!ralphState.completed.has(id)) {
ralphState.issues.set(id, { ...issue, id, status: 'detected' });
}
});
}
if (data.cost) {
ralphState.totalCost += data.cost;
}
break;
case 'fix_started':
if (data.issueIds) {
data.issueIds.forEach(id => {
ralphState.fixing.add(id);
const issue = ralphState.issues.get(id);
if (issue) {
issue.status = 'fixing';
issue.progress = 0;
}
});
}
break;
case 'fix_progress':
ralphState.issues.forEach((issue, id) => {
if (issue.file === data.file && ralphState.fixing.has(id)) {
issue.progress = data.progress;
}
});
break;
case 'fix_complete':
if (data.fixed) {
data.fixed.forEach(id => {
ralphState.fixing.delete(id);
ralphState.completed.add(id);
ralphState.issues.delete(id);
});
}
if (data.failed) {
data.failed.forEach(id => {
ralphState.fixing.delete(id);
const issue = ralphState.issues.get(id);
if (issue) issue.status = 'failed';
});
}
break;
case 'cost_update':
ralphState.totalCost = data.totalCost || ralphState.totalCost;
break;
case 'cycle_complete':
ralphState.totalCost = data.cost || ralphState.totalCost;
break;
case 'ralph_complete':
ralphState.totalCost = data.totalCost || ralphState.totalCost;
break;
case 'log_entry':
ralphState.logs.push({
time: new Date().toLocaleTimeString('en-US', { hour12: false }),
message: data.message,
level: data.level || 'info'
});
if (ralphState.logs.length > 100) ralphState.logs.shift();
break;
}
}
/**
* Get serializable state for sending to clients
* @returns {Object} - Serializable state object
*/
function getSerializableState() {
return {
started: ralphState.started,
cycle: ralphState.cycle,
maxCycles: ralphState.maxCycles,
budgetHard: ralphState.budgetHard,
totalCost: ralphState.totalCost,
startTime: ralphState.startTime,
issues: Array.from(ralphState.issues.values()),
fixing: Array.from(ralphState.fixing),
completed: Array.from(ralphState.completed),
logs: ralphState.logs
};
}
/**
* Emit an event to all connected clients
* @param {string} eventName - Name of the event
* @param {Object} data - Event data
*/
function emitEvent(eventName, data) {
// Update internal state first
updateRalphState(eventName, data);
if (!isRunning) return;
const message = JSON.stringify({
type: eventName,
data: data,
timestamp: new Date().toISOString()
});
clients.forEach(ws => {
if (ws.readyState === WebSocket.OPEN) {
try {
ws.send(message);
} catch (e) {
console.error('[Dashboard] Error sending to client:', e.message);
clients.delete(ws);
}
}
});
}
/**
* Open the dashboard in the default browser
* @param {number} port - Port the server is running on
*/
function openDashboardInBrowser(port = currentPort) {
const url = `http://127.0.0.1:${port}`;
// Cross-platform browser open
const command = process.platform === 'win32'
? `start "" "${url}"`
: process.platform === 'darwin'
? `open "${url}"`
: `xdg-open "${url}"`;
exec(command, (error) => {
if (error) {
console.log(`[Dashboard] Could not open browser: ${error.message}`);
console.log(`[Dashboard] Open manually: ${url}`);
}
});
}
/**
* Check if dependencies are available
* @returns {boolean}
*/
function isAvailable() {
return !!(express && WebSocket);
}
/**
* Get current server status
* @returns {Object}
*/
function getStatus() {
return {
running: isRunning,
port: currentPort,
clients: clients.size,
available: isAvailable()
};
}
// ============================================================================
// EXPORTS
// ============================================================================
module.exports = {
startDashboard,
stopDashboard,
emitEvent,
openDashboardInBrowser,
isAvailable,
getStatus
};