2

Build a Pi-hole-style dashboard and query log

This commit is contained in:
2026-03-12 17:12:19 +01:00
parent b7943e69db
commit 0a14dd1df9
5 changed files with 1012 additions and 450 deletions

View File

@@ -102,8 +102,15 @@ func (s *Service) GetOverview(ctx context.Context, since time.Time, limit int, o
return overview, nil
}
func (s *Service) ListEvents(ctx context.Context, limit int) ([]model.Event, error) {
return s.store.ListRecentEvents(ctx, limit)
func (s *Service) ListEvents(ctx context.Context, since time.Time, limit int, options model.EventListOptions) ([]model.Event, error) {
items, err := s.store.ListEvents(ctx, since, limit, options)
if err != nil {
return nil, err
}
if err := s.decorateEvents(ctx, items); err != nil {
return nil, err
}
return items, nil
}
func (s *Service) ListIPs(ctx context.Context, limit int, state string) ([]model.IPState, error) {
@@ -673,6 +680,47 @@ func recentRowIPs(items []model.RecentIPRow) []string {
return result
}
func eventIPs(items []model.Event) []string {
result := make([]string, 0, len(items))
for _, item := range items {
result = append(result, item.ClientIP)
}
return result
}
func (s *Service) decorateEvents(ctx context.Context, items []model.Event) error {
if len(items) == 0 {
return nil
}
investigations, err := s.store.GetInvestigationsForIPs(ctx, eventIPs(items))
if err != nil {
return err
}
actionsByIP := make(map[string]model.ActionAvailability, len(items))
for index := range items {
ip := items[index].ClientIP
if investigation, ok := investigations[ip]; ok {
items[index].Bot = investigation.Bot
} else {
s.enqueueInvestigation(ip)
}
if _, ok := actionsByIP[ip]; !ok {
state := model.IPState{
IP: ip,
State: items[index].CurrentState,
ManualOverride: items[index].ManualOverride,
}
if state.State == "" {
state.State = model.IPStateObserved
}
backend := s.resolveOPNsenseStatus(ctx, state)
actionsByIP[ip] = actionAvailability(state, backend)
}
items[index].Actions = actionsByIP[ip]
}
return nil
}
func (s *Service) decorateOverviewTopIPs(ctx context.Context, overview *model.Overview) error {
if overview == nil {
return nil