<commit_before><?php

declare(strict_types=1);

namespace App\Controller\Api\Admin;

use App\Controller\Api\Traits\HasLogViewer;
use App\Environment;
use App\Exception;
use App\Http\Response;
use App\Http\ServerRequest;
use Psr\Http\Message\ResponseInterface;

final class LogsAction
{
    use HasLogViewer;

    public function __construct(
        private readonly Environment $environment
    ) {
    }

    public function __invoke(
        ServerRequest $request,
        Response $response,
        ?string $log = null
    ): ResponseInterface {
        $logPaths = $this->getGlobalLogs();

        if (null === $log) {
            $router = $request->getRouter();
            return $response->withJson(
                [
                    'logs' => array_map(
                        function (string $key, array $row) use ($router) {
                            $row['key'] = $key;
                            $row['links'] = [
                                'self' => (string)$router->named(
                                    'api:admin:log',
                                    [
                                        'log' => $key,
                                    ]
                                ),
                            ];
                            return $row;
                        },
                        array_keys($logPaths),
                        array_values($logPaths)
                    ),
                ]
            );
        }

        if (!isset($logPaths[$log])) {
            throw new Exception('Invalid log file specified.');
        }

        return $this->streamLogToResponse(
            $request,
            $response,
            $logPaths[$log]['path'],
            $logPaths[$log]['tail'] ?? true
        );
    }

