|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Queue\Swoole\Command; |
| 6 | + |
| 7 | +use Dot\DependencyInjection\Attribute\Inject; |
| 8 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 9 | +use Symfony\Component\Console\Command\Command; |
| 10 | +use Symfony\Component\Console\Input\InputInterface; |
| 11 | +use Symfony\Component\Console\Input\InputOption; |
| 12 | +use Symfony\Component\Console\Output\OutputInterface; |
| 13 | + |
| 14 | +use function date; |
| 15 | +use function file; |
| 16 | +use function is_numeric; |
| 17 | +use function json_decode; |
| 18 | +use function preg_match; |
| 19 | +use function strtolower; |
| 20 | +use function strtotime; |
| 21 | + |
| 22 | +use const FILE_IGNORE_NEW_LINES; |
| 23 | +use const FILE_SKIP_EMPTY_LINES; |
| 24 | + |
| 25 | +#[AsCommand( |
| 26 | + name: 'processed', |
| 27 | + description: 'Get successfully processed messages', |
| 28 | +)] |
| 29 | +class GetProcessedMessagesCommand extends Command |
| 30 | +{ |
| 31 | + protected static string $defaultName = 'processed'; |
| 32 | + |
| 33 | + #[Inject()] |
| 34 | + public function __construct() |
| 35 | + { |
| 36 | + parent::__construct(self::$defaultName); |
| 37 | + } |
| 38 | + |
| 39 | + protected function configure(): void |
| 40 | + { |
| 41 | + $this->setDescription('Get successfully processed messages') |
| 42 | + ->addOption('start', null, InputOption::VALUE_OPTIONAL, 'Start timestamp (Y-m-d H:i:s)') |
| 43 | + ->addOption('end', null, InputOption::VALUE_OPTIONAL, 'End timestamp (Y-m-d H:i:s)') |
| 44 | + ->addOption('limit', null, InputOption::VALUE_OPTIONAL, 'Limit in days'); |
| 45 | + } |
| 46 | + |
| 47 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 48 | + { |
| 49 | + $start = $input->getOption('start'); |
| 50 | + $end = $input->getOption('end'); |
| 51 | + $limit = $input->getOption('limit'); |
| 52 | + |
| 53 | + if (! $end) { |
| 54 | + $end = date('Y-m-d H:i:s'); |
| 55 | + } elseif (! preg_match('/\d{2}:\d{2}:\d{2}/', $end)) { |
| 56 | + $end .= ' 23:59:59'; |
| 57 | + } |
| 58 | + |
| 59 | + if ($limit && is_numeric($limit) && ! $start) { |
| 60 | + $start = date('Y-m-d H:i:s', strtotime("-{$limit} days", strtotime($end))); |
| 61 | + } elseif ($start && ! preg_match('/\d{2}:\d{2}:\d{2}/', $start)) { |
| 62 | + $start .= ' 00:00:00'; |
| 63 | + } |
| 64 | + |
| 65 | + $startTimestamp = $start ? strtotime($start) : null; |
| 66 | + $endTimestamp = $end ? strtotime($end) : null; |
| 67 | + |
| 68 | + $logPath = 'log/queue-log.log'; |
| 69 | + $lines = file($logPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
| 70 | + |
| 71 | + foreach ($lines as $line) { |
| 72 | + $entry = json_decode($line, true); |
| 73 | + |
| 74 | + if (! $entry || ! isset($entry['levelName'], $entry['timestamp'])) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + if (strtolower($entry['levelName']) !== 'info') { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + $logTimestamp = strtotime($entry['timestamp']); |
| 83 | + if ( |
| 84 | + ($startTimestamp && $logTimestamp < $startTimestamp) || |
| 85 | + ($endTimestamp && $logTimestamp > $endTimestamp) |
| 86 | + ) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + $output->writeln($line); |
| 91 | + } |
| 92 | + |
| 93 | + return Command::SUCCESS; |
| 94 | + } |
| 95 | +} |
0 commit comments