|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Matecat\SimpleS3\Console; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Matecat\SimpleS3\Client; |
|
8
|
|
|
use Matecat\SimpleS3\Helpers\File; |
|
9
|
|
|
use Symfony\Component\Console\Command\Command; |
|
10
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
14
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
15
|
|
|
|
|
16
|
|
|
class CacheStatsCommand extends Command |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Client |
|
20
|
|
|
*/ |
|
21
|
|
|
private Client $s3Client; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* CacheStatsCommand constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param Client $s3Client |
|
27
|
|
|
* @param string|null $name |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(Client $s3Client, ?string $name = null) |
|
30
|
|
|
{ |
|
31
|
|
|
parent::__construct($name); |
|
32
|
|
|
|
|
33
|
|
|
$this->s3Client = $s3Client; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function configure(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$this |
|
39
|
|
|
->setName('ss3:cache:stats') |
|
40
|
|
|
->setDescription('Get the cache statistics.') |
|
41
|
|
|
->setHelp('This command displays the cache statistics.') |
|
42
|
|
|
->addArgument('bucket', InputArgument::REQUIRED, 'The name of the bucket') |
|
43
|
|
|
->addArgument('prefix', InputArgument::REQUIRED, 'The prefix in the bucket'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @throws Exception |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
50
|
|
|
{ |
|
51
|
|
|
if (false === $this->s3Client->hasCache()) { |
|
52
|
|
|
throw new Exception('Cache in not enabled. You have to enable caching to use this command'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
if (false === is_string($input->getArgument('bucket')) or false === is_string($input->getArgument('prefix'))) { |
|
57
|
|
|
throw new InvalidArgumentException('Provided bucket or prefix name were not strings'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$bucket = $input->getArgument('bucket'); |
|
61
|
|
|
$prefix = $input->getArgument('prefix'); |
|
62
|
|
|
|
|
63
|
|
|
try { |
|
64
|
|
|
$items = $this->s3Client->getItemsInABucket([ |
|
65
|
|
|
'bucket' => $bucket, |
|
66
|
|
|
'prefix' => $prefix, |
|
67
|
|
|
]); |
|
68
|
|
|
|
|
69
|
|
|
$tableFeed = []; |
|
70
|
|
|
foreach ($items as $key) { |
|
71
|
|
|
$inCache = $this->s3Client->getCache()->search($bucket, $key); |
|
72
|
|
|
if (count($inCache) > 0) { |
|
73
|
|
|
$index = $this->getDirName($inCache[ 0 ]); |
|
74
|
|
|
|
|
75
|
|
|
$files = []; |
|
76
|
|
|
foreach ($inCache as $item) { |
|
77
|
|
|
$files[ $item ] = $this->s3Client->getConn()->doesObjectExist($bucket, $item); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$tableFeed[ $index ] = [ |
|
81
|
|
|
'count' => count($inCache), |
|
82
|
|
|
'files' => $files, |
|
83
|
|
|
'ttl' => $this->s3Client->getCache()->ttl($bucket, $key), |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$table = new Table($output); |
|
89
|
|
|
$table->setHeaders(['prefix', 'count', 'ttl', 'files', 'align']); |
|
90
|
|
|
|
|
91
|
|
|
foreach ($tableFeed as $prefix => $data) { |
|
92
|
|
|
$count = (int)$data[ 'count' ]; |
|
93
|
|
|
|
|
94
|
|
|
$files = implode(PHP_EOL, array_keys($data[ 'files' ])); |
|
95
|
|
|
$enabled = implode(PHP_EOL, $data[ 'files' ]); |
|
96
|
|
|
$enabled = str_replace('1', '<fg=green>✓</>', $enabled); |
|
97
|
|
|
$enabled = str_replace('0', '<fg=red>✗</>', $enabled); |
|
98
|
|
|
|
|
99
|
|
|
$table->addRow([ |
|
100
|
|
|
$prefix, |
|
101
|
|
|
$count, |
|
102
|
|
|
$data[ 'ttl' ], |
|
103
|
|
|
$files, |
|
104
|
|
|
$enabled |
|
105
|
|
|
]); |
|
106
|
|
|
} |
|
107
|
|
|
$table->render(); |
|
108
|
|
|
|
|
109
|
|
|
return 0; |
|
110
|
|
|
} catch (Exception) { |
|
111
|
|
|
$io = new SymfonyStyle($input, $output); |
|
112
|
|
|
$io->error('No results were found'); |
|
113
|
|
|
|
|
114
|
|
|
return 1; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param string $item |
|
120
|
|
|
* |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
|
|
private function getDirName(string $item): string |
|
124
|
|
|
{ |
|
125
|
|
|
if (File::endsWith($item, $this->s3Client->getPrefixSeparator())) { |
|
126
|
|
|
return $item; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$fileInfo = File::getPathInfo($item); |
|
130
|
|
|
|
|
131
|
|
|
return $fileInfo[ 'dirname' ] . $this->s3Client->getPrefixSeparator(); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|