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:
parent
5496ca1f3a
commit
6b364ab52a
@ -30,15 +30,33 @@ return view.extend({
|
|||||||
}
|
}
|
||||||
// Also handle direct countries object if present
|
// Also handle direct countries object if present
|
||||||
if (data.countries && typeof data.countries === 'object') {
|
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;
|
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) {
|
render: function(data) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var s = data || {};
|
var s = data || {};
|
||||||
s.countries = this.parseCountries(s);
|
s.countries = this.parseCountries(s);
|
||||||
|
s.alerts = this.parseAlerts(s);
|
||||||
|
|
||||||
var view = E('div', { 'class': 'cs-view' }, [
|
var view = E('div', { 'class': 'cs-view' }, [
|
||||||
// Header
|
// Header
|
||||||
@ -178,6 +196,7 @@ return view.extend({
|
|||||||
var self = this;
|
var self = this;
|
||||||
return api.getOverview().then(function(s) {
|
return api.getOverview().then(function(s) {
|
||||||
s.countries = self.parseCountries(s);
|
s.countries = self.parseCountries(s);
|
||||||
|
s.alerts = self.parseAlerts(s);
|
||||||
var el = document.getElementById('cs-stats');
|
var el = document.getElementById('cs-stats');
|
||||||
if (el) dom.content(el, self.renderStats(s));
|
if (el) dom.content(el, self.renderStats(s));
|
||||||
el = document.getElementById('cs-alerts');
|
el = document.getElementById('cs-alerts');
|
||||||
|
|||||||
@ -2317,27 +2317,21 @@ get_overview() {
|
|||||||
json_add_string "top_countries_raw" "[$countries]"
|
json_add_string "top_countries_raw" "[$countries]"
|
||||||
|
|
||||||
# Recent decisions (limited to 10 for display)
|
# 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
|
if [ "$cs_running" = "1" ]; then
|
||||||
run_cscli decisions list -o json 2>/dev/null | \
|
decisions_raw=$(run_cscli decisions list -o json --limit 10 2>/dev/null || echo "[]")
|
||||||
jsonfilter -e '@[0]' -e '@[1]' -e '@[2]' -e '@[3]' -e '@[4]' \
|
[ -z "$decisions_raw" ] && decisions_raw="[]"
|
||||||
-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
|
|
||||||
fi
|
fi
|
||||||
json_close_array
|
json_add_string "decisions_raw" "$decisions_raw"
|
||||||
|
|
||||||
# Recent alerts (limited to 8)
|
# Recent alerts as raw JSON array string
|
||||||
json_add_array "alerts"
|
local alerts_raw="[]"
|
||||||
if [ "$cs_running" = "1" ]; then
|
if [ "$cs_running" = "1" ]; then
|
||||||
run_cscli alerts list -o json --limit 8 2>/dev/null | \
|
alerts_raw=$(run_cscli alerts list -o json --limit 8 2>/dev/null || echo "[]")
|
||||||
jsonfilter -e '@[*]' 2>/dev/null | head -8 | \
|
[ -z "$alerts_raw" ] && alerts_raw="[]"
|
||||||
while IFS= read -r line; do
|
|
||||||
[ -n "$line" ] && json_add_string "" "$line"
|
|
||||||
done
|
|
||||||
fi
|
fi
|
||||||
json_close_array
|
json_add_string "alerts_raw" "$alerts_raw"
|
||||||
|
|
||||||
# CrowdSec logs (last 30 lines)
|
# CrowdSec logs (last 30 lines)
|
||||||
json_add_array "logs"
|
json_add_array "logs"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user