Новые роуты API для статистики и мониторинга
- /api/version - /api/health - /api/stats
This commit is contained in:
@@ -9,6 +9,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Bot;
|
||||
use App\Core\Kernel;
|
||||
use App\Core\StatisticsService;
|
||||
use App\Errors\PlaylistNotFoundException;
|
||||
use chillerlan\QRCode\QRCode;
|
||||
use chillerlan\QRCode\QROptions;
|
||||
@@ -81,4 +84,106 @@ class ApiController extends BasicController
|
||||
return $response->withStatus(200)
|
||||
->withHeader('Content-Type', $mime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает информацию о плейлисте
|
||||
*
|
||||
* @param ServerRequestInterface $request
|
||||
* @param ResponseInterface $response
|
||||
* @return ResponseInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function version(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
return $this->responseJson($response, 200, [
|
||||
'web' => Kernel::VERSION,
|
||||
'php' => PHP_VERSION,
|
||||
'keydb' => redis()->info('server')['redis_version'],
|
||||
'checker' => 'todo',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает информацию о плейлисте
|
||||
*
|
||||
* @param ServerRequestInterface $request
|
||||
* @param ResponseInterface $response
|
||||
* @return ResponseInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function health(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
function getSize(string $directory): int
|
||||
{
|
||||
$size = 0;
|
||||
foreach (glob($directory . '/*') as $path){
|
||||
is_file($path) && $size += filesize($path);
|
||||
is_dir($path) && $size += getSize($path);
|
||||
}
|
||||
return $size;
|
||||
}
|
||||
|
||||
$tgBotInfo = config('bot.token') ? new Bot()->api->getMe() : null;
|
||||
$redisInfoServer = redis()->info('server'); // General information about the Redis server
|
||||
$redisInfoClients = redis()->info('clients'); // Client connections section
|
||||
$redisInfoMemory = redis()->info('memory'); // Memory consumption related information
|
||||
$redisInfoPers = redis()->info('persistence'); // RDB and AOF related information
|
||||
$redisInfoStats = redis()->info('stats'); // General statistics
|
||||
$redisInfoCpu = redis()->info('cpu'); // CPU consumption statistics
|
||||
$redisInfoCmd = redis()->info('commandstats'); // Redis command statistics
|
||||
$redisInfoKeysp = redis()->info('keyspace'); // Database related statistics
|
||||
$redisInfoErr = redis()->info('errorstats'); // Redis error statistics
|
||||
|
||||
$health = [
|
||||
'fileCache' => [
|
||||
'tv-logos' => [
|
||||
'sizeB' => $size = getSize(cache_path('tv-logos')),
|
||||
'sizeMiB' => round($size / 1024 / 1024, 3),
|
||||
'count' => count(glob(cache_path('tv-logos') . '/*')),
|
||||
],
|
||||
'qr-codes' => [
|
||||
'sizeB' => $size = getSize(cache_path('qr-codes')),
|
||||
'sizeMiB' => round($size / 1024 / 1024, 3),
|
||||
'count' => count(glob(cache_path('qr-codes') . '/*')),
|
||||
],
|
||||
],
|
||||
'telegram' => [
|
||||
'id' => $tgBotInfo->getId(),
|
||||
'first_name' => $tgBotInfo->getFirstName(),
|
||||
'username' => $tgBotInfo->getUsername(),
|
||||
],
|
||||
'redis' => [
|
||||
'isConnected' => redis()->isConnected(),
|
||||
'info' => [
|
||||
'server' => [
|
||||
'uptime_in_seconds' => $redisInfoServer['uptime_in_seconds'],
|
||||
'uptime_in_days' => $redisInfoServer['uptime_in_days'],
|
||||
],
|
||||
'clients' => ['connected_clients' => $redisInfoClients['connected_clients']],
|
||||
'memory' => $redisInfoMemory,
|
||||
'persistence' => $redisInfoPers,
|
||||
'stats' => $redisInfoStats,
|
||||
'cpu' => $redisInfoCpu,
|
||||
'commandstats' => $redisInfoCmd,
|
||||
'keyspace' => $redisInfoKeysp,
|
||||
'errorstats' => $redisInfoErr,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
return $this->responseJson($response, 200, $health);
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает информацию о плейлисте
|
||||
*
|
||||
* @param ServerRequestInterface $request
|
||||
* @param ResponseInterface $response
|
||||
* @return ResponseInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function stats(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
return $this->responseJson($response, 200, new StatisticsService()->get());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,8 +49,9 @@ class BasicController
|
||||
* @param array $data
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
protected function responseJson(ResponseInterface $response, int $status, array $data): ResponseInterface
|
||||
protected function responseJson(ResponseInterface $response, int $status, mixed $data): ResponseInterface
|
||||
{
|
||||
is_scalar($data) && $data = [$data];
|
||||
$data = array_merge(['timestamp' => time()], $data);
|
||||
$json = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user