Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d4c70b9
add Cores::of() to statically make sure the value is positive
Baptouuuu May 8, 2025
f7adc02
add Percentage::maybe() to statically make sure the value is positive
Baptouuuu May 8, 2025
55dc16d
add MountPoint::of() to statically make sure the value is correct
Baptouuuu May 8, 2025
ddfd505
add Usage::maybe() to statically make sure the value is correct
Baptouuuu May 8, 2025
acad5d2
make Volume constructor private
Baptouuuu May 8, 2025
779bca9
do not keep in memory string representation of bytes
Baptouuuu May 8, 2025
aa33fd1
add Bytes::of() to statically make sure the value is correct
Baptouuuu May 8, 2025
fda2099
add Command::of() to statically make sure the value is correct
Baptouuuu May 8, 2025
7eeb557
add Memory::maybe() to statically make sure the value is correct
Baptouuuu May 8, 2025
ade191a
add Pid::of() to statically make sure the value is correct
Baptouuuu May 8, 2025
9e09966
add User::of() to statically make sure the value is correct
Baptouuuu May 8, 2025
1f204c9
make Cpu constructor private
Baptouuuu May 8, 2025
d673c15
Server::loadAverage() now returns an Attempt as the load average may …
Baptouuuu May 8, 2025
996a84c
make Memory constructor private
Baptouuuu May 8, 2025
54455c7
remove unused exception
Baptouuuu May 8, 2025
3b93751
make Process constructor private
Baptouuuu May 8, 2025
064e941
flag constructors as pure
Baptouuuu May 8, 2025
cf0e128
add missing return types
Baptouuuu May 8, 2025
b22b1d5
use unwrap to show the error
Baptouuuu May 8, 2025
21d162a
avoid re-parsing bytes
Baptouuuu May 8, 2025
a28ee2d
debug
Baptouuuu May 8, 2025
9a317e0
fix map key
Baptouuuu May 8, 2025
d0f09ed
Revert "debug"
Baptouuuu May 8, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
- Requires `innmind/immutable:~5.14`
- `Innmind\Server\Status\Server::cpu()` now returns `Innmind\Immutable\Attempt<Innmind\Server\Status\Server\Cpu>`
- `Innmind\Server\Status\Server::memory()` now returns `Innmind\Immutable\Attempt<Innmind\Server\Status\Server\Memory>`
- `Innmind\Server\Status\Server::loadAverage()` now returns `Innmind\Immutable\Attempt<Innmind\Server\Status\Server\LoadAverage>`

### Removed

- `Innmind\Server\Status\Exception\EmptyPathNotAllowed`
- `Innmind\Server\Status\Exception\BytesCannotBeNegative`
- `Innmind\Server\Status\Exception\EmptyCommandNotAllowed`
- `Innmind\Server\Status\Exception\OutOfBoundsPercentage`
- `Innmind\Server\Status\Exception\LowestPidPossibleIsOne`
- `Innmind\Server\Status\Exception\EmptyUserNotAllowed`
- `Innmind\Server\Status\Exception\LoadAverageCannotBeNegative`
- `Innmind\Server\Status\Exception\DomainException`

## 4.1.1 - 2024-09-30

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"innmind/time-continuum": "^4.1.1",
"innmind/url": "~4.0",
"psr/log": "~3.0",
"innmind/server-control": "~6.0"
"innmind/server-control": "~6.0",
"innmind/validation": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 0 additions & 8 deletions src/Exception/BytesCannotBeNegative.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Exception/DomainException.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Exception/EmptyCommandNotAllowed.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Exception/EmptyPathNotAllowed.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Exception/EmptyUserNotAllowed.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Exception/LoadAverageCannotBeNegative.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Exception/LowestPidPossibleIsOne.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Exception/OutOfBoundsPercentage.php

This file was deleted.

29 changes: 15 additions & 14 deletions src/Facade/Cpu/LinuxFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Processes,
Command,
};
use Innmind\Validation\Is;
use Innmind\Immutable\{
Str,
Attempt,
Expand Down Expand Up @@ -85,22 +86,22 @@ private function parse(Str $output): Attempt
->toString(),
)
->map(static fn($cores) => (int) $cores)
->match(
static fn($cores) => $cores,
static fn() => 1,
);
->keep(Is::int()->positive()->asPredicate())
->otherwise(static fn() => Maybe::just(1))
->map(Cores::of(...));

