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