    /**
     * @return array<string, array>
     */
    private function getGlobalLogs(): array
    {
        $tempDir = $this->environment->getTempDirectory();
        $logPaths = [];

        $logPaths['azuracast_log'] = [
            'name' => __('AzuraCast Application Log'),
            'path' => $tempDir . '/app-' . gmdate('Y-m-d') . '.log',
            'tail' => true,
        ];

        if ($this->environment->isDocker()) {
            $langServiceLog = __('%s Service Log');
            $logPaths['service_mariadb'] = [
                'name' => sprintf($langServiceLog, __('MariaDB')),
                'path' => $tempDir . '/service_mariadb.log',
                'tail' => true,
            ];
            $logPaths['service_redis'] = [
                'name' => sprintf($langServiceLog, __('Redis')),
                'path' => $tempDir . '/service_redis.log',
                'tail' => true,
            ];
            $logPaths['service_beanstalkd'] = [
                'name' => sprintf($langServiceLog, __('Beanstalkd')),
                'path' => $tempDir . '/service_beanstalkd.log',
                'tail' => true,
            ];
            $logPaths['service_cron'] = [
                'name' => sprintf($langServiceLog, __('Cron')),
                'path' => $tempDir . '/service_cron.log',
                'tail' => true,
            ];
            $logPaths['service_nginx'] = [
                'name' => sprintf($langServiceLog, __('Nginx')),
                'path' => $tempDir . '/service_nginx.log',
                'tail' => true,
            ];
            $logPaths['service_sftpgo'] = [
                'name' => sprintf($langServiceLog, __('SFTPGo')),
                'path' => $tempDir . '/service_sftpgo.log',
                'tail' => true,
            ];
        } else {
            $logPaths['nginx_access'] = [
                'name' => __('Nginx Access Log'),
                'path' => $tempDir . '/access.log',
                'tail' => true,
            ];
            $logPaths['nginx_error'] = [
                'name' => __('Nginx Error Log'),
                'path' => $tempDir . '/error.log',
                'tail' => true,
            ];
            $logPaths['php'] = [
                'name' => __('PHP Application Log'),
                'path' => $tempDir . '/php_errors.log',
                'tail' => true,
            ];
            $logPaths['supervisord'] = [
                'name' => __('Supervisord Log'),
                'path' => $tempDir . '/supervisord.log',<commit_msg>Fixes #5563 -- Fix path for crond log.<commit_after><?php

declare(strict_types=1);

namespace App\Controller\Api\Admin;

use App\Controller\Api\Traits\HasLogViewer;
use App\Environment;
use App\Exception;
use App\Http\Response;
use App\Http\ServerRequest;
use Psr\Http\Message\ResponseInterface;

final class LogsAction
{
    use HasLogViewer;

    public function __construct(
        private readonly Environment $environment
    ) {
    }

    public function __invoke(
        ServerRequest $request,
        Response $response,
        ?string $log = null
    ): ResponseInterface {
        $logPaths = $this->getGlobalLogs();

        if (null === $log) {
            $router = $request->getRouter();
            return $response->withJson(
                [
                    'logs' => array_map(
                        function (string $key, array $row) use ($router) {
                            $row['key'] = $key;
                            $row['links'] = [
                                'self' => (string)$router->named(
                                    'api:admin:log',
                                    [
                                        'log' => $key,
                                    ]
                                ),
                            ];
                            return $row;
                        },
                        array_keys($logPaths),
                        array_values($logPaths)
                    ),
                ]
            );
        }

        if (!isset($logPaths[$log])) {
            throw new Exception('Invalid log file specified.');
        }

        return $this->streamLogToResponse(
            $request,
            $response,
            $logPaths[$log]['path'],
            $logPaths[$log]['tail'] ?? true
        );
    }

    /**
     * @return array<string, array>
     */
    private function getGlobalLogs(): array
    {
        $tempDir = $this->environment->getTempDirectory();
        $logPaths = [];

        $logPaths['azuracast_log'] = [
            'name' => __('AzuraCast Application Log'),
            'path' => $tempDir . '/app-' . gmdate('Y-m-d') . '.log',
            'tail' => true,
        ];

        if ($this->environment->isDocker()) {
            $langServiceLog = __('%s Service Log');
            $logPaths['service_mariadb'] = [
                'name' => sprintf($langServiceLog, __('MariaDB')),
                'path' => $tempDir . '/service_mariadb.log',
                'tail' => true,
            ];
            $logPaths['service_redis'] = [
                'name' => sprintf($langServiceLog, __('Redis')),
                'path' => $tempDir . '/service_redis.log',
                'tail' => true,
            ];
            $logPaths['service_beanstalkd'] = [
                'name' => sprintf($langServiceLog, __('Beanstalkd')),
                'path' => $tempDir . '/service_beanstalkd.log',
                'tail' => true,
            ];
            $logPaths['service_cron'] = [
                'name' => sprintf($langServiceLog, __('Cron')),
                'path' => $tempDir . '/service_crond.log',
                'tail' => true,
            ];
            $logPaths['service_nginx'] = [
                'name' => sprintf($langServiceLog, __('Nginx')),
                'path' => $tempDir . '/service_nginx.log',
                'tail' => true,
            ];
            $logPaths['service_sftpgo'] = [
                'name' => sprintf($langServiceLog, __('SFTPGo')),
                'path' => $tempDir . '/service_sftpgo.log',
                'tail' => true,
            ];
        } else {
            $logPaths['nginx_access'] = [
                'name' => __('Nginx Access Log'),
                'path' => $tempDir . '/access.log',
                'tail' => true,
            ];
            $logPaths['nginx_error'] = [
                'name' => __('Nginx Error Log'),
                'path' => $tempDir . '/error.log',
                'tail' => true,
            ];
            $logPaths['php'] = [
                'name' => __('PHP Application Log'),
                'path' => $tempDir . '/php_errors.log',
                'tail' => true,
            ];
            $logPaths['supervisord'] = [
                'name' => __('Supervisord Log'),
                'path' => $tempDir . '/supervisord.log',
{'fraction_non_alphanumeric': 0.15020766035994462, 'fraction_numerical': 0.0006922011998154131, 'mean_word_length': 1.2557897475930262, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4878283', 'n_tokens_mistral': 2410, 'n_tokens_neox': 2303, 'n_words': 413}