|
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 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
|
|
|
* |
|
27
|
|
|
* @author Vladimir Turnaev <[email protected]> |
|
28
|
|
|
*/ |
|
|
|
|
|
|
29
|
|
|
trait ApiCheckTrait |
|
30
|
|
|
{ |
|
31
|
|
|
public function checkAction(Request $request, string $check): JsonResponse |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
try { |
|
34
|
|
|
$runner = $this->runnerManager->getRunner($check); |
|
35
|
|
|
|
|
36
|
|
|
/** @var $reporter Api */ |
|
|
|
|
|
|
37
|
|
|
$reporter = $this->reporterManager->getReporter('api'); |
|
38
|
|
|
|
|
39
|
|
|
$runner->addReporter($reporter); |
|
40
|
|
|
$runner->run(); |
|
41
|
|
|
|
|
42
|
|
|
$res = $reporter->getCheckResults(); |
|
43
|
|
|
|
|
44
|
|
|
if (isset($res[0])) { |
|
45
|
|
|
return new JsonResponse($res[0]); |
|
46
|
|
|
} |
|
47
|
|
|
throw new NotFoundHttpException(sprintf('Check %s not found', $check)); |
|
48
|
|
|
} catch (NotFoundHttpException $e) { |
|
49
|
|
|
$e = new HttpException(404, $e->getMessage()); |
|
50
|
|
|
|
|
51
|
|
|
return new JsonResponse($e->toArray(), $e->getStatusCode()); |
|
52
|
|
|
} catch (\Exception $e) { |
|
53
|
|
|
$e = new HttpException(500, $e->getMessage()); |
|
54
|
|
|
|
|
55
|
|
|
return new JsonResponse($e->toArray(), $e->getStatusCode()); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function checkListAction(Request $request): JsonResponse |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
try { |
|
62
|
|
|
list($checks, $groups, $tags) = $this->getFilterParams($request); |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
$runner = $this->runnerManager->getRunner($checks, $groups, $tags); |
|
65
|
|
|
|
|
66
|
|
|
$breakOnFailure = (bool) $request->get('break-on-failure', false); |
|
67
|
|
|
$runner->setBreakOnFailure($breakOnFailure); |
|
68
|
|
|
|
|
69
|
|
|
/** @var $reporter Api */ |
|
|
|
|
|
|
70
|
|
|
$reporter = $this->reporterManager->getReporter('api'); |
|
71
|
|
|
|
|
72
|
|
|
$runner->addReporter($reporter); |
|
73
|
|
|
$runner->run(); |
|
74
|
|
|
|
|
75
|
|
|
return new JsonResponse([ |
|
|
|
|
|
|
76
|
|
|
'statusCode' => $reporter->getStatusCode(), |
|
77
|
|
|
'statusName' => $reporter->getStatusName(), |
|
78
|
|
|
|
|
79
|
|
|
'successes' => $reporter->getSuccessCount(), |
|
80
|
|
|
'warnings' => $reporter->getWarningCount(), |
|
81
|
|
|
'failures' => $reporter->getFailureCount(), |
|
82
|
|
|
'unknowns' => $reporter->getUnknownCount(), |
|
83
|
|
|
'total' => $reporter->getTotalCount(), |
|
84
|
|
|
|
|
85
|
|
|
'checks' => $reporter->getCheckResults(), |
|
86
|
|
|
]); |
|
|
|
|
|
|
87
|
|
|
} catch (\Exception $e) { |
|
88
|
|
|
$e = new HttpException(500, $e->getMessage()); |
|
89
|
|
|
|
|
90
|
|
|
return new JsonResponse($e->toArray(), $e->getStatusCode()); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function checkStatusAction(Request $request, ?string $checkSingle = null): Response |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
try { |
|
97
|
|
|
list($checks, $groups, $tags) = $this->getFilterParams($request); |
|
98
|
|
|
|
|
99
|
|
|
$checks = array_filter($checks, static function ($i) { |
|
|
|
|
|
|
100
|
|
|
return null !== $i; |
|
101
|
|
|
}); |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
if (null !== $checkSingle) { |
|
104
|
|
|
$runner = $this->runnerManager->getRunner($checkSingle); |
|
105
|
|
|
} else { |
|
106
|
|
|
$runner = $this->runnerManager->getRunner($checks, $groups, $tags); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$breakOnFailure = (bool) $request->get('break-on-failure', false); |
|
110
|
|
|
$runner->setBreakOnFailure($breakOnFailure); |
|
111
|
|
|
|
|
112
|
|
|
/** @var $reporter Api */ |
|
|
|
|
|
|
113
|
|
|
$reporter = $this->reporterManager->getReporter('api'); |
|
114
|
|
|
|
|
115
|
|
|
$runner->addReporter($reporter); |
|
116
|
|
|
$runner->run(); |
|
117
|
|
|
|
|
118
|
|
|
if ($reporter->getTotalCount()) { |
|
119
|
|
|
$code = $reporter->getStatusCode() === $reporter::STATUS_CODE_SUCCESS ? 200 : 500; |
|
120
|
|
|
|
|
121
|
|
|
return new Response($reporter->getStatusName(), $code); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
throw new NotFoundHttpException('Check(s) not found'); |
|
125
|
|
|
} catch (NotFoundHttpException $e) { |
|
126
|
|
|
return new Response($e->getMessage(), $e->getStatusCode()); |
|
127
|
|
|
} catch (\Exception $e) { |
|
128
|
|
|
return new Response($e->getMessage(), 502); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|