Skip to content

Commit

Permalink
Merge branch 'main' of github.com:paulo-hortelan/requests-graph-pulse
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-hortelan committed Jan 5, 2024
2 parents b411617 + 68a0f61 commit ebb4fa6
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 41 deletions.
24 changes: 12 additions & 12 deletions src/Livewire/RequestsGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace PauloHortelan\RequestsGraphPulse\Livewire;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\View;
use Laravel\Pulse\Facades\Pulse;
use Laravel\Pulse\Livewire\Card;
use Laravel\Pulse\Livewire\Concerns;
use Livewire\Attributes\Lazy;
use Livewire\Attributes\Url;
use Illuminate\Support\Facades\Config;
use Laravel\Pulse\Livewire\Concerns;
use Livewire\Livewire;
use PauloHortelan\RequestsGraphPulse\Recorders\RequestsGraphRecorder;

Expand All @@ -22,34 +22,34 @@ class RequestsGraph extends Card
'successful',
'redirection',
'client_error',
'server_error'
'server_error',
];

#[Url(as: 'requests-graph')]
public function render()
{
{

[$requests, $time, $runAt] = $this->remember(fn () => Pulse::graph(
$this->statusClasses,
'count',
$this->periodAsInterval(),
));
));

if (Livewire::isLivewireRequest()) {
$this->dispatch('requests-chart-update', request: $requests['request']);
}
}

return View::make('livewire.requests-graph', [
'request' => $requests['request'],
'time' => $time,
'runAt' => $runAt,
'config' => [
'sample_rate' => Config::get('pulse.recorders.' . RequestsGraphRecorder::class . '.sample_rate'),
'record_informational' => Config::get('pulse.recorders.' . RequestsGraphRecorder::class . '.record_informational'),
'record_successful' => Config::get('pulse.recorders.' . RequestsGraphRecorder::class . '.record_successful'),
'record_redirection' => Config::get('pulse.recorders.' . RequestsGraphRecorder::class . '.record_redirection'),
'record_client_error' => Config::get('pulse.recorders.' . RequestsGraphRecorder::class . '.record_client_error'),
'record_server_error' => Config::get('pulse.recorders.' . RequestsGraphRecorder::class . '.record_server_error'),
'sample_rate' => Config::get('pulse.recorders.'.RequestsGraphRecorder::class.'.sample_rate'),
'record_informational' => Config::get('pulse.recorders.'.RequestsGraphRecorder::class.'.record_informational'),
'record_successful' => Config::get('pulse.recorders.'.RequestsGraphRecorder::class.'.record_successful'),
'record_redirection' => Config::get('pulse.recorders.'.RequestsGraphRecorder::class.'.record_redirection'),
'record_client_error' => Config::get('pulse.recorders.'.RequestsGraphRecorder::class.'.record_client_error'),
'record_server_error' => Config::get('pulse.recorders.'.RequestsGraphRecorder::class.'.record_server_error'),
],
]);
}
Expand Down
5 changes: 1 addition & 4 deletions src/Recorders/Concern/RequestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

namespace PauloHortelan\RequestsGraphPulse\Recorders\Concern;

use Carbon\CarbonInterval;
use Livewire\Attributes\Url;

