Skip to content

Commit

Permalink
filament v2 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
danilopolani committed Dec 18, 2021
1 parent b6eaa26 commit 458eb3d
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 56 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Track the memory usage of your workers and display them in Filament.

<p align="center"><img src="https://i.imgur.com/Y5gnWfl.png" alt="Filament Worker Memory widget preview"></p>

> If you're using Filament v1, please navigate the [1.x branch](https://github.com/danilopolani/filament-memory-tracker/tree/1.x).
## Installation

Install the package via composer:
Expand All @@ -15,13 +17,15 @@ Install the package via composer:
composer require danilopolani/filament-memory-tracker
```

Then publish the config of the package and the assets as well:
Then publish the assets and the config of the package:

```bash
php artisan vendor:publish --tag=filament-memory-tracker-config
php artisan vendor:publish --tag=filament-memory-tracker-assets
php artisan vendor:publish --tag=filament-memory-tracker-config
```

> If you're upgrading from v1 to v2 please note that the namespace changed from `\DaniloPolani\` to `\DaniloPolani\`.
### Upgrade
When upgrading, you should republish the assets:

Expand Down Expand Up @@ -52,7 +56,7 @@ In your Worker create a new `MemoryTracker` instance and then ping the `track()`

namespace App\Console\Commands;

use Danilopolani\FilamentMemoryTracker\MemoryTracker;
use DaniloPolani\FilamentMemoryTracker\MemoryTracker;
use Illuminate\Console\Command;
use React\EventLoop\Loop;

Expand Down Expand Up @@ -118,7 +122,7 @@ return [

You can track the latest Worker restart date and memory usage as well! If you're working on a custom Worker, you should intercept the exit signals and then call the `$memoryTracker->trackRestart()` method. Otherwise you can use the Trait provided by the package to achieve that:

1. Include `Danilopolani\FilamentMemoryTracker\Concerns\TracksRestart` inside your class;
1. Include `DaniloPolani\FilamentMemoryTracker\Concerns\TracksRestart` inside your class;
2. Call `$this->trackRestartMemory(MemoryTracker $memoryTrackerInstance)` inside your constructor.

⚠️ | The `TracksRestart` requires the extension **`pcntl`** to be enabled.
Expand All @@ -129,8 +133,8 @@ You can track the latest Worker restart date and memory usage as well! If you're

namespace App\Console\Commands;

use Danilopolani\FilamentMemoryTracker\MemoryTracker;
use Danilopolani\FilamentMemoryTracker\Concerns\TracksRestart;
use DaniloPolani\FilamentMemoryTracker\MemoryTracker;
use DaniloPolani\FilamentMemoryTracker\Concerns\TracksRestart;
use Illuminate\Console\Command;
use React\EventLoop\Loop;

Expand Down Expand Up @@ -161,7 +165,7 @@ You can track [Laravel Queue](laravel.com/docs/8.x/queues) too by listening to s

namespace App\Providers;

use Danilopolani\FilamentMemoryTracker\MemoryTracker;
use DaniloPolani\FilamentMemoryTracker\MemoryTracker;
use Filament\Filament;
use Illuminate\Queue\Events\Looping;
use Illuminate\Queue\Events\WorkerStopping;
Expand Down
13 changes: 6 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,24 @@
}
],
"require": {
"php": "^7.4|^8.0",
"filament/filament": "^1.6.4",
"php": "^8.0",
"filament/filament": "^2.5",
"illuminate/support": "^8.0",
"illuminate/contracts": "^8.0",
"livewire/livewire": "^2.4",
"spatie/laravel-package-tools": "^1.5"
"livewire/livewire": "^2.6"
},
"require-dev": {
"orchestra/testbench": "^6.0",
"phpunit/phpunit": "^9.0"
},
"autoload": {
"psr-4": {
"Danilopolani\\FilamentMemoryTracker\\": "src"
"DaniloPolani\\FilamentMemoryTracker\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Danilopolani\\FilamentMemoryTracker\\Tests\\": "tests"
"DaniloPolani\\FilamentMemoryTracker\\Tests\\": "tests"
}
},
"scripts": {
Expand All @@ -50,7 +49,7 @@
"extra": {
"laravel": {
"providers": [
"Danilopolani\\FilamentMemoryTracker\\FilamentMemoryTrackerServiceProvider"
"DaniloPolani\\FilamentMemoryTracker\\WidgetServiceProvider"
]
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Concerns/TracksRestart.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Danilopolani\FilamentMemoryTracker\Concerns;
namespace DaniloPolani\FilamentMemoryTracker\Concerns;

use Danilopolani\FilamentMemoryTracker\MemoryTracker;
use DaniloPolani\FilamentMemoryTracker\MemoryTracker;

trait TracksRestart
{
Expand Down
32 changes: 0 additions & 32 deletions src/FilamentMemoryTrackerServiceProvider.php

This file was deleted.

8 changes: 4 additions & 4 deletions src/MemoryTracker.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Danilopolani\FilamentMemoryTracker;
namespace DaniloPolani\FilamentMemoryTracker;

use Illuminate\Contracts\Cache\Repository as CacheContract;
use Illuminate\Support\Carbon;
Expand Down Expand Up @@ -32,7 +32,7 @@ class MemoryTracker
*/
public function __construct(string $trackerName = 'default', int $historyMaxSize = 100, bool $realUsage = false)
{
$this->cache = Cache::store(Config::get('filament-worker-memory-tracker.cache_store'));
$this->cache = Cache::store(Config::get('filament-memory-tracker.cache_store'));
$this->memoryTrackerKey = 'memory_tracker:' . Str::slug($trackerName);
$this->memoryTrackerPeakKey = 'memory_tracker_peak:' . Str::slug($trackerName);
$this->memoryTrackerRestartKey = 'memory_tracker_restart:' . Str::slug($trackerName);
Expand Down Expand Up @@ -132,7 +132,7 @@ public function getPeak(): ?array

return [
'date' => (new Carbon($peak['date']))->format(
Config::get('filament-worker-memory-tracker.date_format', 'j F Y @ H:i:s')
Config::get('filament-memory-tracker.date_format', 'j F Y @ H:i:s')
),
'value' => number_format($peak['value'] / 1024 / 1024, 3),
];
Expand All @@ -151,7 +151,7 @@ public function getLatestRestart(): ?array

return [
'date' => (new Carbon($latestRestart['date']))->format(
Config::get('filament-worker-memory-tracker.date_format', 'j F Y @ H:i:s')
Config::get('filament-memory-tracker.date_format', 'j F Y @ H:i:s')
),
'value' => number_format($latestRestart['value'] / 1024 / 1024, 3),
];
Expand Down
65 changes: 65 additions & 0 deletions src/WidgetServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace DaniloPolani\FilamentMemoryTracker;

use DaniloPolani\FilamentMemoryTracker\Widgets\MemoryTrackerWidget;
use Filament\FilamentManager;
use Filament\PluginServiceProvider;
use Spatie\LaravelPackageTools\Package;

class WidgetServiceProvider extends PluginServiceProvider
{
public static string $name = 'filament-memory-tracker';

/**
* {@inheritDoc}
*/
public function configurePackage(Package $package): void
{
parent::configurePackage($package);

$package
->hasAssets()
->hasConfigFile();
}

/**
* {@inheritDoc}
*/
public function packageRegistered(): void
{
$this->app->singletonIf('filament', fn (): FilamentManager => new FilamentManager());

parent::packageRegistered();
}

/**
* {@inheritDoc}
*/
protected function getWidgets(): array
{
return [
MemoryTrackerWidget::class,
];
}

/**
* {@inheritDoc}
*/
protected function getStyles(): array
{
return [
self::$name . '-styles' => asset('/vendor/' . self::$name . '/memory-tracker.css'),
];
}

/**
* {@inheritDoc}
*/
protected function getScripts(): array
{
return [
self::$name . '-scripts' => asset('/vendor/' . self::$name . '/memory-tracker.js'),
];
}
}
9 changes: 5 additions & 4 deletions src/Widgets/MemoryTrackerWidget.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<?php

namespace Danilopolani\FilamentMemoryTracker\Widgets;
namespace DaniloPolani\FilamentMemoryTracker\Widgets;

use Danilopolani\FilamentMemoryTracker\MemoryTracker;
use DaniloPolani\FilamentMemoryTracker\MemoryTracker;
use Filament\Widgets\Widget;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;

class MemoryTrackerWidget extends Widget
{
public static $view = 'filament-memory-tracker::widgets.memory-tracker';
protected static string $view = 'filament-memory-tracker::widgets.memory-tracker';

public function render()
public function render(): View
{
$charts = [];

Expand Down

0 comments on commit 458eb3d

Please sign in to comment.