-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
358 lines (310 loc) · 13.8 KB
/
utils.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
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
async function fetchHosts() {
const response = await fetch('/fetch/hosts');
const hosts = await response.json();
console.log('Fetched hosts:', hosts);
return hosts;
}
async function fetchLatestMetrics() {
try {
const response = await fetch('/fetch/latest');
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const metrics = await response.json();
console.log('Fetched latest metrics:', metrics);
return metrics;
} catch (error) {
console.error('Error fetching latest metrics:', error);
return { error: 'Failed to fetch latest metrics' };
}
}
async function fetchMetricHistory(hostname, metricName, startDate, endDate) {
const response = await fetch(`/fetch/history/${hostname}/${metricName}?start=${startDate}&end=${endDate}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const history = await response.json();
console.log(`Fetched history for ${hostname} - ${metricName}:`, history);
return history;
}
let updateInterval;
function setTimeRange(range) {
clearInterval(updateInterval);
const end = new Date();
let start;
document.querySelectorAll('.dropdown-item').forEach(btn => btn.classList.remove('active'));
switch (range) {
case 'realtime':
start = new Date(end.getTime() - 5 * 60 * 1000);
document.getElementById('lastRealtimeButton').classList.add('active');
updateInterval = setInterval(() => {
const currentEnd = new Date();
const currentStart = new Date(currentEnd.getTime() - 5 * 60 * 1000);
document.getElementById('endDate').value = currentEnd.toLocaleString('sv-SE').replace(' ', 'T').slice(0, 16);
document.getElementById('startDate').value = currentStart.toLocaleString('sv-SE').replace(' ', 'T').slice(0, 16);
}, 1000);
break;
case 'hour':
start = new Date(end.getTime() - 60 * 60 * 1000);
document.getElementById('lastHourButton').classList.add('active');
break;
case 'day':
start = new Date(end.getTime() - 24 * 60 * 60 * 1000);
document.getElementById('lastDayButton').classList.add('active');
break;
case 'week':
start = new Date(end.getTime() - 7 * 24 * 60 * 60 * 1000);
document.getElementById('lastWeekButton').classList.add('active');
break;
case 'month':
start = new Date(end.getTime() - 30 * 24 * 60 * 60 * 1000);
document.getElementById('lastMonthButton').classList.add('active');
break;
default:
start = new Date(end.getTime() - 60 * 60 * 1000);
}
document.getElementById('endDate').value = end.toLocaleString('sv-SE').replace(' ', 'T').slice(0, 16);
document.getElementById('startDate').value = start.toLocaleString('sv-SE').replace(' ', 'T').slice(0, 16);
return { start, end };
}
function updateFormVisibility(selectedHostname) {
const hostnameField = document.getElementById('hostnameField');
const downtimeHostnameField = document.getElementById('downtimeHostnameField');
if (selectedHostname === 'all') {
hostnameField.style.display = 'block';
downtimeHostnameField.style.display = 'block';
} else {
hostnameField.style.display = 'none';
downtimeHostnameField.style.display = 'none';
document.getElementById('hostname').value = selectedHostname;
document.getElementById('downtimeHostname').value = selectedHostname;
}
}
function createHostSelector(hosts, updateDashboard) {
console.log('Hosts data received:', hosts);
const selector = document.getElementById('hostSelector');
selector.innerHTML = '<label for="hostSelect" class="form-label">Select Host:</label>';
const select = document.createElement('select');
select.id = 'hostSelect';
select.className = 'form-select';
const allOption = document.createElement('option');
allOption.value = 'all';
allOption.textContent = 'All Hosts';
select.appendChild(allOption);
if (typeof hosts === 'object' && hosts !== null) {
Object.entries(hosts).forEach(([hostname, hostData]) => {
const option = document.createElement('option');
option.value = hostname;
option.textContent = hostData.tags && hostData.tags.alias ? hostData.tags.alias : hostname;
select.appendChild(option);
});
} else {
console.error('Unexpected hosts data format:', hosts);
}
select.addEventListener('change', async () => {
const selectedHostname = select.value;
updateUrlWithHost(selectedHostname);
await updateDashboard(selectedHostname);
updateFormVisibility(selectedHostname);
});
selector.appendChild(select);
}
function updateHostInfo(hostname, tags) {
const hostInfoDiv = document.getElementById('hostInfo');
if (hostInfoDiv) {
let content = `<p><strong>Hostname: </strong>${hostname}</p>
<div class="d-flex flex-wrap gap-2">`;
if (tags && typeof tags === 'object' && Object.keys(tags).length > 0) {
for (const [key, value] of Object.entries(tags)) {
content += `<span class="badge bg-secondary">${key}: ${value}</span>`;
}
} else {
content += '<span class="badge bg-secondary">No tags</span>';
}
content += '</div>';
hostInfoDiv.innerHTML = content;
} else {
console.warn("Element with id 'hostInfo' not found");
}
}
function getVibrantColor(index) {
const vibrantColors = [
'#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231',
'#911eb4', '#46f0f0', '#f032e6', '#bcf60c', '#fabebe',
'#008080', '#e6beff', '#9a6324', '#fffac8', '#800000',
'#aaffc3', '#808000', '#ffd8b1', '#000075', '#808080'
];
return vibrantColors[index % vibrantColors.length];
}
function updateUrlWithHost(hostname) {
const url = new URL(window.location);
url.searchParams.set('host', hostname);
window.history.pushState({}, '', url);
}
async function updateDashboard(hostname, isRealtimeUpdate = false) {
console.log('Updating dashboard...');
console.log('Selected hostname:', hostname);
const startDateInput = document.getElementById('startDate');
const endDateInput = document.getElementById('endDate');
let startDate = new Date(startDateInput.value);
let endDate = new Date(endDateInput.value);
const activeTimeRangeButton = document.querySelector('.dropdown-item.active');
const isCustomRange = !activeTimeRangeButton;
if (isRealtimeUpdate && !isCustomRange) {
endDate = new Date();
const timeDiff = endDate - startDate;
startDate = new Date(endDate - timeDiff);
startDateInput.value = startDate.toISOString().slice(0, 16);
endDateInput.value = endDate.toISOString().slice(0, 16);
}
if (hostname === 'all') {
document.getElementById('hostInfo').style.display = 'none';
document.getElementById('chartContainer').innerHTML = '';
Object.values(charts).forEach(chart => chart.destroy());
charts = {};
} else {
document.getElementById('hostInfo').style.display = 'block';
try {
const latestMetrics = await fetchLatestMetrics();
console.log('Latest metrics:', latestMetrics);
const hostData = latestMetrics[hostname];
if (hostData) {
const metricNames = Object.keys(hostData.metrics || {});
console.log('Metric names:', metricNames);
updateHostInfo(hostname, hostData.tags || {});
const datasets = {};
const messages = [];
const fetchPromises = metricNames.map(async (metricName) => {
try {
const metricData = await fetchMetricHistory(hostname, metricName, startDate.getTime() / 1000, endDate.getTime() / 1000);
console.log(`Fetched data for ${metricName}:`, metricData);
datasets[metricName] = processMetricData(metricData, metricName);
console.log(`Processed datasets for ${metricName}:`, datasets[metricName]);
// Collect messages from the metric data
metricData.forEach(point => {
Object.entries(point).forEach(([subMetricName, subMetricData]) => {
if (subMetricData && typeof subMetricData === 'object' && 'message' in subMetricData) {
console.log(`Found message for ${metricName}.${subMetricName}:`, subMetricData.message);
messages.push({
timestamp: new Date(point.timestamp * 1000),
metricName: `${metricName}.${subMetricName}`,
message: subMetricData.message
});
}
});
});
} catch (error) {
console.error(`Error processing data for ${metricName}:`, error);
}
});
await Promise.all(fetchPromises);
console.log('All collected messages:', messages);
document.getElementById('chartContainer').innerHTML = '';
for (const metricName of metricNames) {
const chartDiv = document.createElement('div');
chartDiv.className = 'col-12';
chartDiv.style.height = '400px';
chartDiv.innerHTML = `<canvas id="${metricName}Chart"></canvas>`;
document.getElementById('chartContainer').appendChild(chartDiv);
}
for (const metricName of metricNames) {
if (datasets[metricName] && datasets[metricName].length > 0) {
if (charts[metricName]) {
charts[metricName].destroy();
}
charts[metricName] = updateChart(null, metricName, datasets[metricName], startDate, endDate);
} else {
console.warn(`No valid data for chart: ${metricName}`);
}
}
// Display messages
displayMessages(messages);
} else {
console.error(`No data found for hostname: ${hostname}`);
updateHostInfo(hostname, {});
document.getElementById('chartContainer').innerHTML = '';
Object.values(charts).forEach(chart => chart.destroy());
charts = {};
}
} catch (error) {
console.error('Error updating dashboard:', error);
document.getElementById('chartContainer').innerHTML = '<p>Error loading dashboard data. Please try again later.</p>';
}
}
if (!isRealtimeUpdate) {
await updateRecentAlerts(hostname);
await updateDowntimes(hostname);
await updateAlertConfigs(hostname);
}
}
function displayMessages(messages) {
console.log('Displaying messages:', messages);
const messageContainer = document.getElementById('messageContainer');
if (!messageContainer) {
console.error('Message container not found in the DOM');
return;
}
messageContainer.innerHTML = '';
if (messages.length === 0) {
console.log('No messages to display');
messageContainer.innerHTML = '<p>No messages to display.</p>';
return;
}
const messageList = document.createElement('ul');
messageList.className = 'list-group';
messages.sort((a, b) => b.timestamp - a.timestamp); // Sort messages by timestamp, newest first
messages.forEach(msg => {
console.log('Creating list item for message:', msg);
const listItem = document.createElement('li');
listItem.className = 'list-group-item';
listItem.innerHTML = `
<strong>${msg.timestamp.toLocaleString()}</strong> -
<span class="badge bg-secondary">${msg.metricName}</span>:
${msg.message}
`;
messageList.appendChild(listItem);
});
messageContainer.appendChild(messageList);
console.log('Messages displayed successfully');
}
function processMetricData(metricData, metricName) {
const datasets = [];
console.log(`Processing metric data for ${metricName}:`, metricData);
if (!Array.isArray(metricData) || metricData.length === 0) {
console.warn(`No valid data for metric: ${metricName}`);
return datasets;
}
const metrics = Object.keys(metricData[0]).filter(key => key !== 'timestamp');
metrics.forEach((metric, index) => {
datasets.push({
label: metric,
data: metricData.map(point => {
const metricPoint = point[metric] || {};
return {
x: new Date(point.timestamp * 1000),
y: metricPoint.value,
message: metricPoint.message
};
}).filter(dataPoint => dataPoint.y != null && !isNaN(dataPoint.y)),
borderColor: getVibrantColor(index),
backgroundColor: getVibrantColor(index),
fill: false
});
});
console.log(`Processed datasets for ${metricName}:`, datasets);
return datasets;
}
export {
fetchHosts,
fetchLatestMetrics,
fetchMetricHistory,
setTimeRange,
updateFormVisibility,
createHostSelector,
updateHostInfo,
getVibrantColor,
updateUrlWithHost,
updateDashboard,
displayMessages,
processMetricData
};