$user = $percentages->get('user');
$sys = $percentages->get('sys');
$idle = $percentages->get('idle');
$user = $percentages
->get('user')
->flatMap(Percentage::maybe(...));
$sys = $percentages
->get('sys')
->flatMap(Percentage::maybe(...));
$idle = $percentages
->get('idle')
->flatMap(Percentage::maybe(...));

return Maybe::all($user, $sys, $idle)
->map(static fn(float $user, float $sys, float $idle) => new Cpu(
new Percentage($user),
new Percentage($sys),
new Percentage($idle),
new Cores($cores),
))
return Maybe::all($user, $sys, $idle, $cores)
->map(Cpu::of(...))
->match(
Attempt::result(...),
static fn() => Attempt::error(new \RuntimeException('Failed to parse CPU usage')),
Expand Down
24 changes: 14 additions & 10 deletions src/Facade/Cpu/OSXFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Processes,
Command,
};
use Innmind\Validation\Is;
use Innmind\Immutable\{
Str,
Attempt,
Expand Down Expand Up @@ -96,18 +97,21 @@ private function parse(Str $output): Attempt
->flatMap(static fn($output) => $output->get('cores'))
->map(static fn($cores) => $cores->toString())
->map(static fn($cores) => (int) $cores)
->otherwise(static fn() => Maybe::just(1));
$user = $percentages->get('user');
$sys = $percentages->get('sys');
$idle = $percentages->get('idle');
->keep(Is::int()->positive()->asPredicate())
->otherwise(static fn() => Maybe::just(1))
->map(Cores::of(...));
$user = $percentages
->get('user')
->flatMap(Percentage::maybe(...));
$sys = $percentages
->get('sys')
->flatMap(Percentage::maybe(...));
$idle = $percentages
->get('idle')
->flatMap(Percentage::maybe(...));

return Maybe::all($user, $sys, $idle, $cores)
->map(static fn(float $user, float $sys, float $idle, int $cores) => new Cpu(
new Percentage($user),
new Percentage($sys),
new Percentage($idle),
new Cores($cores),
))
->map(Cpu::of(...))
->match(
Attempt::result(...),
static fn() => Attempt::error(new \RuntimeException('Failed to parse CPU usage')),
Expand Down
13 changes: 10 additions & 3 deletions src/Facade/LoadAverage/PhpFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@
namespace Innmind\Server\Status\Facade\LoadAverage;

use Innmind\Server\Status\Server\LoadAverage;
use Innmind\Immutable\Attempt;

/**
* @internal
*/
final class PhpFacade
{
public function __invoke(): LoadAverage
/**
* @return Attempt<LoadAverage>
*/
public function __invoke(): Attempt
{
/** @var array{0: int, 1: int, 2: int} */
/** @var array{0: float, 1: float, 2: float} */
$load = \sys_getloadavg();

return new LoadAverage($load[0], $load[1], $load[2]);
return LoadAverage::maybe($load[0], $load[1], $load[2])->match(
Attempt::result(...),
static fn() => Attempt::error(new \RuntimeException('Failed to load load average')),
);
}
}
65 changes: 39 additions & 26 deletions src/Facade/Memory/LinuxFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Processes,
Command,
};
use Innmind\Validation\Is;
use Innmind\Immutable\{
Str,
Map,
Expand Down Expand Up @@ -67,45 +68,57 @@ public function __invoke(): Attempt
*/
private function parse(Str $output): Attempt
{
/** @var Map<string, int> */
$amounts = $output
->trim()
->split("\n")
->filter(static fn(Str $line) => $line->matches(
'~^('.\implode('|', \array_keys(self::$entries)).'):~',
))
->reduce(
Map::of(),
static function(Map $map, Str $line): Map {
$elements = $line
->capture('~^(?P<key>[a-zA-Z]+): +(?P<value>\d+) kB$~')
->map(static fn($_, $part) => $part->toString());

return Maybe::all($elements->get('key'), $elements->get('value'))
->map(static fn(string $key, string $value) => [$key, (int) $value])
->match(
static fn($pair) => ($map)(
self::$entries[$pair[0]],
$pair[1] * 1024, // 1024 represents a kilobyte
),
static fn() => $map,
);
},
->map(
static fn($line) => $line
->capture('~^(?P<key>[a-zA-Z]+): +(?P<value>\d+) kB$~')
->map(static fn($_, $part) => $part->toString()),
)
->flatMap(
static fn($elements) => Maybe::all(
$elements->get('key'),
$elements
->get('value')
->map(static fn($value) => (int) $value)
->keep(
Is::int()
->positive()
->or(Is::value(0))
->asPredicate(),
)
->map(Bytes::of(...)),
)
->map(static fn(string $key, Bytes $value) => [
self::$entries[$key],
$value,
])
->toSequence(),
);
$amounts = Map::of(...$amounts->toList())
->map(static fn($_, $value) => Bytes::of(
$value->toInt() * 1024, // 1024 represents a kilobyte
));
$total = $amounts->get('total');
$free = $amounts->get('free');
$active = $amounts->get('active');
$swap = $amounts->get('swap');
$used = Maybe::all($total, $free)->map(static fn(int $total, int $free) => $total - $free);
$used = Maybe::all($total, $free)
->map(static fn(Bytes $total, Bytes $free) => $total->toInt() - $free->toInt())
->keep(
Is::int()
->positive()
->or(Is::value(0))
->asPredicate(),
)
->map(Bytes::of(...));

return Maybe::all($total, $active, $free, $swap, $used)
->map(static fn(int $total, int $active, int $free, int $swap, int $used) => new Memory(
new Bytes($total),
new Bytes($active),
new Bytes($free),
new Bytes($swap),
new Bytes($used),
))
->map(Memory::of(...))
->match(
Attempt::result(...),
static fn() => Attempt::error(new \RuntimeException('Failed to parse memory usage')),
Expand Down
22 changes: 10 additions & 12 deletions src/Facade/Memory/OSXFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public function __invoke(): Attempt
->trim()
->capture('~^hw.memsize: (?P<total>\d+)$~')
->get('total')
->map(static fn($total) => $total->toString());
->map(static fn($total) => $total->toString())
->flatMap(Bytes::maybe(...));
$swap = $this
->run(
Command::foreground('sysctl')
Expand All @@ -56,7 +57,7 @@ public function __invoke(): Attempt
->capture('~used = (?P<swap>\d+[\.,]?\d*[KMGTP])~')
->get('swap')
->map(static fn($swap) => $swap->toString())
->flatMap(static fn($swap) => Bytes::of($swap));
->flatMap(Bytes::maybe(...));
$amounts = $this
->run(
Command::foreground('top')
Expand All @@ -74,10 +75,10 @@ public function __invoke(): Attempt
->map(static fn($_, $amount) => $amount->toString());
$unused = $amounts
->get('unused')
->flatMap(static fn($unused) => Bytes::of($unused));
->flatMap(Bytes::maybe(...));
$used = $amounts
->get('used')
->flatMap(static fn($used) => Bytes::of($used));
->flatMap(Bytes::maybe(...));
$active = $this
->run(
Command::foreground('vm_stat')->pipe(
Expand All @@ -88,16 +89,13 @@ public function __invoke(): Attempt
->trim()
->capture('~(?P<active>\d+)~')
->get('active')
->map(static fn($active) => $active->toString());
->map(static fn($active) => $active->toString())
->flatMap(Bytes::maybe(...))
->map(static fn($bytes) => $bytes->toInt() * 4096)
->map(Bytes::of(...));

return Maybe::all($total, $active, $unused, $swap, $used)
->map(static fn(string $total, string $active, Bytes $unused, Bytes $swap, Bytes $used) => new Memory(
new Bytes((int) $total),
new Bytes(((int) $active) * 4096),
$unused,
$swap,
$used,
))
->map(Memory::of(...))
->match(
Attempt::result(...),
static fn() => Attempt::error(new \RuntimeException('Failed to parse memory usage')),
Expand Down
6 changes: 5 additions & 1 deletion src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ public function cpu(): Attempt;
*/
public function memory(): Attempt;
public function processes(): Processes;
public function loadAverage(): LoadAverage;

/**
* @return Attempt<LoadAverage>
*/
public function loadAverage(): Attempt;
public function disk(): Disk;
public function tmp(): Path;
}
Loading