Stabilize discovery workflow
This commit is contained in:
@@ -6,14 +6,20 @@ namespace Domovoy\Services\Inventory;
|
||||
|
||||
use Domovoy\Models\Device;
|
||||
use Domovoy\Repositories\DeviceRepository;
|
||||
use Domovoy\Repositories\DiscoveredHostRepository;
|
||||
|
||||
class DeviceService
|
||||
{
|
||||
private DeviceRepository $deviceRepository;
|
||||
private ?DiscoveredHostRepository $discoveredHostRepository;
|
||||
|
||||
public function __construct(DeviceRepository $deviceRepository)
|
||||
public function __construct(
|
||||
DeviceRepository $deviceRepository,
|
||||
?DiscoveredHostRepository $discoveredHostRepository = null
|
||||
)
|
||||
{
|
||||
$this->deviceRepository = $deviceRepository;
|
||||
$this->discoveredHostRepository = $discoveredHostRepository;
|
||||
}
|
||||
|
||||
public function createFromDiscoveredHost(
|
||||
@@ -56,4 +62,31 @@ class DeviceService
|
||||
{
|
||||
$this->deviceRepository->delete($id);
|
||||
}
|
||||
|
||||
public function mergeDiscoveredHost(int $hostId, int $deviceId): void
|
||||
{
|
||||
if ($this->discoveredHostRepository === null) {
|
||||
throw new \RuntimeException('DiscoveredHostRepository is required for merge');
|
||||
}
|
||||
|
||||
$host = $this->discoveredHostRepository->findById($hostId);
|
||||
if ($host === null) {
|
||||
throw new \RuntimeException('Discovered host not found');
|
||||
}
|
||||
|
||||
$device = $this->deviceRepository->findById($deviceId);
|
||||
if ($device === null) {
|
||||
throw new \RuntimeException('Device not found');
|
||||
}
|
||||
|
||||
$device->primaryIp = $device->primaryIp ?: $host->ipAddress;
|
||||
$device->macAddress = $device->macAddress ?: $host->macAddress;
|
||||
$device->hostname = $device->hostname ?: $host->hostname;
|
||||
$device->vendor = $device->vendor ?: $host->vendor;
|
||||
$this->deviceRepository->save($device);
|
||||
|
||||
$host->status = 'merged';
|
||||
$host->matchedDeviceId = (string)$device->id;
|
||||
$this->discoveredHostRepository->save($host);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,9 +48,15 @@ class MergeSuggestionService
|
||||
}
|
||||
}
|
||||
|
||||
// Low confidence: IP match
|
||||
if ($host->primaryIp !== null) {
|
||||
// Would need findByIp in repository — skip for now
|
||||
if ($host->ipAddress !== '') {
|
||||
$device = $this->deviceRepository->findByIp($host->ipAddress);
|
||||
if ($device !== null) {
|
||||
$suggestions[] = [
|
||||
'device' => $device,
|
||||
'confidence' => 40,
|
||||
'reason' => 'IP адрес совпадает',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $suggestions;
|
||||
|
||||
Reference in New Issue
Block a user