|
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 Tvi\MonitorBundle\Exception\HttpException; |
|
17
|
|
|
use Tvi\MonitorBundle\Reporter\Api; |
|
18
|
|
|
use Tvi\MonitorBundle\Reporter\ReporterManager; |
|
19
|
|
|
use Tvi\MonitorBundle\Runner\RunnerManager; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
|
|
|
|
|
22
|
|
|
* @author Vladimir Turnaev <[email protected]> |
|
23
|
|
|
*/ |
|
|
|
|
|
|
24
|
|
|
class ApiController |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
|
|
|
|
|
27
|
|
|
* @var RunnerManager |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $runnerManager; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
|
|
|
|
|
32
|
|
|
* @var ReporterManager |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $reporterManager; |
|
35
|
|
|
|
|
36
|
1 |
|
public function __construct(RunnerManager $runnerManager, ReporterManager $reporterManager) |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
1 |
|
$this->runnerManager = $runnerManager; |
|
39
|
1 |
|
$this->reporterManager = $reporterManager; |
|
40
|
1 |
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
public function checkListAction(Request $request): JsonResponse |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
try { |
|
45
|
1 |
|
list($checks, $groups, $tags) = $this->getFilterParams($request); |
|
46
|
|
|
|
|
47
|
1 |
|
$runner = $this->runnerManager->getRunner($checks, $groups, $tags); |
|
48
|
|
|
|
|
49
|
1 |
|
$breakOnFailure = (boolean)$request->get('bof', false); |
|
50
|
1 |
|
$runner->setBreakOnFailure($breakOnFailure); |
|
51
|
|
|
|
|
52
|
|
|
/** @var $reporter Api */ |
|
|
|
|
|
|
53
|
1 |
|
$reporter = $this->reporterManager->getReporter('api'); |
|
54
|
|
|
|
|
55
|
1 |
|
$runner->addReporter($reporter); |
|
56
|
1 |
|
$runner->run(); |
|
57
|
|
|
|
|
58
|
1 |
|
return new JsonResponse([ |
|
|
|
|
|
|
59
|
1 |
|
'statusCode' => $reporter->getStatusCode(), |
|
60
|
1 |
|
'statusName' => $reporter->getStatusName(), |
|
61
|
|
|
|
|
62
|
1 |
|
'successes' => $reporter->getSuccessCount(), |
|
63
|
1 |
|
'warnings' => $reporter->getWarningCount(), |
|
64
|
1 |
|
'failures' => $reporter->getFailureCount(), |
|
65
|
1 |
|
'unknowns' => $reporter->getUnknownCount(), |
|
66
|
1 |
|
'total' => $reporter->getTotalCount(), |
|
67
|
|
|
|
|
68
|
1 |
|
'checks' => $reporter->getCheckResults(), |
|
69
|
|
|
]); |
|
|
|
|
|
|
70
|
|
|
} catch (\Exception $e) { |
|
71
|
|
|
$e = new HttpException(404, $e->getMessage()); |
|
72
|
|
|
|
|
73
|
|
|
return new JsonResponse($e->toArray(), $e->getStatusCode()); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function checkAction(Request $request, string $check): JsonResponse |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
try { |
|
80
|
|
|
$runner = $this->runnerManager->getRunner($check); |
|
81
|
|
|
|
|
82
|
|
|
/** @var $reporter Api */ |
|
|
|
|
|
|
83
|
|
|
$reporter = $this->reporterManager->getReporter('api'); |
|
84
|
|
|
|
|
85
|
|
|
$runner->addReporter($reporter); |
|
86
|
|
|
$runner->run($check); |
|
87
|
|
|
|
|
88
|
|
|
$res = $reporter->getCheckResults()[0]; |
|
89
|
|
|
|
|
90
|
|
|
return new JsonResponse($res); |
|
91
|
|
|
} catch (\Exception $e) { |
|
92
|
|
|
$e = new HttpException(404, $e->getMessage()); |
|
93
|
|
|
|
|
94
|
|
|
return new JsonResponse($e->toArray(), $e->getStatusCode()); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
|
|
|
|
|
99
|
|
|
* @return array [$checks, $groups, $tags], |
|
100
|
|
|
*/ |
|
101
|
1 |
|
private function getFilterParams(Request $request): array |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
1 |
|
$checks = $request->get('check', []); |
|
104
|
1 |
|
if (\is_string($checks)) { |
|
105
|
|
|
$checks = $checks ? [$checks] : []; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
$groups = $request->get('group', []); |
|
109
|
1 |
|
if (\is_string($groups)) { |
|
110
|
|
|
$groups = $groups ? [$groups] : []; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
1 |
|
$tags = $request->get('tag', []); |
|
114
|
1 |
|
if (\is_string($tags)) { |
|
115
|
|
|
$tags = $tags ? [$tags] : []; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
return [$checks, $groups, $tags]; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|