Skip to content

Commit

Permalink
fixes for timestamp and max y axis value
Browse files Browse the repository at this point in the history
  • Loading branch information
nckrtl committed Jan 3, 2024
1 parent 8d4fd90 commit 2a41a92
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
8 changes: 4 additions & 4 deletions resources/views/uptime.blade.php
Expand Up @@ -34,7 +34,7 @@
<div class="relative flex items-center justify-center w-2 h-2 rounded-full bg-emerald-500 dark:border-t dark:bg-gradient-to-t dark:from-emerald-400 dark:border-emerald-200 dark:to-emerald-300">

</div>
<span>{{$this->getData()[0][1]}}ms</span>
<span>{{$this->performanceRecords[0][1]}}ms</span>
</div>
<div
wire:ignore
Expand All @@ -51,7 +51,7 @@ class="absolute top-0 w-full h-full left-9"
{
label: 'Response time',
borderColor: '#55B685',
data: @js($this->getData()),
data: @js($this->performanceRecords),
borderCapStyle: 'round',
pointHitRadius: 20,
pointRadius: 0,
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -149,7 +149,7 @@ function getGradient(ctx, chartArea) {
<div class="absolute flex justify-between w-full px-6 text-xs text-gray-400 bottom-8 dark:text-gray-500">
<span>Now</span>

<span>{{\Carbon\Carbon::createFromTimestamp($this->getData()[count($this->getData()) -2][0]/1000)->diffInMinutes(now(), true)}} min ago</span>
<span>{{\Carbon\Carbon::createFromTimestamp($this->performanceRecords[count($this->performanceRecords) -2][0]/1000)->diffInMinutes(now(), true)}} min ago</span>
</div>
</div>
</x-pulse::card>
28 changes: 23 additions & 5 deletions src/Livewire/OhDearUptimePulseCardComponent.php
Expand Up @@ -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;
Expand All @@ -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';
Expand All @@ -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,
Expand All @@ -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');
Expand Down

0 comments on commit 2a41a92

Please sign in to comment.