1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Leankoala\HealthFoundation\Result\Format\Koality; |
4
|
|
|
|
5
|
|
|
use Leankoala\HealthFoundation\Check\Check; |
6
|
|
|
use Leankoala\HealthFoundation\Check\MetricAwareResult; |
7
|
|
|
use Leankoala\HealthFoundation\Check\Result; |
8
|
|
|
use Leankoala\HealthFoundation\Result\Format\Format; |
9
|
|
|
use Leankoala\HealthFoundation\RunResult; |
10
|
|
|
|
11
|
|
|
class KoalityFormat implements Format |
12
|
|
|
{ |
13
|
|
|
const DEFAULT_OUTPUT_PASS = 'The health check was passed.'; |
14
|
|
|
const DEFAULT_OUTPUT_WARN = 'Warning.'; |
15
|
|
|
const DEFAULT_OUTPUT_FAIL = 'The health check failed.'; |
16
|
|
|
|
17
|
|
|
private $passMessage = self::DEFAULT_OUTPUT_PASS; |
18
|
|
|
private $failMessage = self::DEFAULT_OUTPUT_FAIL; |
19
|
|
|
|
20
|
|
|
public function __construct($passMessage = null, $failMessage = null) |
21
|
|
|
{ |
22
|
|
|
if ($passMessage) { |
23
|
|
|
$this->passMessage = $passMessage; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if ($failMessage) { |
27
|
|
|
$this->failMessage = $failMessage; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function handle(RunResult $runResult) |
32
|
|
|
{ |
33
|
|
|
header('Content-Type: application/json'); |
34
|
|
|
|
35
|
|
|
$output = $this->getOutput($runResult, $this->passMessage, $this->failMessage); |
36
|
|
|
|
37
|
|
|
$details = []; |
38
|
|
|
|
39
|
|
|
foreach ($runResult->getResults() as $resultArray) { |
40
|
|
|
/** @var Result $result */ |
41
|
|
|
$result = $resultArray['result']; |
42
|
|
|
|
43
|
|
|
/** @var Check $check */ |
44
|
|
|
$check = $resultArray['check']; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
if (is_string($resultArray['identifier'])) { |
48
|
|
|
$identifier = $resultArray['identifier']; |
49
|
|
|
} else { |
50
|
|
|
$identifier = $check->getIdentifier(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$details[$identifier] = [ |
54
|
|
|
'status' => $result->getStatus(), |
55
|
|
|
'output' => $result->getMessage() |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
$description = $resultArray['description']; |
59
|
|
|
if ($description) { |
60
|
|
|
$details[$identifier]['description'] = $description; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($result instanceof MetricAwareResult) { |
64
|
|
|
$details[$identifier]["observedValue"] = $result->getMetricValue(); |
65
|
|
|
$details[$identifier]["observedUnit"] = $result->getMetricUnit(); |
66
|
|
|
|
67
|
|
|
if($result->getMetricType()) { |
68
|
|
|
$details[$identifier]['metricType'] = $result->getMetricType(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (!is_null($result->getObservedValuePrecision())) { |
72
|
|
|
$details[$identifier]['observedValuePrecision'] = $result->getObservedValuePrecision(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (is_numeric($result->getLimit())) { |
76
|
|
|
$details[$identifier]['limit'] = $result->getLimit(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (!is_null($result->getLimitType())) { |
80
|
|
|
$details[$identifier]['limitType'] = $result->getLimitType(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$attributes = $result->getAttributes(); |
85
|
|
|
if (count($attributes) > 0) { |
86
|
|
|
$details['attributes'] = $attributes; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$resultArray = [ |
91
|
|
|
'status' => $runResult->getStatus(), |
92
|
|
|
'output' => $output, |
93
|
|
|
'checks' => $details |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
echo json_encode($resultArray, JSON_PRETTY_PRINT); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function getOutput(RunResult $runResult, $passMessage = null, $failMessage = null) |
100
|
|
|
{ |
101
|
|
|
if ($runResult->getStatus() == Result::STATUS_PASS) { |
102
|
|
|
if (is_null($passMessage)) { |
103
|
|
|
$output = self::DEFAULT_OUTPUT_PASS; |
104
|
|
|
} else { |
105
|
|
|
$output = $passMessage; |
106
|
|
|
} |
107
|
|
|
} else { |
108
|
|
|
if (is_null($failMessage)) { |
109
|
|
|
$output = self::DEFAULT_OUTPUT_FAIL; |
110
|
|
|
} else { |
111
|
|
|
$output = $failMessage; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $output; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|