feat(mac-locations): VLAN-aware port MAC counts, access/trunk thresholds, likely_attachment

Made-with: Cursor
This commit is contained in:
PTah
2026-04-13 13:11:20 +10:00
parent 2d8992549b
commit 42e3c0c925
6 changed files with 244 additions and 16 deletions
+30
View File
@@ -570,6 +570,36 @@ limit $3`
return out
}
func (s *PostgresStore) CountDistinctMACsPerSwitchPort(scanID string) (map[string]int, error) {
query := `
select ip::text, if_index, bridge_port, vlan,
count(distinct lower(replace(trim(coalesce(mac, '')), '-', ':')))::int as c
from scan_port_devices
where scan_id = $1
and trim(coalesce(mac, '')) <> ''
group by ip, if_index, bridge_port, vlan`
rows, err := s.db.QueryContext(context.Background(), query, scanID)
if err != nil {
return nil, err
}
defer rows.Close()
out := make(map[string]int, 4096)
for rows.Next() {
var ip string
var ifIndex, bridgePort, vlan, c int
if err = rows.Scan(&ip, &ifIndex, &bridgePort, &vlan, &c); err != nil {
return nil, err
}
k := PortFDBLocationKey(ip, ifIndex, bridgePort, vlan)
out[k] = c
}
if err = rows.Err(); err != nil {
return nil, err
}
return out, nil
}
func (s *PostgresStore) PurgeAllScanData() error {
// Дочерние таблицы ссылаются на scan_jobs — CASCADE очищает всё; RESTART IDENTITY сбрасывает bigserial.
_, err := s.db.ExecContext(context.Background(), `truncate table scan_jobs restart identity cascade`)