-
Notifications
You must be signed in to change notification settings - Fork 30
/
app.js
140 lines (109 loc) · 4.62 KB
/
app.js
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
document.addEventListener('DOMContentLoaded', () => {
const scanForm = document.getElementById('scanForm');
let defaultNamespace = '';
if (scanForm) {
scanForm.addEventListener('submit', (event) => {
event.preventDefault();
console.log('Form submitted');
const imageInput = document.getElementById('imageInput').value;
console.log('Image input:', imageInput);
scanImage(imageInput);
});
}
const healthCheckButton = document.getElementById('healthCheckButton');
const healthCheckResult = document.getElementById('healthCheckResult');
if (healthCheckButton) {
healthCheckButton.addEventListener('click', () => {
healthCheckResult.textContent = 'Checking...';
fetch('http://127.0.0.1:5000/kubernetes_info?namespace=' + defaultNamespace)
.then(response => response.json())
.then(data => {
const numPods = data.num_pods;
const numPodsRunning = numPods; // You can adjust this based on your actual logic
const allPodsRunning = numPods === numPodsRunning;
if (allPodsRunning) {
healthCheckResult.textContent = 'Good';
healthCheckResult.style.color = 'green';
} else {
healthCheckResult.textContent = 'Bad';
healthCheckResult.style.color = 'red';
}
})
.catch(error => {
healthCheckResult.textContent = 'Error checking health';
healthCheckResult.style.color = 'red';
console.error('Error checking health:', error);
});
});
}
function updateDashboard() {
// Fetch system info from your Python backend's API endpoint
fetch('http://127.0.0.1:5000/system_info')
.then(response => response.json())
.then(data => {
const memoryUtilizationElement = document.querySelector('.memory-utilization .percentage');
memoryUtilizationElement.textContent = data.memory_usage.percent + '%';
const cpuUtilizationElement = document.querySelector('.cpu-utilization .percentage');
cpuUtilizationElement.textContent = data.cpu_percent + '%';
const storageUtilizationElement = document.querySelector('.storage-used .percentage');
storageUtilizationElement.textContent = data.disk_usage.percent + '%';
});
// Fetch Kubernetes namespaces from your Python backend's API endpoint
fetch('http://127.0.0.1:5000/kubernetes_namespaces')
.then(response => response.json())
.then(namespaces => {
const namespaceDropdown = document.getElementById('namespace-dropdown');
// Clear existing options
namespaceDropdown.innerHTML = '';
// Add available namespaces to the dropdown
namespaces.forEach(namespace => {
const option = document.createElement('option');
option.value = namespace;
option.textContent = namespace;
namespaceDropdown.appendChild(option);
});
// Attach event handler to the namespace dropdown
namespaceDropdown.addEventListener('change', () => {
const selectedNamespace = namespaceDropdown.value;
fetchKubernetesInfo(selectedNamespace);
});
// Fetch Kubernetes info for the first namespace by default
const defaultNamespace = namespaces[0];
fetchKubernetesInfo(defaultNamespace);
});
}
// Attach event handler to the scan form
function fetchKubernetesInfo(namespace) {
// Fetch Kubernetes info from your Python backend's API endpoint for the selected namespace
fetch(`http://127.0.0.1:5000/kubernetes_info?namespace=${namespace}`)
.then(response => response.json())
.then(data => {
const numDeploymentsElement = document.querySelector('.deployments .count');
numDeploymentsElement.textContent = data.num_deployments;
const numPodsElement = document.querySelector('.pods-running .count');
numPodsElement.textContent = data.num_pods;
const numServicesElement = document.querySelector('.services-running .count');
numServicesElement.textContent = data.num_services;
});
}
function scanImage(imageName) {
// Fetch Trivy scan results from your Python backend's API endpoint
console.log('scanImage function called');
fetch('http://127.0.0.1:5000/scan_image', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ container_id: imageName }),
})
.then(response => response.json())
.then(data => {
const scanResultsElement = document.getElementById('scanResults');
scanResultsElement.textContent = JSON.stringify(data.scan_results, null, 2);
})
.catch(error => {
console.error('Error scanning image:', error);
});
}
updateDashboard();
});