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
|
|
|
return new Response($e->getMessage(), 500); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
12 |
|
public function checksAction(Request $request): JsonResponse |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
try { |
64
|
12 |
|
list($checks, $groups, $tags) = $this->getFilterParams($request); |
|
|
|
|
65
|
|
|
|
66
|
12 |
|
$runner = $this->runnerManager->getRunner($checks, $groups, $tags); |
67
|
|
|
|
68
|
12 |
|
$breakOnFailure = (bool) $request->get('break-on-failure', false); |
69
|
12 |
|
$runner->setBreakOnFailure($breakOnFailure); |
70
|
|
|
|
71
|
|
|
/** @var $reporter Api */ |
|
|
|
|
72
|
12 |
|
$reporter = $this->reporterManager->getReporter('api'); |
73
|
|
|
|
74
|
12 |
|
$runner->addReporter($reporter); |
75
|
12 |
|
$runner->run(); |
76
|
|
|
$data = [ |
77
|
12 |
|
'statusCode' => $reporter->getStatusCode(), |
78
|
12 |
|
'statusName' => $reporter->getStatusName(), |
79
|
|
|
|
80
|
12 |
|
'successes' => $reporter->getSuccessCount(), |
81
|
12 |
|
'warnings' => $reporter->getWarningCount(), |
82
|
12 |
|
'failures' => $reporter->getFailureCount(), |
83
|
12 |
|
'unknowns' => $reporter->getUnknownCount(), |
84
|
12 |
|
'total' => $reporter->getTotalCount(), |
85
|
|
|
|
86
|
12 |
|
'checks' => $reporter->getCheckResults(), |
87
|
|
|
]; |
88
|
12 |
|
$json = $this->serializer->serialize($data, 'json'); |
89
|
|
|
|
90
|
12 |
|
return JsonResponse::fromJsonString($json); |
91
|
|
|
} catch (\Exception $e) { |
92
|
|
|
return new Response($e->getMessage(), 500); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
5 |
|
public function checkStatusAction(Request $request, string $id): Response |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
try { |
99
|
5 |
|
$runner = $this->runnerManager->getRunner($id); |
100
|
|
|
|
101
|
5 |
|
$breakOnFailure = (bool) $request->get('break-on-failure', false); |
102
|
5 |
|
$runner->setBreakOnFailure($breakOnFailure); |
103
|
|
|
|
104
|
|
|
/** @var $reporter Api */ |
|
|
|
|
105
|
5 |
|
$reporter = $this->reporterManager->getReporter('api'); |
106
|
|
|
|
107
|
5 |
|
$runner->addReporter($reporter); |
108
|
5 |
|
$runner->run(); |
109
|
|
|
|
110
|
5 |
|
if ($reporter->getTotalCount()) { |
111
|
5 |
|
$code = $reporter->getStatusCode() === $reporter::STATUS_CODE_SUCCESS ? 200 : 500; |
112
|
|
|
|
113
|
5 |
|
return new Response($reporter->getStatusName(), $code); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
throw new NotFoundHttpException(sprintf('Check %s not found', $id)); |
117
|
|
|
} catch (NotFoundHttpException $e) { |
118
|
|
|
return new Response($e->getMessage(), $e->getStatusCode()); |
119
|
|
|
} catch (\Exception $e) { |
120
|
|
|
return new Response($e->getMessage(), 502); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
16 |
|
public function checkStatusesAction(Request $request): Response |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
try { |
127
|
16 |
|
list($checks, $groups, $tags) = $this->getFilterParams($request); |
128
|
|
|
|
129
|
16 |
|
$runner = $this->runnerManager->getRunner($checks, $groups, $tags); |
130
|
|
|
|
131
|
16 |
|
$breakOnFailure = (bool) $request->get('break-on-failure', false); |
132
|
16 |
|
$runner->setBreakOnFailure($breakOnFailure); |
133
|
|
|
|
134
|
|
|
/** @var $reporter Api */ |
|
|
|
|
135
|
16 |
|
$reporter = $this->reporterManager->getReporter('api'); |
136
|
|
|
|
137
|
16 |
|
$runner->addReporter($reporter); |
138
|
16 |
|
$runner->run(); |
139
|
|
|
|
140
|
16 |
|
if ($reporter->getTotalCount()) { |
141
|
13 |
|
$code = $reporter->getStatusCode() === $reporter::STATUS_CODE_SUCCESS ? 200 : 500; |
142
|
|
|
|
143
|
13 |
|
return new Response($reporter->getStatusName(), $code); |
144
|
|
|
} |
145
|
|
|
|
146
|
3 |
|
throw new NotFoundHttpException('Check(s) not found'); |
147
|
3 |
|
} catch (NotFoundHttpException $e) { |
148
|
3 |
|
return new Response($e->getMessage(), $e->getStatusCode()); |
149
|
|
|
} catch (\Exception $e) { |
150
|
|
|
return new Response($e->getMessage(), 502); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|