bretrzaun / statuspage

Add a simple status page to applications.
MIT License
6 stars 4 forks source link

Add possibility to pass details #25

Open ojooss opened 4 weeks ago

ojooss commented 4 weeks ago

Each check (especially connection checks) should have possibility to pass details showing on "success" state additionally or instead of OK. It would be nice to be able to pass a callback function e.g. to read database version (only if connection is successful)

bretrzaun commented 1 week ago

You can use the setDetails() method on the Result object to specify any detail information.. They will be displayed instead of "OK" if the check passes.

ojooss commented 1 week ago

I already use setDetails() for some CallbackChecks, but I want to add some details to a default check. For example, I want to show database server name in details of DoctrineConnectionCheck. Result object is created within DoctrineConnectionCheck::checkStatus() and if I use simple code like this

$checker = new StatusChecker();
$checker->addCheck(new DoctrineConnectionCheck('Database connection', $mysqlConnection))
$checker->check();

I can not add details to result object.

So maybe passing ?string $details = null to BretRZaun\StatusPage\Check constructor and forwarding to BretRZaun\StatusPage\Result if given? In case of error, details could be concatenated.

ojooss commented 1 week ago

I use this solution/workaround now:

<?php

namespace App\Check;

use BretRZaun\StatusPage\Check\AbstractCheck;
use BretRZaun\StatusPage\Check\CheckInterface;
use BretRZaun\StatusPage\Result;

class DecoratedCheck implements CheckInterface
{
    protected $detailsCallback;

    public function __construct(
        protected AbstractCheck $check,
        callable $detailsCallback,
    ) {
        $this->detailsCallback = $detailsCallback;
    }

    public function __call(string $name, array $arguments)
    {
        return call_user_func_array([$this->check, $name], $arguments);
    }

    public function checkStatus(): Result
    {
        $result = $this->check->checkStatus();
        \call_user_func($this->detailsCallback, $result);
        return $result;
    }
}

- - -

$checker = new StatusChecker();
$checker->addCheck(
    new DecoratedCheck(
        new DoctrineConnectionCheck('Database connection', $mysqlConnection)),
        function (Result $result) use ($mysqlConnection) {
            if ($result->isSuccess()) {
                ...
                $result->setDetails('OK (' . $serverVersion . ')');
            }
        }
    }
);
$checker->check();

Maybe it could be possible to pass a callback function to Check constructor, that is called in checkStatus() before returning the result?