|
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\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
17
|
|
|
use Tvi\MonitorBundle\Exception\HttpException; |
|
18
|
|
|
use Tvi\MonitorBundle\Reporter\Api; |
|
19
|
|
|
use Tvi\MonitorBundle\Reporter\ReporterManager; |
|
20
|
|
|
use Tvi\MonitorBundle\Runner\RunnerManager; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
|
|
|
|
|
23
|
|
|
* @author Vladimir Turnaev <[email protected]> |
|
24
|
|
|
*/ |
|
|
|
|
|
|
25
|
|
|
class ApiController |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
|
|
|
|
|
28
|
|
|
* @var RunnerManager |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $runnerManager; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
|
|
|
|
|
33
|
|
|
* @var ReporterManager |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $reporterManager; |
|
36
|
1 |
|
|
|
37
|
|
|
public function __construct(RunnerManager $runnerManager, ReporterManager $reporterManager) |
|
|
|
|
|
|
38
|
1 |
|
{ |
|
39
|
1 |
|
$this->runnerManager = $runnerManager; |
|
40
|
1 |
|
$this->reporterManager = $reporterManager; |
|
41
|
|
|
} |
|
42
|
1 |
|
|
|
43
|
|
|
public function checkListAction(Request $request): JsonResponse |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
1 |
|
try { |
|
46
|
|
|
list($checks, $groups, $tags) = $this->getFilterParams($request); |
|
47
|
1 |
|
|
|
48
|
|
|
$runner = $this->runnerManager->getRunner($checks, $groups, $tags); |
|
49
|
1 |
|
|
|
50
|
1 |
|
$breakOnFailure = (bool) $request->get('bof', false); |
|
51
|
|
|
$runner->setBreakOnFailure($breakOnFailure); |
|
52
|
|
|
|
|
53
|
1 |
|
/** @var $reporter Api */ |
|
|
|
|
|
|
54
|
|
|
$reporter = $this->reporterManager->getReporter('api'); |
|
55
|
1 |
|
|
|
56
|
1 |
|
$runner->addReporter($reporter); |
|
57
|
|
|
$runner->run(); |
|
58
|
1 |
|
|
|
59
|
1 |
|
return new JsonResponse([ |
|
|
|
|
|
|
60
|
1 |
|
'statusCode' => $reporter->getStatusCode(), |
|
61
|
|
|
'statusName' => $reporter->getStatusName(), |
|
62
|
1 |
|
|
|
63
|
1 |
|
'successes' => $reporter->getSuccessCount(), |
|
64
|
1 |
|
'warnings' => $reporter->getWarningCount(), |
|
65
|
1 |
|
'failures' => $reporter->getFailureCount(), |
|
66
|
1 |
|
'unknowns' => $reporter->getUnknownCount(), |
|
67
|
|
|
'total' => $reporter->getTotalCount(), |
|
68
|
1 |
|
|
|
69
|
|
|
'checks' => $reporter->getCheckResults(), |
|
70
|
|
|
]); |
|
|
|
|
|
|
71
|
|
|
} catch (\Exception $e) { |
|
72
|
|
|
$e = new HttpException(404, $e->getMessage()); |
|
73
|
|
|
|
|
74
|
|
|
return new JsonResponse($e->toArray(), $e->getStatusCode()); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function checkAction(Request $request, string $check): JsonResponse |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
try { |
|
81
|
|
|
$runner = $this->runnerManager->getRunner($check); |
|
82
|
|
|
|
|
83
|
|
|
/** @var $reporter Api */ |
|
|
|
|
|
|
84
|
|
|
$reporter = $this->reporterManager->getReporter('api'); |
|
85
|
|
|
|
|
86
|
|
|
$runner->addReporter($reporter); |
|
87
|
|
|
$runner->run(); |
|
88
|
|
|
|
|
89
|
|
|
$res = $reporter->getCheckResults()[0]; |
|
90
|
|
|
|
|
91
|
|
|
return new JsonResponse($res); |
|
92
|
|
|
} catch (\Exception $e) { |
|
93
|
|
|
$e = new HttpException(404, $e->getMessage()); |
|
94
|
|
|
|
|
95
|
|
|
return new JsonResponse($e->toArray(), $e->getStatusCode()); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function checkStatusAction(Request $request, ?string $check = null): Response |
|
|
|
|
|
|
100
|
|
|
{ |
|
101
|
1 |
|
try { |
|
102
|
|
|
|
|
103
|
1 |
|
list($checks, $groups, $tags) = $this->getFilterParams($request); |
|
104
|
1 |
|
|
|
105
|
|
|
if($check !== null) { |
|
|
|
|
|
|
106
|
|
|
$runner = $this->runnerManager->getRunner($check); |
|
107
|
|
|
} else { |
|
108
|
1 |
|
$runner = $this->runnerManager->getRunner($checks, $groups, $tags); |
|
109
|
1 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
$breakOnFailure = (bool) $request->get('bof', false); |
|
112
|
|
|
$runner->setBreakOnFailure($breakOnFailure); |
|
113
|
1 |
|
|
|
114
|
1 |
|
/** @var $reporter Api */ |
|
|
|
|
|
|
115
|
|
|
$reporter = $this->reporterManager->getReporter('api'); |
|
116
|
|
|
|
|
117
|
|
|
$runner->addReporter($reporter); |
|
118
|
1 |
|
$runner->run(); |
|
119
|
|
|
|
|
120
|
|
|
$code = $reporter->getStatusCode() == $reporter::STATUS_CODE_SUCCESS ? 200 : 500; |
|
121
|
|
|
|
|
122
|
|
|
return new Response($reporter->getStatusName(), $code); |
|
123
|
|
|
|
|
124
|
|
|
} catch (\Exception $e) { |
|
125
|
|
|
$e = new HttpException(404, $e->getMessage()); |
|
126
|
|
|
|
|
127
|
|
|
return new JsonResponse($e->toArray(), $e->getStatusCode()); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
|
|
|
|
|
132
|
|
|
* @return array [$checks, $groups, $tags], |
|
133
|
|
|
*/ |
|
134
|
|
|
private function getFilterParams(Request $request): array |
|
|
|
|
|
|
135
|
|
|
{ |
|
136
|
|
|
$checks = $request->get('check', []); |
|
137
|
|
|
if (\is_scalar($checks)) { |
|
138
|
|
|
$checks = $checks ? [$checks] : []; |
|
139
|
|
|
} |
|
140
|
|
|
$checks = !\is_array($checks) ? [$checks] : $checks; |
|
141
|
|
|
|
|
142
|
|
|
$groups = $request->get('group', []); |
|
143
|
|
|
if (\is_scalar($groups)) { |
|
144
|
|
|
$groups = $groups ? [$groups] : []; |
|
145
|
|
|
} |
|
146
|
|
|
$groups = !\is_array($groups) ? [$groups] : $groups; |
|
147
|
|
|
|
|
148
|
|
|
$tags = $request->get('tag', []); |
|
149
|
|
|
if (\is_scalar($tags)) { |
|
150
|
|
|
$tags = $tags ? [$tags] : []; |
|
151
|
|
|
} |
|
152
|
|
|
$tags = !\is_array($tags) ? [$tags] : $tags; |
|
153
|
|
|
|
|
154
|
|
|
return [$checks, $groups, $tags]; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|