Skip to content

Commit

Permalink
Regroup improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Nov 8, 2023
1 parent 8da68d6 commit 56eb58f
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 46 deletions.
53 changes: 12 additions & 41 deletions src/Commands/RegroupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Database\DatabaseManager;
use Illuminate\Support\Facades\Config;
use Laravel\Pulse\Contracts\Grouping;
use Laravel\Pulse\Contracts\Groupable;
use Laravel\Pulse\Contracts\Storage;
use Laravel\Pulse\Pulse;
use Laravel\Pulse\Queries\Concerns\InteractsWithConnection;
use ReflectionClass;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'pulse:regroup')]
class RegroupCommand extends Command
{
use ConfirmableTrait;
use InteractsWithConnection;
use ConfirmableTrait, InteractsWithConnection;

/**
* The command's signature.
Expand All @@ -32,53 +33,23 @@ class RegroupCommand extends Command
*/
public $description = 'Re-apply grouping for supporting recorders.';

/**
* Create a new command instance.
*/
public function __construct(
protected Repository $config,
protected DatabaseManager $db
) {
parent::__construct();
}

/**
* Handle the command.
*/
public function handle(): int
public function handle(
Pulse $pulse,
Storage $storage,
): int
{
if (! $this->confirmToProceed()) {
return Command::FAILURE;
}

collect(array_keys(Config::get('pulse.recorders')))
->filter(fn ($recorder) => (new ReflectionClass($recorder))->implementsInterface(Grouping::class)) // @phpstan-ignore argument.type
->map(fn ($recorder) => app($recorder)) // @phpstan-ignore argument.type
->each(function ($recorder) {
$this->info("Re-grouping {$recorder->table}...");

$this->connection()->query()
->from($recorder->table)
->select($recorder->groupColumn())
->distinct()
->pluck($recorder->groupColumn())
->each(function ($value) use ($recorder) {
$newValue = $recorder->group($value)();

if ($newValue === $value) {
return;
}

$this->info(" - [{$value}] => [{$newValue}]");
$pulse->recorders()
->filter(fn ($recorder) => class_implements($recorder, Groupable::class))

Check failure on line 49 in src/Commands/RegroupCommand.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Parameter #1 $callback of method Illuminate\Support\Collection<int,object>::filter() expects (callable(object, int): bool)|null, Closure(mixed): (array<string, class-string>|false) given.

Check failure on line 49 in src/Commands/RegroupCommand.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Parameter #2 $autoload of function class_implements expects bool, string given.
->pipe($storage->regroup(...));

Check failure on line 50 in src/Commands/RegroupCommand.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Call to an undefined method Laravel\Pulse\Contracts\Storage::regroup().

$this->connection()->query()
->from($recorder->table)
->where($recorder->groupColumn(), $value)
->update([
$recorder->groupColumn() => $newValue,
]);
});
});
$this->components->info('Recorders regrouped.');

return Command::SUCCESS;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Grouping.php → src/Contracts/Groupable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Closure;

interface Grouping
interface Groupable
{
/**
* Return a closure that groups the given value.
Expand Down
10 changes: 10 additions & 0 deletions src/Pulse.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ protected function shouldRecord(Entry|Update $entry): bool
return $this->filters->every(fn (callable $filter) => $filter($entry));
}

/**
* Get the recorders.
*
* @return \Illuminate\Support\Collection<int, object>
*/
public function recorders(): Collection
{
return $this->recorders;
}

/**
* Get the tables used by the recorders.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Recorders/CacheInteractions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
use Illuminate\Cache\Events\CacheHit;
use Illuminate\Cache\Events\CacheMissed;
use Illuminate\Config\Repository;
use Laravel\Pulse\Contracts\Grouping;
use Laravel\Pulse\Contracts\Groupable;
use Laravel\Pulse\Entry;
use Laravel\Pulse\Pulse;

/**
* @internal
*/
class CacheInteractions implements Grouping
class CacheInteractions implements Groupable
{
use Concerns\Ignores;
use Concerns\Sampling;
Expand Down
4 changes: 2 additions & 2 deletions src/Recorders/OutgoingRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Illuminate\Foundation\Application;
use Illuminate\Http\Client\Factory as HttpFactory;
use Laravel\Pulse\Concerns\ConfiguresAfterResolving;
use Laravel\Pulse\Contracts\Grouping;
use Laravel\Pulse\Contracts\Groupable;
use Laravel\Pulse\Entry;
use Laravel\Pulse\Pulse;
use Psr\Http\Message\RequestInterface;
Expand All @@ -19,7 +19,7 @@
/**
* @internal
*/
class OutgoingRequests implements Grouping
class OutgoingRequests implements Groupable
{
use Concerns\Ignores;
use Concerns\Sampling;
Expand Down
33 changes: 33 additions & 0 deletions src/Storage/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Connection;
use Illuminate\Database\DatabaseManager;
use Illuminate\Support\Collection;
use Laravel\Pulse\Contracts\Groupable;
use Laravel\Pulse\Contracts\Storage;
use Laravel\Pulse\Entry;
use Laravel\Pulse\Update;
Expand Down Expand Up @@ -93,6 +94,38 @@ public function purge(Collection $tables): void
->truncate());
}

/*
* Regroup the recorders.
*
* @param \Illuminate\Support\Collection<int, \Laravel\Pulse\Contracts\Groupable> $recorders
*/
public function regroup(Collection $recorders): void

Check failure on line 102 in src/Storage/Database.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Method Laravel\Pulse\Storage\Database::regroup() has parameter $recorders with generic class Illuminate\Support\Collection but does not specify its types: TKey, TValue
{
$recorders->each(function (Groupable $recorder) {
$this->connection()
->table($recorder->table)

Check failure on line 106 in src/Storage/Database.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Access to an undefined property Laravel\Pulse\Contracts\Groupable::$table.
->select($recorder->groupColumn())
->distinct()
->orderBy('date')
->each(function ($record) use ($recorder) {
$value = $record->{$recorder->groupColumn()};

$newValue = $recorder->group($value)();

if ($newValue === $value) {
return;
}

$this->connection()
->table($recorder->table)

Check failure on line 120 in src/Storage/Database.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Access to an undefined property Laravel\Pulse\Contracts\Groupable::$table.
->where($recorder->groupColumn(), $value)
->update([
$recorder->groupColumn() => $newValue,
]);
});
});
}

/**
* The interval to trim the storage to.
*/
Expand Down

0 comments on commit 56eb58f

Please sign in to comment.