|
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
|
|
|
private $dataProctection = false; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct($passMessage = null, $failMessage = null, $dataProctection = false) |
|
23
|
|
|
{ |
|
24
|
|
|
if ($passMessage) { |
|
25
|
|
|
$this->passMessage = $passMessage; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
if ($failMessage) { |
|
29
|
|
|
$this->failMessage = $failMessage; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$this->dataProctection = $dataProctection; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
private function comvertToDataProtectedResult(Result $result) |
|
36
|
|
|
{ |
|
37
|
|
|
if ($result instanceof MetricAwareResult) { |
|
38
|
|
|
$result->setLimitType(Result::LIMIT_TYPE_MIN); |
|
39
|
|
|
$result->setLimit(1); |
|
40
|
|
|
$result->setObservedValuePrecision(2); |
|
41
|
|
|
$result->setMetric(1, 'percent', MetricAwareResult::METRIC_TYPE_PERCENT); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $result; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function handle(RunResult $runResult, $echoValue = true) |
|
48
|
|
|
{ |
|
49
|
|
|
header('Content-Type: application/json'); |
|
50
|
|
|
|
|
51
|
|
|
$output = $this->getOutput($runResult, $this->passMessage, $this->failMessage); |
|
52
|
|
|
|
|
53
|
|
|
$details = []; |
|
54
|
|
|
|
|
55
|
|
|
foreach ($runResult->getResults() as $resultArray) { |
|
56
|
|
|
|
|
57
|
|
|
/** @var Result $result */ |
|
58
|
|
|
$result = $resultArray['result']; |
|
59
|
|
|
|
|
60
|
|
|
if ($this->dataProctection) { |
|
61
|
|
|
$result = $this->comvertToDataProtectedResult($result); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** @var Check $check */ |
|
65
|
|
|
$check = $resultArray['check']; |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
if (is_string($resultArray['identifier'])) { |
|
69
|
|
|
$identifier = $resultArray['identifier']; |
|
70
|
|
|
} else { |
|
71
|
|
|
$identifier = $check->getIdentifier(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$details[$identifier] = [ |
|
75
|
|
|
'status' => $result->getStatus(), |
|
76
|
|
|
'output' => $result->getMessage() |
|
77
|
|
|
]; |
|
78
|
|
|
|
|
79
|
|
|
$description = $resultArray['description']; |
|
80
|
|
|
if ($description) { |
|
81
|
|
|
$details[$identifier]['description'] = $description; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ($result instanceof MetricAwareResult) { |
|
85
|
|
|
$details[$identifier]["observedValue"] = $result->getMetricValue(); |
|
86
|
|
|
$details[$identifier]["observedUnit"] = $result->getMetricUnit(); |
|
87
|
|
|
|
|
88
|
|
|
if ($result->getMetricType()) { |
|
89
|
|
|
$details[$identifier]['metricType'] = $result->getMetricType(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (!is_null($result->getObservedValuePrecision())) { |
|
93
|
|
|
$details[$identifier]['observedValuePrecision'] = $result->getObservedValuePrecision(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (is_numeric($result->getLimit())) { |
|
97
|
|
|
$details[$identifier]['limit'] = $result->getLimit(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if (!is_null($result->getLimitType())) { |
|
101
|
|
|
$details[$identifier]['limitType'] = $result->getLimitType(); |
|
102
|
|
|
} |
|
103
|
|
|
} else { |
|
104
|
|
|
if ($result->getStatus() == Result::STATUS_PASS) { |
|
105
|
|
|
$details[$identifier]["observedValue"] = 1; |
|
106
|
|
|
} else { |
|
107
|
|
|
$details[$identifier]["observedValue"] = 0; |
|
108
|
|
|
} |
|
109
|
|
|
$details[$identifier]["metricType"] = MetricAwareResult::METRIC_TYPE_PERCENT; |
|
110
|
|
|
$details[$identifier]["observedUnit"] = 'percent'; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$attributes = $result->getAttributes(); |
|
114
|
|
|
if (count($attributes) > 0) { |
|
115
|
|
|
$details['attributes'] = $attributes; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$resultArray = [ |
|
120
|
|
|
'status' => $runResult->getStatus(), |
|
121
|
|
|
'output' => $output, |
|
122
|
|
|
'checks' => $details |
|
123
|
|
|
]; |
|
124
|
|
|
|
|
125
|
|
|
$resultJson = json_encode($resultArray, JSON_PRETTY_PRINT); |
|
126
|
|
|
|
|
127
|
|
|
if ($echoValue) { |
|
128
|
|
|
echo $resultJson; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $resultArray; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
private function getOutput(RunResult $runResult, $passMessage = null, $failMessage = null) |
|
135
|
|
|
{ |
|
136
|
|
|
if ($runResult->getStatus() == Result::STATUS_PASS) { |
|
137
|
|
|
if (is_null($passMessage)) { |
|
138
|
|
|
$output = self::DEFAULT_OUTPUT_PASS; |
|
139
|
|
|
} else { |
|
140
|
|
|
$output = $passMessage; |
|
141
|
|
|
} |
|
142
|
|
|
} else { |
|
143
|
|
|
if (is_null($failMessage)) { |
|
144
|
|
|
$output = self::DEFAULT_OUTPUT_FAIL; |
|
145
|
|
|
} else { |
|
146
|
|
|
$output = $failMessage; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $output; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|