Refactor source status fetching logic for improved error handling
Some checks failed
CI / ci (push) Has been cancelled

- Enhanced the JavaScript fetch logic in base.html to better handle API responses, including improved error messaging and structured data handling.
- Updated the way source status is processed, ensuring clearer differentiation between successful and error states for Prometheus and OpenStack metrics.
This commit is contained in:
2026-02-12 15:03:56 +03:00
parent 09136a28e3
commit 572274333c

View File

@@ -107,13 +107,27 @@
var promEl = document.getElementById('source-status-prometheus');
var osEl = document.getElementById('source-status-openstack');
if (!promEl || !osEl) return;
fetch('/api/source-status/').then(function(r) { return r.ok ? r.json() : {}; }).then(function(data) {
updateSourceStatus(promEl, 'Prometheus', data.prometheus);
updateSourceStatus(osEl, 'OpenStack', data.openstack);
}).catch(function() {
updateSourceStatus(promEl, 'Prometheus', { status: 'error', message: 'Failed to fetch status' });
updateSourceStatus(osEl, 'OpenStack', { status: 'error', message: 'Failed to fetch status' });
});
fetch('/api/source-status/')
.then(function(r) {
if (r.ok) return r.json().then(function(data) { return { data: data }; });
return r.json().catch(function() { return {}; }).then(function(body) {
return { error: true, message: (body && body.message) || 'Failed to fetch status' };
});
})
.then(function(result) {
if (result && result.error) {
updateSourceStatus(promEl, 'Prometheus', { status: 'error', message: result.message });
updateSourceStatus(osEl, 'OpenStack', { status: 'error', message: result.message });
} else {
var data = result && result.data;
updateSourceStatus(promEl, 'Prometheus', data && data.prometheus);
updateSourceStatus(osEl, 'OpenStack', data && data.openstack);
}
})
.catch(function() {
updateSourceStatus(promEl, 'Prometheus', { status: 'error', message: 'Failed to fetch status' });
updateSourceStatus(osEl, 'OpenStack', { status: 'error', message: 'Failed to fetch status' });
});
});
})();
</script>