Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed May 1, 2024
1 parent c9e9c36 commit 13720b1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
41 changes: 23 additions & 18 deletions src/Livewire/Servers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace Laravel\Pulse\Livewire;

use Carbon\CarbonImmutable;
use Carbon\CarbonInterval;
use Carbon\Exceptions\InvalidFormatException;
use DateInterval;
use DateTime;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Facades\View;
use Illuminate\Support\InteractsWithTime;
use Livewire\Attributes\Lazy;
use Livewire\Livewire;

Expand All @@ -16,18 +17,9 @@
#[Lazy]
class Servers extends Card
{
public int|string|null $ignoreAfter = null;
use InteractsWithTime;

/**
* Get the ignore after seconds from the ignoreAfter property.
*/
protected function getIgnoreAfterSeconds(): int|float|null
{
if (is_string($this->ignoreAfter)) {
return CarbonInterval::make($this->ignoreAfter)?->totalSeconds ?? null;
}
return is_int($this->ignoreAfter) ? $this->ignoreAfter : null;
}
public int|DateTime|null $ignoreAfter = null;

/**
* Render the component.
Expand All @@ -36,16 +28,15 @@ public function render(): Renderable
{
[$servers, $time, $runAt] = $this->remember(function () {
$graphs = $this->graph(['cpu', 'memory'], 'avg');
$ignoreAfter = $this->getIgnoreAfterSeconds();

return $this->values('system')
->map(function ($system, $slug) use ($graphs, $ignoreAfter) {
$values = json_decode($system->value, flags: JSON_THROW_ON_ERROR);

if (is_numeric($ignoreAfter) && CarbonImmutable::createFromTimestamp($system->timestamp)->isBefore(now()->subSeconds($ignoreAfter))) {
->map(function ($system, $slug) use ($graphs) {
if ($this->ignoreSystem($system)) {
return null;
}

$values = json_decode($system->value, flags: JSON_THROW_ON_ERROR);

return (object) [
'name' => (string) $values->name,
'cpu_current' => (int) $values->cpu,
Expand Down Expand Up @@ -80,4 +71,18 @@ public function placeholder(): Renderable
{
return View::make('pulse::components.servers-placeholder', ['cols' => $this->cols, 'rows' => $this->rows, 'class' => $this->class]);
}

/**
* Determine if the system should be ignored.
*
* @param object{ timestamp: int, key: string, value: string } $system
*/
protected function ignoreSystem(object $system): bool
{
if ($this->ignoreAfter === null) {
return false;
}

return CarbonImmutable::createFromTimestamp($system->timestamp)->addSeconds($this->secondsUntil($this->ignoreAfter))->isPast();
}
}
28 changes: 17 additions & 11 deletions tests/Feature/Livewire/ServersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
->assertSeeInOrder(['A Web', 'B Web', 'C Web']);
});

it('can ignore servers that have stopped reporting', function () {
it('can ignore servers that have stopped reporting', function ($ignoreAfter, $see, $dontSee) {
Carbon::setTestNow(now()->startOfSecond());

$data = [
Expand All @@ -108,20 +108,26 @@
Pulse::set('system', 'server-2', json_encode([
'name' => 'Server 2',
...$data,
]), now()->subSeconds(601));
]), now()->subSeconds(600));

Pulse::set('system', 'server-3', json_encode([
'name' => 'Server 3',
...$data,
]), now()->subSeconds(600));
]), now()->subSeconds(601));

Pulse::ingest();

Livewire::test(Servers::class, ['lazy' => false, 'ignoreAfter' => 600])
->assertSeeTextInOrder(['Server 1', 'Server 3'])
->assertDontSeeText('Server 2');

Livewire::test(Servers::class, ['lazy' => false, 'ignoreAfter' => '10 minutes'])
->assertSeeTextInOrder(['Server 1', 'Server 3'])
->assertDontSeeText('Server 2');
});
Livewire::test(Servers::class, ['lazy' => false, 'ignoreAfter' => value($ignoreAfter)])
->assertSeeInOrder($see)
->assertDontSeeText($dontSee);
})->with([
[null, ['Server 1', 'Server 2', 'Server 3'], []],
[588, [], ['Server 1', 'Server 2', 'Server 3']],
[599, ['Server 1'], ['Server 2', 'Server 3']],
[600, ['Server 1', 'Server 2'], ['Server 3']],
[601, ['Server 1', 'Server 2', 'Server 3'], []],
[fn () => now()->addSeconds(588), [], ['Server 1', 'Server 2', 'Server 3']],
[fn () => now()->addSeconds(599), ['Server 1'], ['Server 2', 'Server 3']],
[fn () => now()->addSeconds(600), ['Server 1', 'Server 2'], ['Server 3']],
[fn () => now()->addSeconds(601), ['Server 1', 'Server 2', 'Server 3'], []],
]);

0 comments on commit 13720b1

Please sign in to comment.