-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathindex.html
More file actions
514 lines (467 loc) · 17 KB
/
Copy pathindex.html
File metadata and controls
514 lines (467 loc) · 17 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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>System Monitor</title>
<style>
body {
font-family: system-ui, sans-serif;
max-width: 900px;
margin: 0 auto;
padding: 20px;
background: #f5f7fa;
}
h1 {
color: #333;
border-bottom: 1px solid #eee;
padding-bottom: 10px;
margin-bottom: 24px;
}
section {
background: #fff;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.06);
margin-bottom: 24px;
padding: 20px;
}
.controls {
display: flex;
gap: 10px;
margin-bottom: 16px;
}
button {
background: #4caf50;
border: none;
color: #fff;
padding: 10px 16px;
border-radius: 4px;
font-size: 1rem;
cursor: pointer;
transition: background 0.2s;
}
button.disconnect { background: #f44336; }
button:disabled { background: #ccc; cursor: not-allowed; }
/* Current stats display */
.stats-display {
display: flex;
flex-wrap: wrap;
gap: 20px;
margin-top: 20px;
}
.stat-card {
flex: 1;
min-width: 200px;
padding: 16px;
background: #fff;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.stat-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 12px;
}
.stat-title {
font-weight: 600;
color: #555;
}
.stat-value {
font-size: 1.8em;
font-weight: 700;
color: #333;
}
.stat-bar-container {
height: 8px;
background: #eee;
border-radius: 4px;
overflow: hidden;
margin-top: 8px;
}
.stat-bar {
height: 100%;
border-radius: 4px;
transition: width 0.5s ease;
}
.cpu-bar { background: #2196f3; }
.memory-bar { background: #4caf50; }
/* Graph */
#chart-container {
height: 300px;
margin-top: 20px;
position: relative;
background: #fff;
padding: 30px 20px 40px 40px;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
.chart-grid {
position: absolute;
top: 30px;
left: 40px;
right: 20px;
bottom: 40px;
display: grid;
grid-template-rows: repeat(5, 1fr);
border-left: 2px solid #aaa;
border-bottom: 2px solid #aaa;
}
.grid-line {
border-top: 1px dashed #aaa;
position: relative;
}
.grid-label {
position: absolute;
left: -30px;
top: -10px;
font-size: 12px;
color: #666;
font-weight: 500;
}
.chart-area {
position: absolute;
top: 30px;
left: 40px;
right: 20px;
bottom: 40px;
overflow: hidden;
}
/* Lines for the graph */
.line-chart {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
}
.chart-line {
fill: none;
stroke-width: 3;
stroke-linejoin: round;
stroke-linecap: round;
}
.cpu-line {
stroke: #2196f3;
stroke-dasharray: none;
}
.memory-line {
stroke: #4caf50;
stroke-dasharray: none;
}
/* Data points */
.data-points {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 2;
}
.data-point {
position: absolute;
width: 8px;
height: 8px;
border-radius: 50%;
margin-left: -4px;
margin-top: -4px;
z-index: 3;
}
.cpu-point {
background-color: #2196f3;
border: 2px solid white;
box-shadow: 0 0 4px rgba(0,0,0,0.3);
}
.memory-point {
background-color: #4caf50;
border: 2px solid white;
box-shadow: 0 0 4px rgba(0,0,0,0.3);
}
.data-tooltip {
position: absolute;
top: -30px;
left: 50%;
transform: translateX(-50%);
background: rgba(0,0,0,0.8);
color: white;
padding: 4px 8px;
border-radius: 4px;
font-size: 12px;
white-space: nowrap;
opacity: 0;
transition: opacity 0.2s;
pointer-events: none;
z-index: 10;
}
.data-point:hover .data-tooltip {
opacity: 1;
}
/* Time axis */
.time-axis {
position: absolute;
left: 40px;
right: 20px;
bottom: 10px;
display: flex;
justify-content: space-between;
}
.time-label {
font-size: 11px;
color: #666;
white-space: nowrap;
font-weight: 500;
}
/* Legend */
.chart-legend {
display: flex;
justify-content: center;
gap: 20px;
margin-top: 40px;
}
.legend-item {
display: flex;
align-items: center;
gap: 5px;
}
.legend-color {
width: 15px;
height: 15px;
border-radius: 3px;
}
.cpu-legend { background: #2196f3; }
.memory-legend { background: #4caf50; }
/* Status */
.status {
color: #4caf50;
font-weight: bold;
}
.status.disconnected {
color: #f44336;
}
</style>
</head>
<body>
<h1>System Resource Monitor</h1>
<section>
<h2>Connection Status: <span id="status" class="status disconnected">Disconnected</span></h2>
<div class="controls">
<button id="connect">Start Monitoring</button>
<button id="disconnect" class="disconnect" disabled>Stop Monitoring</button>
</div>
<!-- Current stats cards -->
<div class="stats-display">
<div class="stat-card">
<div class="stat-header">
<div class="stat-title">CPU Usage</div>
<div class="stat-value" id="cpu-value">--</div>
</div>
<div class="stat-bar-container">
<div class="stat-bar cpu-bar" id="cpu-bar" style="width: 0%"></div>
</div>
</div>
<div class="stat-card">
<div class="stat-header">
<div class="stat-title">Memory Usage</div>
<div class="stat-value" id="memory-value">--</div>
</div>
<div class="stat-bar-container">
<div class="stat-bar memory-bar" id="memory-bar" style="width: 0%"></div>
</div>
</div>
</div>
<div id="last-update" style="text-align: right; margin-top: 10px; color: #777; font-size: 0.9em;">Last update: --</div>
</section>
<section>
<h2>Resource Usage Graph</h2>
<div id="chart-container">
<div class="chart-grid">
<div class="grid-line"><span class="grid-label">100%</span></div>
<div class="grid-line"><span class="grid-label">75%</span></div>
<div class="grid-line"><span class="grid-label">50%</span></div>
<div class="grid-line"><span class="grid-label">25%</span></div>
<div class="grid-line"><span class="grid-label">0%</span></div>
</div>
<div class="chart-area">
<svg id="line-chart" class="line-chart" preserveAspectRatio="none"></svg>
<div id="data-points" class="data-points"></div>
</div>
<div id="time-axis" class="time-axis"></div>
</div>
<div class="chart-legend">
<div class="legend-item">
<div class="legend-color cpu-legend"></div>
<span>CPU</span>
</div>
<div class="legend-item">
<div class="legend-color memory-legend"></div>
<span>Memory</span>
</div>
</div>
</section>
<script>
document.addEventListener('DOMContentLoaded', () => {
const connectBtn = document.getElementById('connect');
const disconnectBtn = document.getElementById('disconnect');
const statusEl = document.getElementById('status');
// Get chart elements
let eventSource = null;
const dataPoints = [];
const MAX_DATA_POINTS = 10;
// Connect to SSE stream
connectBtn.addEventListener('click', () => {
// Prevent multiple connections
if (eventSource && eventSource.readyState !== EventSource.CLOSED) {
return;
}
// Create new EventSource
eventSource = new EventSource('/monitor');
// Update UI
connectBtn.disabled = true;
disconnectBtn.disabled = false;
statusEl.textContent = 'Connected';
statusEl.classList.remove('disconnected');
// Listen for open event
eventSource.onopen = (e) => {
console.log('Connection established', e);
};
// Listen for 'message' events
eventSource.addEventListener('message', (e) => {
console.log('Usage data received:', e);
try {
// Parse the JSON data from the event
const usageData = JSON.parse(e.data);
// Add to data points array
dataPoints.push(usageData);
// Keep only the last MAX_DATA_POINTS
if (dataPoints.length > MAX_DATA_POINTS) {
dataPoints.shift();
}
// Update the current stats display
updateStatsDisplay(usageData);
// Update the chart
updateChart();
} catch (error) {
console.error('Error parsing usage data:', error);
}
});
// Listen for error event
eventSource.onerror = (e) => {
console.error('EventSource error:', e);
};
});
// Disconnect from SSE stream
disconnectBtn.addEventListener('click', () => {
if (eventSource) {
eventSource.close();
}
handleDisconnect();
});
// Handle disconnect
function handleDisconnect() {
if (eventSource && eventSource.readyState !== EventSource.CLOSED) {
eventSource.close();
}
eventSource = null;
connectBtn.disabled = false;
disconnectBtn.disabled = true;
statusEl.textContent = 'Disconnected';
statusEl.classList.add('disconnected');
}
// Function to update the current stats display
function updateStatsDisplay(data) {
// Update CPU stats
document.getElementById('cpu-value').textContent = `${data.CPU}%`;
document.getElementById('cpu-bar').style.width = `${data.CPU}%`;
// Update Memory stats
document.getElementById('memory-value').textContent = `${data.Memory}%`;
document.getElementById('memory-bar').style.width = `${data.Memory}%`;
// Update timestamp
const timestamp = new Date(data.Timestamp).toLocaleTimeString();
document.getElementById('last-update').textContent = `Last update: ${timestamp}`;
}
// Function to update the chart
function updateChart() {
const chartArea = document.querySelector('.chart-area');
const lineChart = document.getElementById('line-chart');
const dataPointsContainer = document.getElementById('data-points');
const timeAxis = document.getElementById('time-axis');
// Clear previous chart data
lineChart.innerHTML = '';
dataPointsContainer.innerHTML = '';
timeAxis.innerHTML = '';
// Set SVG dimensions
const chartWidth = chartArea.clientWidth;
const chartHeight = chartArea.clientHeight;
lineChart.setAttribute('width', chartWidth);
lineChart.setAttribute('height', chartHeight);
lineChart.setAttribute('viewBox', `0 0 ${chartWidth} ${chartHeight}`);
// Skip if no data points
if (dataPoints.length === 0) return;
// Create path data for CPU and memory lines
let cpuPathData = '';
let memoryPathData = '';
// Calculate x positions based on number of data points
const xStep = chartWidth / (Math.max(1, dataPoints.length - 1));
// Create time labels
dataPoints.forEach((data, index) => {
const x = index * xStep;
const cpuY = chartHeight - (data.CPU / 100) * chartHeight;
const memoryY = chartHeight - (data.Memory / 100) * chartHeight;
// Add points to path data
if (index === 0) {
cpuPathData = `M ${x},${cpuY}`;
memoryPathData = `M ${x},${memoryY}`;
} else {
cpuPathData += ` L ${x},${cpuY}`;
memoryPathData += ` L ${x},${memoryY}`;
}
// Create CPU data point
const cpuPoint = document.createElement('div');
cpuPoint.className = 'data-point cpu-point';
cpuPoint.style.left = `${x}px`;
cpuPoint.style.top = `${cpuY}px`;
// Create tooltip for CPU
const cpuTooltip = document.createElement('div');
cpuTooltip.className = 'data-tooltip';
cpuTooltip.textContent = `CPU: ${data.CPU}%`;
cpuPoint.appendChild(cpuTooltip);
// Create Memory data point
const memoryPoint = document.createElement('div');
memoryPoint.className = 'data-point memory-point';
memoryPoint.style.left = `${x}px`;
memoryPoint.style.top = `${memoryY}px`;
// Create tooltip for Memory
const memoryTooltip = document.createElement('div');
memoryTooltip.className = 'data-tooltip';
memoryTooltip.textContent = `Memory: ${data.Memory}%`;
memoryPoint.appendChild(memoryTooltip);
// Add data points to container
dataPointsContainer.appendChild(cpuPoint);
dataPointsContainer.appendChild(memoryPoint);
// Create time label
const timeLabel = document.createElement('div');
timeLabel.className = 'time-label';
// Format time to show hours:minutes:seconds
const time = new Date(data.Timestamp);
const formattedTime = time.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', second: '2-digit' });
timeLabel.textContent = formattedTime;
timeLabel.style.transform = `translateX(-50%)`;
timeLabel.style.left = `${x}px`;
timeAxis.appendChild(timeLabel);
});
// Create SVG paths for CPU and memory lines
const cpuPath = document.createElementNS('http://www.w3.org/2000/svg', 'path');
cpuPath.setAttribute('d', cpuPathData);
cpuPath.setAttribute('class', 'chart-line cpu-line');
const memoryPath = document.createElementNS('http://www.w3.org/2000/svg', 'path');
memoryPath.setAttribute('d', memoryPathData);
memoryPath.setAttribute('class', 'chart-line memory-line');
// Add paths to SVG
lineChart.appendChild(cpuPath);
lineChart.appendChild(memoryPath);
}
});
</script>
</body>
</html>