-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalerts.js
149 lines (135 loc) · 5.78 KB
/
alerts.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
import { updateFormVisibility } from './utils.js';
async function updateAlertConfigs(hostname) {
try {
const response = await fetch(`/alert_config?hostname=${hostname}`);
const configs = await response.json();
const configList = document.getElementById('alertConfigList');
configList.innerHTML = '';
configs.filter(config => hostname === 'all' || config.hostname === hostname).forEach(config => {
const configDiv = document.createElement('div');
configDiv.className = 'alert alert-secondary';
configDiv.innerHTML = `
<strong>${hostname === 'all' ? config.hostname + ' - ' : ''}${config.metric_name}</strong>:
${config.condition} ${config.threshold}
for ${config.duration} seconds
<div class="float-end">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="alertEnabled_${config.id}"
${config.enabled ? 'checked' : ''} onchange="toggleAlertState(${config.id}, '${config.hostname}', '${config.metric_name}', this.checked)">
<label class="form-check-label" for="alertEnabled_${config.id}">Enabled</label>
</div>
<button class="btn btn-danger btn-sm" onclick="deleteAlertConfig(${config.id})">Delete</button>
</div>
`;
configList.appendChild(configDiv);
});
} catch (error) {
console.error('Error updating alert configs:', error);
}
}
async function addAlertConfig(event) {
event.preventDefault();
const alertConfig = {
hostname: document.getElementById('hostname').value,
metric_name: document.getElementById('metric_name').value,
condition: document.getElementById('condition').value,
threshold: parseFloat(document.getElementById('threshold').value),
duration: parseInt(document.getElementById('duration').value)
};
try {
const response = await fetch('/alert_config', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(alertConfig),
});
if (response.ok) {
console.log('Alert config added successfully');
document.getElementById('alertForm').reset();
const selectedHostname = document.querySelector('#hostSelector select').value;
updateFormVisibility(selectedHostname);
await updateAlertConfigs(selectedHostname);
} else {
const errorData = await response.json();
console.error('Failed to add alert config:', errorData.error);
}
} catch (error) {
console.error('Error adding alert config:', error);
}
}
async function deleteAlertConfig(id) {
try {
const response = await fetch(`/alert_config`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ id }),
});
if (response.ok) {
console.log(`Alert config deleted for id ${id}`);
await updateAlertConfigs(document.querySelector('#hostSelector select').value);
} else {
console.error('Failed to delete alert config');
}
} catch (error) {
console.error('Error deleting alert config:', error);
}
}
async function toggleAlertState(id, hostname, metric_name, enabled) {
try {
const response = await fetch('/alert_state', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ id, enabled }),
});
if (response.ok) {
console.log(`Alert state updated for ${hostname} - ${metric_name}`);
} else {
console.error('Failed to update alert state');
}
} catch (error) {
console.error('Error updating alert state:', error);
}
}
async function updateRecentAlerts(hostname) {
try {
const response = await fetch(`/fetch/recent_alerts?hostname=${hostname}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const alerts = await response.json();
console.log('Fetched recent alerts:', alerts); // For debugging
const alertsList = document.getElementById('recentAlertsList');
alertsList.innerHTML = '';
if (alerts.length === 0) {
alertsList.innerHTML = '<div class="alert alert-info">No recent alerts.</div>';
} else {
alerts.forEach(alert => {
const alertDiv = document.createElement('div');
alertDiv.className = 'alert alert-warning';
alertDiv.innerHTML = `
<strong>${hostname === 'all' ? alert.hostname + ' - ' : ''}${alert.metric_name}</strong>:
Value ${alert.value} ${alert.condition} ${alert.threshold}
at ${new Date(alert.timestamp * 1000).toLocaleString()}
`;
alertsList.appendChild(alertDiv);
});
}
} catch (error) {
console.error('Error updating recent alerts:', error);
}
}
function setupAlertUpdates() {
const updateInterval = 60000; // Update every minute
updateRecentAlerts(document.querySelector('#hostSelector select').value);
setInterval(() => {
updateRecentAlerts(document.querySelector('#hostSelector select').value);
}, updateInterval);
}
window.deleteAlertConfig = deleteAlertConfig;
window.toggleAlertState = toggleAlertState;
export { updateAlertConfigs, addAlertConfig, deleteAlertConfig, toggleAlertState, updateRecentAlerts, setupAlertUpdates };