Files
michaelschiemer/src/Framework/Filesystem/ValueObjects/ScannerMemoryUsage.php
Michael Schiemer 55a330b223 Enable Discovery debug logging for production troubleshooting
- Add DISCOVERY_LOG_LEVEL=debug
- Add DISCOVERY_SHOW_PROGRESS=true
- Temporary changes for debugging InitializerProcessor fixes on production
2025-08-11 20:13:26 +02:00

90 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Framework\Filesystem\ValueObjects;
use App\Framework\Core\ValueObjects\Byte;
use App\Framework\Core\ValueObjects\Percentage;
use App\Framework\Performance\MemoryMonitor;
/**
* Represents memory usage statistics for the scanner
*/
final readonly class ScannerMemoryUsage
{
public function __construct(
public Byte $current,
public Byte $peak,
public Byte $limit,
public Percentage $usage
) {
}
public static function fromMemoryMonitor(MemoryMonitor $monitor): self
{
return new self(
current: $monitor->getCurrentMemory(),
peak: $monitor->getPeakMemory(),
limit: $monitor->getMemoryLimit(),
usage: $monitor->getMemoryUsagePercentage()
);
}
public static function current(): self
{
$monitor = new MemoryMonitor();
return self::fromMemoryMonitor($monitor);
}
/**
* Check if memory usage is critical (>90%)
*/
public function isCritical(): bool
{
return $this->usage->greaterThan(Percentage::from(90.0));
}
/**
* Check if memory usage is high (>80%)
*/
public function isHigh(): bool
{
return $this->usage->greaterThan(Percentage::from(80.0));
}
/**
* Get available memory
*/
public function getAvailable(): Byte
{
return $this->limit->subtract($this->current);
}
/**
* Check if we have enough memory for an operation
*/
public function hasEnoughMemoryFor(Byte $required): bool
{
return $this->getAvailable()->greaterThan($required);
}
public function toArray(): array
{
return [
'current' => $this->current->toBytes(),
'current_human' => $this->current->toHumanReadable(),
'peak' => $this->peak->toBytes(),
'peak_human' => $this->peak->toHumanReadable(),
'limit' => $this->limit->toBytes(),
'limit_human' => $this->limit->toHumanReadable(),
'usage_percentage' => $this->usage->getValue(),
'is_critical' => $this->isCritical(),
'is_high' => $this->isHigh(),
'available' => $this->getAvailable()->toBytes(),
'available_human' => $this->getAvailable()->toHumanReadable(),
];
}
}