Persist per-host scan results and expose hosts endpoint.
Store checked hosts for each scan and add API to fetch host-level up/down probe results. Made-with: Cursor
This commit is contained in:
@@ -280,3 +280,38 @@ where id = $1`
|
||||
|
||||
return current, true
|
||||
}
|
||||
|
||||
func (s *PostgresStore) SaveHostResult(scanID string, host HostResult) error {
|
||||
query := `
|
||||
insert into scan_hosts (scan_id, ip, is_up, checked_at)
|
||||
values ($1, $2, $3, $4)`
|
||||
_, err := s.db.ExecContext(context.Background(), query, scanID, host.IP, host.IsUp, host.CheckedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *PostgresStore) ListHostResults(scanID string, limit int) []HostResult {
|
||||
if limit <= 0 {
|
||||
limit = 500
|
||||
}
|
||||
query := `
|
||||
select ip::text, is_up, checked_at
|
||||
from scan_hosts
|
||||
where scan_id = $1
|
||||
order by checked_at desc
|
||||
limit $2`
|
||||
rows, err := s.db.QueryContext(context.Background(), query, scanID, limit)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
out := make([]HostResult, 0, limit)
|
||||
for rows.Next() {
|
||||
var h HostResult
|
||||
if err = rows.Scan(&h.IP, &h.IsUp, &h.CheckedAt); err != nil {
|
||||
return nil
|
||||
}
|
||||
out = append(out, h)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -70,6 +70,11 @@ func (r *Runner) run(job ScanJob) {
|
||||
defer wg.Done()
|
||||
for ip := range workCh {
|
||||
isUp := probeHost(ip, job.Options.PingTimeoutMS)
|
||||
_ = r.store.SaveHostResult(job.ID, HostResult{
|
||||
IP: ip,
|
||||
IsUp: isUp,
|
||||
CheckedAt: time.Now().UTC(),
|
||||
})
|
||||
|
||||
mu.Lock()
|
||||
done++
|
||||
|
||||
+42
-3
@@ -59,16 +59,26 @@ type Store interface {
|
||||
GetScan(id string) (ScanJob, bool)
|
||||
ListScans(limit int) []ScanJob
|
||||
UpdateScan(id string, update ScanUpdate) (ScanJob, bool)
|
||||
SaveHostResult(scanID string, host HostResult) error
|
||||
ListHostResults(scanID string, limit int) []HostResult
|
||||
}
|
||||
|
||||
type HostResult struct {
|
||||
IP string `json:"ip"`
|
||||
IsUp bool `json:"is_up"`
|
||||
CheckedAt time.Time `json:"checked_at"`
|
||||
}
|
||||
|
||||
type MemoryStore struct {
|
||||
mu sync.RWMutex
|
||||
jobs map[string]ScanJob
|
||||
mu sync.RWMutex
|
||||
jobs map[string]ScanJob
|
||||
hosts map[string][]HostResult
|
||||
}
|
||||
|
||||
func NewMemoryStore() *MemoryStore {
|
||||
return &MemoryStore{
|
||||
jobs: make(map[string]ScanJob),
|
||||
jobs: make(map[string]ScanJob),
|
||||
hosts: make(map[string][]HostResult),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,6 +171,35 @@ func (s *MemoryStore) UpdateScan(id string, update ScanUpdate) (ScanJob, bool) {
|
||||
return job, true
|
||||
}
|
||||
|
||||
func (s *MemoryStore) SaveHostResult(scanID string, host HostResult) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if _, ok := s.jobs[scanID]; !ok {
|
||||
return errors.New("scan not found")
|
||||
}
|
||||
s.hosts[scanID] = append(s.hosts[scanID], host)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) ListHostResults(scanID string, limit int) []HostResult {
|
||||
if limit <= 0 {
|
||||
limit = 500
|
||||
}
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
items := s.hosts[scanID]
|
||||
if len(items) <= limit {
|
||||
out := make([]HostResult, len(items))
|
||||
copy(out, items)
|
||||
return out
|
||||
}
|
||||
out := make([]HostResult, limit)
|
||||
copy(out, items[:limit])
|
||||
return out
|
||||
}
|
||||
|
||||
func applyDefaultOptions(opts *ScanOptions) {
|
||||
if opts.PingTimeoutMS <= 0 {
|
||||
opts.PingTimeoutMS = 700
|
||||
|
||||
Reference in New Issue
Block a user