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
|
|
|
} else { |
83
|
|
|
if ($result->getStatus() == Result::STATUS_PASS) { |
84
|
|
|
$details[$identifier]["observedValue"] = 1; |
85
|
|
|
} else { |
86
|
|
|
$details[$identifier]["observedValue"] = 0; |
87
|
|
|
} |
88
|
|
|
$details[$identifier]["metricType"] = MetricAwareResult::METRIC_TYPE_PERCENT; |
89
|
|
|
$details[$identifier]["observedUnit"] = 'percent'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$attributes = $result->getAttributes(); |
93
|
|
|
if (count($attributes) > 0) { |
94
|
|
|
$details['attributes'] = $attributes; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$resultArray = [ |
99
|
|
|
'status' => $runResult->getStatus(), |
100
|
|
|
'output' => $output, |
101
|
|
|
'checks' => $details |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
echo json_encode($resultArray, JSON_PRETTY_PRINT); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function getOutput(RunResult $runResult, $passMessage = null, $failMessage = null) |
108
|
|
|
{ |
109
|
|
|
if ($runResult->getStatus() == Result::STATUS_PASS) { |
110
|
|
|
if (is_null($passMessage)) { |
111
|
|
|
$output = self::DEFAULT_OUTPUT_PASS; |
112
|
|
|
} else { |
113
|
|
|
$output = $passMessage; |
114
|
|
|
} |
115
|
|
|
} else { |
116
|
|
|
if (is_null($failMessage)) { |
117
|
|
|
$output = self::DEFAULT_OUTPUT_FAIL; |
118
|
|
|
} else { |
119
|
|
|
$output = $failMessage; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $output; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|