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:
+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