-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGetProcessedMessagesCommand.php
More file actions
95 lines (76 loc) · 2.82 KB
/
GetProcessedMessagesCommand.php
File metadata and controls
95 lines (76 loc) · 2.82 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
<?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 date;
use function file;
use function is_numeric;
use function json_decode;
use function preg_match;
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
{
protected static string $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
{
$start = $input->getOption('start');
$end = $input->getOption('end');
$limit = $input->getOption('limit');
if (! $end) {
$end = date('Y-m-d H:i:s');
} elseif (! preg_match('/\d{2}:\d{2}:\d{2}/', $end)) {
$end .= ' 23:59:59';
}
if ($limit && is_numeric($limit) && ! $start) {
$start = date('Y-m-d H:i:s', strtotime("-{$limit} days", strtotime($end)));
} elseif ($start && ! preg_match('/\d{2}:\d{2}:\d{2}/', $start)) {
$start .= ' 00:00:00';
}
$startTimestamp = $start ? strtotime($start) : null;
$endTimestamp = $end ? strtotime($end) : null;
$logPath = 'log/queue-log.log';
$lines = file($logPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
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);
}
return Command::SUCCESS;
}
}