From 3d67d5a369d47e4fcfd7b554b5b6e796b342fe86 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Fri, 3 Jan 2025 23:28:57 -0700 Subject: [PATCH] Add Pulse --- app/Providers/AppServiceProvider.php | 140 ++++++----- composer.json | 1 + composer.lock | 221 +++++++++++++++- config/pulse.php | 236 ++++++++++++++++++ .../2023_06_07_000001_create_pulse_tables.php | 84 +++++++ .../views/vendor/pulse/dashboard.blade.php | 19 ++ 6 files changed, 631 insertions(+), 70 deletions(-) create mode 100644 config/pulse.php create mode 100644 database/migrations/2023_06_07_000001_create_pulse_tables.php create mode 100644 resources/views/vendor/pulse/dashboard.blade.php diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index b080b3b2f..87d0ceb05 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,81 +2,83 @@ namespace App\Providers; -use App\Observers\{ - AvatarObserver, - FollowerObserver, - HashtagFollowObserver, - LikeObserver, - NotificationObserver, - ModLogObserver, - ProfileObserver, - StatusHashtagObserver, - StatusObserver, - UserObserver, - UserFilterObserver, -}; -use App\{ - Avatar, - Follower, - HashtagFollow, - Like, - Notification, - ModLog, - Profile, - StatusHashtag, - Status, - User, - UserFilter -}; -use Auth, Horizon, URL; -use Illuminate\Support\Facades\Blade; -use Illuminate\Support\Facades\Schema; -use Illuminate\Support\ServiceProvider; -use Illuminate\Pagination\Paginator; -use Illuminate\Support\Facades\Validator; +use App\Avatar; +use App\Follower; +use App\HashtagFollow; +use App\Like; +use App\ModLog; +use App\Notification; +use App\Observers\AvatarObserver; +use App\Observers\FollowerObserver; +use App\Observers\HashtagFollowObserver; +use App\Observers\LikeObserver; +use App\Observers\ModLogObserver; +use App\Observers\NotificationObserver; +use App\Observers\ProfileObserver; +use App\Observers\StatusHashtagObserver; +use App\Observers\StatusObserver; +use App\Observers\UserFilterObserver; +use App\Observers\UserObserver; +use App\Profile; +use App\Status; +use App\StatusHashtag; +use App\User; +use App\UserFilter; +use Auth; +use Horizon; use Illuminate\Database\Eloquent\Model; +use Illuminate\Pagination\Paginator; +use Illuminate\Support\Facades\Gate; +use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Facades\Validator; +use Illuminate\Support\ServiceProvider; +use URL; class AppServiceProvider extends ServiceProvider { - /** - * Bootstrap any application services. - * - * @return void - */ - public function boot() - { - if(config('instance.force_https_urls', true)) { - URL::forceScheme('https'); - } + /** + * Bootstrap any application services. + * + * @return void + */ + public function boot() + { + if (config('instance.force_https_urls', true)) { + URL::forceScheme('https'); + } - Schema::defaultStringLength(191); - Paginator::useBootstrap(); - Avatar::observe(AvatarObserver::class); - Follower::observe(FollowerObserver::class); - HashtagFollow::observe(HashtagFollowObserver::class); - Like::observe(LikeObserver::class); - Notification::observe(NotificationObserver::class); - ModLog::observe(ModLogObserver::class); - Profile::observe(ProfileObserver::class); - StatusHashtag::observe(StatusHashtagObserver::class); - User::observe(UserObserver::class); + Schema::defaultStringLength(191); + Paginator::useBootstrap(); + Avatar::observe(AvatarObserver::class); + Follower::observe(FollowerObserver::class); + HashtagFollow::observe(HashtagFollowObserver::class); + Like::observe(LikeObserver::class); + Notification::observe(NotificationObserver::class); + ModLog::observe(ModLogObserver::class); + Profile::observe(ProfileObserver::class); + StatusHashtag::observe(StatusHashtagObserver::class); + User::observe(UserObserver::class); Status::observe(StatusObserver::class); - UserFilter::observe(UserFilterObserver::class); - Horizon::auth(function ($request) { - return Auth::check() && $request->user()->is_admin; - }); - Validator::includeUnvalidatedArrayKeys(); + UserFilter::observe(UserFilterObserver::class); + Horizon::auth(function ($request) { + return Auth::check() && $request->user()->is_admin; + }); + Validator::includeUnvalidatedArrayKeys(); - // Model::preventLazyLoading(true); - } + Gate::define('viewPulse', function (User $user) { + return $user->is_admin === 1; + }); - /** - * Register any application services. - * - * @return void - */ - public function register() - { - // - } + // Model::preventLazyLoading(true); + } + + /** + * Register any application services. + * + * @return void + */ + public function register() + { + // + } } diff --git a/composer.json b/composer.json index 26b975f41..7d3320ba6 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "laravel/helpers": "^1.1", "laravel/horizon": "^5.0", "laravel/passport": "^12.0", + "laravel/pulse": "^1.3", "laravel/tinker": "^2.9", "laravel/ui": "^4.2", "league/flysystem-aws-s3-v3": "^3.0", diff --git a/composer.lock b/composer.lock index 2e8c215d2..79aaa3a6f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0035325cb0240e92fc378e49f76447bd", + "content-hash": "3bb2ed96bc8ff080f3415b9bcf6ac307", "packages": [ { "name": "aws/aws-crt-php", @@ -1110,6 +1110,62 @@ ], "time": "2024-02-05T11:56:58+00:00" }, + { + "name": "doctrine/sql-formatter", + "version": "1.5.1", + "source": { + "type": "git", + "url": "https://github.com/doctrine/sql-formatter.git", + "reference": "b784cbde727cf806721451dde40eff4fec3bbe86" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/sql-formatter/zipball/b784cbde727cf806721451dde40eff4fec3bbe86", + "reference": "b784cbde727cf806721451dde40eff4fec3bbe86", + "shasum": "" + }, + "require": { + "php": "^8.1" + }, + "require-dev": { + "doctrine/coding-standard": "^12", + "ergebnis/phpunit-slow-test-detector": "^2.14", + "phpstan/phpstan": "^1.10", + "phpunit/phpunit": "^10.5", + "vimeo/psalm": "^5.24" + }, + "bin": [ + "bin/sql-formatter" + ], + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\SqlFormatter\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jeremy Dorn", + "email": "jeremy@jeremydorn.com", + "homepage": "https://jeremydorn.com/" + } + ], + "description": "a PHP SQL highlighting library", + "homepage": "https://github.com/doctrine/sql-formatter/", + "keywords": [ + "highlight", + "sql" + ], + "support": { + "issues": "https://github.com/doctrine/sql-formatter/issues", + "source": "https://github.com/doctrine/sql-formatter/tree/1.5.1" + }, + "time": "2024-10-21T18:21:57+00:00" + }, { "name": "dragonmantank/cron-expression", "version": "v3.4.0", @@ -2861,6 +2917,93 @@ }, "time": "2024-11-12T14:59:47+00:00" }, + { + "name": "laravel/pulse", + "version": "v1.3.2", + "source": { + "type": "git", + "url": "https://github.com/laravel/pulse.git", + "reference": "f0bf3959faa89c05fa211632b6d2665131b017fc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel/pulse/zipball/f0bf3959faa89c05fa211632b6d2665131b017fc", + "reference": "f0bf3959faa89c05fa211632b6d2665131b017fc", + "shasum": "" + }, + "require": { + "doctrine/sql-formatter": "^1.4.1", + "guzzlehttp/promises": "^1.0|^2.0", + "illuminate/auth": "^10.48.4|^11.0.8", + "illuminate/cache": "^10.48.4|^11.0.8", + "illuminate/config": "^10.48.4|^11.0.8", + "illuminate/console": "^10.48.4|^11.0.8", + "illuminate/contracts": "^10.48.4|^11.0.8", + "illuminate/database": "^10.48.4|^11.0.8", + "illuminate/events": "^10.48.4|^11.0.8", + "illuminate/http": "^10.48.4|^11.0.8", + "illuminate/queue": "^10.48.4|^11.0.8", + "illuminate/redis": "^10.48.4|^11.0.8", + "illuminate/routing": "^10.48.4|^11.0.8", + "illuminate/support": "^10.48.4|^11.0.8", + "illuminate/view": "^10.48.4|^11.0.8", + "livewire/livewire": "^3.4.9", + "nesbot/carbon": "^2.67|^3.0", + "php": "^8.1", + "symfony/console": "^6.0|^7.0" + }, + "conflict": { + "nunomaduro/collision": "<7.7.0" + }, + "require-dev": { + "guzzlehttp/guzzle": "^7.7", + "mockery/mockery": "^1.0", + "orchestra/testbench": "^8.23.1|^9.0", + "pestphp/pest": "^2.0", + "pestphp/pest-plugin-laravel": "^2.2", + "phpstan/phpstan": "^1.11", + "predis/predis": "^1.0|^2.0" + }, + "type": "library", + "extra": { + "laravel": { + "aliases": { + "Pulse": "Laravel\\Pulse\\Facades\\Pulse" + }, + "providers": [ + "Laravel\\Pulse\\PulseServiceProvider" + ] + }, + "branch-alias": { + "dev-master": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Laravel\\Pulse\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "Laravel Pulse is a real-time application performance monitoring tool and dashboard for your Laravel application.", + "homepage": "https://github.com/laravel/pulse", + "keywords": [ + "laravel" + ], + "support": { + "issues": "https://github.com/laravel/pulse/issues", + "source": "https://github.com/laravel/pulse" + }, + "time": "2024-12-12T18:17:53+00:00" + }, { "name": "laravel/serializable-closure", "version": "v2.0.1", @@ -4003,6 +4146,82 @@ ], "time": "2024-12-08T08:18:47+00:00" }, + { + "name": "livewire/livewire", + "version": "v3.5.18", + "source": { + "type": "git", + "url": "https://github.com/livewire/livewire.git", + "reference": "62f0fa6b340a467c25baa590a567d9a134b357da" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/livewire/livewire/zipball/62f0fa6b340a467c25baa590a567d9a134b357da", + "reference": "62f0fa6b340a467c25baa590a567d9a134b357da", + "shasum": "" + }, + "require": { + "illuminate/database": "^10.0|^11.0", + "illuminate/routing": "^10.0|^11.0", + "illuminate/support": "^10.0|^11.0", + "illuminate/validation": "^10.0|^11.0", + "laravel/prompts": "^0.1.24|^0.2|^0.3", + "league/mime-type-detection": "^1.9", + "php": "^8.1", + "symfony/console": "^6.0|^7.0", + "symfony/http-kernel": "^6.2|^7.0" + }, + "require-dev": { + "calebporzio/sushi": "^2.1", + "laravel/framework": "^10.15.0|^11.0", + "mockery/mockery": "^1.3.1", + "orchestra/testbench": "^8.21.0|^9.0", + "orchestra/testbench-dusk": "^8.24|^9.1", + "phpunit/phpunit": "^10.4", + "psy/psysh": "^0.11.22|^0.12" + }, + "type": "library", + "extra": { + "laravel": { + "aliases": { + "Livewire": "Livewire\\Livewire" + }, + "providers": [ + "Livewire\\LivewireServiceProvider" + ] + } + }, + "autoload": { + "files": [ + "src/helpers.php" + ], + "psr-4": { + "Livewire\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Caleb Porzio", + "email": "calebporzio@gmail.com" + } + ], + "description": "A front-end framework for Laravel.", + "support": { + "issues": "https://github.com/livewire/livewire/issues", + "source": "https://github.com/livewire/livewire/tree/v3.5.18" + }, + "funding": [ + { + "url": "https://github.com/livewire", + "type": "github" + } + ], + "time": "2024-12-23T15:05:02+00:00" + }, { "name": "minishlink/web-push", "version": "v8.0.0", diff --git a/config/pulse.php b/config/pulse.php new file mode 100644 index 000000000..7341ca7f0 --- /dev/null +++ b/config/pulse.php @@ -0,0 +1,236 @@ + env('PULSE_DOMAIN'), + + /* + |-------------------------------------------------------------------------- + | Pulse Path + |-------------------------------------------------------------------------- + | + | This is the path which the Pulse dashboard will be accessible from. Feel + | free to change this path to anything you'd like. Note that this won't + | affect the path of the internal API that is never exposed to users. + | + */ + + 'path' => env('PULSE_PATH', 'pulse'), + + /* + |-------------------------------------------------------------------------- + | Pulse Master Switch + |-------------------------------------------------------------------------- + | + | This configuration option may be used to completely disable all Pulse + | data recorders regardless of their individual configurations. This + | provides a single option to quickly disable all Pulse recording. + | + */ + + 'enabled' => env('PULSE_ENABLED', false), + + /* + |-------------------------------------------------------------------------- + | Pulse Storage Driver + |-------------------------------------------------------------------------- + | + | This configuration option determines which storage driver will be used + | while storing entries from Pulse's recorders. In addition, you also + | may provide any options to configure the selected storage driver. + | + */ + + 'storage' => [ + 'driver' => env('PULSE_STORAGE_DRIVER', 'database'), + + 'trim' => [ + 'keep' => env('PULSE_STORAGE_KEEP', '7 days'), + ], + + 'database' => [ + 'connection' => env('PULSE_DB_CONNECTION'), + 'chunk' => 1000, + ], + ], + + /* + |-------------------------------------------------------------------------- + | Pulse Ingest Driver + |-------------------------------------------------------------------------- + | + | This configuration options determines the ingest driver that will be used + | to capture entries from Pulse's recorders. Ingest drivers are great to + | free up your request workers quickly by offloading the data storage. + | + */ + + 'ingest' => [ + 'driver' => env('PULSE_INGEST_DRIVER', 'storage'), + + 'buffer' => env('PULSE_INGEST_BUFFER', 5_000), + + 'trim' => [ + 'lottery' => [1, 1_000], + 'keep' => env('PULSE_INGEST_KEEP', '7 days'), + ], + + 'redis' => [ + 'connection' => env('PULSE_REDIS_CONNECTION'), + 'chunk' => 1000, + ], + ], + + /* + |-------------------------------------------------------------------------- + | Pulse Cache Driver + |-------------------------------------------------------------------------- + | + | This configuration option determines the cache driver that will be used + | for various tasks, including caching dashboard results, establishing + | locks for events that should only occur on one server and signals. + | + */ + + 'cache' => env('PULSE_CACHE_DRIVER'), + + /* + |-------------------------------------------------------------------------- + | Pulse Route Middleware + |-------------------------------------------------------------------------- + | + | These middleware will be assigned to every Pulse route, giving you the + | chance to add your own middleware to this list or change any of the + | existing middleware. Of course, reasonable defaults are provided. + | + */ + + 'middleware' => [ + 'web', + Authorize::class, + ], + + /* + |-------------------------------------------------------------------------- + | Pulse Recorders + |-------------------------------------------------------------------------- + | + | The following array lists the "recorders" that will be registered with + | Pulse, along with their configuration. Recorders gather application + | event data from requests and tasks to pass to your ingest driver. + | + */ + + 'recorders' => [ + Recorders\CacheInteractions::class => [ + 'enabled' => env('PULSE_CACHE_INTERACTIONS_ENABLED', true), + 'sample_rate' => env('PULSE_CACHE_INTERACTIONS_SAMPLE_RATE', 1), + 'ignore' => [ + ...Pulse::defaultVendorCacheKeys(), + ], + 'groups' => [ + '/^job-exceptions:.*/' => 'job-exceptions:*', + // '/:\d+/' => ':*', + ], + ], + + Recorders\Exceptions::class => [ + 'enabled' => env('PULSE_EXCEPTIONS_ENABLED', true), + 'sample_rate' => env('PULSE_EXCEPTIONS_SAMPLE_RATE', 1), + 'location' => env('PULSE_EXCEPTIONS_LOCATION', true), + 'ignore' => [ + // '/^Package\\\\Exceptions\\\\/', + ], + ], + + Recorders\Queues::class => [ + 'enabled' => env('PULSE_QUEUES_ENABLED', true), + 'sample_rate' => env('PULSE_QUEUES_SAMPLE_RATE', 1), + 'ignore' => [ + // '/^Package\\\\Jobs\\\\/', + ], + ], + + Recorders\Servers::class => [ + 'server_name' => env('PULSE_SERVER_NAME', gethostname()), + 'directories' => explode(':', env('PULSE_SERVER_DIRECTORIES', '/')), + ], + + Recorders\SlowJobs::class => [ + 'enabled' => env('PULSE_SLOW_JOBS_ENABLED', true), + 'sample_rate' => env('PULSE_SLOW_JOBS_SAMPLE_RATE', 1), + 'threshold' => env('PULSE_SLOW_JOBS_THRESHOLD', 1000), + 'ignore' => [ + // '/^Package\\\\Jobs\\\\/', + ], + ], + + Recorders\SlowOutgoingRequests::class => [ + 'enabled' => env('PULSE_SLOW_OUTGOING_REQUESTS_ENABLED', true), + 'sample_rate' => env('PULSE_SLOW_OUTGOING_REQUESTS_SAMPLE_RATE', 1), + 'threshold' => env('PULSE_SLOW_OUTGOING_REQUESTS_THRESHOLD', 1000), + 'ignore' => [ + // '#^http://127\.0\.0\.1:13714#', // Inertia SSR... + ], + 'groups' => [ + // '#^https://api\.github\.com/repos/.*$#' => 'api.github.com/repos/*', + // '#^https?://([^/]*).*$#' => '\1', + // '#/\d+#' => '/*', + ], + ], + + Recorders\SlowQueries::class => [ + 'enabled' => env('PULSE_SLOW_QUERIES_ENABLED', true), + 'sample_rate' => env('PULSE_SLOW_QUERIES_SAMPLE_RATE', 1), + 'threshold' => env('PULSE_SLOW_QUERIES_THRESHOLD', 1000), + 'location' => env('PULSE_SLOW_QUERIES_LOCATION', true), + 'max_query_length' => env('PULSE_SLOW_QUERIES_MAX_QUERY_LENGTH'), + 'ignore' => [ + '/(["`])pulse_[\w]+?\1/', // Pulse tables... + '/(["`])telescope_[\w]+?\1/', // Telescope tables... + ], + ], + + Recorders\SlowRequests::class => [ + 'enabled' => env('PULSE_SLOW_REQUESTS_ENABLED', true), + 'sample_rate' => env('PULSE_SLOW_REQUESTS_SAMPLE_RATE', 1), + 'threshold' => env('PULSE_SLOW_REQUESTS_THRESHOLD', 1000), + 'ignore' => [ + '#^/'.env('PULSE_PATH', 'pulse').'$#', // Pulse dashboard... + '#^/telescope#', // Telescope dashboard... + ], + ], + + Recorders\UserJobs::class => [ + 'enabled' => env('PULSE_USER_JOBS_ENABLED', true), + 'sample_rate' => env('PULSE_USER_JOBS_SAMPLE_RATE', 1), + 'ignore' => [ + // '/^Package\\\\Jobs\\\\/', + ], + ], + + Recorders\UserRequests::class => [ + 'enabled' => env('PULSE_USER_REQUESTS_ENABLED', true), + 'sample_rate' => env('PULSE_USER_REQUESTS_SAMPLE_RATE', 1), + 'ignore' => [ + '#^/'.env('PULSE_PATH', 'pulse').'$#', // Pulse dashboard... + '#^/telescope#', // Telescope dashboard... + ], + ], + ], +]; diff --git a/database/migrations/2023_06_07_000001_create_pulse_tables.php b/database/migrations/2023_06_07_000001_create_pulse_tables.php new file mode 100644 index 000000000..5d194e2c9 --- /dev/null +++ b/database/migrations/2023_06_07_000001_create_pulse_tables.php @@ -0,0 +1,84 @@ +shouldRun()) { + return; + } + + Schema::create('pulse_values', function (Blueprint $table) { + $table->id(); + $table->unsignedInteger('timestamp'); + $table->string('type'); + $table->mediumText('key'); + match ($this->driver()) { + 'mariadb', 'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'), + 'pgsql' => $table->uuid('key_hash')->storedAs('md5("key")::uuid'), + 'sqlite' => $table->string('key_hash'), + }; + $table->mediumText('value'); + + $table->index('timestamp'); // For trimming... + $table->index('type'); // For fast lookups and purging... + $table->unique(['type', 'key_hash']); // For data integrity and upserts... + }); + + Schema::create('pulse_entries', function (Blueprint $table) { + $table->id(); + $table->unsignedInteger('timestamp'); + $table->string('type'); + $table->mediumText('key'); + match ($this->driver()) { + 'mariadb', 'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'), + 'pgsql' => $table->uuid('key_hash')->storedAs('md5("key")::uuid'), + 'sqlite' => $table->string('key_hash'), + }; + $table->bigInteger('value')->nullable(); + + $table->index('timestamp'); // For trimming... + $table->index('type'); // For purging... + $table->index('key_hash'); // For mapping... + $table->index(['timestamp', 'type', 'key_hash', 'value']); // For aggregate queries... + }); + + Schema::create('pulse_aggregates', function (Blueprint $table) { + $table->id(); + $table->unsignedInteger('bucket'); + $table->unsignedMediumInteger('period'); + $table->string('type'); + $table->mediumText('key'); + match ($this->driver()) { + 'mariadb', 'mysql' => $table->char('key_hash', 16)->charset('binary')->virtualAs('unhex(md5(`key`))'), + 'pgsql' => $table->uuid('key_hash')->storedAs('md5("key")::uuid'), + 'sqlite' => $table->string('key_hash'), + }; + $table->string('aggregate'); + $table->decimal('value', 20, 2); + $table->unsignedInteger('count')->nullable(); + + $table->unique(['bucket', 'period', 'type', 'aggregate', 'key_hash']); // Force "on duplicate update"... + $table->index(['period', 'bucket']); // For trimming... + $table->index('type'); // For purging... + $table->index(['period', 'type', 'aggregate', 'bucket']); // For aggregate queries... + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('pulse_values'); + Schema::dropIfExists('pulse_entries'); + Schema::dropIfExists('pulse_aggregates'); + } +}; diff --git a/resources/views/vendor/pulse/dashboard.blade.php b/resources/views/vendor/pulse/dashboard.blade.php new file mode 100644 index 000000000..6a95bb19e --- /dev/null +++ b/resources/views/vendor/pulse/dashboard.blade.php @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + +