feat: add app and database modules
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Monitoring;
|
||||
|
||||
class MonitoringFormatter
|
||||
{
|
||||
public static function bytes(int|float $bytes, int $precision = 1): string
|
||||
{
|
||||
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
$bytes = max($bytes, 0);
|
||||
$pow = (int) floor(($bytes ? log($bytes) : 0) / log(1024));
|
||||
$pow = min($pow, count($units) - 1);
|
||||
$bytes /= (1 << (10 * $pow));
|
||||
|
||||
return round($bytes, $precision).' '.$units[$pow];
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a human-readable byte string (e.g. "1.5 KB", "2 GB") back into a float byte count.
|
||||
* Falls back to numeric cast for plain numbers and returns the leading number when the
|
||||
* unit suffix is unknown.
|
||||
*/
|
||||
public static function parseBytes(string $value): float
|
||||
{
|
||||
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
$parts = explode(' ', trim($value));
|
||||
if (count($parts) < 2) {
|
||||
return (float) $value;
|
||||
}
|
||||
$num = (float) $parts[0];
|
||||
$idx = array_search(strtoupper($parts[1]), $units, true);
|
||||
if ($idx === false) {
|
||||
return $num;
|
||||
}
|
||||
|
||||
return $num * (1024 ** $idx);
|
||||
}
|
||||
|
||||
public static function duration(int|float $seconds): string
|
||||
{
|
||||
$days = (int) floor($seconds / 86400);
|
||||
$hours = (int) floor(($seconds % 86400) / 3600);
|
||||
$minutes = (int) floor(($seconds % 3600) / 60);
|
||||
|
||||
$parts = [];
|
||||
if ($days > 0) {
|
||||
$parts[] = "{$days}d";
|
||||
}
|
||||
if ($hours > 0) {
|
||||
$parts[] = "{$hours}h";
|
||||
}
|
||||
if ($minutes > 0) {
|
||||
$parts[] = "{$minutes}m";
|
||||
}
|
||||
|
||||
return count($parts) > 0 ? implode(' ', $parts) : '< 1m';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user