-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdowntimes.js
78 lines (72 loc) · 2.87 KB
/
downtimes.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
async function updateDowntimes(hostname) {
try {
const response = await fetch(`/downtime?hostname=${hostname}`);
const downtimes = await response.json();
const downtimeList = document.getElementById('downtimeList');
downtimeList.innerHTML = '';
if (downtimes.length === 0) {
downtimeList.innerHTML = '<div class="alert alert-info">No downtimes scheduled.</div>';
} else {
downtimes.forEach(downtime => {
const downtimeDiv = document.createElement('div');
downtimeDiv.className = 'alert alert-info';
downtimeDiv.innerHTML = `
<strong>${hostname === 'all' ? downtime.hostname + ': ' : ''}</strong>
${new Date(downtime.start_time * 1000).toLocaleString()} -
${new Date(downtime.end_time * 1000).toLocaleString()}
<button class="btn btn-danger btn-sm float-end" onclick="deleteDowntime(${downtime.id})">Delete</button>
`;
downtimeList.appendChild(downtimeDiv);
});
}
} catch (error) {
console.error('Error updating downtimes:', error);
}
}
async function addDowntime(event) {
event.preventDefault();
const hostname = document.querySelector('#hostSelector select').value;
const downtimeConfig = {
hostname: hostname === 'all' ? document.getElementById('downtimeHostname').value : hostname,
start_time: new Date(document.getElementById('downtimeStart').value).getTime() / 1000,
end_time: new Date(document.getElementById('downtimeEnd').value).getTime() / 1000
};
try {
const response = await fetch('/downtime', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(downtimeConfig),
});
if (response.ok) {
console.log('Downtime added successfully');
document.getElementById('downtimeForm').reset();
} else {
console.error('Failed to add downtime');
}
} catch (error) {
console.error('Error adding downtime:', error);
}
}
async function deleteDowntime(id) {
try {
const response = await fetch('/downtime', {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ id }),
});
if (response.ok) {
console.log(`Downtime deleted for id ${id}`);
await updateDowntimes(document.querySelector('#hostSelector select').value);
} else {
console.error('Failed to delete downtime');
}
} catch (error) {
console.error('Error deleting downtime:', error);
}
}
window.deleteDowntime = deleteDowntime;
export { updateDowntimes, addDowntime, deleteDowntime };