trait RequestResponse
{
/**
* Determine if the response should be recorded.
*/
protected function shouldRecord(string $statusClass): bool
{
return $this->config->get('pulse.recorders.' . static::class . '.record_' . $statusClass) ?? false;
return $this->config->get('pulse.recorders.'.static::class.'.record_'.$statusClass) ?? false;
}

/**
Expand Down
20 changes: 10 additions & 10 deletions src/Recorders/RequestsGraphRecorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@
namespace PauloHortelan\RequestsGraphPulse\Recorders;

use Carbon\CarbonImmutable;
use Laravel\Pulse\Pulse;
use Illuminate\Config\Repository;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Support\Carbon;
use Symfony\Component\HttpFoundation\Response;
use Laravel\Pulse\Concerns\ConfiguresAfterResolving;
use Laravel\Pulse\Pulse;
use Laravel\Pulse\Recorders\Concerns;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Routing\Route;
use PauloHortelan\RequestsGraphPulse\Recorders\Concern\RequestResponse;
use Symfony\Component\HttpFoundation\Response;

class RequestsGraphRecorder
{
use Concerns\Ignores,
Concerns\LivewireRoutes,
Concerns\Sampling,
Concerns\Thresholds,
RequestResponse,
ConfiguresAfterResolving;
ConfiguresAfterResolving,
RequestResponse;

public function __construct(
protected Pulse $pulse,
Expand All @@ -45,21 +45,21 @@ public function register(callable $record, Application $app): void

public function record(Carbon $startedAt, Request $request, Response $response): void
{
if (!$request->route() instanceof Route || !$this->shouldSample() || $this->shouldIgnore($this->resolveRoutePath($request)[0])) {
if (! $request->route() instanceof Route || ! $this->shouldSample() || $this->shouldIgnore($this->resolveRoutePath($request)[0])) {
return;
}

$statusCode = $response->getStatusCode();
$statusClass = $this->getStatusClass($statusCode);

if (!$this->shouldRecord($statusClass)) {
if (! $this->shouldRecord($statusClass)) {
return;
}

$this->pulse->lazy(function () use ($statusClass) {
$this->pulse->record(
type: $statusClass,
key: "request",
key: 'request',
timestamp: CarbonImmutable::now()->getTimestamp(),
)->count()->onlyBuckets();
});
Expand Down
4 changes: 2 additions & 2 deletions src/RequestsGraphServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace PauloHortelan\RequestsGraphPulse;

use Illuminate\Contracts\Foundation\Application;
use Spatie\LaravelPackageTools\Package;
use Livewire\LivewireManager;
use PauloHortelan\RequestsGraphPulse\Livewire\RequestsGraph;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

class RequestsGraphServiceProvider extends PackageServiceProvider
Expand All @@ -24,5 +24,5 @@ public function boot(): void
$this->callAfterResolving('livewire', function (LivewireManager $livewire, Application $app) {
$livewire->component('requests-graph', RequestsGraph::class);
});
}
}
}
10 changes: 5 additions & 5 deletions tests/RequestsGraphRecorderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
Config::set('pulse.recorders.'.RequestsGraphRecorder::class.'.record_informational', true);
get('test-route-informational')->assertStatus(100);

Pulse::ignore(fn () => expect(DB::table('pulse_aggregates')->where('type','informational')->get())->toHaveCount(4));
Pulse::ignore(fn () => expect(DB::table('pulse_aggregates')->where('type', 'informational')->get())->toHaveCount(4));
Pulse::ignore(fn () => expect(DB::table('pulse_entries')->count())->toBe(0));
Pulse::ignore(fn () => expect(DB::table('pulse_values')->count())->toBe(0));
Pulse::ignore(fn () => expect(DB::table('pulse_values')->count())->toBe(0));
});

it('captures successful requests', function () {
Expand All @@ -34,9 +34,9 @@
Config::set('pulse.recorders.'.RequestsGraphRecorder::class.'.record_successful', true);
get('test-route-successful')->assertStatus(200);

Pulse::ignore(fn () => expect(DB::table('pulse_aggregates')->where('type','successful')->get())->toHaveCount(4));
Pulse::ignore(fn () => expect(DB::table('pulse_aggregates')->where('type', 'successful')->get())->toHaveCount(4));
Pulse::ignore(fn () => expect(DB::table('pulse_entries')->count())->toBe(0));
Pulse::ignore(fn () => expect(DB::table('pulse_values')->count())->toBe(0));
Pulse::ignore(fn () => expect(DB::table('pulse_values')->count())->toBe(0));
});

it('captures client_error requests', function () {
Expand Down Expand Up @@ -67,4 +67,4 @@
Pulse::ignore(fn () => expect(DB::table('pulse_entries')->get())->toHaveCount(0));
Pulse::ignore(fn () => expect(DB::table('pulse_aggregates')->get())->toHaveCount(0));
Pulse::ignore(fn () => expect(DB::table('pulse_values')->count())->toBe(0));
});
});
42 changes: 42 additions & 0 deletions tests/RequestsLivewireTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

use Illuminate\Support\Carbon;
use Laravel\Pulse\Facades\Pulse;
use Livewire\Livewire;
use PauloHortelan\RequestsGraphPulse\Livewire\RequestsGraph;

// it('includes the card on the dashboard', function () {
// $this
// ->get('/pulse')
// ->assertSeeLivewire(RequestsGraph::class);
// });

// it('renders 4xx requests', function () {
// $request1 = json_encode(['GET', '/users', 418, null]);
// $request2 = json_encode(['GET', '/users/{user}', 403, null]);

// // Add entries outside of the window.
// Carbon::setTestNow('2000-01-01 12:00:00');
// Pulse::record('4xx_request', $request1, now()->timestamp)->max()->count();
// Pulse::record('4xx_request', $request2, now()->timestamp)->max()->count();

// // Add entries to the "tail".
// Carbon::setTestNow('2000-01-01 12:00:01');
// Pulse::record('4xx_request', $request1, now()->timestamp)->max()->count();
// Pulse::record('4xx_request', $request1, now()->timestamp)->max()->count();
// Pulse::record('4xx_request', $request2, now()->timestamp)->max()->count();

// // Add entries to the current buckets.
// Carbon::setTestNow('2000-01-01 13:00:00');
// Pulse::record('4xx_request', $request1, now()->timestamp)->max()->count();
// Pulse::record('4xx_request', $request1, now()->timestamp)->max()->count();
// Pulse::record('4xx_request', $request2, now()->timestamp)->max()->count();

// Pulse::store();

// Livewire::test(FourXxCard::class, ['lazy' => false])
// ->assertViewHas('fourXxRequests', collect([
// (object) ['method' => 'GET', 'uri' => '/users', 'status' => 418, 'count' => '4.00', 'latest' => now()],
// (object) ['method' => 'GET', 'uri' => '/users/{user}', 'status' => 403, 'count' => '2.00', 'latest' => now()],
// ]));
// });
9 changes: 4 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase as Orchestra;
use Orchestra\Workbench\WorkbenchServiceProvider;

class TestCase extends Orchestra
{
protected $enablesPackageDiscoveries = true;

use RefreshDatabase;
use WithWorkbench;

// protected function getPackageProviders($app)
// {
// return [
// \PauloHortelan\RequestsGraphPulse\RequestsServiceProvider::class,
// \PauloHortelan\RequestsGraphPulse\RequestsServiceProvider::class,
// \Workbench\App\Providers\WorkbenchServiceProvider::class
// ];
// }
Expand All @@ -36,9 +35,9 @@ protected function getEnvironmentSetUp($app)
{
$app['config']->set('database.default', 'testbench');
$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
'prefix' => '',
]);
}
}
6 changes: 3 additions & 3 deletions workbench/config/pulse.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@
'record_server_error' => env('PULSE_REQUESTS_RECORD_SERVER_ERROR', true),
'ignore' => [
'#^/pulse$#', // Pulse dashboard...
],
],
],
],
],
];
];

0 comments on commit ebb4fa6

Please sign in to comment.