1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the bugloos/fault-tolerance-bundle project. |
5
|
|
|
* (c) Bugloos <https://bugloos.com/> |
6
|
|
|
* For the full copyright and license information, please view |
7
|
|
|
* the LICENSE file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Bugloos\FaultToleranceBundle\RequestLog; |
11
|
|
|
|
12
|
|
|
use Bugloos\FaultToleranceBundle\Contract\Command; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Mojtaba Gheytasi <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class RequestLog |
18
|
|
|
{ |
19
|
|
|
protected array $executedCommands; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Returns commands executed during the current request |
23
|
|
|
*/ |
24
|
|
|
public function getExecutedCommands(): array |
25
|
|
|
{ |
26
|
|
|
return $this->executedCommands; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Adds an executed command |
31
|
|
|
*/ |
32
|
|
|
public function addExecutedCommand(Command $command): void |
33
|
|
|
{ |
34
|
|
|
$this->executedCommands[] = $command; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Formats the log of executed commands into a string usable for logging purposes. |
39
|
|
|
* |
40
|
|
|
* Examples: |
41
|
|
|
* |
42
|
|
|
* TestCommand[SUCCESS][1ms] |
43
|
|
|
* TestCommand[SUCCESS][1ms], TestCommand[SUCCESS, RESPONSE_FROM_CACHE][1ms]x4 |
44
|
|
|
* TestCommand[TIMEOUT][1ms] |
45
|
|
|
* TestCommand[FAILURE][1ms] |
46
|
|
|
* |
47
|
|
|
* If a command has a multiplier such as <code>x4</code>, |
48
|
|
|
* that means this command was executed 4 times with the same events. |
49
|
|
|
* The time in milliseconds is the sum of the 4 executions. |
50
|
|
|
* |
51
|
|
|
* For example, <code>TestCommand[SUCCESS][15ms]x4</code> represents |
52
|
|
|
* TestCommand being executed 4 times and the sum of those 4 executions was 15ms. |
53
|
|
|
* These 4 each executed the run() method since |
54
|
|
|
* |
55
|
|
|
* <code>RESPONSE_FROM_CACHE</code> was not present as an event. |
56
|
|
|
*/ |
57
|
|
|
public function getExecutedCommandsAsString(): string |
58
|
|
|
{ |
59
|
|
|
$output = ""; |
60
|
|
|
$executedCommands = $this->getExecutedCommands(); |
61
|
|
|
$aggregatedCommandsExecuted = []; |
62
|
|
|
$aggregatedCommandExecutionTime = []; |
63
|
|
|
|
64
|
|
|
/** @var Command $executedCommand */ |
65
|
|
|
foreach ($executedCommands as $executedCommand) { |
66
|
|
|
$outputForExecutedCommand = $this->getOutputForExecutedCommand($executedCommand); |
67
|
|
|
|
68
|
|
|
if (!isset($aggregatedCommandsExecuted[$outputForExecutedCommand])) { |
69
|
|
|
$aggregatedCommandsExecuted[$outputForExecutedCommand] = 0; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$aggregatedCommandsExecuted[$outputForExecutedCommand] += 1; |
73
|
|
|
|
74
|
|
|
$executionTime = $executedCommand->getExecutionTimeInMilliseconds(); |
75
|
|
|
|
76
|
|
|
if ($executionTime < 0) { |
77
|
|
|
$executionTime = 0; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (isset($aggregatedCommandExecutionTime[$outputForExecutedCommand]) && $executionTime > 0) { |
81
|
|
|
$aggregatedCommandExecutionTime[$outputForExecutedCommand] += $executionTime; |
82
|
|
|
} else { |
83
|
|
|
$aggregatedCommandExecutionTime[$outputForExecutedCommand] = $executionTime; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
foreach ($aggregatedCommandsExecuted as $outputForExecutedCommand => $count) { |
88
|
|
|
if (!empty($output)) { |
89
|
|
|
$output .= ", "; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$output .= "{$outputForExecutedCommand}"; |
93
|
|
|
|
94
|
|
|
$output .= "[" . $aggregatedCommandExecutionTime[$outputForExecutedCommand] . "ms]"; |
95
|
|
|
|
96
|
|
|
if ($count > 1) { |
97
|
|
|
$output .= "x{$count}"; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $output; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function getOutputForExecutedCommand(Command $executedCommand): string |
105
|
|
|
{ |
106
|
|
|
$display = $executedCommand->getCommandKey() . "["; |
107
|
|
|
$events = $executedCommand->getExecutionEvents(); |
108
|
|
|
|
109
|
|
|
if (! empty($events)) { |
110
|
|
|
foreach ($events as $event) { |
111
|
|
|
$display .= "{$event}, "; |
112
|
|
|
} |
113
|
|
|
$display = substr($display, 0, -2); |
114
|
|
|
} else { |
115
|
|
|
$display .= "Executed"; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$display .= "]"; |
119
|
|
|
|
120
|
|
|
return $display; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|