Skip to content

Commit

Permalink
Add trackers and connection info to status command
Browse files Browse the repository at this point in the history
  • Loading branch information
jkocik committed Nov 6, 2018
1 parent b013bd9 commit 806125f
Show file tree
Hide file tree
Showing 11 changed files with 511 additions and 23 deletions.
78 changes: 71 additions & 7 deletions src/Console/StatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
namespace JKocik\Laravel\Profiler\Console;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Event;
use JKocik\Laravel\Profiler\Services\ConfigService;
use JKocik\Laravel\Profiler\Services\ConsoleService;
use JKocik\Laravel\Profiler\Processors\StatusCommandProcessor;
use JKocik\Laravel\Profiler\Events\ProfilerServerConnectionFailed;
use JKocik\Laravel\Profiler\Events\ProfilerServerConnectionSuccessful;

class StatusCommand extends Command
{
Expand All @@ -22,43 +27,102 @@ class StatusCommand extends Command
*/
protected $configService;

/**
* @var ConsoleService
*/
protected $consoleService;

/**
* @var bool
*/
protected $connectionStatusIsUnknown = true;

/**
* StatusCommand constructor.
* @param ConfigService $configService
* @param ConsoleService $consoleService
*/
public function __construct(ConfigService $configService)
public function __construct(ConfigService $configService, ConsoleService $consoleService)
{
parent::__construct();

$this->configService = $configService;
$this->consoleService = $consoleService;
}

/**
* @return void
*/
public function handle(): void
{
$this->configService->overrideProcessors([
StatusCommandProcessor::class,
]);

$this->printProfilerStatus();

if (! $this->configService->isProfilerEnabled()) {
$this->printDisabledStatus();
return;
}

$this->printEnabledStatus();
$this->printTrackersStatus();

$this->printConnectionStatus();
}

/**
* @return void
*/
protected function printEnabledStatus(): void
protected function printProfilerStatus(): void
{
$this->info('Laravel Profiler is enabled');
$this->line("1) {$this->consoleService->envInfo()}");
$this->info($this->consoleService->profilerStatusInfo());
}

/**
* @return void
*/
protected function printDisabledStatus(): void
protected function printTrackersStatus(): void
{
$this->info('Laravel Profiler is disabled');
$this->line('');
$this->line("2) {$this->consoleService->trackersStatusInfo()}");

$this->configService->trackers()->each(function ($tracker) {
$this->info("- {$tracker}");
});

$this->comment($this->consoleService->trackersCommentLine1());
$this->comment($this->consoleService->trackersCommentLine2());
}

/**
* @return void
*/
protected function printConnectionStatus(): void
{
$this->line('');
$this->line("3) {$this->consoleService->connectionStatusInfo()}");

Event::listen(ProfilerServerConnectionSuccessful::class, function (ProfilerServerConnectionSuccessful $event) {
$this->info($this->consoleService->connectionSuccessfulInfo());
$this->info($this->consoleService->connectionSuccessfulSocketsInfo($event->socketsPort));
$this->info($this->consoleService->connectionSuccessfulClientsInfo($event->countClients));

$this->connectionStatusIsUnknown = false;
});

Event::listen(ProfilerServerConnectionFailed::class, function () {
$this->error($this->error($this->consoleService->connectionFailedInfo()));

$this->connectionStatusIsUnknown = false;
});

app()->terminating(function () {
if (! $this->connectionStatusIsUnknown) {
return;
}

$this->error($this->consoleService->connectionStatusUnknownInfo());
});
}
}
8 changes: 8 additions & 0 deletions src/Events/ProfilerServerConnectionFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace JKocik\Laravel\Profiler\Events;

class ProfilerServerConnectionFailed
{

}
27 changes: 27 additions & 0 deletions src/Events/ProfilerServerConnectionSuccessful.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace JKocik\Laravel\Profiler\Events;

