fix: use map for alarm queries to handle mixed formats

This commit is contained in:
tian 2026-05-07 12:30:15 +08:00
parent 25046d0a7b
commit fd90faba47
2 changed files with 5 additions and 18 deletions

View File

@ -17,19 +17,6 @@ import (
"rk3588sys/agent/internal/files"
)
type alarmRecord struct {
ID string `json:"id"`
Timestamp string `json:"timestamp"`
Channel string `json:"channel"`
RuleName string `json:"rule_name"`
RuleType string `json:"rule_type"`
ObjectLabel string `json:"object_label,omitempty"`
Confidence float64 `json:"confidence,omitempty"`
SnapshotURL string `json:"snapshot_url,omitempty"`
ClipURL string `json:"clip_url,omitempty"`
DurationMs int64 `json:"duration_ms,omitempty"`
}
func (s *Server) alarmsPath() string {
return filepath.Join(s.baseDir, "logs", "alarms.jsonl")
}
@ -121,13 +108,13 @@ func (s *Server) handleAlarmsRecent(w http.ResponseWriter, r *http.Request) {
return
}
if alarms == nil {
alarms = make([]alarmRecord, 0)
alarms = make([]map[string]any, 0)
}
writeJSON(w, http.StatusOK, map[string]any{"alarms": alarms})
}
func (s *Server) readRecentAlarms(limit int) ([]alarmRecord, error) {
func (s *Server) readRecentAlarms(limit int) ([]map[string]any, error) {
path := s.alarmsPath()
f, err := os.Open(path)
if err != nil {
@ -163,11 +150,11 @@ func (s *Server) readRecentAlarms(limit int) ([]alarmRecord, error) {
start = len(lines) - limit
}
alarms := make([]alarmRecord, 0, limit)
alarms := make([]map[string]any, 0, limit)
for i := start; i < len(lines); i++ {
var alarm alarmRecord
var alarm map[string]any
if err := json.Unmarshal([]byte(lines[i]), &alarm); err != nil {
continue // skip malformed lines
continue
}
alarms = append(alarms, alarm)
}

Binary file not shown.