Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Dec 5, 2023
1 parent 64eb791 commit 413ab28
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 31 deletions.
69 changes: 69 additions & 0 deletions resources/views/cron.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
@php
use Illuminate\Support\Str;
@endphp
<x-pulse::card :cols="$cols" :rows="$rows" :class="$class">
<x-pulse::card-header
name="Cron"
>
<x-slot:icon>
<x-pulse::icons.rocket-launch/>
</x-slot:icon>
</x-pulse::card-header>

<div>
<div class="flex flex-col gap-6">
<div>
<x-pulse::table>
<colgroup>
<col width="100%"/>
<col width="0%"/>
<col width="0%"/>
<col width="0%"/>
</colgroup>
<x-pulse::thead>
<tr>
<x-pulse::th>Task</x-pulse::th>
<x-pulse::th class="text-right">Last executed at</x-pulse::th>
<x-pulse::th class="text-right">Cron expression</x-pulse::th>
<x-pulse::th class="text-right whitespace-nowrap">Result</x-pulse::th>
</tr>
</x-pulse::thead>
<tbody>
@foreach ($cronChecks as $cronCheck)
<tr class="h-2 first:h-0"></tr>
<tr wire:key="cronCheck.{{ $cronCheck->id }}">
<x-pulse::td class="max-w-[1px]">
<code class="block text-xs text-gray-900 dark:text-gray-100 truncate"
title="{{ $cronCheck->name }}">
{{ $cronCheck->name }}
</code>
</x-pulse::td>
<x-pulse::td class="text-gray-700 dark:text-gray-300 font-bold">
<div>
{{ $cronCheck->humanReadableLatestPingAt }}
</div>
<div>
{{ $cronCheck->latestPingAt }}
</div>

</x-pulse::td>
<x-pulse::td class="text-gray-700 dark:text-gray-300 font-bold">
{{ $cronCheck->humanReadableCronExpression }}
{{ $cronCheck->cronExpression }}
</x-pulse::td>
<x-pulse::td class="text-gray-700 dark:text-gray-300 font-bold">
<div>
{{ $cronCheck->latestResultLabel }}
</div>
<div>
Result color: {{ $cronCheck->latestResultLabelColor }}
</div>
</x-pulse::td>
</tr>
@endforeach
</tbody>
</x-pulse::table>
</div>
</div>
</div>
</x-pulse::card>
45 changes: 23 additions & 22 deletions resources/views/uptime.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,33 @@
@endphp
<x-pulse::card :cols="$cols" :rows="$rows" :class="$class">
<x-pulse::card-header
name="Oh Dear in package"
title="Custom title"
details="Custom details"
name="Uptime"
>
<x-slot:icon>
<x-pulse::icons.queue-list />
<x-pulse::icons.rocket-launch/>
</x-slot:icon>
</x-pulse::card-header>

<x-pulse::scroll :expand="$expand" wire:poll.5s="">
@if ($sites->isEmpty())
<x-pulse::no-results />
@else
<div class="grid gap-3 mx-px mb-px">
@foreach ($sites as $site)
<div wire:key="{{ $site->id }}">
<h3 class="font-bold text-gray-700 dark:text-gray-300">
Site name
</h3>

<div class="mt-3 relative">
Details of the site
</div>
</div>
@endforeach
<div>
<div class="flex flex-col gap-6">
<div class="grid grid-cols-3 gap-3 text-center">
<div class="flex flex-col justify-center @sm:block">
<span class="text-xl uppercase font-bold text-gray-700 dark:text-gray-300 tabular-nums">
{{ $status }}
</span>
<span class="text-xs uppercase font-bold text-gray-500 dark:text-gray-400">
Status
</span>
</div>
<div class="flex flex-col justify-center @sm:block">
<span class="text-xl uppercase font-bold text-gray-700 dark:text-gray-300 tabular-nums">
{{ $performance }}
</span>
<span class="text-xs uppercase font-bold text-gray-500 dark:text-gray-400">
Performance
</span>
</div>
</div>
@endif
</x-pulse::scroll>
</div>
</div>
</x-pulse::card>
21 changes: 21 additions & 0 deletions src/Livewire/Concerns/RemembersApiCalls.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace OhDear\OhDearPulse\Livewire\Concerns;

use Carbon\CarbonInterval;
use Illuminate\Support\Facades\App;
use Laravel\Pulse\Support\CacheStoreResolver;

trait RemembersApiCalls
{
public function remember(callable $apiCall, string $key, CarbonInterval $interval = null): mixed
{
$interval ??= CarbonInterval::minute();

return App::make(CacheStoreResolver::class)->store()->remember(
'laravel:pulse:' . static::class . ':' . $key,
$interval,
fn() => $apiCall(),
);
}
}
14 changes: 14 additions & 0 deletions src/Livewire/Concerns/UsesOhDearApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace OhDear\OhDearPulse\Livewire\Concerns;

use OhDear\PhpSdk\OhDear;