class ProfilerServerConnectionSuccessful
{
/**
* @var int
*/
public $socketsPort;

/**
* @var int
*/
public $countClients;

/**
* ProfilerServerConnectionSuccessful constructor.
* @param int $socketsPort
* @param int $countClients
*/
public function __construct(int $socketsPort, int $countClients)
{
$this->socketsPort = $socketsPort;
$this->countClients = $countClients;
}
}
17 changes: 16 additions & 1 deletion src/Processors/BroadcastingProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace JKocik\Laravel\Profiler\Processors;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use JKocik\Laravel\Profiler\Contracts\Processor;
use JKocik\Laravel\Profiler\Contracts\DataTracker;
use JKocik\Laravel\Profiler\Services\ConfigService;
Expand Down Expand Up @@ -36,7 +37,21 @@ public function __construct(Client $client, ConfigService $configService)
*/
public function process(DataTracker $dataTracker): void
{
$this->client->request('POST', $this->configService->serverHttpConnectionUrl(), [
$this->broadcast(
$dataTracker,
$this->configService->serverHttpConnectionUrl()
);
}

/**
* @param DataTracker $dataTracker
* @param string $url
* @return Response
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function broadcast(DataTracker $dataTracker, string $url): Response
{
return $this->client->request('POST', $url, [
'json' => [
'meta' => $dataTracker->meta()->toArray(),
'data' => $dataTracker->data()->toArray(),
Expand Down
32 changes: 32 additions & 0 deletions src/Processors/StatusCommandProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace JKocik\Laravel\Profiler\Processors;

use GuzzleHttp\Exception\ConnectException;
use JKocik\Laravel\Profiler\Contracts\DataTracker;
use JKocik\Laravel\Profiler\Events\ProfilerServerConnectionFailed;
use JKocik\Laravel\Profiler\Events\ProfilerServerConnectionSuccessful;

class StatusCommandProcessor extends BroadcastingProcessor
{
/**
* @param DataTracker $dataTracker
* @return void
*/
public function process(DataTracker $dataTracker): void
{
try {
$response = $this->broadcast(
$dataTracker,
$this->configService->serverHttpConnectionUrl() . '/status'
);

$body = json_decode($response->getBody());

event(new ProfilerServerConnectionSuccessful($body->sockets, $body->clients));
} catch (ConnectException $e) {
event(new ProfilerServerConnectionFailed());
throw $e;
}
}
}
8 changes: 8 additions & 0 deletions src/Services/ConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ public function processors(): Collection
return Collection::make($this->config->get('profiler.processors'));
}

/**
* @param array $processors
*/
public function overrideProcessors(array $processors): void
{
$this->config->set('profiler.processors', $processors);
}

/**
* @return string
*/
Expand Down
103 changes: 102 additions & 1 deletion src/Services/ConsoleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,123 @@

namespace JKocik\Laravel\Profiler\Services;

use Illuminate\Foundation\Application;

class ConsoleService
{
/**
* @var Application
*/
protected $app;

/**
* @var ConfigService
*/
protected $configService;

/**
* ConsoleService constructor.
* @param Application $app
* @param ConfigService $configService
*/
public function __construct(ConfigService $configService)
public function __construct(Application $app, ConfigService $configService)
{
$this->app = $app;
$this->configService = $configService;
}

/**
* @return string
*/
public function envInfo(): string
{
return "Your current environment is: {$this->app->environment()}";
}

/**
* @return string
*/
public function profilerStatusInfo(): string
{
$status = $this->configService->isProfilerEnabled() ? 'enabled' : 'disabled';

return "Laravel Profiler is: {$status}";
}

/**
* @return string
*/
public function trackersStatusInfo(): string
{
return "You have {$this->configService->trackers()->count()} tracker(s) turned on";
}

/**
* @return string
*/
public function trackersCommentLine1(): string
{
return 'There are 13 trackers available out of the box';
}

/**
* @return string
*/
public function trackersCommentLine2(): string
{
return 'turn them on and off in profiler.php configuration file';
}

/**
* @return string
*/
public function connectionStatusInfo(): string
{
return "Trying to connect to Profiler Server on {$this->configService->serverHttpConnectionUrl()} ...";
}

/**
* @return string
*/
public function connectionSuccessfulInfo(): string
{
return 'Connected successfully';
}

/**
* @param int $socketsPort
* @return string
*/
public function connectionSuccessfulSocketsInfo(int $socketsPort): string
{
return "Profiler Server sockets listening on port: {$socketsPort}";
}

/**
* @param int $countClients
* @return string
*/
public function connectionSuccessfulClientsInfo(int $countClients): string
{
return "You have {$countClients} Profiler Client(s) connected at the moment";
}

/**
* @return string
*/
public function connectionFailedInfo(): string
{
return 'Connection failed';
}

/**
* @return string
*/
public function connectionStatusUnknownInfo(): string
{
return 'BroadcastingProcessor did not report connection status, connection status is unknown';
}

/**
* @return string
*/
Expand Down
Loading

0 comments on commit 806125f

Please sign in to comment.