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