-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGetProcessedMessagesCommand.php
More file actions
130 lines (103 loc) · 3.93 KB
/
GetProcessedMessagesCommand.php
File metadata and controls
130 lines (103 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare(strict_types=1);
namespace Queue\Swoole\Command;
use Dot\DependencyInjection\Attribute\Inject;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function dirname;
use function file;
use function file_exists;
use function is_numeric;
use function json_decode;
use function strtolower;
use function strtotime;
use const FILE_IGNORE_NEW_LINES;
use const FILE_SKIP_EMPTY_LINES;
#[AsCommand(
name: 'processed',
description: 'Get successfully processed messages',
)]
class GetProcessedMessagesCommand extends Command
{
/** @var string $defaultName */
protected static $defaultName = 'processed';
#[Inject()]
public function __construct()
{
parent::__construct(self::$defaultName);
}
protected function configure(): void
{
$this->setDescription('Get successfully processed messages')
->addOption('start', null, InputOption::VALUE_OPTIONAL, 'Start timestamp (Y-m-d H:i:s)')
->addOption('end', null, InputOption::VALUE_OPTIONAL, 'End timestamp (Y-m-d H:i:s)')
->addOption('limit', null, InputOption::VALUE_OPTIONAL, 'Limit in days');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
try {
$startOption = $input->getOption('start');
$endOption = $input->getOption('end');
$limit = $input->getOption('limit');
$startDate = $startOption ? new \DateTimeImmutable($startOption) : null;
$endDate = $endOption ? new \DateTimeImmutable($endOption) : null;
} catch (\Exception $e) {
$output->writeln('<error>Invalid date format provided.</error>');
return Command::FAILURE;
}
if ($startDate && $startDate->format('H:i:s') === '00:00:00') {
$startDate = $startDate->setTime(0, 0, 0);
}
if ($endDate && $endDate->format('H:i:s') === '00:00:00') {
$endDate = $endDate->setTime(23, 59, 59);
}
if ($limit && is_numeric($limit)) {
if ($startDate && ! $endDate) {
$endDate = $startDate->modify("+{$limit} days");
} elseif (! $startDate && $endDate) {
$startDate = $endDate->modify("-{$limit} days");
}
}
if (! $endDate) {
$endDate = new \DateTime();
}
if ($startDate > $endDate) {
$output->writeln('<error>The start date cannot be after the end date.</error>');
return Command::FAILURE;
}
$logPath = dirname(__DIR__, 3) . '/log/queue-log.log';
if (! file_exists($logPath)) {
$output->writeln("<error>Log file not found: $logPath</error>");
return Command::FAILURE;
}
$lines = file($logPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$startTimestamp = $startDate?->getTimestamp();
$endTimestamp = $endDate->getTimestamp();
$found = false;
foreach ($lines as $line) {
$entry = json_decode($line, true);
if (! $entry || ! isset($entry['levelName'], $entry['timestamp'])) {
continue;
}
if (strtolower($entry['levelName']) !== 'info') {
continue;
}
$logTimestamp = strtotime($entry['timestamp']);
if (
($startTimestamp && $logTimestamp < $startTimestamp) ||
($endTimestamp && $logTimestamp > $endTimestamp)
) {
continue;
}
$output->writeln($line);
$found = true;
}
if (! $found) {
$output->writeln('<comment>No matching log entries found.</comment>');
}
return Command::SUCCESS;
}
}