From 2a41a9258533174510b7632d0c507720385557f8 Mon Sep 17 00:00:00 2001 From: Nick Retel Date: Wed, 3 Jan 2024 07:58:11 +0100 Subject: [PATCH] fixes for timestamp and max y axis value --- resources/views/uptime.blade.php | 8 +++--- .../OhDearUptimePulseCardComponent.php | 28 +++++++++++++++---- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/resources/views/uptime.blade.php b/resources/views/uptime.blade.php index c600567..a0fff66 100644 --- a/resources/views/uptime.blade.php +++ b/resources/views/uptime.blade.php @@ -34,7 +34,7 @@
- {{$this->getData()[0][1]}}ms + {{$this->performanceRecords[0][1]}}ms
getData()), + data: @js($this->performanceRecords), borderCapStyle: 'round', pointHitRadius: 20, pointRadius: 0, @@ -95,7 +95,7 @@ class="absolute top-0 w-full h-full left-9" y: { display: false, min: 0, - max: 100, + max: @js($this->maxPerformanceRecord), }, }, plugins: { @@ -149,7 +149,7 @@ function getGradient(ctx, chartArea) {
Now - {{\Carbon\Carbon::createFromTimestamp($this->getData()[count($this->getData()) -2][0]/1000)->diffInMinutes(now(), true)}} min ago + {{\Carbon\Carbon::createFromTimestamp($this->performanceRecords[count($this->performanceRecords) -2][0]/1000)->diffInMinutes(now(), true)}} min ago
diff --git a/src/Livewire/OhDearUptimePulseCardComponent.php b/src/Livewire/OhDearUptimePulseCardComponent.php index 72fccb5..87367e1 100644 --- a/src/Livewire/OhDearUptimePulseCardComponent.php +++ b/src/Livewire/OhDearUptimePulseCardComponent.php @@ -5,6 +5,7 @@ use Carbon\Carbon; 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; @@ -22,6 +23,12 @@ class OhDearUptimePulseCardComponent extends Card public int $siteId; + public Collection $sites; + + public array $performanceRecords; + + public int $maxPerformanceRecord = 0; + protected function css() { return __DIR__.'/../../dist/output.css'; @@ -32,9 +39,12 @@ public function mount(?int $siteId = null) $this->sites = collect(); $this->siteId = $siteId ?? config('services.oh_dear.pulse.site_id'); + + $this->setPerformanceRecords(); + $this->setMaxPerformanceRecord(); } - public function getData() + public function setPerformanceRecords() { $performanceRecords = $this->ohDear()->performanceRecords( $this->siteId, @@ -47,17 +57,25 @@ public function getData() $createdAt = Carbon::createFromFormat('Y-m-d H:i:s', $record->createdAt); return [ - $createdAt->timestamp, - $record->totalTimeInSeconds * 1000, + $createdAt->getTimestampMs(), + ceil($record->totalTimeInSeconds * 1000), ]; })->toArray(); - return $performanceRecords; + $this->performanceRecords = $performanceRecords; + } + + public function setMaxPerformanceRecord() + { + + $this->maxPerformanceRecord = (int) ceil(collect($this->performanceRecords) + ->max(fn (array $dataPoint) => $dataPoint[1])) + 10; + } protected function getLabels(): array { - return collect($this->getData()) + return collect($this->performanceRecords) ->map(function (array $dataPoint) { return Carbon::createFromTimestamp($dataPoint[0] / 1000) ->format('Y-m-d H:i');