├── .gitignore ├── LICENSE.md ├── README.md ├── composer.json ├── example.png ├── resources └── views │ └── livewire │ └── database-card.blade.php └── src ├── DatabaseServiceProvider.php ├── Livewire └── Database.php └── Recorders └── DatabaseRecorder.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | .env 3 | .phpunit.result.cache 4 | composer.lock 5 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Jamie Schouten j4mie@hey.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Database status card for Laravel Pulse 2 | 3 | Get real-time insights into the status of your database 4 | 5 | ## Example 6 | 7 | ![example](example.png) 8 | 9 | ## Installation 10 | 11 | Install the package using Composer: 12 | 13 | ```shell 14 | composer require maantje/pulse-database 15 | ``` 16 | 17 | ## Register the recorder 18 | 19 | In your `pulse.php` configuration file, register the DatabaseRecorder with the desired settings: 20 | 21 | Uncertain about the available values? You can execute `show status` against your database to view all the options at your disposal. 22 | 23 | ```php 24 | return [ 25 | // ... 26 | 27 | 'recorders' => [ 28 | \Maantje\Pulse\Database\Recorders\DatabaseRecorder::class => [ 29 | 'connections' => [ 30 | 'mysql_another' => [ 31 | 'values' => [ 32 | 'Connections', 33 | 'Threads_connected', 34 | 'Threads_running', 35 | 'Innodb_buffer_pool_reads', 36 | 'Innodb_buffer_pool_read_requests', 37 | 'Innodb_buffer_pool_pages_total', 38 | 'Max_used_connections' 39 | ], 40 | 'aggregates' => [ 41 | 'avg' => [ 42 | 'Threads_connected', 43 | 'Threads_running', 44 | 'Innodb_buffer_pool_reads', 45 | 'Innodb_buffer_pool_read_requests', 46 | 'Innodb_buffer_pool_pages_total', 47 | ], 48 | 'max' => [ 49 | // 50 | ], 51 | 'count' => [ 52 | // 53 | ], 54 | ], 55 | ], 56 | 'mysql' => [ 57 | 'values' => [ 58 | 'Connections', 59 | 'Threads_connected', 60 | 'Threads_running', 61 | 'Innodb_buffer_pool_reads', 62 | 'Innodb_buffer_pool_read_requests', 63 | 'Innodb_buffer_pool_pages_total', 64 | 'Max_used_connections' 65 | ], 66 | 'aggregates' => [ 67 | 'avg' => [ 68 | 'Threads_connected', 69 | 'Threads_running', 70 | 'Innodb_buffer_pool_reads', 71 | 'Innodb_buffer_pool_read_requests', 72 | 'Innodb_buffer_pool_pages_total', 73 | ], 74 | 'max' => [ 75 | // 76 | ], 77 | 'count' => [ 78 | // 79 | ], 80 | ], 81 | ] 82 | ] 83 | ], 84 | ] 85 | ] 86 | ``` 87 | 88 | Ensure you're running [the `pulse:check` command](https://laravel.com/docs/10.x/pulse#capturing-entries). 89 | 90 | ## Add to your dashboard 91 | 92 | Integrate the card into your Pulse dashboard by [publish the vendor view](https://laravel.com/docs/10.x/pulse#dashboard-customization). 93 | and then adding the following to the `dashboard.blade.php` file: 94 | 95 | ```html 96 | 99 | 100 | 101 | 102 | 105 | ``` 106 | 107 | And that's it! Enjoy enhanced visibility into your database status on your Pulse dashboard. 108 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "maantje/pulse-database", 3 | "description": "A Laravel Pulse card for database status", 4 | "keywords": ["laravel", "database", "database-status"], 5 | "type": "library", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Jamie Schouten", 10 | "email": "j4mie@hey.com" 11 | } 12 | ], 13 | "require": { 14 | "php": "^8.1", 15 | "illuminate/support": "*", 16 | "laravel/pulse": "^1" 17 | }, 18 | "require-dev": { 19 | "orchestra/testbench": "^8", 20 | "mockery/mockery": "^1.5.0", 21 | "phpunit/phpunit": "^10" 22 | }, 23 | "autoload": { 24 | "psr-4": { 25 | "Maantje\\Pulse\\Database\\": "src/" 26 | } 27 | }, 28 | "extra": { 29 | "laravel": { 30 | "providers": [ 31 | "Maantje\\Pulse\\Database\\DatabaseServiceProvider" 32 | ] 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maantje/pulse-database/a3f3205dcfc197aae7413f0cff0e79293ae3a2ea/example.png -------------------------------------------------------------------------------- /resources/views/livewire/database-card.blade.php: -------------------------------------------------------------------------------- 1 | @php use Illuminate\Support\Str; @endphp 2 | 3 | 8 | 9 |
10 | @foreach($graphs as $type => $aggregates) 11 | @foreach($aggregates as $aggregate => $color) 12 |
13 |
14 | {{ str_replace('_', ' ', ucfirst($aggregate)) }} {{ $type }} 15 |
16 | @endforeach 17 | @endforeach 18 |
19 |
20 |
21 | @if ($connections->isEmpty()) 22 | 23 | @else 24 |
25 | @foreach ($connections as $slug => $connection) 26 |
27 |
28 | @php 29 | $highest = $connection->aggregates->flatten()->max(); 30 | @endphp 31 |
32 |
33 |
36 | @if ($connection->recently_reported) 37 |
38 |
39 |
40 | @else 41 | 42 | @endif 43 |
44 |
46 | 47 | {{ $connection->name }} 49 |
50 |
51 | @foreach($values as $value) 52 |
53 | 54 | {{ \Illuminate\Support\Number::format($connection->$value) }} 55 | 56 | 57 | {{ str_replace('_', ' ', ucfirst($value)) }} 58 | 59 |
60 | @endforeach 61 |
62 | 63 | @if(count($graphs) > 0) 64 |
65 |
67 | {{ number_format($highest) }} 68 |
69 |
166 | 168 |
169 |
170 | @endif 171 |
172 |
173 | @endforeach 174 |
175 | 176 | @endif 177 |
178 | -------------------------------------------------------------------------------- /src/DatabaseServiceProvider.php: -------------------------------------------------------------------------------- 1 | loadViewsFrom(__DIR__ . '/../resources/views', 'database'); 15 | 16 | $this->callAfterResolving('livewire', function (LivewireManager $livewire, Application $app) { 17 | $livewire->component('database', Database::class); 18 | }); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Livewire/Database.php: -------------------------------------------------------------------------------- 1 | remember(function () { 35 | $graphs = []; 36 | 37 | foreach ($this->graphs as $aggregate => $graph) { 38 | $graphs[$aggregate] = Pulse::graph(array_keys($graph), $aggregate, $this->periodAsInterval()); 39 | } 40 | 41 | 42 | return Pulse::values('database_connection') 43 | ->map(function ($fpm, $slug) use ($graphs) { 44 | $values = json_decode($fpm->value, flags: JSON_THROW_ON_ERROR); 45 | 46 | return (object) [ 47 | ...((array) $values), 48 | 'aggregates' => collect($this->graphs)->reduce(function (Collection $carry, $aggregates, $type) use ($slug, $graphs) { 49 | $carry[$type] = collect(array_keys($aggregates))->mapWithKeys(function ($value) use ($slug, $type, $graphs) { 50 | return [$value => $graphs[$type]->get($slug)?->get($value) ?? collect()]; 51 | }); 52 | 53 | return $carry; 54 | }, collect()), 55 | 'updated_at' => $updatedAt = CarbonImmutable::createFromTimestamp($fpm->timestamp), 56 | 'recently_reported' => $updatedAt->isAfter(now()->subSeconds(30)), 57 | ]; 58 | }); 59 | }, Str::slug($this->title)); 60 | 61 | if (Request::hasHeader('X-Livewire')) { 62 | $this->dispatch(Str::slug($this->title). '-database-chart-update', connections: $connections); 63 | } 64 | 65 | return View::make('database::livewire.database-card', [ 66 | 'connections' => $connections, 67 | 'time' => $time, 68 | 'runAt' => $runAt, 69 | ]); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Recorders/DatabaseRecorder.php: -------------------------------------------------------------------------------- 1 | config->get('pulse.recorders.'.self::class.'.connections', []) as $connectionName => $config) { 36 | $connection = $this->manager->connection($connectionName); 37 | 38 | $keys = collect() 39 | ->merge($this->config->get("pulse.recorders.$class.connections.$connectionName.values", [])) 40 | ->merge($maxAggregates = $this->config->get("pulse.recorders.$class.connections.$connectionName.aggregates.max", [])) 41 | ->merge($avgAggregates = $this->config->get("pulse.recorders.$class.connections.$connectionName.aggregates.avg", [])) 42 | ->merge($countAggregates = $this->config->get("pulse.recorders.$class.connections.$connectionName.aggregates.count", [])); 43 | 44 | $status = collect($connection->select('show status')) 45 | ->filter(function ($row) use ($keys) { 46 | return $keys->contains($row->Variable_name); 47 | }) 48 | ->mapWithKeys(function ($row) { 49 | return [$row->Variable_name => $row->Value]; 50 | }); 51 | 52 | 53 | $slug = Str::slug($connection->getName()); 54 | 55 | foreach ($maxAggregates as $max) { 56 | $this->pulse->record($max, $slug, $status->get($max), $event->time)->max()->onlyBuckets(); 57 | } 58 | 59 | foreach ($avgAggregates as $avg) { 60 | $this->pulse->record($avg, $slug, $status->get($avg), $event->time)->avg()->onlyBuckets(); 61 | } 62 | 63 | foreach ($countAggregates as $count) { 64 | $this->pulse->record($count, $slug, $status->get($count), $event->time)->count()->onlyBuckets(); 65 | } 66 | 67 | $this->pulse->set('database_connection', $slug, json_encode([ 68 | ...$status, 69 | 'name' => $connection->getName(), 70 | ])); 71 | } 72 | } 73 | } 74 | --------------------------------------------------------------------------------