|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the `tvi/monitor-bundle` project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) https://github.com/turnaev/monitor-bundle/graphs/contributors |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace Tvi\MonitorBundle\Reporter; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
16
|
|
|
use ZendDiagnostics\Check\CheckInterface; |
|
17
|
|
|
use ZendDiagnostics\Result\Collection as ResultsCollection; |
|
18
|
|
|
use ZendDiagnostics\Result\ResultInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
|
|
|
|
|
21
|
|
|
* @author Kevin Bond <[email protected]>, Vladimir Turnaev <[email protected]> |
|
|
|
|
|
|
22
|
|
|
*/ |
|
|
|
|
|
|
23
|
|
|
class ConsoleReporter extends AbstractReporter |
|
24
|
|
|
{ |
|
25
|
|
|
private const STATUS_TAG_SUCCESS = '<fg=green;options=bold>'; |
|
26
|
|
|
private const STATUS_TAG_WARNING = '<fg=yellow;options=bold>'; |
|
27
|
|
|
private const STATUS_TAG_SKIP = '<fg=blue;options=bold>'; |
|
28
|
|
|
private const STATUS_TAG_FAILUR = '<fg=red;options=bold>'; |
|
29
|
|
|
private const STATUS_TAG_UNKNOWN = '<fg=white;options=bold>'; |
|
30
|
|
|
/** |
|
|
|
|
|
|
31
|
|
|
* @var OutputInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $output; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
|
|
|
|
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $withData; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
|
|
|
|
|
41
|
|
|
* @param OutputInterface $output |
|
|
|
|
|
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(OutputInterface $output = null, $withData = false) |
|
44
|
|
|
{ |
|
45
|
|
|
if (null === $output) { |
|
46
|
|
|
$output = new ConsoleOutput(); |
|
47
|
|
|
} |
|
48
|
|
|
$this->output = $output; |
|
49
|
|
|
|
|
50
|
|
|
$this->withData = $withData; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
|
|
|
|
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
|
|
|
|
|
56
|
|
|
public function onAfterRun(CheckInterface $check, ResultInterface $result, $checkAlias = null) |
|
57
|
|
|
{ |
|
58
|
|
|
list($status, $code) = $this->getStatusByResul($result); |
|
59
|
|
|
|
|
60
|
|
|
switch ($code) { |
|
61
|
|
|
case self::STATUS_CODE_SUCCESS: |
|
|
|
|
|
|
62
|
|
|
$tag = self::STATUS_TAG_SUCCESS; |
|
63
|
|
|
break; |
|
64
|
|
|
case self::STATUS_CODE_WARNING: |
|
|
|
|
|
|
65
|
|
|
$tag = self::STATUS_TAG_WARNING; |
|
66
|
|
|
break; |
|
67
|
|
|
case self::STATUS_CODE_SKIP: |
|
|
|
|
|
|
68
|
|
|
$tag = self::STATUS_TAG_SKIP; |
|
69
|
|
|
break; |
|
70
|
|
|
case self::STATUS_CODE_FAILURE: |
|
|
|
|
|
|
71
|
|
|
$tag = self::STATUS_TAG_FAILUR; |
|
72
|
|
|
break; |
|
73
|
|
|
default: |
|
|
|
|
|
|
74
|
|
|
$tag = self::STATUS_TAG_UNKNOWN; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$groupTag = $check->getGroup(); |
|
|
|
|
|
|
78
|
|
|
$tags = $check->getTags(); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
if ($tags) { |
|
81
|
|
|
$tags = implode(', ', $tags); |
|
82
|
|
|
$tags = "[$tags]"; |
|
83
|
|
|
$groupTag .= ' / '.$tags; |
|
84
|
|
|
} else { |
|
85
|
|
|
$tags = null; |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$message = $result->getMessage(); |
|
89
|
|
|
if ($message) { |
|
90
|
|
|
$message = ", {$message}"; |
|
91
|
|
|
} |
|
92
|
|
|
$this->output->writeln(sprintf('<info>%-30s</info> %-40s %s%-10s</> %s%s', |
|
|
|
|
|
|
93
|
|
|
$check->getId(), $groupTag, $tag, $status, $check->getLabel(), $message)); |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
if ($this->withData) { |
|
96
|
|
|
$dataOut = $this->getDataOut($result); |
|
97
|
|
|
if ($dataOut) { |
|
98
|
|
|
$this->output->writeln(sprintf('%-71s < %s >', ' ', $dataOut)); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
|
|
|
|
|
104
|
|
|
* {@inheritdoc} |
|
105
|
|
|
*/ |
|
|
|
|
|
|
106
|
|
|
public function onStart(\ArrayObject $checks, $runnerConfig) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->output->writeln(sprintf('<info>%-30s %-40s %-10s %s, %s</info>', 'Check', 'Group / Tag(s)', 'Status', 'Info', 'Message')); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
|
|
|
|
|
112
|
|
|
* {@inheritdoc} |
|
113
|
|
|
*/ |
|
|
|
|
|
|
114
|
|
|
public function onBeforeRun(CheckInterface $check, $checkAlias = null) |
|
115
|
|
|
{ |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
|
|
|
|
|
119
|
|
|
* {@inheritdoc} |
|
120
|
|
|
*/ |
|
|
|
|
|
|
121
|
|
|
public function onStop(ResultsCollection $results) |
|
122
|
|
|
{ |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
|
|
|
|
|
126
|
|
|
* {@inheritdoc} |
|
127
|
|
|
*/ |
|
|
|
|
|
|
128
|
|
|
public function onFinish(ResultsCollection $results) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->output->writeln('--------'); |
|
131
|
|
|
$this->output->writeln(sprintf('%s: %s', 'total', $results->count())); |
|
132
|
|
|
$this->output->writeln(''); |
|
133
|
|
|
$this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_SUCCESS, 'SUCCESSES', $results->getSuccessCount())); |
|
134
|
|
|
$this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_WARNING, 'WARNINGS', $results->getWarningCount())); |
|
135
|
|
|
$this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_SKIP, 'SKIP', $results->getSkipCount())); |
|
136
|
|
|
$this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_FAILUR, 'FAILURES', $results->getFailureCount())); |
|
137
|
|
|
$this->output->writeln(sprintf('%s%-10s</>: %s', self::STATUS_TAG_UNKNOWN, 'UNKNOWNS', $results->getUnknownCount())); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
|
|
|
|
|
141
|
|
|
* @return null|string |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function getDataOut(ResultInterface $result) |
|
144
|
|
|
{ |
|
145
|
|
|
$dataOut = null; |
|
146
|
|
|
$message = $result->getMessage(); |
|
147
|
|
|
if ($message) { |
|
148
|
|
|
$data = $result->getData(); |
|
149
|
|
|
if (null !== $data) { |
|
150
|
|
|
$dataOut = json_encode($data); |
|
151
|
|
|
|
|
152
|
|
|
if (\mb_strlen($dataOut) > 100) { |
|
153
|
|
|
$dataOut .= "\n"; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $dataOut; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|