fix(crowdsec-dashboard): Fix alerts and countries display in overview

- Change RPCD to return alerts_raw and decisions_raw as JSON strings
- Add parseAlerts() to parse alerts_raw in JavaScript
- Fix countries and alerts now display correctly in overview

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
CyberMind-FR 2026-02-01 09:05:24 +01:00
parent 5496ca1f3a
commit 6b364ab52a
2 changed files with 30 additions and 17 deletions

View File

@ -30,15 +30,33 @@ return view.extend({
}
// Also handle direct countries object if present
if (data.countries && typeof data.countries === 'object') {
Object.assign(countries, data.countries);
for (var k in data.countries) countries[k] = data.countries[k];
}
return countries;
},
parseAlerts: function(data) {
var alerts = [];
// Handle alerts_raw (JSON string array)
if (data.alerts_raw) {
try {
alerts = typeof data.alerts_raw === 'string'
? JSON.parse(data.alerts_raw)
: data.alerts_raw;
} catch (e) {}
}
// Also handle direct alerts array if present
if (Array.isArray(data.alerts) && data.alerts.length > 0) {
alerts = data.alerts;
}
return Array.isArray(alerts) ? alerts : [];
},
render: function(data) {
var self = this;
var s = data || {};
s.countries = this.parseCountries(s);
s.alerts = this.parseAlerts(s);
var view = E('div', { 'class': 'cs-view' }, [
// Header
@ -178,6 +196,7 @@ return view.extend({
var self = this;
return api.getOverview().then(function(s) {
s.countries = self.parseCountries(s);
s.alerts = self.parseAlerts(s);
var el = document.getElementById('cs-stats');
if (el) dom.content(el, self.renderStats(s));
el = document.getElementById('cs-alerts');

View File

@ -2317,27 +2317,21 @@ get_overview() {
json_add_string "top_countries_raw" "[$countries]"
# Recent decisions (limited to 10 for display)
json_add_array "decisions"
# Recent decisions as raw JSON array string
local decisions_raw="[]"
if [ "$cs_running" = "1" ]; then
run_cscli decisions list -o json 2>/dev/null | \
jsonfilter -e '@[0]' -e '@[1]' -e '@[2]' -e '@[3]' -e '@[4]' \
-e '@[5]' -e '@[6]' -e '@[7]' -e '@[8]' -e '@[9]' 2>/dev/null | \
while IFS= read -r line; do
[ -n "$line" ] && [ "$line" != "null" ] && json_add_string "" "$line"
done
decisions_raw=$(run_cscli decisions list -o json --limit 10 2>/dev/null || echo "[]")
[ -z "$decisions_raw" ] && decisions_raw="[]"
fi
json_close_array
json_add_string "decisions_raw" "$decisions_raw"
# Recent alerts (limited to 8)
json_add_array "alerts"
# Recent alerts as raw JSON array string
local alerts_raw="[]"
if [ "$cs_running" = "1" ]; then
run_cscli alerts list -o json --limit 8 2>/dev/null | \
jsonfilter -e '@[*]' 2>/dev/null | head -8 | \
while IFS= read -r line; do
[ -n "$line" ] && json_add_string "" "$line"
done
alerts_raw=$(run_cscli alerts list -o json --limit 8 2>/dev/null || echo "[]")
[ -z "$alerts_raw" ] && alerts_raw="[]"
fi
json_close_array
json_add_string "alerts_raw" "$alerts_raw"
# CrowdSec logs (last 30 lines)
json_add_array "logs"