diff --git a/CHANGELOG.md b/CHANGELOG.md index ac2ea15..b619e15 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,10 @@ # Changelog -All notable changes to `rename-me` will be documented in this file +All notable changes to `SlowQueryNotifer` will be documented in this file -## 1.0.0 - 201X-XX-XX +## 1.0.0 - 2020-02-18 -- initial release +- Initial release + - Get an email if you violate the threshold + - Configure the threshold in your app service provider + - Enable/disable in your environment file diff --git a/README.md b/README.md index 6df4f7d..3070e5e 100755 --- a/README.md +++ b/README.md @@ -1,22 +1,85 @@ -# Deliverables +# Slow Query Notifier for Laravel -## Install & Config -- Set threshold in code -- Set email address in code +[![Latest Version on Packagist](https://img.shields.io/packagist/v/thomasjohnkane/slow-query-notifier.svg?style=flat-square)](https://packagist.org/packages/thomasjohnkane/slow-query-notifier) +[![Build Status](https://img.shields.io/travis/thomasjohnkane/slow-query-notifier/master.svg?style=flat-square)](https://travis-ci.org/thomasjohnkane/slow-query-notifier) +[![Quality Score](https://img.shields.io/scrutinizer/g/thomasjohnkane/slow-query-notifier.svg?style=flat-square)](https://scrutinizer-ci.com/g/thomasjohnkane/slow-query-notifier) +[![Total Downloads](https://img.shields.io/packagist/dt/thomasjohnkane/slow-query-notifier.svg?style=flat-square)](https://packagist.org/packages/thomasjohnkane/slow-query-notifier) -## Threshold Notifications -- Run test command via command line to get test notification -- Receive notification when threshold is met -- Handle queuing (graceful) +Get notified if your app ever runs an objectively slow database call. -## Error handling -- Handle errors silently (catch statement, logging) +## Installation -# Steps -- [X] Install orchestra testbench (Laravel Collective) -- [X] Build tests -- [ ] Benchmark performance impact (seed data with package, and without...laravel start time diff??) -- [ ] Branding/marketing planning with Caleb -- [ ] Update Readme +You can install the package via composer: +```bash +composer require thomasjohnkane/slow-query-notifier +``` +## Usage +### Set an email address +```bash + // app/Providers/AppServiceProvider.php + use SlowQueryNotifier\SlowQueryNotifierFacade as SlowQueryNotifier; + + public function boot() + { + SlowQueryNotifier::toEmail('admin@example.com'); + } +``` +## Test it works +```bash + php artisan sqn:test +``` +This command will test two things: + +- We can detect slow queries in your app +- We can send an email to you if a slow query runs + +# Configuration + +In general, we setup all of the configuration for you with sensible defaults. However, you can change the default settings if you'd like. To learn why we chose these defaults, read the blog post. + +## Threshold + +The default is 99ms. Set a different `threshold` in milliseconds in your configuration: +```bash + SlowQueryNotifier::threshold(200)->toEmail('admin@example.com'); +``` +### Enable/Disable + +The package is enabled by default. Set this value to `false` in your `.env` to bypass the listener. +```bash + SLOW_QUERY_NOTIFIER_ENABLED=false +``` +### Testing + +``` bash +phpunit +``` + +### Changelog + +Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. + +## Contributing + +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. + +### Security + +If you discover any security related issues, please email thomasjohnkane@gmail.com instead of using the issue tracker. + +## Credits + +- [Thomas Kane](https://github.com/thomasjohnkane) +- Thanks to Marcel Pociot for the original inspiration +- Thanks to Caleb Porzio for the guidance +- [All Contributors](../../contributors) + +## License + +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. + +## Laravel Package Boilerplate + +This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com). diff --git a/build/report.junit.xml b/build/report.junit.xml deleted file mode 100644 index 85158c3..0000000 --- a/build/report.junit.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - diff --git a/src/SlowQueryNotification.php b/src/SlowQueryNotification.php index 8f08365..f4af25c 100644 --- a/src/SlowQueryNotification.php +++ b/src/SlowQueryNotification.php @@ -3,6 +3,7 @@ namespace SlowQueryNotifier; use Illuminate\Bus\Queueable; +use \Illuminate\Support\HtmlString; use Illuminate\Support\Facades\Log; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue; @@ -13,6 +14,13 @@ class SlowQueryNotification extends Notification implements ShouldQueue { use Queueable; + protected $query; + + public function __construct($query) + { + $this->query = $query; + } + public function via($notifiable) { return ['mail']; @@ -21,6 +29,13 @@ public function via($notifiable) public function toMail($notifiable): MailMessage { return (new MailMessage) - ->subject('Query Threshold Exceeded'); + ->subject('Query Threshold Exceeded') + ->greeting('We found a slow query:') + ->line('Threshold: '.app(SlowQueryNotifier::class)->getThreshold().' ms') + ->line('Time: '.$this->query->time.' ms') + ->line(new HtmlString('
')) + ->line(new HtmlString(''.$this->query->sql.'')) + ->line(new HtmlString('
')) + ->salutation(new HtmlString('Yours Truley,
Slow Query Notifier')); } } diff --git a/src/SlowQueryNotifier.php b/src/SlowQueryNotifier.php index 5760aa6..56bd9f8 100755 --- a/src/SlowQueryNotifier.php +++ b/src/SlowQueryNotifier.php @@ -42,7 +42,7 @@ public function checkQuery($query) { if ($query->time > $this->threshold) { try { Notification::route('mail', $this->email) - ->notify(new SlowQueryNotification()); + ->notify(new SlowQueryNotification($query)); } catch (\Exception $e) { if ($this->throwsExceptions) { throw $e;