|
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 ZendDiagnostics\Result\Collection as ResultsCollection; |
|
15
|
|
|
use ZendDiagnostics\Result\ResultInterface; |
|
16
|
|
|
use ZendDiagnostics\Check\CheckInterface; |
|
17
|
|
|
use Tvi\MonitorBundle\Check\CheckInterface as TviCheckInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
|
|
|
|
|
20
|
|
|
* @author Kevin Bond <[email protected]>, Vladimir Turnaev <[email protected]> |
|
|
|
|
|
|
21
|
|
|
*/ |
|
|
|
|
|
|
22
|
|
|
class Api extends ReporterAbstract |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
|
|
|
|
|
25
|
|
|
* @var int |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $statusCode = self::STATUS_CODE_SUCCESS; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
|
|
|
|
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $statusName = self::STATUS_NAME_SUCCESS; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
|
|
|
|
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $checkResults = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
|
|
|
|
|
40
|
|
|
* @param CheckInterface|TviCheckInterface $check |
|
|
|
|
|
|
41
|
|
|
* @param null|mixed $checkAlias |
|
|
|
|
|
|
42
|
|
|
* |
|
43
|
|
|
* @return bool|void |
|
44
|
|
|
*/ |
|
45
|
46 |
|
public function onAfterRun(CheckInterface $check, ResultInterface $result, $checkAlias = null) |
|
46
|
|
|
{ |
|
47
|
46 |
|
list($statusName, $statusCode) = $this->getStatusByResul($result); |
|
48
|
|
|
|
|
49
|
46 |
|
$this->statusCode = max($this->statusCode, $statusCode); |
|
50
|
|
|
|
|
51
|
|
|
$res = [ |
|
52
|
46 |
|
'statusCode' => $statusCode, |
|
53
|
46 |
|
'statusName' => $statusName, |
|
54
|
46 |
|
'label' => $check->getLabel(), |
|
55
|
46 |
|
'check' => $checkAlias, |
|
56
|
46 |
|
'message' => $result->getMessage(), |
|
57
|
46 |
|
'importance' => $check->getImportance(), |
|
|
|
|
|
|
58
|
46 |
|
'tags' => $check->getTags(), |
|
|
|
|
|
|
59
|
46 |
|
'group' => $check->getGroup(), |
|
|
|
|
|
|
60
|
|
|
]; |
|
61
|
|
|
|
|
62
|
46 |
|
$data = $result->getData(); |
|
63
|
46 |
|
if (null !== $data) { |
|
64
|
46 |
|
if ($data instanceof \Exception) { |
|
65
|
|
|
$res['data'] = $data->getMessage(); |
|
66
|
|
|
} else { |
|
67
|
46 |
|
$res['data'] = $data; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
46 |
|
$res = array_filter($res, static function ($v) { |
|
|
|
|
|
|
72
|
46 |
|
return \is_array($v) ? !empty($v) : (null !== $v); |
|
73
|
46 |
|
}); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
46 |
|
$this->checkResults[] = $res; |
|
76
|
46 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
|
|
|
|
|
79
|
|
|
* @return int |
|
80
|
|
|
*/ |
|
81
|
33 |
|
public function getStatusCode() |
|
82
|
|
|
{ |
|
83
|
33 |
|
return $this->statusCode; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
|
|
|
|
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
33 |
|
public function getStatusName() |
|
90
|
|
|
{ |
|
91
|
33 |
|
return $this->statusName; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
38 |
|
public function getCheckResults(): array |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
38 |
|
return $this->checkResults; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
|
|
|
|
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
|
|
|
|
|
102
|
56 |
|
public function onFinish(ResultsCollection $results) |
|
103
|
|
|
{ |
|
104
|
56 |
|
parent::onFinish($results); |
|
105
|
|
|
|
|
106
|
56 |
|
$this->statusName = self::getStatusNameByCode($this->statusCode); |
|
107
|
56 |
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|