Skip to content

Prunable scans #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is deze niet gevaarlijk dat die dan de hele table leegt? @Baspa

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes je had gelijk

return static::query();
}

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