Skip to content

Commit

Permalink
Prunable scans (#30)
Browse files Browse the repository at this point in the history
* Make pruning configurable

* Fix styling

* Use subDays instead of subDay

* Fix styling

* Update README

* Add missing model properties

* Fix styling

* Disable parallel testing due testing errors

* Revert "Disable parallel testing due testing errors"

This reverts commit 97fe3f7.

* wip

---------

Co-authored-by: Baspa <[email protected]>
  • Loading branch information
Baspa and Baspa authored May 26, 2023
1 parent 730d42b commit c0eed61
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.1]
laravel: [9.*]
php: [8.2, 8.1]
laravel: [10.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 9.*
testbench: 7.*
- laravel: 10.*
testbench: 8.*
carbon: ^2.63

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

Expand All @@ -40,11 +41,11 @@ jobs:
- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "nesbot/carbon:${{ matrix.carbon }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: List Installed Dependencies
run: composer show -D

- name: Execute tests
run: vendor/bin/pest --parallel --processes=12
run: vendor/bin/pest --ci
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,17 @@ return [
| Database
|--------------------------------------------------------------------------
|
| Here you can specify the database connection that will be
| used to save the SEO scores. When you set the save option to true, the
| SEO score will be saved to the database.
| Here you can specify database related configurations like the connection
| that will be used to save the SEO scores. When you set the save
| option to true, the SEO score will be saved to the database.
|
*/
'database' => [
'connection' => 'mysql',
'save' => true,
'prune' => [
'older_than_days' => 30,
]
],

/*
Expand Down Expand Up @@ -374,11 +377,29 @@ When you want to save the SEO score to the database, you need to set the `save`
'database' => [
'connection' => 'mysql',
'save' => true,
'prune' => [
'older_than_days' => 30,
],
],
```

Optionally you can specify the database connection in the config file. If you want to save the SEO score to a model, you need to add the model to the `models` array in the config file. More information about this can be found in the [Check the SEO score of a model](#check-the-seo-score-of-a-model) section.

#### Pruning the database
Per default the package will prune the database from old scans. You can disable this by setting the `prune` option to `false` in the config file. If you want to prune the database, you can specify the number of days you want to keep the scans in the database. The default is 30 days.

If you want to prune the database, you need to add the prune command to your `App\Console\Kernel`:

```php
protected function schedule(Schedule $schedule)
{
// ...
$schedule->command('model:prune')->daily();
}
```

Please refer to the [Laravel documentation](https://laravel.com/docs/10.x/eloquent#pruning-models) for more information about pruning the database.

### Listening to events

When you run the `seo:scan` command, the package will fire an event to let you know it's finished. You can listen to this events and do something with the data. For example, you can send an email to the administrator when the SEO score of a page is below a certain threshold. Add the following code to your `EventServiceProvider`:
Expand Down
5 changes: 4 additions & 1 deletion config/seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,17 @@
| Database
|--------------------------------------------------------------------------
|
| Here you can specify the database connection that will be
| Here you can specify database related configurations like the connection that will be
| used to save the SEO scores. When you set the save option to true, the
| SEO score will be saved to the database.
|
*/
'database' => [
'connection' => 'mysql',
'save' => true,
'prune' => [
'older_than_days' => 30,
],
],

/*
Expand Down
12 changes: 12 additions & 0 deletions src/Models/SeoScan.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vormkracht10\Seo\Models;

use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Prunable;
use Illuminate\Database\Eloquent\Relations\HasMany;
Expand All @@ -12,6 +13,8 @@
* @property int $total_checks
* @property array $failed_checks
* @property float $time
* @property \Illuminate\Support\Carbon $created_at
* @property \Illuminate\Support\Carbon $updated_at
* @property \Illuminate\Support\Carbon $started_at
* @property \Illuminate\Support\Carbon $finished_at
*/
Expand Down Expand Up @@ -40,4 +43,13 @@ public function scores(): HasMany
{
return $this->hasMany(SeoScore::class);
}

public function prunable(): Builder
{
if (! config('seo.database.prune.older_than_days')) {
return static::query();
}

return static::where('created_at', '<=', now()->subDays(config('seo.database.prune.older_than_days')));
}
}

0 comments on commit c0eed61

Please sign in to comment.