Skip to content

Commit

Permalink
Merge pull request #18 from JackWH/custom-models
Browse files Browse the repository at this point in the history
Add support for custom HealthCheckResultHistoryItem models
  • Loading branch information
freekmurze authored Dec 14, 2021
2 parents 3473e1a + b51754f commit d193f2e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
1 change: 1 addition & 0 deletions config/health.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
'result_stores' => [
Spatie\Health\ResultStores\EloquentHealthResultStore::class => [
'model' => Spatie\Health\Models\HealthCheckResultHistoryItem::class,
'keep_history_for_days' => 5,
],

Expand Down
5 changes: 4 additions & 1 deletion database/migrations/create_health_tables.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Spatie\Health\ResultStores\EloquentHealthResultStore;

return new class extends Migration
{
public function up()
{
Schema::create('health_check_result_history_items', function (Blueprint $table) {
$tableName = EloquentHealthResultStore::getHistoryItemInstance()->getTable();

Schema::create($tableName, function (Blueprint $table) {
$table->id();

$table->string('check_name');
Expand Down
1 change: 1 addition & 0 deletions docs/installation-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ return [
*/
'result_stores' => [
Spatie\Health\ResultStores\EloquentHealthResultStore::class => [
'model' => Spatie\Health\Models\HealthCheckResultHistoryItem::class,
'keep_history_for_days' => 5,
],

Expand Down
13 changes: 13 additions & 0 deletions docs/storing-results/in-the-database.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ return [

The results will be written in the `health_check_result_history_items` table. All field names should be self-explanatory. Using the `App\Spatie\Models\HealthCheckResultHistoryItem` model, you can easily query all results.

## Using a custom model

If you'd like to use a custom model for storing health check results, extend the `Spatie\Health\Models\HealthCheckResultHistoryItem` class, and add it to the `EloquentHealthResultStore` configuration:

```php
return [
'result_stores' => [
Spatie\Health\ResultStores\EloquentHealthResultStore::class => [
'model' => App\Models\CustomHealthCheckResultModel::class,
],
],
```

## Pruning the results table

The model uses the [Laravel's `MassPrunable` trait](https://laravel.com/docs/8.x/eloquent#mass-pruning). In the `health` config file, you can specify the maximum age of a model in the `keep_history_for_days` key. Don't forget to schedule the `model:prune` command, as instructed in Laravel's docs.
10 changes: 10 additions & 0 deletions src/Exceptions/CouldNotSaveResultsInStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Spatie\Health\ResultStores\ResultStore;
use Spatie\Health\Models\HealthCheckResultHistoryItem;

class CouldNotSaveResultsInStore extends Exception
{
Expand All @@ -16,4 +17,13 @@ public static function make(ResultStore $store, Exception $exception): self
previous: $exception,
);
}

public static function doesNotExtendHealthCheckResultHistoryItem(mixed $invalidValue): self
{
$className = HealthCheckResultHistoryItem::class;

return new self(
"You tried to register an invalid HealthCheckResultHistoryItem model: `{$invalidValue}`. A valid model should extend `{$className}`"
);
}
}
30 changes: 27 additions & 3 deletions src/ResultStores/EloquentHealthResultStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,45 @@

namespace Spatie\Health\ResultStores;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Spatie\Health\Exceptions\CouldNotSaveResultsInStore;
use Spatie\Health\Checks\Result;
use Spatie\Health\Models\HealthCheckResultHistoryItem;
use Spatie\Health\ResultStores\StoredCheckResults\StoredCheckResult;
use Spatie\Health\ResultStores\StoredCheckResults\StoredCheckResults;

class EloquentHealthResultStore implements ResultStore
{
public static function determineHistoryItemModel(): string
{
$defaultHistoryClass = HealthCheckResultHistoryItem::class;
$eloquentResultStore = EloquentHealthResultStore::class;

$historyItemModel = config("health.result_stores.{$eloquentResultStore}.model",$defaultHistoryClass);

if (! is_a($historyItemModel, $defaultHistoryClass, true)) {
throw CouldNotSaveResultsInStore::doesNotExtendHealthCheckResultHistoryItem($historyItemModel);
}

return $historyItemModel;
}

/** @return HealthCheckResultHistoryItem|object */
public static function getHistoryItemInstance()
{
$historyItemClassName = static::determineHistoryItemModel();

return new $historyItemClassName();
}

/** @param Collection<int, Result> $checkResults */
public function save(Collection $checkResults): void
{
$batch = Str::uuid();
$checkResults->each(function (Result $result) use ($batch) {
HealthCheckResultHistoryItem::create([
(static::determineHistoryItemModel())::create([
'check_name' => $result->check->getName(),
'check_label' => $result->check->getLabel(),
'status' => $result->status,
Expand All @@ -31,12 +55,12 @@ public function save(Collection $checkResults): void

public function latestResults(): ?StoredCheckResults
{
if (! $latestItem = HealthCheckResultHistoryItem::latest()->first()) {
if (! $latestItem = (static::determineHistoryItemModel())::latest()->first()) {
return null;
}

/** @var Collection<int, StoredCheckResult> $storedCheckResults */
$storedCheckResults = HealthCheckResultHistoryItem::query()
$storedCheckResults = (static::determineHistoryItemModel())::query()
->where('batch', $latestItem->batch)
->get()
->map(function (HealthCheckResultHistoryItem $historyItem) {
Expand Down

0 comments on commit d193f2e

Please sign in to comment.