Skip to content

Commit bb5ba44

Browse files
author
Tom Softreck
committed
update
1 parent a0e747f commit bb5ba44

File tree

1 file changed

+108
-12
lines changed

1 file changed

+108
-12
lines changed

webtask/static/webtask.js

Lines changed: 108 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,36 @@ class WebTask {
6969

7070
// Fetch initial data from API
7171
fetchInitialData() {
72-
// Fetch system info first to get CPU cores count
73-
this.fetchSystemInfo()
74-
.then(() => {
75-
// Initialize CPU cores display with the correct number of cores
76-
this.initializeCpuCoresDisplay();
72+
// Fetch system info from API
73+
fetch('/api/system')
74+
.then(response => response.json())
75+
.then(data => {
76+
// Update CPU cores count from API
77+
this.cpuCores = data.cpu.cores || 4;
78+
79+
// Initialize CPU core values
80+
this.cpuCoreValues = Array(this.cpuCores).fill(0);
7781

78-
// Start fetching processes
79-
this.fetchProcesses();
82+
// Initialize CPU history with zeros
83+
this.cpuHistory = Array(60).fill(0);
8084

81-
// Start the update interval
85+
// Initialize CPU visualization
86+
this.initializeCpuVisualization();
87+
88+
// Start updating data
8289
this.startUpdating();
8390
})
8491
.catch(error => {
85-
console.error('Error fetching initial data:', error);
86-
// Fallback to simulated data if API fails
87-
this.simulateData();
92+
console.error('Error fetching system info:', error);
93+
// Fallback to default values
94+
this.cpuCores = 4;
95+
this.cpuCoreValues = Array(this.cpuCores).fill(0);
96+
this.cpuHistory = Array(60).fill(0);
97+
this.initializeCpuVisualization();
98+
this.startUpdating();
8899
});
89100
}
90-
101+
91102
// Start periodic updates
92103
startUpdating() {
93104
// Set interval to update data periodically
@@ -96,6 +107,7 @@ class WebTask {
96107
this.fetchProcesses();
97108
}, 3000); // Update every 3 seconds
98109
}
110+
99111

100112
// Fetch system information from API
101113
fetchSystemInfo() {
@@ -602,6 +614,90 @@ class WebTask {
602614
this.updateCpuHistoryChart(cpuUsage);
603615
}
604616

617+
// Update processes with real data from API
618+
updateProcesses() {
619+
// Fetch processes from API
620+
fetch('/api/processes')
621+
.then(response => response.json())
622+
.then(data => {
623+
// Transform API data to match our expected format
624+
this.processes = data.map(proc => {
625+
return {
626+
pid: proc.pid,
627+
user: proc.user || 'system',
628+
cpu: proc.cpu || 0,
629+
memory: proc.memory || 0,
630+
time: this.generateTime(),
631+
port: null, // API doesn't provide port info
632+
command: proc.name || 'unknown',
633+
file: null,
634+
service: proc.name ? proc.name.split(' ')[0] : 'unknown',
635+
parent: null,
636+
children: [],
637+
transparency: this.calculateTransparency(proc.name ? proc.name.split(' ')[0] : 'unknown'),
638+
startTime: Date.now() - (Math.random() * 3600000) // Random start time within the last hour
639+
};
640+
});
641+
642+
// Build process hierarchy
643+
this.buildProcessHierarchy();
644+
645+
// Sort processes according to current sort configuration
646+
this.sortProcesses();
647+
648+
// Apply current filter
649+
this.applyFilter();
650+
651+
// Render the process list
652+
this.renderProcesses();
653+
})
654+
.catch(error => {
655+
console.error('Error fetching processes:', error);
656+
// Fallback to simulated data if API fails
657+
this.simulateProcesses();
658+
});
659+
}
660+
661+
// Simulate processes (fallback if API fails)
662+
simulateProcesses() {
663+
// Simulate process changes
664+
this.processes.forEach(process => {
665+
process.cpu += (Math.random() - 0.5) * 2;
666+
process.cpu = Math.max(0, Math.min(100, process.cpu));
667+
668+
process.memory += (Math.random() - 0.5) * 1;
669+
process.memory = Math.max(0, Math.min(100, process.memory));
670+
});
671+
672+
// Occasionally add new processes
673+
if (Math.random() < 0.1 && this.processes.length < 50) {
674+
const commands = ['node app.js', 'python3 server.py', 'java -jar app.jar', 'go run main.go'];
675+
const command = commands[Math.floor(Math.random() * commands.length)];
676+
const port = Math.random() < 0.3 ? Math.floor(Math.random() * 9000) + 1000 : null;
677+
678+
this.processes.push({
679+
pid: this.processCounter++,
680+
user: 'user',
681+
cpu: Math.random() * 5,
682+
memory: Math.random() * 10,
683+
time: this.generateTime(),
684+
port: port,
685+
command: command,
686+
file: command.includes('node') ? '/var/www/html/app.js' : null,
687+
service: command.split(' ')[0],
688+
parent: null,
689+
children: [],
690+
transparency: 0.9,
691+
startTime: Date.now()
692+
});
693+
}
694+
695+
// Sort and apply filter
696+
this.sortProcesses();
697+
this.applyFilter();
698+
this.renderProcesses();
699+
}
700+
605701
// Sort processes according to current sort configuration
606702
sortProcesses() {
607703
const { column, direction } = this.sortConfig;

0 commit comments

Comments
 (0)