<commit_before><?php

namespace PragmaRX\Health\Checkers;

use GuzzleHttp\Client as Guzzle;
use Illuminate\Support\Collection;
use Mockery\Exception;
use PragmaRX\Health\Support\Result;

class HealthPanel extends Http
{
    /**
     * @var
     */
    protected $guzzle;

    /**
     * @var
     */
    private $totalTime;

    /**
     * @var
     */
    private $url;

    /**
     * @var
     */
    protected $secure = true;

    /**
     * HTTP Checker.
     *
     * @return Result
     */
    public function check()
    {
        $resources = $this->getResourceUrlArray();

        $first = collect($resources)->first();

        if (filled($first)) {
            $this->target->setDisplay("{$first['url']}");
        }

        try {
            foreach ($resources as $url) {
                [$healthy, $message] = $this->checkHealthPanel($url);

                if (! $healthy) {
                    return $this->makeResult(false, $message);
                }
            }

            return $this->makeHealthyResult();
        } catch (\Exception $exception) {
            report($exception);

            return $this->makeResultFromException($exception);
        }
    }

    /**
     *  Get array of resource urls.
     *
     * @return array
     */
    private function getResourceUrlArray()
    {
        $urls = $this->target->urls;

        if (! is_a($urls, Collection::class)) {
            $urls = collect($urls);
        }

        $result = collect();

        $index = 0;

        foreach ($urls as $urlGroup) {
            foreach ($urlGroup as $url => $values) {
                if (blank($values['url'] ?? null)) {
                    $values['url'] = $url;
                }

                $result[$index] = $values;

                $index++;
            }
        }

        return $result->toArray();
    }

    /**
     * HTTP Checker.
     *
     * @return Result
     */
    public function checkHealthPanel($url)
    {
        $resources = $this->getJson($url);

        if ($resources === null) {
            throw new \Exception('Error reading Health Panel json from '.$url['url']);
        }

        $messages = [];

        foreach ($resources as $resource) {
            foreach ($resource['targets'] as $target) {
                if (! $target['result']['healthy']) {
                    $messages[] = "{$resource['name']}: failing.";
                }
            }
        }

        if (count($messages) > 0) {
            throw new Exception(join(' ', $messages));
        }

        return [true, null];
    }

    /**
     * Send an http request and fetch the panel json.
     *
     * @param $url
     * @return mixed|\Psr\Http\Message\ResponseInterface
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    private function fetchJson($url, $parameters = [])
    {
        $this->url = $url;

        return (new Guzzle())->request(
            $parameters['method'],
            $this->url,
            array_merge($this->getConnectionOptions($this->secure), $parameters)
        );
    }

    public function getJson($url)
    {
        return json_decode((string) $this->fetchJson($url['url'], ['method' => $url['method']])->getBody(), true);
    }
}<commit_msg>Fix not all parameters being sent to Guzzle<commit_after><?php

namespace PragmaRX\Health\Checkers;

use GuzzleHttp\Client as Guzzle;
use Illuminate\Support\Collection;
use Mockery\Exception;
use PragmaRX\Health\Support\Result;

class HealthPanel extends Http
{
    /**
     * @var
     */
    protected $guzzle;

    /**
     * @var
     */
    private $totalTime;

    /**
     * @var
     */
    private $url;

    /**
     * @var
     */
    protected $secure = true;

    /**
     * HTTP Checker.
     *
     * @return Result
     */
    public function check()
    {
        $resources = $this->getResourceUrlArray();

        $first = collect($resources)->first();

        if (filled($first)) {
            $this->target->setDisplay("{$first['url']}");
        }

        try {
            foreach ($resources as $url) {
                [$healthy, $message] = $this->checkHealthPanel($url);

                if (! $healthy) {
                    return $this->makeResult(false, $message);
                }
            }

            return $this->makeHealthyResult();
        } catch (\Exception $exception) {
            report($exception);

            return $this->makeResultFromException($exception);
        }
    }

    /**
     *  Get array of resource urls.
     *
     * @return array
     */
    private function getResourceUrlArray()
    {
        $urls = $this->target->urls;

        if (! is_a($urls, Collection::class)) {
            $urls = collect($urls);
        }

        $result = collect();

        $index = 0;

        foreach ($urls as $urlGroup) {
            foreach ($urlGroup as $url => $values) {
                if (blank($values['url'] ?? null)) {
                    $values['url'] = $url;
                }

                $result[$index] = $values;

                $index++;
            }
        }

        return $result->toArray();
    }

    /**
     * HTTP Checker.
     *
     * @return Result
     */
    public function checkHealthPanel($url)
    {
        $resources = $this->getJson($url);

        if ($resources === null) {
            throw new \Exception('Error reading Health Panel json from '.$url['url']);
        }

        $messages = [];

        foreach ($resources as $resource) {
            foreach ($resource['targets'] as $target) {
                if (! $target['result']['healthy']) {
                    $messages[] = "{$resource['name']}: failing.";
                }
            }
        }

        if (count($messages) > 0) {
            throw new Exception(join(' ', $messages));
        }

        return [true, null];
    }

    /**
     * Send an http request and fetch the panel json.
     *
     * @param $url
     * @return mixed|\Psr\Http\Message\ResponseInterface
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    private function fetchJson($url, $parameters = [])
    {
        $this->url = $url;

        return (new Guzzle())->request(
            $parameters['method'],
            $this->url,
            array_merge($this->getConnectionOptions($this->secure), $parameters)
        );
    }

    public function getJson($parameters)
    {
        $url = $parameters['url'];

        unset($parameters['url']);

        return json_decode((string) $this->fetchJson($url, $parameters)->getBody(), true);
    }
}
{'fraction_non_alphanumeric': 0.1514089870525514, 'fraction_numerical': 0.0006092916984006092, 'mean_word_length': 1.6159362549800798, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1755432', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1800, 'n_words': 383}