Skip to content

Commit

Permalink
Filament 3 (#3)
Browse files Browse the repository at this point in the history
* Add support for Filament 3

* Add support for Filament 3

* Remove config

* Fix use statement

* Show key in table

* Composer packages

* Update composer.json

* Update FilamentOpcacheServiceProvider.php

* widgets display w/o error

* composer dependency changes

- we want an explicit dependency on filament 3
- we do not want an explicit dependency on carbon via illuminate (ties us to laravel version 10)
- removed unnecessary php version requirement... filament already requires 8.1

* update README

* forgot to save

---------

Co-authored-by: Haz <me@hazjohnson.com>
Co-authored-by: Haz <hello@haztakki.solutions>
  • Loading branch information
3 people authored Oct 20, 2023
1 parent 8b9b93d commit 1e93897
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 62 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ This package allows you to view OPcache data from your Filament admin panel.
composer require stechstudio/filament-opcache
```

In your `AdminPanelProvider` (or other `\Filament\PanelProvider`), add this package to your plugins:
```php
$panel
->plugins([
\STS\FilamentOpcache\FilamentOpcachePlugin::make(),
])
```

| Status Page |
|---|
| ![Status Page](/screenshots/Status.png) |
Expand Down
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
{
"name": "David Palmer",
"email": "dp@reproconnect.com"
},
{
"name": "Haz Johnson",
"email": "haz@stechstudio.com"
}
],
"require": {
"php": "^8.0",
"filament/filament": "^2.0",
"appstract/laravel-opcache": "^4.0",
"filament/filament": "^3.0",
"appstract/laravel-opcache": "^4.0.2",
"nesbot/carbon": "^2.0"
},
"extra": {
Expand Down
20 changes: 10 additions & 10 deletions resources/views/status.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@
</div>

<div wire:poll.10s>
<x-tables::container>
<x-tables::table>
<x-filament-tables::container>
<x-filament-tables::table>
<x-slot name="header">
<x-tables::header-cell width="50%">Key</x-tables::header-cell>
<x-tables::header-cell>Value</x-tables::header-cell>
<x-filament-tables::header-cell>Key</x-filament-tables::header-cell>
<x-filament-tables::header-cell>Value</x-filament-tables::header-cell>
</x-slot>

@foreach($$activeTab as $key => $value)
<x-tables::row>
<x-tables::cell class="px-4 py-1">{{ $key }}</x-tables::cell>
<x-tables::cell class="px-4 py-1">{{ $value }}</x-tables::cell>
</x-tables::row>
<x-filament-tables::row>
<x-filament-tables::cell class="px-4 py-1">{{ $key }}</x-filament-tables::cell>
<x-filament-tables::cell class="px-4 py-1">{{ $value }}</x-filament-tables::cell>
</x-filament-tables::row>
@endforeach
</x-tables::table>
</x-tables::container>
</x-filament-tables::table>
</x-filament-tables::container>
</div>
</div>
</x-filament::page>
37 changes: 37 additions & 0 deletions src/FilamentOpcachePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace STS\FilamentOpcache;

use Filament\Contracts\Plugin;
use Filament\Panel;

class FilamentOpcachePlugin implements Plugin
{
public static function make(): static
{
return app(static::class);
}

public function getId(): string
{
return 'filament-opcache';
}

public function register(Panel $panel): void
{
$panel
->pages([
Pages\Config::class,
Pages\Status::class,
])
->widgets([
Widgets\OpcacheHitsWidget::class,
Widgets\OpcacheMemoryWidget::class,
]);
}

public function boot(Panel $panel): void
{
//
}
}
22 changes: 7 additions & 15 deletions src/FilamentOpcacheServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
<?php

declare(strict_types=1);

namespace STS\FilamentOpcache;

use Filament\PluginServiceProvider;
use Spatie\LaravelPackageTools\Package;
use STS\FilamentOpcache\Pages;
use STS\FilamentOpcache\Widgets;
use Spatie\LaravelPackageTools\PackageServiceProvider;

class FilamentOpcacheServiceProvider extends PluginServiceProvider
class FilamentOpcacheServiceProvider extends PackageServiceProvider
{
protected array $pages = [
Pages\Config::class,
Pages\Status::class,
];

protected array $widgets = [
Widgets\OpcacheHitsWidget::class,
Widgets\OpcacheMemoryWidget::class,
];
public static string $name = 'filament-opcache';

public function configurePackage(Package $package): void
{
$package
->name('laravel-filament-opcache')
->hasViews('laravel-filament-opcache');
->name(static::$name)
->hasViews();
}
}
9 changes: 6 additions & 3 deletions src/Memory.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

declare(strict_types=1);

namespace STS\FilamentOpcache;

class Memory
{
public static function humanReadable($bytes = 0)
public static function humanReadable(int $bytes = 0): string
{
$size = ['B', 'kB', 'MB', 'GB', 'TB'];
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.2f", $bytes / pow(1024, $factor)) . ' ' . @$size[intval($factor)];
$factor = floor((strlen((string)$bytes) - 1) / 3);

return sprintf("%.2f", (string)$bytes / pow(1024, $factor)) . ' ' . @$size[intval($factor)];
}
}
17 changes: 10 additions & 7 deletions src/Pages/Config.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

declare(strict_types=1);

namespace STS\FilamentOpcache\Pages;

use Appstract\Opcache\OpcacheFacade;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Pages\Actions\Action;
use Filament\Pages\Page;

class Config extends Page
Expand All @@ -17,14 +19,15 @@ class Config extends Page

protected static ?string $navigationIcon = 'heroicon-o-cog';

protected static string $view = 'laravel-filament-opcache::status';
protected static string $view = 'filament-opcache::status';

protected static ?string $navigationGroup = 'OPcache';

public $tabs = ['Directives', 'Version', 'Blacklist'];
public array $tabs = ['Directives', 'Version', 'Blacklist'];

public $activeTab = 'directives';
public string $activeTab = 'directives';

/** @noinspection DuplicatedCode */
protected function getActions(): array
{
return [
Expand Down Expand Up @@ -57,15 +60,15 @@ protected function getViewData(): array

return [
'directives' => collect($config['directives'])
->map(function ($value, $key) {
->map(function ($value) {
if (is_bool($value)) {
return $value ? 'true' : 'false';
}

return (string)$value;
}),
'version' => collect($config['version']),
'blacklist' => collect($config['blacklist'])
'version' => collect($config['version']),
'blacklist' => collect($config['blacklist']),
];
}
}
35 changes: 18 additions & 17 deletions src/Pages/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ class Status extends Page

protected static ?string $slug = 'opcache-status';

protected static ?string $navigationIcon = 'heroicon-o-database';
protected static ?string $navigationIcon = 'heroicon-o-circle-stack';

protected static string $view = 'laravel-filament-opcache::status';
protected static string $view = 'filament-opcache::status';

protected static ?string $navigationGroup = 'OPcache';

public $tabs = ['Lifecycle', 'Memory', 'Strings', 'Statistics', 'JIT'];
public array $tabs = ['Lifecycle', 'Memory', 'Strings', 'Statistics', 'JIT'];

public $activeTab = 'lifecycle';
public string $activeTab = 'lifecycle';

/** @noinspection DuplicatedCode */
protected function getActions(): array
{
return [
Expand Down Expand Up @@ -59,16 +60,16 @@ protected function getViewData(): array
$status = OpcacheFacade::getStatus();

return [
'lifecycle' => collect($status)
->filter(fn($value, $key) => !is_array($value))
->map(fn ($value) => $value ? 'true' : 'false'),
'memory' => collect($status['memory_usage'])
->map(fn ($value, $key) => Str::contains($key, 'percentage') ?
'lifecycle' => collect($status)
->filter(fn($value, $key) => ! is_array($value))
->map(fn($value) => $value ? 'true' : 'false'),
'memory' => collect($status['memory_usage'])
->map(fn($value, $key) => Str::contains($key, 'percentage') ?
number_format($value, 2) . '%' :
Memory::humanReadable($value)),
'strings' => collect($status['interned_strings_usage'])
->map(fn ($value, $key) => Str::contains($key, 'number_of') ?
number_format($value, 0) :
'strings' => collect($status['interned_strings_usage'])
->map(fn($value, $key) => Str::contains($key, 'number_of') ?
number_format($value) :
Memory::humanReadable($value)),
'statistics' => collect($status['opcache_statistics'])
->map(function ($value, $key) {
Expand All @@ -84,16 +85,16 @@ protected function getViewData(): array
return (string)(new Carbon($value));
}

return number_format($value, 0);
return number_format($value);
}),
'jit' => collect($status['jit'])
->map(function ($value, $key) {
'jit' => collect($status['jit'])
->map(function ($value) {
if (is_bool($value)) {
return $value ? 'true' : 'false';
}

return number_format($value, 0);
})
return number_format($value);
}),
];
}
}
2 changes: 2 additions & 0 deletions src/Widgets/OpcacheHitsWidget.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace STS\FilamentOpcache\Widgets;

use Appstract\Opcache\OpcacheFacade;
Expand Down
2 changes: 2 additions & 0 deletions src/Widgets/OpcacheMemoryWidget.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace STS\FilamentOpcache\Widgets;

use Appstract\Opcache\OpcacheFacade;
Expand Down
18 changes: 11 additions & 7 deletions src/Widgets/PercentageChart.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
<?php

declare(strict_types=1);

namespace STS\FilamentOpcache\Widgets;

abstract class PercentageChart extends \Filament\Widgets\PieChartWidget
use Filament\Widgets\PieChartWidget;

abstract class PercentageChart extends PieChartWidget
{
protected function chartDataFor($title, $labelA, $valueA, $labelB, $valueB): array
{
return [
'labels' => [
'labels' => [
$labelA . ' ' . $this->percentage($valueA, $valueA + $valueB),
$labelB . ' ' . $this->percentage($valueB, $valueA + $valueB),
],
'datasets' =>[
'datasets' => [
[
'label' => $title,
'data' => [
'label' => $title,
'data' => [
$valueA,
$valueB,
],
'backgroundColor' => [
'#1c3d5a',
'#8795a1'
'#8795a1',
],
]
],
],
];
}
Expand Down

0 comments on commit 1e93897

Please sign in to comment.