Рефакторинг комментариев

This commit is contained in:
parent 6b6239ef9d
commit 82a91c0e88
2 changed files with 3 additions and 12 deletions

View file

@ -1,4 +1,3 @@
# models.py
from pydantic import BaseModel
from typing import Optional

View file

@ -84,7 +84,7 @@ class DeviceInfoCollector:
def __init__(self):
self.mac_lookup = MacLookup()
@lru_cache(maxsize=1000) # Снижено с 10000
@lru_cache(maxsize=1000)
def get_vendor(self, mac: str) -> str:
try:
return self.mac_lookup.lookup(mac)
@ -180,10 +180,8 @@ class SubnetService:
devices = []
now = datetime.now(ZoneInfo("UTC")).isoformat()
try:
# Загружаем старую карту
saved_devices = self.storage.load_devices()
# Получаем локальное устройство
local_device = self._get_local_device(subnet)
if local_device:
local_device.status = "online"
@ -192,18 +190,15 @@ class SubnetService:
if local_device.mac:
saved_devices[local_device.mac] = local_device
# Сканируем подсеть
discovered_devices = self.network_scanner.scan(subnet)
# Обрабатываем обнаруженные устройства
def process_device(ip_mac: Tuple[str, str]) -> Optional[Device]:
ip, mac = ip_mac
if local_device and ip == local_device.ip:
return None
# Проверяем, есть ли устройство в старой карте
existing_device = saved_devices.get(mac)
if existing_device:
# Обновляем данные, если MAC совпадает
return Device(
ip=ip,
mac=mac,
@ -213,7 +208,6 @@ class SubnetService:
status="online",
last_seen=now
)
# Новое устройство
vendor = self.device_info.get_vendor(mac)
hostname = self.device_info.get_hostname(ip)
open_ports = self.port_scanner.scan(ip)
@ -231,17 +225,15 @@ class SubnetService:
device_results = executor.map(process_device, discovered_devices)
devices.extend([d for d in device_results if d is not None])
# Добавляем оффлайн-устройства из старой карты
# Добавляет устройства со старой карты, в нынешнюю при отсутствии со статусом offline
active_macs = {d.mac for d in devices if d.mac}
for mac, device in saved_devices.items():
if mac not in active_macs:
device.status = "offline"
devices.append(device)
# Сортировка по IP
devices.sort(key=lambda x: int(ipaddress.IPv4Address(x.ip)))
# Сохраняем обновлённую карту
for device in devices:
if device.mac:
saved_devices[device.mac] = device