trait UsesOhDearApi
{
public function ohDear(): ?OhDear
{
return app('ohdear-pulse');
}

}
50 changes: 50 additions & 0 deletions src/Livewire/OhDearCronPulseCardComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace OhDear\OhDearPulse\Livewire;

use Illuminate\Contracts\Support\Renderable;
use Laravel\Pulse\Livewire\Card;
use Livewire\Attributes\Lazy;
use OhDear\OhDearPulse\Livewire\Concerns\RemembersApiCalls;
use OhDear\OhDearPulse\Livewire\Concerns\UsesOhDearApi;
use OhDear\OhDearPulse\OhDearPulse;
use OhDear\PhpSdk\Resources\Check;
use OhDear\PhpSdk\Resources\Site;

#[Lazy]
class OhDearCronPulseCardComponent extends Card
{
use UsesOhDearApi;
use RemembersApiCalls;

public int $siteId;

public function mount(int $siteId = null)
{
$this->siteId = $siteId ?? config('services.oh_dear.pulse.site_id');
}

public function render(): Renderable
{
$cronChecks = $this->getCronChecks();

return view('ohdear-pulse::cron', [
'cronChecks' => $cronChecks,
]);
}

/**
* @return array<\OhDear\PhpSdk\Resources\CronCheck>|null
*/
public function getCronChecks(): ?array
{
if (! OhDearPulse::isConfigured()) {
return null;
}

return $this->remember(
fn() => $this->ohDear()?->cronChecks($this->siteId),
'site:' . $this->siteId,
);
}
}
62 changes: 55 additions & 7 deletions src/Livewire/OhDearUptimePulseCardComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,88 @@

namespace OhDear\OhDearPulse\Livewire;

use Carbon\CarbonInterval;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Collection;
use Laravel\Pulse\Livewire\Card;
use Livewire\Attributes\Lazy;
use OhDear\OhDearPulse\Livewire\Concerns\RemembersApiCalls;
use OhDear\OhDearPulse\Livewire\Concerns\UsesOhDearApi;
use OhDear\OhDearPulse\OhDearPulse;
use OhDear\PhpSdk\OhDear;
use OhDear\PhpSdk\Resources\Check;
use OhDear\PhpSdk\Resources\Site;

#[Lazy]
class OhDearUptimePulseCardComponent extends Card
{
use UsesOhDearApi;
use RemembersApiCalls;

public int $siteId;

public function mount(int $siteId = null)
{
$this->sites = collect();

$this->siteId = $siteId;
$this->siteId = $siteId ?? config('services.oh_dear.pulse.site_id');
}

public function render(): Renderable
{
$site = $this->getSite();

return view('ohdear-pulse::uptime', [
'site' => $site,
'status' => $this->getStatus($site),
'performance' => $this->getPerformance($site),
]);
}

public function render()
public function getSite(): ?Site
{
$site = $this->ohDear()->site($this->siteId);
if (! OhDearPulse::isConfigured()) {
return null;
}

$siteAttributes = $this->remember(
fn() => $this->ohDear()?->site($this->siteId)?->attributes,
'site:' . $this->siteId,
CarbonInterval::seconds(10),
);

return new Site($siteAttributes);
}

protected function getStatus(?Site $site): ?string
{
if (! $site) {
return null;
}

return view('ohdear-pulse::uptime');
if (! $check = $this->getCheck($site, 'uptime')) {
return null;
}

return $check->summary;
}

public function fetchData()
protected function getPerformance(?Site $site): ?string
{
if (! $site) {
return null;
}

if (! $check = $this->getCheck($site, 'performance')) {
return null;
}

return $check->summary;
}

protected function ohDear(): OhDear
protected function getCheck(Site $site, string $type): ?Check
{
return app('ohdear-pulse');
return collect($site->checks)
->first(fn (Check $check) => $check->type === $type);
}
}
4 changes: 4 additions & 0 deletions src/OhDearPulse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@

class OhDearPulse
{
public static function isConfigured()
{
return config('services.oh_dear.pulse.api_key') !== null;
}
}
6 changes: 4 additions & 2 deletions src/OhDearPulseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OhDear\OhDearPulse;

use Livewire\Livewire;
use OhDear\OhDearPulse\Livewire\OhDearCronPulseCardComponent;
use OhDear\OhDearPulse\Livewire\OhDearUptimePulseCardComponent;
use OhDear\PhpSdk\OhDear;
use Spatie\LaravelPackageTools\Package;
Expand All @@ -21,13 +22,14 @@ public function configurePackage(Package $package): void
public function packageBooted()
{
Livewire::component('ohdear.pulse.uptime', OhDearUptimePulseCardComponent::class);
Livewire::component('ohdear.pulse.cron', OhDearCronPulseCardComponent::class);

$this->app->bind('ohdear-pulse', function () {
if(! config('services.ohdear-pulse.api_key')) {
if(! OhDearPulse::isConfigured()) {
return null;
}

return new OhDear(config('services.ohdear-pulse.api_key'));
return new OhDear(config('services.oh_dear.pulse.api_key'));
});
}
}

0 comments on commit 413ab28

Please sign in to comment.