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

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 pydantic import BaseModel
from typing import Optional from typing import Optional

View file

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