Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sorting to the status command #17

Merged
merged 1 commit into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/CachesValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vormkracht10\PermanentCache;

use Cron\CronExpression;
use Illuminate\Bus\Queueable;
use Illuminate\Console\Scheduling\CallbackEvent;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -166,7 +167,7 @@ final public static function get($parameters = [], $default = null, bool $update
return static::updateAndGet($parameters ?? []);
}

return $cache->get($cacheKey, $default)?->value;
return $cache->get($cacheKey)?->value ?? $default;
}

final public function getMeta($parameters = []): mixed
Expand Down Expand Up @@ -195,9 +196,7 @@ final protected function value($default = null): mixed

[$store, $cacheKey] = $this->store($this->getParameters());

return Cache::store($store)->get(
$cacheKey, $default,
)?->value;
return Cache::store($store)->get($cacheKey)?->value ?? $default;
}

public function getName(): string
Expand Down Expand Up @@ -309,4 +308,13 @@ public function addMarkers($value): mixed

return $this->getMarker().$value.$this->getMarker(close: true);
}

public function expression(): ?CronExpression
{
if (! isset($this->expression)) {
return null;
}

return new CronExpression($this->expression);
}
}
75 changes: 65 additions & 10 deletions src/Commands/PermanentCachesStatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use Illuminate\Console\Command;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Collection;
use Lorisleiva\CronTranslator\CronTranslator;
use Spatie\Emoji\Emoji;
use Symfony\Component\Console\Helper\TableSeparator;
use Vormkracht10\PermanentCache\CachesValue;
use Vormkracht10\PermanentCache\Facades\PermanentCache;
use Vormkracht10\PermanentCache\Scheduled;

class PermanentCachesStatusCommand extends Command
{
Expand All @@ -16,7 +19,11 @@ class PermanentCachesStatusCommand extends Command
*
* @var string
*/
protected $signature = 'permanent-cache:status {--P|parameters} {--F|filter=}';
protected $signature = 'permanent-cache:status
{--P|parameters}
{--F|filter=}
{--S|sort= : The statistic to sort on, this can be one of ["size", "updated", "frequency"]}
{--A|ascending : Whether the sorting should be done ascending instead of descending}';

/**
* The console command description.
Expand All @@ -25,12 +32,65 @@ class PermanentCachesStatusCommand extends Command
*/
protected $description = 'Show status for all registered Permanent Caches';

protected function getSize($cache): int
{
return strlen(serialize($cache));
}

protected function sortOnSize($a, $b): int
{
$sizeA = $this->getSize($a[0]);
$sizeB = $this->getSize($b[0]);

return match (true) {
$sizeA == $sizeB => 0,
default => $sizeA > $sizeB ? 1 : -1,
};
}

protected function sortOnUpdated($a, $b): int
{
return $a[2]->updated_at > $b[2]->updated_at ? 1 : -1;
}

protected function sortOnFrequency($a, $b): int
{
$a = $a[0]->expression()?->getNextRunDate();
$b = $b[0]->expression()?->getNextRunDate();

if (is_null($a)) return is_null($b) ? 0 : -1;
if (is_null($b)) return 1;

return $a > $b ? 1 : -1;
}

/**
* Execute the console command.
*/
public function handle()
{
$caches = PermanentCache::configuredCaches();
$sortOn = $this->option('sort');

$items = new Collection;

foreach (($caches = PermanentCache::configuredCaches()) as $cache) {
$items->push([
$cache,
$parameters = $caches->getInfo(),
$cache->getMeta($parameters),
]);
}

$caches = $items->when($sortOn, fn ($collection) => $collection->sort(function ($a, $b) use ($sortOn) {
$result = match ($sortOn) {
'size' => $this->sortOnSize($a, $b),
'updated' => $this->sortOnUpdated($a, $b),
'frequency' => $this->sortOnFrequency($a, $b),
default => throw new \Exception("Invalid sorting method: \"{$sortOn}\"")
};

return $this->option('ascending') ? $result : -$result;
}));

$frequencies = collect(app(Schedule::class)->events())
->mapWithKeys(function ($schedule) {
Expand All @@ -39,24 +99,19 @@ public function handle()

$tableRows = [];

foreach ($caches as $c) {
$cache = $caches->current();
$parameters = $caches->getInfo();

foreach ($caches as [$cache, $parameters, $meta]) {
if (
$this->option('filter') &&
! str_contains(strtolower($cache->getName()), strtolower($this->option('filter')))
) {
continue;
}

$cached = $cache->getMeta($parameters);

$row = [
$cache->isCached($parameters) ? Emoji::checkMarkButton() : Emoji::crossMark(),
$cache->getName(),
$cached ? readable_size(strlen(serialize($cached))) : 'N/A',
$cached?->updated_at?->diffForHumans() ?: 'N/A',
$meta ? readable_size($this->getSize($meta)) : 'N/A',
$meta?->updated_at?->diffForHumans() ?: 'N/A',
$frequencies[$cache->getName()] ?? 'N/A',
];

Expand Down
Loading